iotControl.vue 10 KB

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