iotControl.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <view class="iotControl">
  3. <view class="device-type">
  4. <view v-for="(item,index) in deviceType" :key="index" @click="deviceTypeFun(item)">
  5. <img :src="item.img">
  6. <view>{{item.hardwareName}}</view>
  7. <img :src="imagesUrl('commonality/icon_wdwg_gd.png')">
  8. </view>
  9. </view>
  10. <!-- 传感器监测 -->
  11. <view class="small-title" v-if="sensorData[0]">
  12. <view></view>
  13. <view>传感器监测</view>
  14. </view>
  15. <view class="sensor">
  16. <view class="sensor-min" v-for="(item,index) in sensorData" :key="index">
  17. <img class="sensor-l" :src="baseUrl+item.icon" />
  18. <view class="sensor-r" :class="item.type?'color-B':'color-A'">{{item.deviceName}}:
  19. {{item.deviceValue?item.deviceValue:''}}{{item.unit?item.unit:''}}
  20. </view>
  21. </view>
  22. </view>
  23. <!-- 智能控制 -->
  24. <view class="small-title" v-if="labHardwareVOList[0]">
  25. <view></view>
  26. <view>智能控制</view>
  27. </view>
  28. <view class="intelligent-control">
  29. <view v-for="(item,index) in labHardwareVOList" :key="index" v-if="item.hardwareTypeKey !='airConditioner'&&item.hardwareTypeKey !='evacuationLight'">
  30. <view class="for-button-p">{{item.hardwareName}}
  31. <text v-if="item.reservedThree">({{item.reservedThree}})</text>
  32. </view>
  33. <img v-if="item.operatingState" :src="imagesUrl('manage/icon_10.png')"
  34. @click="buttonClick('switch',item,'0')">
  35. <img v-if="!item.operatingState" :src="imagesUrl('manage/icon_11.png')"
  36. @click="buttonClick('switch',item,'1')">
  37. <!-- <view class="for-button-p" v-if="!item.online">离线</view> -->
  38. </view>
  39. </view>
  40. <!-- 智能终端 -->
  41. <view class="small-title" v-if="terminalData[0]">
  42. <view></view>
  43. <view>智能终端</view>
  44. </view>
  45. <view class="intelligent-control">
  46. <view v-for="(item,index) in terminalData" :key="index">
  47. <view class="for-button-p">{{item.deviceName}}</view>
  48. <view class="for-button-p" v-if="!item.online">离线</view>
  49. <view class="for-button-p" v-if="item.online" style="color:#0183FA;">在线</view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. itoOrVideoLimits
  57. } from '@/utils/index'
  58. import $mqtt from '@/utils/mqtt.min.js';
  59. import {
  60. iotAppSensorFindBySubId,
  61. iotAppHardwareFindByType,
  62. iotAppHardwareOperatingHardware,
  63. iotAppDeviceFindByType,
  64. } from '@/pages_manage/api/index.js'
  65. import {
  66. config
  67. } from '@/api/request/config.js'
  68. export default {
  69. name: "iotControl",
  70. props: {
  71. subjectData: {},
  72. },
  73. data() {
  74. return {
  75. baseUrl: config.base_url,
  76. //MQTT请求参数-传感器
  77. mtopic: "iot/device/sensor/sub/",
  78. //MQTT请求参数-智能控制
  79. mtopicOne: "iot/hardware/all/sub/",
  80. client: {},
  81. newData: {},
  82. subId: '',
  83. deviceType: [],
  84. sensorData: [],
  85. labHardwareVOList: [],
  86. terminalData: [{
  87. name: '智能管控一体机',
  88. pcType: 1,
  89. }],
  90. planSensorList: [],
  91. }
  92. },
  93. created() {
  94. this.planSensorList = uni.getStorageSync("planSensorList");
  95. },
  96. mounted() {
  97. this.$set(this, 'newData', this.subjectData);
  98. this.$set(this, 'subId', this.subjectData.subId);
  99. this.iotAppSensorFindBySubId();
  100. this.iotAppHardwareFindByType();
  101. this.iotAppDeviceFindByType();
  102. this.offMQTT('on');
  103. },
  104. methods: {
  105. //物联控制设备
  106. deviceTypeFun(item) {
  107. if (item.type == 'video') {
  108. if (!itoOrVideoLimits(this.newData.adminId)) {
  109. uni.showToast({
  110. title: '没有相关操作权限,请联系管理员',
  111. icon: "none",
  112. mask: true,
  113. duration: 2000
  114. });
  115. return
  116. }
  117. // #ifdef WEB
  118. this.$parent.$parent.goVideoPage();
  119. // #endif
  120. // #ifdef MP-WEIXIN
  121. this.$parent.goVideoPage();
  122. // #endif
  123. } else {
  124. if (!itoOrVideoLimits(this.newData.adminId)) {
  125. uni.showToast({
  126. title: '没有相关操作权限,请联系管理员',
  127. icon: "none",
  128. mask: true,
  129. duration: 2000
  130. });
  131. return
  132. }
  133. if (item.type == 'speech') {
  134. //语音广播弹窗开启
  135. // #ifdef WEB
  136. this.$parent.$parent.buttonClick('broadcastOpen', '');
  137. // #endif
  138. // #ifdef MP-WEIXIN
  139. this.$parent.buttonClick('broadcastOpen', '');
  140. // #endif
  141. } else if (item.type == 'conditioning') {
  142. //空调弹窗开启
  143. // #ifdef WEB
  144. this.$parent.$parent.buttonClick('conditioningOpen', item);
  145. // #endif
  146. // #ifdef MP-WEIXIN
  147. this.$parent.buttonClick('conditioningOpen', item);
  148. // #endif
  149. }
  150. }
  151. },
  152. buttonClick(type, row, status) {
  153. if (!itoOrVideoLimits(this.newData.adminId)) {
  154. uni.showToast({
  155. title: '没有相关操作权限,请联系管理员',
  156. icon: "none",
  157. mask: true,
  158. duration: 2000
  159. });
  160. return
  161. }
  162. let self = this;
  163. if (type == 'switch') {
  164. //开关
  165. let text = '';
  166. if (status == '0') {
  167. text = '关闭';
  168. } else if (status == '1') {
  169. text = '开启';
  170. }
  171. uni.showModal({
  172. content: '确认要' + text + '吗?',
  173. cancelColor: "#999",
  174. confirmColor: "#0183FA",
  175. success: function(res) {
  176. if (res.confirm) {
  177. self.mangerControl(row, status);
  178. setTimeout(function() {
  179. self.iotAppHardwareFindByType();
  180. }, 200)
  181. console.log('用户点击确定');
  182. } else if (res.cancel) {
  183. console.log('用户点击取消');
  184. }
  185. }
  186. });
  187. }
  188. },
  189. //设备开关
  190. async mangerControl(item, status) {
  191. let obj = {
  192. hardwareNo: item.hardwareNo,
  193. command: status,
  194. };
  195. const {
  196. data
  197. } = await iotAppHardwareOperatingHardware(obj);
  198. if (data.code == 200) {
  199. uni.showToast({
  200. title: '操作成功',
  201. icon: "none",
  202. mask: true,
  203. duration: 2000
  204. });
  205. }
  206. },
  207. //查询传感器状态
  208. async iotAppSensorFindBySubId() {
  209. let self = this;
  210. const {
  211. data
  212. } = await iotAppSensorFindBySubId({
  213. subId: this.subId
  214. });
  215. if (data.code == 200) {
  216. data.data.forEach((item) => {
  217. let num = 0;
  218. if (self.planSensorList[0]) {
  219. self.planSensorList.forEach((minItem) => {
  220. if (item.deviceNo == minItem) {
  221. num++
  222. }
  223. })
  224. }
  225. item.type = num != 0;
  226. })
  227. this.$set(this, 'sensorData', data.data);
  228. }
  229. },
  230. //查询硬件设备
  231. async iotAppHardwareFindByType() {
  232. const {
  233. data
  234. } = await iotAppHardwareFindByType({
  235. subjectId: this.subId
  236. });
  237. if (data.code == 200) {
  238. let VOList = [];
  239. data.data.forEach((item) => {
  240. if (item.hardwareTypeKey == 'airConditioner') {
  241. if (item.reservedThree == 0) {
  242. item.switchType = 0;
  243. } else if (item.reservedThree == 2) {
  244. item.switchType = 1;
  245. item.functionType = 3;
  246. } else if (item.reservedThree.indexOf('-') != -1) {
  247. let list = item.reservedThree.split('-');
  248. item.switchType = 1;
  249. item.functionType = list[0] == 3 ? 1 : (list[0] == 4 ? 2 : '');
  250. item.orderType = list[1] ? list[1] : '';
  251. } else {
  252. item.switchType = '';
  253. item.functionType = '';
  254. item.orderType = '';
  255. }
  256. }else{
  257. VOList.push(item)
  258. }
  259. })
  260. this.$set(this, 'labHardwareVOList', VOList);
  261. let list = [{
  262. type: 'video',
  263. hardwareName: '视频监控',
  264. img: this.imagesUrl('manage/icon_xq_spjk.png'),
  265. },
  266. {
  267. type: 'speech',
  268. hardwareName: '语音广播',
  269. img: this.imagesUrl('commonality/icon_sskz_xz.png'),
  270. },
  271. ];
  272. for (let i = 0; i < data.data.length; i++) {
  273. if (data.data[i].hardwareTypeKey == 'airConditioner') {
  274. data.data[i].type = 'conditioning';
  275. data.data[i].img = this.imagesUrl('manage/icon_xq_kt.png');
  276. list.push(data.data[i])
  277. }
  278. }
  279. this.$set(this, 'deviceType', list);
  280. }
  281. },
  282. //查询物联设备
  283. async iotAppDeviceFindByType() {
  284. const {
  285. data
  286. } = await iotAppDeviceFindByType({
  287. subjectId: this.subId,
  288. typeKeyList: ['aio']
  289. });
  290. if (data.code == 200) {
  291. this.$set(this, 'terminalData', data.data);
  292. }
  293. },
  294. //MQTT订阅
  295. sensorMQTT() {
  296. let self = this;
  297. this.client = $mqtt.connect(
  298. // #ifdef WEB
  299. 'wss://' + uni.getStorageSync('mqttUrl')
  300. // #endif
  301. // #ifdef MP-WEIXIN
  302. 'wxs://' + uni.getStorageSync('mqttUrl')
  303. // #endif
  304. , {
  305. username: uni.getStorageSync('mqttUser'),
  306. password: uni.getStorageSync('mqttPassword')
  307. });
  308. this.client.on("connect", e => {
  309. this.client.subscribe(this.mtopic + self.subId, (err) => {
  310. if (!err) {
  311. console.log("传感器订阅成功:" + this.mtopic + self.subId);
  312. } else {
  313. // console.log("连接错误:" + err);
  314. }
  315. });
  316. this.client.subscribe(this.mtopicOne + self.subId, (err) => {
  317. if (!err) {
  318. console.log("智能控制订阅成功:" + this.mtopicOne + self.subId);
  319. } else {
  320. // console.log("连接错误:" + err);
  321. }
  322. });
  323. });
  324. this.client.on("message", (topic, message) => {
  325. if (message) {
  326. console.log('topic',topic)
  327. console.log('传感器',this.mtopic + this.subId)
  328. console.log('智能控制',this.mtopicOne + this.subId)
  329. if (topic == this.mtopic + this.subId) {
  330. //传感器
  331. let data = JSON.parse(message)
  332. let list = JSON.parse(JSON.stringify(this.sensorData))
  333. console.log('传感器data',data)
  334. console.log('传感器list',list)
  335. list.forEach((item) => {
  336. data.forEach((minItem) => {
  337. if (item.deviceNo == minItem.deviceNo) {
  338. item.deviceValue = minItem.deviceValue;
  339. item.online = minItem.online;
  340. }
  341. })
  342. })
  343. this.$set(this, 'sensorData', list);
  344. } else if (topic == this.mtopicOne + this.subId) {
  345. //智能控制
  346. let data = JSON.parse(message)
  347. let list = JSON.parse(JSON.stringify(this.labHardwareVOList))
  348. console.log('智能控制data',data)
  349. console.log('智能控制list',list)
  350. list.forEach((item) => {
  351. if (item.hardwareNo == data.hardwareNo) {
  352. if (item.hardwareTypeKey == 'airConditioner') {
  353. if (data.command == 0) {
  354. item.switchType = 0;
  355. item.functionType = '';
  356. item.orderType = '';
  357. } else if (data.command == 2) {
  358. item.switchType = 1;
  359. item.functionType = 3;
  360. item.orderType = '';
  361. } else if (data.command.indexOf('-') != -1) {
  362. let list = data.command.split('-');
  363. item.switchType = 1;
  364. item.functionType = list[0] == 3 ? 1 : (list[0] == 4 ? 2 : '');
  365. item.orderType = list[1] ? list[1] : '';
  366. } else {
  367. item.switchType = '';
  368. item.functionType = '';
  369. item.orderType = '';
  370. }
  371. } else {
  372. item.operatingState = data.operatingState;
  373. item.online = data.online;
  374. }
  375. }
  376. })
  377. this.$set(this, 'labHardwareVOList', list);
  378. }
  379. }
  380. });
  381. },
  382. //取消订阅关闭MQTT连接
  383. offMQTT(type) {
  384. let self = this;
  385. if (self.client.unsubscribe) {
  386. self.client.unsubscribe(self.mtopicOne + self.subId, error => {
  387. if (error) {
  388. // console.log('mqtt关闭连接错误:', error)
  389. }
  390. })
  391. self.client.end();
  392. this.$set(this, 'client', {});
  393. }
  394. //判断传入参数如果存在 发起一次新的连接
  395. if (type) {
  396. this.sensorMQTT();
  397. }
  398. },
  399. },
  400. onHide() {
  401. //清除定时器
  402. let self = this;
  403. self.offMQTT();
  404. },
  405. beforeDestroy() {
  406. //清除定时器
  407. let self = this;
  408. self.offMQTT();
  409. },
  410. }
  411. </script>
  412. <style lang="stylus" scoped>
  413. .iotControl {
  414. flex:1;
  415. display: flex;
  416. flex-direction: column;
  417. overflow-y: scroll !important;
  418. width: 750rpx;
  419. .device-type {
  420. background: #fff;
  421. padding: 24rpx 20rpx;
  422. box-sizing: border-box;
  423. display: flex;
  424. justify-content: flex-start;
  425. flex-wrap: wrap;
  426. >view {
  427. //width: 356rpx;
  428. height: 80rpx;
  429. display: flex;
  430. justify-content: flex-start;
  431. align-items: center;
  432. padding-left: 10rpx;
  433. padding-right: 10rpx;
  434. box-sizing: border-box;
  435. >img:nth-of-type(1) {
  436. width: 42rpx;
  437. height: 42rpx;
  438. margin-right: 12rpx;
  439. }
  440. >view {
  441. font-family: PingFang SC;
  442. font-weight: 500;
  443. font-size: 30rpx;
  444. color: #222222;
  445. line-height: 80rpx;
  446. width: 244rpx;
  447. display:block;
  448. overflow:hidden;
  449. text-overflow:ellipsis;
  450. white-space:nowrap;
  451. }
  452. >img:nth-of-type(2) {
  453. width: 24rpx;
  454. height: 22rpx;
  455. }
  456. }
  457. >view:nth-child(2n) {
  458. border-right: none;
  459. padding-left: 20rpx;
  460. box-sizing: border-box;
  461. }
  462. >view:nth-last-child(1) {
  463. border-bottom: none;
  464. }
  465. >view:nth-last-child(2) {
  466. border-bottom: none;
  467. }
  468. }
  469. .small-title {
  470. height: 80rpx;
  471. border-bottom: 1rpx solid #E0E0E0;
  472. background: #FFFFFF;
  473. margin-top: 20rpx;
  474. display: flex;
  475. align-items: center;
  476. >view:nth-of-type(1) {
  477. width: 4rpx;
  478. height: 34rpx;
  479. background: #0D97EB;
  480. margin: 0rpx 24rpx 0 20rpx;
  481. }
  482. >view:nth-of-type(2) {
  483. font-family: PingFang SC;
  484. font-weight: 500;
  485. font-size: 32rpx;
  486. color: #333333;
  487. line-height: 80rpx;
  488. }
  489. }
  490. .sensor {
  491. display: flex;
  492. justify-content: flex-start;
  493. flex-wrap: wrap;
  494. background: #fff;
  495. padding: 30rpx 20rpx 0;
  496. box-sizing: border-box;
  497. .sensor-min {
  498. min-height: 66rpx;
  499. width: 690rpx;
  500. display: flex;
  501. justify-content: flex-start;
  502. align-content: center;
  503. .sensor-l {
  504. flex-shrink: 0;
  505. width: 42rpx;
  506. height: 42rpx;
  507. margin-right: 20rpx;
  508. }
  509. .sensor-r {
  510. font-family: PingFang SC;
  511. font-weight: 500;
  512. font-size: 28rpx;
  513. line-height: 30rpx;
  514. overflow: hidden;
  515. text-overflow: ellipsis;
  516. white-space: nowrap;
  517. }
  518. .color-A {
  519. color: #222222;
  520. }
  521. .color-B {
  522. color: red;
  523. }
  524. }
  525. }
  526. .intelligent-control {
  527. background: #fff;
  528. padding: 0 20rpx;
  529. box-sizing: border-box;
  530. >view {
  531. display flex;
  532. border-bottom: 1rpx solid #e0e0e0;
  533. .for-button-p {
  534. flex: 1;
  535. line-height: 90rpx;
  536. color: #333333;
  537. font-size: 28rpx;
  538. >text {
  539. font-size: 28rpx;
  540. color: #0183FA;
  541. }
  542. }
  543. >img {
  544. height: 50rpx;
  545. width: 100rpx;
  546. margin: 20rpx 0 20rpx 0;
  547. }
  548. view:nth-child(2) {
  549. text-align right;
  550. color: #999;
  551. }
  552. .pcType-button {
  553. width: 120rpx;
  554. line-height: 42rpx;
  555. border: 4rpx solid #0183FA;
  556. border-radius: 30rpx;
  557. color: #0183FA !important;
  558. font-size: 24rpx;
  559. text-align center !important;
  560. margin: 20rpx 0 20rpx 0;
  561. }
  562. }
  563. >view:nth-last-child(1) {
  564. border-bottom: none;
  565. }
  566. }
  567. }
  568. </style>