iotControl.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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', '');
  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. this.$set(this, 'labHardwareVOList', data.data);
  214. for (let i = 0; i < data.data.length; i++) {
  215. if (data.data[i].hardwareTypeKey == 'airConditioner') {
  216. data.data[i].type = 'conditioning';
  217. data.data[i].img = require('@/pages_manage/images/icon_xq_kt.png');
  218. this.deviceType.push(data.data[i])
  219. }
  220. }
  221. }
  222. },
  223. //查询物联设备
  224. async iotAppDeviceFindByType() {
  225. const {
  226. data
  227. } = await iotAppDeviceFindByType({
  228. subjectId: this.subId,
  229. typeKeyList: ['aio']
  230. });
  231. if (data.code == 200) {
  232. this.$set(this, 'terminalData', data.data);
  233. }
  234. },
  235. //MQTT订阅
  236. sensorMQTT() {
  237. let self = this;
  238. this.client = $mqtt.connect('wxs://' + uni.getStorageSync('mqttUrl'), {
  239. username: uni.getStorageSync('mqttUser'),
  240. password: uni.getStorageSync('mqttPassword')
  241. });
  242. this.client.on("connect", e => {
  243. this.client.subscribe(this.mtopic + self.subId, (err) => {
  244. if (!err) {
  245. console.log("传感器订阅成功:" + this.mtopic + self.subId);
  246. } else {
  247. // console.log("连接错误:" + err);
  248. }
  249. });
  250. this.client.subscribe(this.mtopicOne + self.subId, (err) => {
  251. if (!err) {
  252. console.log("智能控制订阅成功:" + this.mtopicOne + self.subId);
  253. } else {
  254. // console.log("连接错误:" + err);
  255. }
  256. });
  257. });
  258. this.client.on("message", (topic, message) => {
  259. console.log('message', message);
  260. console.log('topic,', topic);
  261. if (message) {
  262. if (topic == this.mtopic + this.subId) {
  263. //传感器
  264. let data = JSON.parse(message)
  265. let list = JSON.parse(JSON.stringify(this.sensorData))
  266. list.forEach((item) => {
  267. data.forEach((minItem) => {
  268. if (item.deviceNo == minItem.deviceNo) {
  269. item.deviceValue = minItem.deviceValue;
  270. item.online = minItem.online;
  271. }
  272. })
  273. })
  274. this.$set(this, 'sensorData', list);
  275. } else if (topic == this.mtopicOne + this.subId) {
  276. //智能控制
  277. let data = JSON.parse(message)
  278. let list = JSON.parse(JSON.stringify(this.labHardwareVOList))
  279. list.forEach((item) => {
  280. if (item.hardwareNo == data.hardwareNo) {
  281. item.operatingState = data.operatingState;
  282. item.online = data.online;
  283. }
  284. })
  285. this.$set(this, 'labHardwareVOList', list);
  286. }
  287. }
  288. });
  289. },
  290. //取消订阅关闭MQTT连接
  291. offMQTT(type) {
  292. let self = this;
  293. if (self.client.unsubscribe) {
  294. self.client.unsubscribe(self.mtopicOne + self.subId, error => {
  295. if (error) {
  296. // console.log('mqtt关闭连接错误:', error)
  297. }
  298. })
  299. self.client.end();
  300. this.$set(this, 'client', {});
  301. }
  302. //判断传入参数如果存在 发起一次新的连接
  303. if (type) {
  304. this.sensorMQTT();
  305. }
  306. },
  307. },
  308. beforeDestroy() {
  309. //清除定时器
  310. let self = this;
  311. self.offMQTT();
  312. },
  313. }
  314. </script>
  315. <style lang="stylus" scoped>
  316. .iotControl {
  317. width: 750rpx;
  318. .device-type {
  319. background: #fff;
  320. padding: 24rpx 20rpx;
  321. box-sizing: border-box;
  322. display: flex;
  323. justify-content: flex-start;
  324. flex-wrap: wrap;
  325. >view {
  326. //width: 356rpx;
  327. height: 80rpx;
  328. display: flex;
  329. justify-content: flex-start;
  330. align-items: center;
  331. border-right: 1px dashed #E0E0E0;
  332. border-bottom: 1px dashed #E0E0E0;
  333. padding-left: 10rpx;
  334. padding-right: 10rpx;
  335. box-sizing: border-box;
  336. >img:nth-of-type(1) {
  337. width: 42rpx;
  338. height: 42rpx;
  339. margin-right: 12rpx;
  340. }
  341. >view {
  342. font-family: PingFang SC;
  343. font-weight: 500;
  344. font-size: 30rpx;
  345. color: #222222;
  346. line-height: 80rpx;
  347. width: 244rpx;
  348. }
  349. >img:nth-of-type(2) {
  350. width: 24rpx;
  351. height: 22rpx;
  352. }
  353. }
  354. >view:nth-child(2n) {
  355. border-right: none;
  356. padding-left: 20rpx;
  357. box-sizing: border-box;
  358. }
  359. >view:nth-last-child(1) {
  360. border-bottom: none;
  361. }
  362. >view:nth-last-child(2) {
  363. border-bottom: none;
  364. }
  365. }
  366. .small-title {
  367. height: 80rpx;
  368. border-bottom: 1rpx solid #E0E0E0;
  369. background: #FFFFFF;
  370. margin-top: 20rpx;
  371. display: flex;
  372. align-items: center;
  373. >view:nth-of-type(1) {
  374. width: 4rpx;
  375. height: 34rpx;
  376. background: #0D97EB;
  377. margin: 0rpx 24rpx 0 20rpx;
  378. }
  379. >view:nth-of-type(2) {
  380. font-family: PingFang SC;
  381. font-weight: 500;
  382. font-size: 32rpx;
  383. color: #333333;
  384. line-height: 80rpx;
  385. }
  386. }
  387. .sensor {
  388. display: flex;
  389. justify-content: flex-start;
  390. flex-wrap: wrap;
  391. background: #fff;
  392. padding: 12rpx 20rpx 30rpx;
  393. box-sizing: border-box;
  394. >view {
  395. display: flex;
  396. justify-content: flex-start;
  397. align-items: center;
  398. height: 66rpx;
  399. width: 355rpx;
  400. >img {
  401. width: 42rpx;
  402. height: 42rpx;
  403. margin-right: 20rpx;
  404. }
  405. >view {
  406. font-family: PingFang SC;
  407. font-weight: 500;
  408. font-size: 28rpx;
  409. color: #222222;
  410. line-height: 30rpx;
  411. }
  412. }
  413. }
  414. .intelligent-control {
  415. background: #fff;
  416. padding: 0 20rpx;
  417. box-sizing: border-box;
  418. >view {
  419. display flex;
  420. border-bottom: 1rpx solid #e0e0e0;
  421. .for-button-p {
  422. flex: 1;
  423. line-height: 90rpx;
  424. color: #333333;
  425. font-size: 28rpx;
  426. >text {
  427. font-size: 28rpx;
  428. color: #0183FA;
  429. }
  430. }
  431. >img {
  432. height: 50rpx;
  433. width: 100rpx;
  434. margin: 20rpx 0 20rpx 0;
  435. }
  436. view:nth-child(2) {
  437. text-align right;
  438. color: #999;
  439. }
  440. .pcType-button {
  441. width: 120rpx;
  442. line-height: 42rpx;
  443. border: 4rpx solid #0183FA;
  444. border-radius: 30rpx;
  445. color: #0183FA !important;
  446. font-size: 24rpx;
  447. text-align center !important;
  448. margin: 20rpx 0 20rpx 0;
  449. }
  450. }
  451. >view:nth-last-child(1) {
  452. border-bottom: none;
  453. }
  454. }
  455. }
  456. </style>