index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. uni.navigateTo({
  78. url: '/pages/device/detail?id='+item.deviceId
  79. });
  80. },
  81. searchDevice(){
  82. console.log(this.search.deviceName)
  83. this.mqttUnSubscribe(this.deviceList);
  84. this.listDevice();
  85. },
  86. listDevice(){
  87. let self = this;
  88. listDevice(this.page,this.search.deviceName,this.deptId).then(res=>{
  89. let rows = res.rows;
  90. self.deviceList = rows;
  91. self.mqttSubscribe(self.deviceList);
  92. })
  93. },
  94. listDeviceGroup(){
  95. let self = this;
  96. let id = this.$store.state.user.id;
  97. listDeviceGroup(id).then(res=>{
  98. self.columns = [];
  99. let groups = [];
  100. let rows = res.rows;
  101. for (let i = 0; i < rows.length; i++) {
  102. let row = rows[i];
  103. let id = row.groupId;
  104. let name = row.groupName;
  105. groups.push({
  106. label:name,
  107. id:id
  108. })
  109. }
  110. self.columns.push(groups) ;
  111. });
  112. },
  113. chooseGroup(e){
  114. let data = e.value[0];
  115. console.log(data.id+","+data.label);
  116. this.groupName = data.label;
  117. this.show = false
  118. },
  119. /* 连接Mqtt消息服务器 */
  120. async connectMqtt() {
  121. if (this.$mqttTool.client == null) {
  122. await this.$mqttTool.connect();
  123. }
  124. this.mqttCallback();
  125. this.listDevice();
  126. this.startCheck();
  127. },
  128. /* Mqtt回调处理 */
  129. mqttCallback() {
  130. this.$mqttTool.client.on('message', (topic, message, buffer) => {
  131. let topics = topic.split('/');
  132. let productId = topics[1];
  133. let deviceNum = topics[2];
  134. message = JSON.parse(message.toString());
  135. if (topics[3] == 'status') {
  136. console.log('接收到【设备状态】主题:', topic);
  137. console.log('接收到【设备状态】内容:', message);
  138. // 更新列表中设备的状态
  139. for (let i = 0; i < this.deviceList.length; i++) {
  140. if (this.deviceList[i].serialNumber == deviceNum) {
  141. this.deviceList[i].status = message.status;
  142. this.deviceList[i].isShadow = message.isShadow;
  143. this.deviceList[i].rssi = message.rssi;
  144. return;
  145. }
  146. }
  147. }
  148. if (topics[3] == "monitor") {
  149. console.log('接收到【设备状态】主题:', topic);
  150. console.log('接收到【设备状态】内容:', message);
  151. // 实时监测
  152. this.chartLoading = false;
  153. for (let k = 0; k < message.length; k++) {
  154. let value = message[k].value;
  155. let id = message[k].id;
  156. let remark = message[k].remark;
  157. // 数据加载到图表
  158. for (let i = 0; i < this.dataList.length; i++) {
  159. if (id == this.dataList[i].id) {
  160. if (this.dataList[i].length > 50) {
  161. this.dataList[i].shift();
  162. }
  163. this.dataList[i].data.push([this.getTime(), value]);
  164. // 更新图表
  165. this.chart[i].setOption({
  166. series: [{
  167. data: this.dataList[i].data
  168. }]
  169. });
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. if (topics[3] == 'property' || topics[3] == 'function') {
  176. console.log('接收到【物模型】主题:', topic);
  177. console.log('接收到【物模型】内容:', message);
  178. // 更新列表中设备的属性
  179. for (let i = 0; i < this.deviceList.length; i++) {
  180. if (this.deviceList[i].serialNumber == deviceNum) {
  181. for (let j = 0; j < message.length; j++) {
  182. let isComplete = false;
  183. // 布尔类型
  184. for (let k = 0; k < this.deviceList[i].boolList.length && !isComplete; k++) {
  185. if (this.deviceList[i].boolList[k].id == message[j].id) {
  186. this.deviceList[i].boolList[k].shadow = message[j].value;
  187. isComplete = true;
  188. break;
  189. }
  190. }
  191. // 枚举类型
  192. for (let k = 0; k < this.deviceList[i].enumList.length && !isComplete; k++) {
  193. if (this.deviceList[i].enumList[k].id == message[j].id) {
  194. this.deviceList[i].enumList[k].shadow = message[j].value;
  195. isComplete = true;
  196. break;
  197. }
  198. }
  199. // 字符串类型
  200. for (let k = 0; k < this.deviceList[i].stringList.length && !isComplete; k++) {
  201. if (this.deviceList[i].stringList[k].id == message[j].id) {
  202. this.deviceList[i].stringList[k].shadow = message[j].value;
  203. isComplete = true;
  204. break;
  205. }
  206. }
  207. // 数组类型
  208. for (let k = 0; k < this.deviceList[i].arrayList.length && !isComplete; k++) {
  209. if (this.deviceList[i].arrayList[k].id == message[j].id) {
  210. this.deviceList[i].arrayList[k].shadow = message[j].value;
  211. isComplete = true;
  212. break;
  213. }
  214. }
  215. // 整数类型
  216. for (let k = 0; k < this.deviceList[i].integerList.length && !isComplete; k++) {
  217. if (this.deviceList[i].integerList[k].id == message[j].id) {
  218. this.deviceList[i].integerList[k].shadow = message[j].value;
  219. isComplete = true;
  220. break;
  221. }
  222. }
  223. // 小数类型
  224. for (let k = 0; k < this.deviceList[i].decimalList.length && !isComplete; k++) {
  225. if (this.deviceList[i].decimalList[k].id == message[j].id) {
  226. this.deviceList[i].decimalList[k].shadow = message[j].value;
  227. isComplete = true;
  228. break;
  229. }
  230. }
  231. // 监测数据
  232. for (let k = 0; k < this.deviceList[i].readOnlyList.length && !isComplete; k++) {
  233. if (this.deviceList[i].readOnlyList[k].id == message[j].id) {
  234. this.deviceList[i].readOnlyList[k].shadow = message[j].value;
  235. isComplete = true;
  236. break;
  237. }
  238. }
  239. }
  240. return;
  241. }
  242. }
  243. }
  244. });
  245. },
  246. /* 订阅消息 */
  247. mqttSubscribe(list) {
  248. // 订阅当前页面设备状态和实时监测
  249. let topics = [];
  250. for (let i = 0; i < list.length; i++) {
  251. let topicStatus = '/' + list[i].productId + '/' + list[i].serialNumber + '/status/post';
  252. let topicMonitor = "/" + list[i].productId + "/" + list[i].serialNumber + "/monitor/post";
  253. let topicProperty = '/' + list[i].productId + '/' + list[i].serialNumber + '/property/post';
  254. let topicFunction = '/' + list[i].productId + '/' + list[i].serialNumber + '/function/post';
  255. topics.push(topicStatus);
  256. topics.push(topicMonitor);
  257. topics.push(topicProperty);
  258. topics.push(topicFunction);
  259. }
  260. this.$mqttTool.subscribe(topics);
  261. },
  262. mqttUnSubscribe(list){
  263. let topics = [];
  264. for (let i = 0; i < list.length; i++) {
  265. let topicStatus = '/' + list[i].productId + '/' + list[i].serialNumber + '/status/post';
  266. let topicMonitor = "/" + list[i].productId + "/" + list[i].serialNumber + "/monitor/post";
  267. let topicProperty = '/' + list[i].productId + '/' + list[i].serialNumber + '/property/post';
  268. let topicFunction = '/' + list[i].productId + '/' + list[i].serialNumber + '/function/post';
  269. topics.push(topicStatus);
  270. topics.push(topicMonitor);
  271. topics.push(topicProperty);
  272. topics.push(topicFunction);
  273. }
  274. console.log('取消订阅', topics);
  275. this.$mqttTool.unsubscribe(topics);
  276. },
  277. startCheck(){
  278. let self = this;
  279. this.checkTimer = setInterval(function (){
  280. self.sendHeart();
  281. },20000);
  282. },
  283. checkActive(){
  284. let self = this;
  285. setTimeout(function (){
  286. if(self.publishMsg){
  287. self.resetConn()
  288. }
  289. },3000);
  290. },
  291. sendHeart(){
  292. console.log("发送心跳")
  293. let device = this.deviceInfo;
  294. let self = this;
  295. let topic = "/property-offline/post";
  296. self.publishMsg = true;
  297. self.checkActive();
  298. this.$mqttTool.publish(topic, "ok", "heart").then(res => {
  299. self.publishMsg = false;
  300. }).catch(res => {
  301. self.publishMsg = false;
  302. });
  303. },
  304. resetConn(){
  305. console.log("检测异常,重连")
  306. this.$mqttTool.end();
  307. this.$mqttTool.client = null;
  308. this.connectMqtt();
  309. this.getDetail();
  310. }
  311. }
  312. }
  313. </script>
  314. <style>
  315. .header{
  316. height: 100rpx;
  317. width: 100%;
  318. background: white;
  319. padding:0px 20rpx
  320. }
  321. .content {
  322. display: flex;
  323. flex-direction: column;
  324. align-items: center;
  325. justify-content: center;
  326. }
  327. .logo {
  328. height: 200rpx;
  329. width: 200rpx;
  330. margin-top: 200rpx;
  331. margin-left: auto;
  332. margin-right: auto;
  333. margin-bottom: 50rpx;
  334. }
  335. .text-area {
  336. margin:10px;
  337. padding-bottom: 10px;
  338. justify-content: center;
  339. width: 100%;
  340. }
  341. .grid-text {
  342. font-size: 14px;
  343. color: #909399;
  344. padding: 10rpx 0 20rpx 0rpx;
  345. /* #ifndef APP-PLUS */
  346. box-sizing: border-box;
  347. /* #endif */
  348. }
  349. .title {
  350. font-size: 36rpx;
  351. color: #8f8f94;
  352. }
  353. </style>