index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <template>
  2. <view class="content">
  3. <view class="header" style="padding:0 20rpx">
  4. <u-search placeholder="请输入设备名称" :showAction="false" :clearabled="true" @clear="searchDevice" @custom="searchDevice" v-model="search.deviceName" @search="searchDevice"></u-search>
  5. </view>
  6. <view class="text-area">
  7. <view v-for="(item,listIndex) in deviceList" @click="goDeviceDetail(item)">
  8. <uni-card>
  9. <template v-slot:cover>
  10. <view style="position: relative;display: flex;flex-direction: row;height: 100rpx">
  11. <view style="width: 60rpx;height: 40rpx;position: absolute;top: 32rpx;">
  12. <u-image :showLoading="true" src="/static/images/device/device.png" width="80rpx" height="80rpx" ></u-image>
  13. </view>
  14. <view style="position: absolute;top: 32rpx;left: 100rpx;display: flex;flex-direction: column;color: #545454;">
  15. <view style="font-size: 24rpx;font-weight:600;margin-bottom:16rpx">
  16. {{ item.deviceName }}
  17. </view>
  18. <view style="font-size: 20rpx;">
  19. {{item.serialNumber}}
  20. </view>
  21. </view>
  22. <view style="position: absolute;right:20rpx;top:24rpx;display: flex;flex-direction: column;">
  23. <view v-if="item.status == 3">
  24. <u-image :showLoading="true" src="/static/images/device/online.png" width="60rpx" height="60rpx" ></u-image>
  25. <view style="color:#48C373;font-size: 20rpx;text-align: center">在线</view>
  26. </view>
  27. <view v-if="item.status == 4">
  28. <u-image :showLoading="true" src="/static/images/device/outline.png" width="60rpx" height="60rpx" ></u-image>
  29. <view style="color:#CECDCD;font-size: 20rpx;text-align: center">离线</view>
  30. </view>
  31. <view v-if="item.status == 2">
  32. <u-image :showLoading="true" src="/static/images/device/outline.png" width="60rpx" height="60rpx" ></u-image>
  33. <view style="color:#CECDCD;font-size: 20rpx;text-align: center">禁用</view>
  34. </view>
  35. <view v-if="item.status == 1">
  36. <u-image :showLoading="true" src="/static/images/device/outline.png" width="60rpx" height="60rpx" ></u-image>
  37. <view style="color:#CECDCD;font-size: 20rpx;text-align: center">未激活</view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. </uni-card>
  43. </view>
  44. </view>
  45. <u-picker @cancel="show=false" :show="show" :columns="columns" @confirm="chooseGroup" keyName="label"></u-picker>
  46. </view>
  47. </template>
  48. <script>
  49. import { listDeviceGroup,listDevice } from '@/api/device/device.js'
  50. export default {
  51. data(){
  52. return {
  53. value:"",
  54. show:false,
  55. groupName:"",
  56. columns: [
  57. ],
  58. deviceList:[],
  59. page:1,
  60. search:{
  61. deviceName:""
  62. },
  63. deptId:0,
  64. }
  65. },
  66. onLoad: function(opt) {
  67. this.deptId = opt.deptId;
  68. this.connectMqtt();
  69. },
  70. destroyed() {
  71. // 取消订阅主题
  72. this.mqttUnSubscribe(this.deviceList);
  73. clearInterval(this.checkTimer);
  74. },
  75. methods:{
  76. goDeviceDetail(item){
  77. let type = item.type;
  78. if(type == 24){
  79. uni.navigateTo({
  80. url: '/subpkg/device/detail_new_20250528?id='+item.deviceId
  81. });
  82. return;
  83. }
  84. if(type == 16 || type == 17 || type == 18 || type == 19){
  85. uni.navigateTo({
  86. url: '/subpkg/device/detail_6?id='+item.deviceId
  87. });
  88. }else if(type == 20 || type == 21 || type == 22){
  89. uni.navigateTo({
  90. url: '/subpkg/device/detail_new_5?id='+item.deviceId
  91. });
  92. }else{
  93. uni.navigateTo({
  94. url: '/subpkg/device/detail?id='+item.deviceId
  95. });
  96. }
  97. },
  98. searchDevice(){
  99. console.log(this.search.deviceName)
  100. this.mqttUnSubscribe(this.deviceList);
  101. this.listDevice();
  102. },
  103. listDevice(){
  104. let self = this;
  105. listDevice(this.page,this.search.deviceName,this.deptId).then(res=>{
  106. let rows = res.rows;
  107. self.deviceList = rows;
  108. self.mqttSubscribe(self.deviceList);
  109. })
  110. },
  111. listDeviceGroup(){
  112. let self = this;
  113. let id = this.$store.state.user.id;
  114. listDeviceGroup(id).then(res=>{
  115. self.columns = [];
  116. let groups = [];
  117. let rows = res.rows;
  118. for (let i = 0; i < rows.length; i++) {
  119. let row = rows[i];
  120. let id = row.groupId;
  121. let name = row.groupName;
  122. groups.push({
  123. label:name,
  124. id:id
  125. })
  126. }
  127. self.columns.push(groups) ;
  128. });
  129. },
  130. chooseGroup(e){
  131. let data = e.value[0];
  132. console.log(data.id+","+data.label);
  133. this.groupName = data.label;
  134. this.show = false
  135. },
  136. /* 连接Mqtt消息服务器 */
  137. async connectMqtt() {
  138. if (this.$mqttTool.client == null) {
  139. await this.$mqttTool.connect();
  140. }
  141. this.mqttCallback();
  142. this.listDevice();
  143. this.startCheck();
  144. },
  145. /* Mqtt回调处理 */
  146. mqttCallback() {
  147. this.$mqttTool.client.on('message', (topic, message, buffer) => {
  148. let topics = topic.split('/');
  149. let productId = topics[1];
  150. let deviceNum = topics[2];
  151. message = JSON.parse(message.toString());
  152. if (topics[3] == 'status') {
  153. console.log('接收到【设备状态】主题:', topic);
  154. console.log('接收到【设备状态】内容:', message);
  155. // 更新列表中设备的状态
  156. for (let i = 0; i < this.deviceList.length; i++) {
  157. if (this.deviceList[i].serialNumber == deviceNum) {
  158. this.deviceList[i].status = message.status;
  159. this.deviceList[i].isShadow = message.isShadow;
  160. this.deviceList[i].rssi = message.rssi;
  161. return;
  162. }
  163. }
  164. }
  165. if (topics[3] == "monitor") {
  166. console.log('接收到【设备状态】主题:', topic);
  167. console.log('接收到【设备状态】内容:', message);
  168. // 实时监测
  169. this.chartLoading = false;
  170. for (let k = 0; k < message.length; k++) {
  171. let value = message[k].value;
  172. let id = message[k].id;
  173. let remark = message[k].remark;
  174. // 数据加载到图表
  175. for (let i = 0; i < this.dataList.length; i++) {
  176. if (id == this.dataList[i].id) {
  177. if (this.dataList[i].length > 50) {
  178. this.dataList[i].shift();
  179. }
  180. this.dataList[i].data.push([this.getTime(), value]);
  181. // 更新图表
  182. this.chart[i].setOption({
  183. series: [{
  184. data: this.dataList[i].data
  185. }]
  186. });
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. if (topics[3] == 'property' || topics[3] == 'function') {
  193. console.log('接收到【物模型】主题:', topic);
  194. console.log('接收到【物模型】内容:', message);
  195. // 更新列表中设备的属性
  196. for (let i = 0; i < this.deviceList.length; i++) {
  197. if (this.deviceList[i].serialNumber == deviceNum) {
  198. for (let j = 0; j < message.length; j++) {
  199. let isComplete = false;
  200. // 布尔类型
  201. for (let k = 0; k < this.deviceList[i].boolList.length && !isComplete; k++) {
  202. if (this.deviceList[i].boolList[k].id == message[j].id) {
  203. this.deviceList[i].boolList[k].shadow = message[j].value;
  204. isComplete = true;
  205. break;
  206. }
  207. }
  208. // 枚举类型
  209. for (let k = 0; k < this.deviceList[i].enumList.length && !isComplete; k++) {
  210. if (this.deviceList[i].enumList[k].id == message[j].id) {
  211. this.deviceList[i].enumList[k].shadow = message[j].value;
  212. isComplete = true;
  213. break;
  214. }
  215. }
  216. // 字符串类型
  217. for (let k = 0; k < this.deviceList[i].stringList.length && !isComplete; k++) {
  218. if (this.deviceList[i].stringList[k].id == message[j].id) {
  219. this.deviceList[i].stringList[k].shadow = message[j].value;
  220. isComplete = true;
  221. break;
  222. }
  223. }
  224. // 数组类型
  225. for (let k = 0; k < this.deviceList[i].arrayList.length && !isComplete; k++) {
  226. if (this.deviceList[i].arrayList[k].id == message[j].id) {
  227. this.deviceList[i].arrayList[k].shadow = message[j].value;
  228. isComplete = true;
  229. break;
  230. }
  231. }
  232. // 整数类型
  233. for (let k = 0; k < this.deviceList[i].integerList.length && !isComplete; k++) {
  234. if (this.deviceList[i].integerList[k].id == message[j].id) {
  235. this.deviceList[i].integerList[k].shadow = message[j].value;
  236. isComplete = true;
  237. break;
  238. }
  239. }
  240. // 小数类型
  241. for (let k = 0; k < this.deviceList[i].decimalList.length && !isComplete; k++) {
  242. if (this.deviceList[i].decimalList[k].id == message[j].id) {
  243. this.deviceList[i].decimalList[k].shadow = message[j].value;
  244. isComplete = true;
  245. break;
  246. }
  247. }
  248. // 监测数据
  249. for (let k = 0; k < this.deviceList[i].readOnlyList.length && !isComplete; k++) {
  250. if (this.deviceList[i].readOnlyList[k].id == message[j].id) {
  251. this.deviceList[i].readOnlyList[k].shadow = message[j].value;
  252. isComplete = true;
  253. break;
  254. }
  255. }
  256. }
  257. return;
  258. }
  259. }
  260. }
  261. });
  262. },
  263. /* 订阅消息 */
  264. mqttSubscribe(list) {
  265. // 订阅当前页面设备状态和实时监测
  266. let topics = [];
  267. for (let i = 0; i < list.length; i++) {
  268. let topicStatus = '/' + list[i].productId + '/' + list[i].serialNumber + '/status/post';
  269. let topicMonitor = "/" + list[i].productId + "/" + list[i].serialNumber + "/monitor/post";
  270. let topicProperty = '/' + list[i].productId + '/' + list[i].serialNumber + '/property/post';
  271. let topicFunction = '/' + list[i].productId + '/' + list[i].serialNumber + '/function/post';
  272. topics.push(topicStatus);
  273. topics.push(topicMonitor);
  274. topics.push(topicProperty);
  275. topics.push(topicFunction);
  276. }
  277. this.$mqttTool.subscribe(topics);
  278. },
  279. mqttUnSubscribe(list){
  280. let topics = [];
  281. for (let i = 0; i < list.length; i++) {
  282. let topicStatus = '/' + list[i].productId + '/' + list[i].serialNumber + '/status/post';
  283. let topicMonitor = "/" + list[i].productId + "/" + list[i].serialNumber + "/monitor/post";
  284. let topicProperty = '/' + list[i].productId + '/' + list[i].serialNumber + '/property/post';
  285. let topicFunction = '/' + list[i].productId + '/' + list[i].serialNumber + '/function/post';
  286. topics.push(topicStatus);
  287. topics.push(topicMonitor);
  288. topics.push(topicProperty);
  289. topics.push(topicFunction);
  290. }
  291. console.log('取消订阅', topics);
  292. this.$mqttTool.unsubscribe(topics);
  293. },
  294. startCheck(){
  295. let self = this;
  296. this.checkTimer = setInterval(function (){
  297. self.sendHeart();
  298. },20000);
  299. },
  300. checkActive(){
  301. let self = this;
  302. setTimeout(function (){
  303. if(self.publishMsg){
  304. self.resetConn()
  305. }
  306. },3000);
  307. },
  308. sendHeart(){
  309. console.log("发送心跳")
  310. let device = this.deviceInfo;
  311. let self = this;
  312. let topic = "/property-offline/post";
  313. self.publishMsg = true;
  314. self.checkActive();
  315. this.$mqttTool.publish(topic, "ok", "heart").then(res => {
  316. self.publishMsg = false;
  317. }).catch(res => {
  318. self.publishMsg = false;
  319. });
  320. },
  321. resetConn(){
  322. console.log("检测异常,重连")
  323. this.$mqttTool.end();
  324. this.$mqttTool.client = null;
  325. this.connectMqtt();
  326. this.getDetail();
  327. }
  328. }
  329. }
  330. </script>
  331. <style>
  332. .header{
  333. height: 100rpx;
  334. width: 100%;
  335. background: white;
  336. padding:0px 20rpx
  337. }
  338. .content {
  339. display: flex;
  340. flex-direction: column;
  341. align-items: center;
  342. justify-content: center;
  343. }
  344. .logo {
  345. height: 200rpx;
  346. width: 200rpx;
  347. margin-top: 200rpx;
  348. margin-left: auto;
  349. margin-right: auto;
  350. margin-bottom: 50rpx;
  351. }
  352. .text-area {
  353. margin:10px;
  354. padding-bottom: 10px;
  355. justify-content: center;
  356. width: 100%;
  357. }
  358. .grid-text {
  359. font-size: 14px;
  360. color: #909399;
  361. padding: 10rpx 0 20rpx 0rpx;
  362. /* #ifndef APP-PLUS */
  363. box-sizing: border-box;
  364. /* #endif */
  365. }
  366. .title {
  367. font-size: 36rpx;
  368. color: #8f8f94;
  369. }
  370. </style>