iotControl.vue 13 KB

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