iotControl.vue 11 KB

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