iotControl.vue 10 KB

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