iotControl.vue 13 KB

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