App.vue 11 KB

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