App.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <script>
  2. import $mqtt from '@/utils/mqtt.min.js';
  3. import { config } from '@/api/request/config.js'
  4. export default {
  5. onLaunch: function() {
  6. console.log('App Launch')
  7. //获取openid
  8. uni.login({
  9. success(loginRes) {
  10. console.log('获取code成功');
  11. uni.setStorageSync('code', loginRes.code);
  12. }
  13. })
  14. },
  15. onShow: function() {
  16. // this.onMQTT();
  17. console.log('App Show')
  18. },
  19. onHide: function() {
  20. // this.offMQTT();
  21. console.log('App Hide')
  22. },
  23. data(){
  24. return{
  25. //MQTT数据
  26. client:{},
  27. //MQTT请求参数
  28. mtopicBigview:"lab/bigview",
  29. mtopicFunction:"lab/function/data",
  30. mtopicHardware:"lab/hardware/data",
  31. mtopicLine:"lab/exit/line",
  32. //监听数据
  33. watchObj:{},
  34. //当前通道数组
  35. mqttIdList:[],
  36. }
  37. },
  38. globalData:{
  39. mqttAlarmData:{},
  40. mqttSensorData:{},
  41. mqttDeviceData:{},
  42. warnData:{},
  43. lineData:{},
  44. },
  45. methods: {
  46. //删除报警监听
  47. delWarnData(){
  48. delete this.watchObj.warnData;
  49. },
  50. //删除报警监听
  51. deleteAlarmWatch(){
  52. delete this.watchObj.mqttAlarmData;
  53. },
  54. //删除传感器监听
  55. deleteSensorWatch(){
  56. delete this.watchObj.mqttSensorData;
  57. },
  58. //删除设备监听
  59. deleteDeviceWatch(){
  60. delete this.watchObj.mqttDeviceData;
  61. },
  62. //删除应急监听
  63. deleteLineData(){
  64. delete this.watchObj.lineData;
  65. },
  66. //监听对应的mqtt数据
  67. watch:function(method,istr){
  68. this.watchObj = this.globalData;
  69. Object.defineProperty(this.watchObj,istr,{
  70. configurable:true,
  71. enumerable:true,
  72. immediate: true,
  73. deep: true,
  74. set:function(value){
  75. this._consumerGoodsStatus=value
  76. method(value)
  77. },
  78. get:function(value){
  79. return this._consumerGoodsStatus
  80. }
  81. })
  82. },
  83. // 子页面调用
  84. appMqttOn(type,list){
  85. let self = this;
  86. if(this.mqttIdList){
  87. this.offMQTT();
  88. }
  89. uni.setStorageSync('mqttIdList',JSON.stringify(list));
  90. this.mqttIdList = list;
  91. setTimeout(function(){
  92. self.subscriptionMQTT();
  93. },100);
  94. },
  95. //MQTT订阅
  96. subscriptionMQTT(){
  97. let self = this;
  98. const mqttOptions = {
  99. keepalive: 30,
  100. clean: true,//保留会话
  101. connectTimeout: 5000, // 超时时间
  102. reconnectPeriod:5000, // 重连间隔
  103. clientId: uni.getStorageSync('token')+'lab/function/data',
  104. username: uni.getStorageSync('mqttUrl'),
  105. password: uni.getStorageSync('mqttUrl'),
  106. }
  107. const connectUrl = uni.getStorageSync('mqttUrl');
  108. // this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  109. this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  110. this.client.on('connect', () => {
  111. // 这是为了给自己发条消息,其它无作用
  112. for(let i=0;i<self.mqttIdList.length;i++){
  113. if(self.mqttIdList[i].type == 'lab/function/data'){
  114. //传感器
  115. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  116. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], (err) => {
  117. if (!err) {
  118. console.log("传感器信息订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].idList[o])
  119. }
  120. });
  121. }
  122. }else if(self.mqttIdList[i].type == 'lab/hardware/data'){
  123. //传感器
  124. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  125. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], (err) => {
  126. if (!err) {
  127. console.log("硬件订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].idList[o])
  128. }
  129. });
  130. }
  131. }else if(self.mqttIdList[i].type == 'lab/bigview'){
  132. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  133. if (!err) {
  134. console.log("报警信息订阅成功",self.mqttIdList[i].type)
  135. }
  136. });
  137. }else if(self.mqttIdList[i].type == 'lab/exit/line'){
  138. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  139. if (!err) {
  140. console.log("应急疏散订阅成功",self.mqttIdList[i].type)
  141. }
  142. });
  143. }else if(self.mqttIdList[i].type == 'lab/fireDevice/Warn/'){
  144. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  145. if (!err) {
  146. console.log("一键灭火订阅成功",self.mqttIdList[i].type)
  147. }
  148. });
  149. }
  150. }
  151. });
  152. // 自动重连
  153. this.client.on('reconnect', (msg) => {
  154. console.log('自动重连-reconnect-1', msg)
  155. });
  156. // 错误
  157. this.client.on('error', () => {
  158. console.log('错误-error-1')
  159. });
  160. // 断开
  161. this.client.on('end', () => {
  162. console.log('断开-end-1')
  163. });
  164. // 掉线
  165. this.client.on('offline', (msg) => {
  166. console.log('掉线-offline-1',msg)
  167. });
  168. // 收到消息
  169. this.client.on('message', (topic, message) => {
  170. let data = JSON.parse(message)
  171. // console.log('消息message',data);
  172. if(data){
  173. if(topic == 'lab/exit/line'){
  174. // console.log("应急",data);
  175. this.globalData.lineData = data.data;
  176. }else if(topic == 'lab/bigview'){
  177. // console.log("报警",data);
  178. if(data.data == 'SUB_RISKPLAN_GROUP'){
  179. this.globalData.mqttAlarmData = data.data;
  180. }
  181. }else {
  182. if(data.data.subId){
  183. // console.log('传感器',data);
  184. this.globalData.mqttSensorData = data.data;
  185. }else{
  186. // console.log("设备开关",data);
  187. this.globalData.mqttDeviceData = data.data;
  188. }
  189. }
  190. }
  191. })
  192. // 全局监听是否有关闭mqtt的消息的事件
  193. uni.$on('closeMqtt',() => {
  194. this.client.end(true); // 主动断开连接
  195. })
  196. },
  197. //取消订阅关闭MQTT连接
  198. offMQTT(){
  199. let self = this;
  200. if(self.mqttIdList[0]){
  201. uni.removeStorageSync('mqttIdList')
  202. for(let i=0;i<self.mqttIdList.length;i++){
  203. if(self.mqttIdList[i].type == 'lab/function/data'){
  204. //断开传感器
  205. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  206. self.client.unsubscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], error => {
  207. if (error) {
  208. console.log('Unsubscribe error mtopicBigview', error)
  209. }
  210. })
  211. }
  212. }else if(self.mqttIdList[i].type == 'lab/hardware/data'){
  213. //断开硬件
  214. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  215. self.client.unsubscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], error => {
  216. if (error) {
  217. console.log('Unsubscribe error mtopicBigview', error)
  218. }
  219. })
  220. }
  221. }else if(self.mqttIdList[i].type == 'lab/bigview'){
  222. //断开报警
  223. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  224. if (error) {
  225. console.log('Unsubscribe error mtopicBigview', error)
  226. }
  227. })
  228. }else if(self.mqttIdList[i].type == 'lab/exit/line'){
  229. //断开疏散
  230. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  231. if (error) {
  232. console.log('Unsubscribe error mtopicBigview', error)
  233. }
  234. })
  235. }
  236. }
  237. this.client.end(true);
  238. this.client = null;
  239. this.mqttIdList = [];
  240. }
  241. },
  242. }
  243. }
  244. </script>
  245. <style lang="stylus">
  246. page{
  247. height:100%;
  248. width:100%;
  249. background:#f5f5f5;
  250. font-family: PingFang SC;
  251. font-weight: 400;
  252. font-size:30rpx;
  253. color:#333;
  254. }
  255. img{
  256. margin:0;
  257. padding:0;
  258. display:block;
  259. }
  260. .name-box-p{
  261. display block
  262. overflow:hidden;
  263. text-overflow:ellipsis;
  264. white-space:nowrap;
  265. }
  266. view{
  267. margin:0;
  268. padding:0;
  269. }
  270. /*每个页面公共css */
  271. @import './styles/index.styl'
  272. </style>