index.vue 10 KB

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