index.vue 10 KB

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