index.vue 12 KB

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