iotControl.vue 12 KB

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