index.vue 13 KB

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