iotControl.vue 12 KB

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