iotControl.vue 13 KB

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