App.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <script>
  2. import {
  3. systemInfo
  4. } from '@/component/system-info.js'
  5. import $mqtt from '@/utils/mqtt.min.js';
  6. import {
  7. config
  8. } from '@/api/request/config.js'
  9. export default {
  10. onLaunch: function() {
  11. /* 获取设备信息 */
  12. const SystemInfomations = systemInfo()
  13. this.navHeight = SystemInfomations.navHeight + SystemInfomations.statusBarHeight //头部导航栏总高度
  14. uni.setStorageSync('navHeight', this.navHeight)
  15. // #ifdef MP-WEIXIN
  16. //小程序热更新
  17. if (wx.canIUse('getUpdateManager')) {
  18. const updateManager = wx.getUpdateManager()
  19. updateManager.onCheckForUpdate(function(res) {
  20. if (res.hasUpdate) {
  21. updateManager.onUpdateReady(function() {
  22. wx.showModal({
  23. title: '更新提示',
  24. content: '新版本已经准备好,是否重启应用?',
  25. success: function(res) {
  26. if (res.confirm) {
  27. updateManager.applyUpdate()
  28. }
  29. }
  30. })
  31. })
  32. updateManager.onUpdateFailed(function() {
  33. wx.showModal({
  34. title: '已经有新版本了哟~',
  35. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  36. })
  37. })
  38. }
  39. })
  40. } else {
  41. wx.showModal({
  42. title: '提示',
  43. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  44. })
  45. }
  46. // #endif
  47. },
  48. onShow: function() {},
  49. onHide: function() {},
  50. data() {
  51. return {
  52. //MQTT数据
  53. client: {},
  54. //MQTT请求参数
  55. mtopicBigview: "lab/bigview",
  56. mtopicFunction: "lab/function/data",
  57. mtopicHardware: "lab/hardware/data",
  58. mtopicLine: "lab/riskPlan/trigger/notice",
  59. //监听数据
  60. watchObj: {},
  61. //当前通道数组
  62. mqttIdList: [],
  63. }
  64. },
  65. globalData: {
  66. mqttAlarmData: {},
  67. mqttSensorData: {},
  68. mqttDeviceData: {},
  69. warnData: {},
  70. lineData: {},
  71. mqttfireData: {}, //灭火传感器状态异常
  72. mqttOnlineData: {}, //设备是否在线
  73. mqttPerformData: {}, //是否正在执行灭火操作
  74. },
  75. created() {
  76. },
  77. methods: {
  78. //删除报警监听
  79. delWarnData() {
  80. delete this.watchObj.warnData;
  81. },
  82. //删除报警监听
  83. deleteAlarmWatch() {
  84. delete this.watchObj.mqttAlarmData;
  85. },
  86. //删除传感器监听
  87. deleteSensorWatch() {
  88. delete this.watchObj.mqttSensorData;
  89. },
  90. //删除设备监听
  91. deleteDeviceWatch() {
  92. delete this.watchObj.mqttDeviceData;
  93. },
  94. //删除应急监听
  95. deleteLineData() {
  96. delete this.watchObj.lineData;
  97. },
  98. //删除一键灭火-传感器状态
  99. deletefireData() {
  100. delete this.watchObj.mqttfireData;
  101. },
  102. //删除一键灭火-设备是否在线
  103. deleteOnlineData() {
  104. delete this.watchObj.mqttOnlineData;
  105. },
  106. //删除一键灭火-是否正在执行灭火
  107. deletePerformData() {
  108. delete this.watchObj.mqttPerformData;
  109. },
  110. //监听对应的mqtt数据
  111. watch: function(method, istr) {
  112. this.watchObj = this.globalData;
  113. Object.defineProperty(this.watchObj, istr, {
  114. configurable: true,
  115. enumerable: true,
  116. immediate: true,
  117. deep: true,
  118. set: function(value) {
  119. this._consumerGoodsStatus = value
  120. method(value)
  121. },
  122. get: function(value) {
  123. return this._consumerGoodsStatus
  124. }
  125. })
  126. },
  127. // 子页面调用
  128. appMqttOn(type, list) {
  129. let self = this;
  130. if (this.mqttIdList) {
  131. this.offMQTT();
  132. }
  133. uni.setStorageSync('mqttIdList', JSON.stringify(list));
  134. this.mqttIdList = list;
  135. setTimeout(function() {
  136. self.subscriptionMQTT();
  137. }, 100);
  138. },
  139. //MQTT订阅
  140. subscriptionMQTT() {
  141. let self = this;
  142. const mqttOptions = {
  143. keepalive: 30,
  144. clean: true, //保留会话
  145. connectTimeout: 5000, // 超时时间
  146. reconnectPeriod: 5000, // 重连间隔
  147. clientId: uni.getStorageSync('token') + 'lab/function/data',
  148. username: uni.getStorageSync('mqttUser'),
  149. password: uni.getStorageSync('mqttPassword'),
  150. }
  151. const connectUrl = uni.getStorageSync('mqttUrl');
  152. // this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  153. this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  154. this.client.on('connect', () => {
  155. // 这是为了给自己发条消息,其它无作用
  156. for (let i = 0; i < self.mqttIdList.length; i++) {
  157. if (self.mqttIdList[i].type == 'lab/function/data') {
  158. //传感器
  159. for (let o = 0; o < self.mqttIdList[i].idList.length; o++) {
  160. self.client.subscribe(self.mqttIdList[i].type + self.mqttIdList[i].idList[o], (err) => {
  161. if (!err) {
  162. console.log("传感器信息订阅成功", self.mqttIdList[i].type + self.mqttIdList[i].idList[o])
  163. }
  164. });
  165. }
  166. } else if (self.mqttIdList[i].type == 'lab/hardware/data') {
  167. //传感器
  168. for (let o = 0; o < self.mqttIdList[i].idList.length; o++) {
  169. self.client.subscribe(self.mqttIdList[i].type + self.mqttIdList[i].idList[o], (err) => {
  170. if (!err) {
  171. console.log("硬件订阅成功", self.mqttIdList[i].type + self.mqttIdList[i].idList[o])
  172. }
  173. });
  174. }
  175. } else if (self.mqttIdList[i].type == 'lab/bigview') {
  176. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  177. if (!err) {
  178. console.log("报警信息订阅成功", self.mqttIdList[i].type)
  179. }
  180. });
  181. } else if (self.mqttIdList[i].type == 'lab/riskPlan/trigger/notice') {
  182. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  183. if (!err) {
  184. console.log("应急疏散订阅成功", self.mqttIdList[i].type)
  185. }
  186. });
  187. } else if (self.mqttIdList[i].type == 'lab/fireDevice/Warn/') {
  188. self.client.subscribe(self.mqttIdList[i].type + self.mqttIdList[i].fireCode, (err) => {
  189. if (!err) {
  190. console.log("火灾传感器状态-订阅成功", self.mqttIdList[i].type + self.mqttIdList[i].fireCode)
  191. }
  192. });
  193. } else if (self.mqttIdList[i].type == 'lab/fireDevice/isOnline/') {
  194. self.client.subscribe(self.mqttIdList[i].type + self.mqttIdList[i].fireCode, (err) => {
  195. if (!err) {
  196. console.log("火灾设备是否在线-订阅成功", self.mqttIdList[i].type + self.mqttIdList[i].fireCode)
  197. }
  198. });
  199. } else if (self.mqttIdList[i].type == 'lab/fireDevice/executing/') {
  200. self.client.subscribe(self.mqttIdList[i].type + self.mqttIdList[i].fireCode, (err) => {
  201. if (!err) {
  202. console.log("火灾是否正在执行灭火操作-订阅成功:", self.mqttIdList[i].type + self.mqttIdList[i].fireCode)
  203. }
  204. });
  205. }
  206. }
  207. });
  208. // 自动重连
  209. this.client.on('reconnect', (msg) => {
  210. console.log('自动重连-reconnect-1', msg)
  211. });
  212. // 错误
  213. this.client.on('error', () => {
  214. console.log('错误-error-1')
  215. });
  216. // 断开
  217. this.client.on('end', () => {
  218. console.log('断开-end-1')
  219. });
  220. // 掉线
  221. this.client.on('offline', (msg) => {
  222. console.log('掉线-offline-1', msg)
  223. });
  224. // 收到消息
  225. this.client.on('message', (topic, message) => {
  226. let data = JSON.parse(message)
  227. //console.log('消息message',data);
  228. if (data) {
  229. if (topic == 'lab/riskPlan/trigger/notice') {
  230. // console.log("应急",data);
  231. this.globalData.lineData = data.data;
  232. } else if (topic.search("lab/fireDevice/Warn/") != -1) {
  233. console.log("一键灭火-传感器状态", data);
  234. this.globalData.mqttfireData = data;
  235. } else if (topic.search("lab/fireDevice/isOnline/") != -1) {
  236. console.log("一键灭火-设备是否在线", data);
  237. this.globalData.mqttOnlineData = data;
  238. } else if (topic.search("lab/fireDevice/executing/") != -1) {
  239. console.log("一键灭火-是否正在执行灭火", data);
  240. this.globalData.mqttPerformData = data;
  241. } else if (topic == 'lab/bigview') {
  242. if (data.data == 'SUB_RISKPLAN_GROUP') {
  243. this.globalData.mqttAlarmData = data.data;
  244. }
  245. } else {
  246. if (data.data.subId) {
  247. this.globalData.mqttSensorData = data.data;
  248. } else {
  249. this.globalData.mqttDeviceData = data.data;
  250. }
  251. }
  252. }
  253. })
  254. // 全局监听是否有关闭mqtt的消息的事件
  255. uni.$on('closeMqtt', () => {
  256. this.client.end(true); // 主动断开连接
  257. })
  258. },
  259. //取消订阅关闭MQTT连接
  260. offMQTT() {
  261. let self = this;
  262. if (self.mqttIdList[0]) {
  263. uni.removeStorageSync('mqttIdList')
  264. for (let i = 0; i < self.mqttIdList.length; i++) {
  265. if (self.mqttIdList[i].type == 'lab/function/data') {
  266. //断开传感器
  267. for (let o = 0; o < self.mqttIdList[i].idList.length; o++) {
  268. self.client.unsubscribe(self.mqttIdList[i].type + self.mqttIdList[i].idList[o], error => {
  269. if (error) {
  270. console.log('Unsubscribe error mtopicBigview', error)
  271. }
  272. })
  273. }
  274. } else if (self.mqttIdList[i].type == 'lab/hardware/data') {
  275. //断开硬件
  276. for (let o = 0; o < self.mqttIdList[i].idList.length; o++) {
  277. self.client.unsubscribe(self.mqttIdList[i].type + self.mqttIdList[i].idList[o], error => {
  278. if (error) {
  279. console.log('Unsubscribe error mtopicBigview', error)
  280. }
  281. })
  282. }
  283. } else if (self.mqttIdList[i].type == 'lab/bigview') {
  284. //断开报警
  285. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  286. if (error) {
  287. console.log('Unsubscribe error mtopicBigview', error)
  288. }
  289. })
  290. } else if (self.mqttIdList[i].type == 'lab/riskPlan/trigger/notice') {
  291. //断开疏散
  292. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  293. if (error) {
  294. console.log('Unsubscribe error mtopicBigview', error)
  295. }
  296. })
  297. }
  298. }
  299. this.client.end(true);
  300. this.client = null;
  301. this.mqttIdList = [];
  302. }
  303. },
  304. }
  305. }
  306. </script>
  307. <style lang="stylus">
  308. page {
  309. height: 100%;
  310. width: 100%;
  311. background: #f5f5f5;
  312. font-family: PingFang SC;
  313. font-weight: 400;
  314. font-size: 30rpx;
  315. color: #333;
  316. }
  317. img {
  318. margin: 0;
  319. padding: 0;
  320. display: block;
  321. }
  322. .name-box-p {
  323. display block;
  324. overflow: hidden;
  325. text-overflow: ellipsis;
  326. white-space: nowrap;
  327. }
  328. view {
  329. margin: 0;
  330. padding: 0;
  331. }
  332. /*每个页面公共css */
  333. @import './styles/index.styl'
  334. </style>