iotControl.vue 13 KB

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