App.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/riskPlan/trigger/notice",
  32. //监听数据
  33. watchObj:{},
  34. //当前通道数组
  35. mqttIdList:[],
  36. }
  37. },
  38. globalData:{
  39. mqttAlarmData:{},
  40. mqttSensorData:{},
  41. mqttDeviceData:{},
  42. warnData:{},
  43. lineData:{},
  44. mqttfireData:{},//灭火传感器状态异常
  45. mqttOnlineData:{},//设备是否在线
  46. mqttPerformData:{},//是否正在执行灭火操作
  47. },
  48. methods: {
  49. //删除报警监听
  50. delWarnData(){
  51. delete this.watchObj.warnData;
  52. },
  53. //删除报警监听
  54. deleteAlarmWatch(){
  55. delete this.watchObj.mqttAlarmData;
  56. },
  57. //删除传感器监听
  58. deleteSensorWatch(){
  59. delete this.watchObj.mqttSensorData;
  60. },
  61. //删除设备监听
  62. deleteDeviceWatch(){
  63. delete this.watchObj.mqttDeviceData;
  64. },
  65. //删除应急监听
  66. deleteLineData(){
  67. delete this.watchObj.lineData;
  68. },
  69. //删除一键灭火-传感器状态
  70. deletefireData(){
  71. delete this.watchObj.mqttfireData;
  72. },
  73. //删除一键灭火-设备是否在线
  74. deleteOnlineData(){
  75. delete this.watchObj.mqttOnlineData;
  76. },
  77. //删除一键灭火-是否正在执行灭火
  78. deletePerformData(){
  79. delete this.watchObj.mqttPerformData;
  80. },
  81. //监听对应的mqtt数据
  82. watch:function(method,istr){
  83. this.watchObj = this.globalData;
  84. Object.defineProperty(this.watchObj,istr,{
  85. configurable:true,
  86. enumerable:true,
  87. immediate: true,
  88. deep: true,
  89. set:function(value){
  90. this._consumerGoodsStatus=value
  91. method(value)
  92. },
  93. get:function(value){
  94. return this._consumerGoodsStatus
  95. }
  96. })
  97. },
  98. // 子页面调用
  99. appMqttOn(type,list){
  100. let self = this;
  101. if(this.mqttIdList){
  102. this.offMQTT();
  103. }
  104. uni.setStorageSync('mqttIdList',JSON.stringify(list));
  105. this.mqttIdList = list;
  106. setTimeout(function(){
  107. self.subscriptionMQTT();
  108. },100);
  109. },
  110. //MQTT订阅
  111. subscriptionMQTT(){
  112. let self = this;
  113. const mqttOptions = {
  114. keepalive: 30,
  115. clean: true,//保留会话
  116. connectTimeout: 5000, // 超时时间
  117. reconnectPeriod:5000, // 重连间隔
  118. clientId: uni.getStorageSync('token')+'lab/function/data',
  119. username: uni.getStorageSync('mqttUser'),
  120. password: uni.getStorageSync('mqttPassword'),
  121. }
  122. const connectUrl = uni.getStorageSync('mqttUrl');
  123. // this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  124. this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  125. this.client.on('connect', () => {
  126. // 这是为了给自己发条消息,其它无作用
  127. for(let i=0;i<self.mqttIdList.length;i++){
  128. if(self.mqttIdList[i].type == 'lab/function/data'){
  129. //传感器
  130. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  131. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], (err) => {
  132. if (!err) {
  133. console.log("传感器信息订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].idList[o])
  134. }
  135. });
  136. }
  137. }else if(self.mqttIdList[i].type == 'lab/hardware/data'){
  138. //传感器
  139. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  140. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], (err) => {
  141. if (!err) {
  142. console.log("硬件订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].idList[o])
  143. }
  144. });
  145. }
  146. }else if(self.mqttIdList[i].type == 'lab/bigview'){
  147. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  148. if (!err) {
  149. console.log("报警信息订阅成功",self.mqttIdList[i].type)
  150. }
  151. });
  152. }else if(self.mqttIdList[i].type == 'lab/riskPlan/trigger/notice'){
  153. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  154. if (!err) {
  155. console.log("应急疏散订阅成功",self.mqttIdList[i].type)
  156. }
  157. });
  158. }else if(self.mqttIdList[i].type == 'lab/fireDevice/Warn/'){
  159. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].fireCode, (err) => {
  160. if (!err) {
  161. console.log("火灾传感器状态-订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].fireCode)
  162. }
  163. });
  164. }else if(self.mqttIdList[i].type == 'lab/fireDevice/isOnline/'){
  165. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].fireCode, (err) => {
  166. if (!err) {
  167. console.log("火灾设备是否在线-订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].fireCode)
  168. }
  169. });
  170. }else if(self.mqttIdList[i].type == 'lab/fireDevice/executing/'){
  171. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].fireCode, (err) => {
  172. if (!err) {
  173. console.log("火灾是否正在执行灭火操作-订阅成功:",self.mqttIdList[i].type+self.mqttIdList[i].fireCode)
  174. }
  175. });
  176. }
  177. }
  178. });
  179. // 自动重连
  180. this.client.on('reconnect', (msg) => {
  181. console.log('自动重连-reconnect-1', msg)
  182. });
  183. // 错误
  184. this.client.on('error', () => {
  185. console.log('错误-error-1')
  186. });
  187. // 断开
  188. this.client.on('end', () => {
  189. console.log('断开-end-1')
  190. });
  191. // 掉线
  192. this.client.on('offline', (msg) => {
  193. console.log('掉线-offline-1',msg)
  194. });
  195. // 收到消息
  196. this.client.on('message', (topic, message) => {
  197. let data = JSON.parse(message)
  198. //console.log('消息message',data);
  199. if(data){
  200. if(topic == 'lab/riskPlan/trigger/notice'){
  201. // console.log("应急",data);
  202. this.globalData.lineData = data.data;
  203. }else if(topic.search("lab/fireDevice/Warn/") != -1){
  204. console.log("一键灭火-传感器状态",data);
  205. this.globalData.mqttfireData = data;
  206. }else if(topic.search("lab/fireDevice/isOnline/") != -1){
  207. console.log("一键灭火-设备是否在线",data);
  208. this.globalData.mqttOnlineData = data;
  209. }else if(topic.search("lab/fireDevice/executing/") != -1){
  210. console.log("一键灭火-是否正在执行灭火",data);
  211. this.globalData.mqttPerformData = data;
  212. }else if(topic == 'lab/bigview'){
  213. // console.log("报警",data);
  214. if(data.data == 'SUB_RISKPLAN_GROUP'){
  215. this.globalData.mqttAlarmData = data.data;
  216. }
  217. }else {
  218. if(data.data.subId){
  219. // console.log('传感器',data);
  220. this.globalData.mqttSensorData = data.data;
  221. }else{
  222. // console.log("设备开关",data);
  223. this.globalData.mqttDeviceData = data.data;
  224. }
  225. }
  226. }
  227. })
  228. // 全局监听是否有关闭mqtt的消息的事件
  229. uni.$on('closeMqtt',() => {
  230. this.client.end(true); // 主动断开连接
  231. })
  232. },
  233. //取消订阅关闭MQTT连接
  234. offMQTT(){
  235. let self = this;
  236. if(self.mqttIdList[0]){
  237. uni.removeStorageSync('mqttIdList')
  238. for(let i=0;i<self.mqttIdList.length;i++){
  239. if(self.mqttIdList[i].type == 'lab/function/data'){
  240. //断开传感器
  241. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  242. self.client.unsubscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], error => {
  243. if (error) {
  244. console.log('Unsubscribe error mtopicBigview', error)
  245. }
  246. })
  247. }
  248. }else if(self.mqttIdList[i].type == 'lab/hardware/data'){
  249. //断开硬件
  250. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  251. self.client.unsubscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], error => {
  252. if (error) {
  253. console.log('Unsubscribe error mtopicBigview', error)
  254. }
  255. })
  256. }
  257. }else if(self.mqttIdList[i].type == 'lab/bigview'){
  258. //断开报警
  259. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  260. if (error) {
  261. console.log('Unsubscribe error mtopicBigview', error)
  262. }
  263. })
  264. }else if(self.mqttIdList[i].type == 'lab/riskPlan/trigger/notice'){
  265. //断开疏散
  266. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  267. if (error) {
  268. console.log('Unsubscribe error mtopicBigview', error)
  269. }
  270. })
  271. }
  272. }
  273. this.client.end(true);
  274. this.client = null;
  275. this.mqttIdList = [];
  276. }
  277. },
  278. }
  279. }
  280. </script>
  281. <style lang="stylus">
  282. page{
  283. height:100%;
  284. width:100%;
  285. background:#f5f5f5;
  286. font-family: PingFang SC;
  287. font-weight: 400;
  288. font-size:30rpx;
  289. color:#333;
  290. }
  291. img{
  292. margin:0;
  293. padding:0;
  294. display:block;
  295. }
  296. .name-box-p{
  297. display block
  298. overflow:hidden;
  299. text-overflow:ellipsis;
  300. white-space:nowrap;
  301. }
  302. view{
  303. margin:0;
  304. padding:0;
  305. }
  306. /*每个页面公共css */
  307. @import './styles/index.styl'
  308. </style>