iotControl.vue 15 KB

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