notice.js 20.4 KB
Newer Older
崔佳豪's avatar
崔佳豪 committed
1 2 3 4 5
import logger from '@wisdom-utils/utils/lib/logger';
import _, { isEmpty, isString } from 'lodash';
import MqttClient from 'mqtt-client';
import { isJSON } from '@wisdom-utils/components/lib/AppLayout/helpers/index';
import { api } from './api';
6
import { replaceSpeak, generatedId } from './utils';
崔佳豪's avatar
崔佳豪 committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
import { postInformationStatus } from './api/service';
import {
  DEFAULT_KEEPLIVE,
  DEFAULT_MQTT_PATH,
  DEFAULT_PARSE_LEVEL,
  DEFAULT_TCP_IP,
  DEFAULT_TCP_PORT,
  DEFAULT_TIMEOUT,
  ERR_OK,
  MESSAGE_TYPE,
  NEW_MESSAGE,
  PASSWORD,
  PLATFORM_LEVEL,
  REQUEST_SERVICE,
  SYS_LEVEL,
  USERNAME,
  VIDEO_LEVEL,
} from './constants';
import { createMessageFromHis, createMessageFromReal } from './message';

/* eslint-disable */
// eslint-disable-next-line no-undef
const Logger = logger('mqtt');
class Notifier {
  constructor(userInfo, renderVideo, renderPlatform, renderSysPlatform, props) {
    this.userInfo = userInfo;
    this.messageCache = {
      totalCount: 0,
      messages: [],
    };
    // 当前消息缓存
    this._subscribers = {}; // 订阅器缓存
    this._siteConfig = {
      site_code: this.userInfo.site,
      TcpIP: '',
      TcpPort: DEFAULT_TCP_PORT,
      TimeOut: '',
      KeepAlive: '',
      IsSSL: true,
      mqtt_path: DEFAULT_MQTT_PATH,
      mqtt_mess: {},
      nginxStart: false,
    };
    this.MQTTCount = 0;
    this.MQTTClient = null;
    this.MQTTOptions = {};
    this.IsNeedReconnect = true;
    this.currentPageIndex = 1;
    this.currentPageSize = 10;

    this.start = this.start.bind(this);
    this.stop = this.stop.bind(this);
    this.subscribe = this.subscribe.bind(this);
    this.unsubscribe = this.unsubscribe.bind(this);
    this.confirmRead = this.confirmRead.bind(this);
    this.loadMore = this.loadMore.bind(this);
    this.hasMore = this.hasMore.bind(this);
    this.renderVideo = renderVideo;
    this.renderPlatform = renderPlatform;
    this.renderSysPlatform = renderSysPlatform;
    this.props = props;

    this.speakState = new window.SpeechSynthesisUtterance();
    this.speakState.rate = 1;
    this.speakState.lang = 'zh';
  }

  // 对外接口
  async start() {
    this.getMqttSiteCode().then(res => {
      this.loadHisMessages(this.currentPageIndex, this.currentPageSize);
      this.connectMQTTServer();
    });
  }

  stop() {
    this.disconnectMQTTServer();
  }

  subscribe(type, handler) {
    if (!(type in this._subscribers)) {
      this._subscribers[type] = [];
    }
    this._subscribers[type].push(handler);
  }

  unsubscribe(type, handler) {
    if (!(type in this._subscribers)) {
      logger.info('无效事件无法删除');
    }
    if (!handler) {
      delete this._subscribers[type];
    } else {
      const idx = this._subscribers[type].findIndex(ele => ele === handler);
      if (idx === -1) {
        logger.info('无效事件无法删除');
        return;
      }
      this._subscribers[type].splice(idx, 1);
      if (this._subscribers[type].length === 0) {
        delete this._subscribers[type];
      }
    }
  }

  publish(type, payload) {
    if (!(type in this._subscribers) || this._subscribers[type].length === 0) {
      return;
    }
    this._subscribers[type].forEach(handler => {
      try {
        handler(payload);
      } catch (e) {
        // eslint-disable-next-line no-undef
        logger.warn(`订阅器委托错误${e.message}`);
      }
    });
  }

  confirmRead(isAll = false, hisIDs = []) {
    if (this.messageCache && this.messageCache.totalCount === 0 && this.messageCache.messages.length === 0) {
      return;
    }
    if (isAll) hisIDs = this.messageCache.messages.map(item => item.id);
    const self = this;
    // eslint-disable-next-line no-undef
    postInformationStatus({
      query: {
        userID: this.userInfo.OID || window.globalConfig.userInfo.OID,
        hisID: hisIDs.join(','),
        isAll: isAll ? 1 : '',
      },
    })
      .then(res => {
        if (res.code !== 0) {
          Logger.info(res.errMsg);
          return;
        }
        if (isAll) {
          self.messageCache.totalCount = 0;
          self.messageCache.messages = [];
          self.currentPageIndex = 1;
        } else {
          hisIDs.forEach(id => {
            const index = self.messageCache.messages.findIndex(item => item.id === id);
            if (index > -1) {
              self.messageCache.messages.splice(index, 1);
              // eslint-disable-next-line no-plusplus
              self.messageCache.totalCount--;
            }
          });
        }
        self.publish(NEW_MESSAGE, self.messageCache);
      })
      .catch(err => {
        // eslint-disable-next-line no-undef
        logger.error(`postInformationStatus调用失败${err}`);
      });
  }

  hasMore() {
    if (!this.messageCache) return false;
    if (!this.messageCache.totalCount) return false;
    return this.messageCache.totalCount > this.messageCache.messages.length;
  }

  loadMore(callback) {
    if (!this.hasMore()) return Promise.resolve([]);
    this.currentPageIndex += 1;
    return this.loadHisMessages(this.currentPageIndex, this.currentPageSize);
  }

  // mqtt
  async connectMQTTServer() {
    const hostname = this._siteConfig.TcpIP;
    const port = this._siteConfig.TcpPort;
    const clientId = `client-${generatedId()}`;
    const timeout = DEFAULT_TIMEOUT;
    const keepAlive = DEFAULT_KEEPLIVE;
    const cleanSession = true;
    const ssl = this._siteConfig.IsSSL;
    const userName = USERNAME;
    const password = PASSWORD;
    const path = this._siteConfig.mqtt_path;
杨思琦's avatar
杨思琦 committed
191
    const siteCode = this._siteConfig.site_code ?? '';
崔佳豪's avatar
崔佳豪 committed
192 193
    this.MQTTCount = 0;
    if (hostname) {
杨思琦's avatar
杨思琦 committed
194
      this.MQTTClient = new MqttClient.Client(hostname, port, `${path}?_site=${siteCode}`, clientId);
崔佳豪's avatar
崔佳豪 committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      this.MQTTOptions = {
        invocationContext: {
          host: hostname,
          port,
          path,
          clientId,
        },
        timeout,
        keepAliveInterval: keepAlive,
        cleanSession,
        useSSL: ssl,
        userName,
        password,
        onSuccess: this.onMQTTConnect.bind(this),
        onFailure(e) {
          console.log(e);
        },
      };
      this.MQTTClient.connect(this.MQTTOptions);
      this.MQTTClient.onConnectionLost = this.onMQTTConnectionLost.bind(this);
      this.MQTTClient.onMessageArrived = this.onMessageArrived.bind(this);
    }
  }

  disconnectMQTTServer() {
    if (this.MQTTClient) {
      this.IsNeedReconnect = false;
      this.MQTTClient.disconnect();
      this.MQTTClient = null;
    }
  }

  async getMqttSiteCode() {
    const self = this;
    return api.getMqttSiteCode({ 'request.preventCache': Date.now() }).then(res => {
230
      if (res && res.code === 0) {
崔佳豪's avatar
崔佳豪 committed
231 232 233 234 235 236
        let mqttConfig = {
          mqtt_mess: {},
          mqtt_path: self._siteConfig.mqtt_path,
          nginxStart: self._siteConfig.NginxStart,
          mqtt_IsSSL: true,
        };
237 238 239
        if (Array.isArray(res.data) && res.data.length > 0) {
          if (res.data[0]) {
            const data = res.data[0];
崔佳豪's avatar
崔佳豪 committed
240
            mqttConfig.mqtt_IsSSL = self._siteConfig.IsSSL = data.IsSSL ? data.IsSSL : false;
李纪文's avatar
李纪文 committed
241
            mqttConfig.mqtt_mess.site_code = mqttConfig.mqtt_site_code = self._siteConfig.site_code = data.SiteCode || self._siteConfig.site_code;
崔佳豪's avatar
崔佳豪 committed
242 243 244 245 246 247 248 249 250 251
            mqttConfig.mqtt_mess.TcpIP = self._siteConfig.TcpIP = data.TcpIP;
            mqttConfig.mqtt_mess.TcpPort = self._siteConfig.TcpPort = data.TcpPort ? parseInt(data.TcpPort) : 8083;
            mqttConfig.mqtt_mess.MessageLevel = self._siteConfig.MessageLevel = data.MessageLevel
              ? data.MessageLevel
              : DEFAULT_PARSE_LEVEL;

            if (data.NginxStart) {
              mqttConfig.NginxStart = self._siteConfig.NginxStart = data.NginxStart;
              mqttConfig.mqtt_mess.TcpIP = self._siteConfig.TcpIP = self._siteConfig.mqtt_mess.TcpIP =
                window.location.hostname;
252
              mqttConfig.mqtt_mess.TcpPort = self._siteConfig.TcpPort = self._siteConfig.mqtt_mess.TcpPort = parseInt(window.location.port) || 443;
崔佳豪's avatar
崔佳豪 committed
253 254 255 256 257 258 259 260 261 262 263 264
              mqttConfig.mqtt_path = self._siteConfig.mqtt_path = '/ws/';
            } else {
              mqttConfig.nginxStart = data.NginxStart;
            }
          } else {
            mqttConfig.mqtt_mess.TcpIP = self._siteConfig.TcpIP = self._siteConfig.mqtt_mess.TcpIP = DEFAULT_TCP_IP;
            mqttConfig.mqtt_mess.TcpPort = self._siteConfig.TcpPort = self._siteConfig.mqtt_mess.TcpPort = DEFAULT_TCP_PORT;
            mqttConfig.mqtt_IsSSL = self._siteConfig.IsSSL =
              self._siteConfig.mqtt_mess.TcpIP + ':' + self._siteConfig.mqtt_mess.TcpPort;
          }

          mqttConfig.mqtt_iotIP = self._siteConfig.mqtt_iotIP =
杨思琦's avatar
杨思琦 committed
265
            mqttConfig.mqtt_mess.TcpIP + ':' + (mqttConfig.mqtt_mess.TcpPort ? mqttConfig.mqtt_mess.TcpPort : '443');
崔佳豪's avatar
崔佳豪 committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

          // self.props.updateConfig && self.props.updateConfig(Object.assign({}, self.props.global, {
          //   ...mqttConfig
          // }))

          // 应用共享状态只在initMicro中更新了一次,异步修改了的globalConfig,子应用读取到的是旧的,需要更新一下应用共享状态
          // setGlobalState({
          //   globalConfig: window.globalConfig
          // });
        }
      } else {
        Logger.info('获取mqtt服务器参数失败');
      }
    });
  }

  getSiteCode() {
    return this._siteConfig.site_code;
  }

  getUserInfo() {
    return this.userInfo;
  }

  getSiteConfig() {
    return this._siteConfig;
  }

  getMessageVersion() {
    return this._siteConfig.MessageLevel;
  }

  onMQTTConnect() {
    const site = this.getSiteCode();
    // 信息化主题
    this.MQTTClient.subscribe(site + REQUEST_SERVICE.EIMTopic);
    // 节水主题
    this.MQTTClient.subscribe(site + REQUEST_SERVICE.SaveWaTopic);
    // 系统主题
    this.MQTTClient.subscribe(site + REQUEST_SERVICE.SystemTopic);
    // 工单主题
    this.MQTTClient.subscribe(site + REQUEST_SERVICE.WorkerOrderTopic);
    // 报警主题
    this.MQTTClient.subscribe(site + REQUEST_SERVICE.ScadaTopic);
    // 用户主题
    this.MQTTClient.subscribe(`${site}${REQUEST_SERVICE.UserTopic}${this.userInfo.OID}`);
  }

  onMQTTConnectionLost(responseObject) {
    const self = this;
    if (this.IsNeedReconnect) {
      this.MQTTClient.connect(self.MQTTOptions);
      this.MQTTtester = setInterval(function() {
        if (self.MQTTClient.isConnected) {
          clearInterval(self.MQTTtester);
        } else {
          self.MQTTClient.connect(self.MQTTOptions);
        }
      }, 1000);
    }
  }

  onMessageArrived(buffer) {
    try {
      const parseMessage = JSON.parse(buffer.payloadString);
      const userInfo = this.getUserInfo();
      if (
        _.isEmpty(parseMessage.tousers) ||
        userInfo.OID == parseMessage.tousers ||
        parseMessage.tousers.includes(`${userInfo.OID},`) ||
        parseMessage.tousers.includes(`,${userInfo.OID}`)
      ) {
        const message = createMessageFromReal(parseMessage, {
          version: this.getMessageVersion(),
        }); // 创建消息对象
        this.messageCache.totalCount += 1;
        // if (message.infoLevel === SYS_LEVEL) {
        this.messageCache.messages.unshift(message); // 新消息置顶
        // } else {
        //   this.messageCache.messages.push(message);
        // }
        this.publish(NEW_MESSAGE, this.messageCache);

        this.renderWindowsInfo(buffer); // 1.windows桌面消息提醒
        this.speakMessage(message); // 2.语音播报
        this.noticeHandler(message); // 3.其他业务
      }
    } catch (e) {
      Logger.error(`收到消息处理异常:${e.message}`);
355 356
    } finally {
      window?.share?.event?.emit?.('event:messageArrived', buffer);
崔佳豪's avatar
崔佳豪 committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    }
  }

  /**
   * 实时消息浏览器桌面通知
   * @param {*} message 推送的原始实时消息
   */
  renderWindowsInfo(message) {
    if (document.visibilityState !== 'visible' && !document.visibilityState !== 'hidden') return;
    const self = this;
    function notifyMessage(message) {
      const parseMessage = JSON.parse(message.payloadString);
      let content = '';
      if (message.level !== SYS_LEVEL) {
        if (self.getMessageVersion() === '2.0') {
          if (message.level === '4') {
            const messageContent = JSON.parse(parseMessage.content);
            content += `${messageContent.alarmType} ${messageContent.alarmDevice} ${messageContent.alarmContent} ${
              messageContent.alarmValue
            } / ${messageContent.alarmThreshold}`;
          }
        }
      }

      const messageBody = {
        title: '',
        content,
      };

      if (parseMessage.tousers === '') {
        messageBody.title = '新公告:';
      } else {
        messageBody.title = '新通知:';
      }
      if (content !== '') {
        if (!('Notification' in window)) {
          message.warn('This browser does not support desktop notification');
        } else if (Notification.permission === 'granted') {
          const notification = new Notification(messageBody.title, {
            body: `${messageBody.content}`,
            icon: 'https://panda-water.com/web4/assets/images/icon/熊猫新1.png',
          });
          notification.onclick = () => {
            notification.close();
          };
        } else if (Notification.permission !== 'denied') {
          Notification.requestPermission(permission => {
            if (permission === 'granted') {
              const notification = new Notification(messageBody.title, {
                body: `${messageBody.content}`,
                icon: 'https://panda-water.com/web4/assets/images/icon/熊猫新1.png',
              });
              notification.onclick = () => {
                notification.close();
              };
            }
          });
        }
      }
    }
    try {
      notifyMessage(message);
    } catch (error) {}
  }

  /**
   * 实时消息语音播报
   * @param {*} message
   * @returns
   */
  speakMessage = message => {
    if (!message) return;
杨思琦's avatar
杨思琦 committed
429 430
    const { version, webVoice } = message;
    if(webVoice) return this.speakWebVoice(message);
杨思琦's avatar
杨思琦 committed
431
    if(version === '3.0') return this.speakDefault(message);
崔佳豪's avatar
崔佳豪 committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    switch(message.infoType) {
      case 'SCADA报警':
      case '通用报警':
        this.speakAlarm(message);
        break;
      case '工单流程':
      case '工单提醒':
        this.speakCase(message);
        break;
      case '系统通知':
      case '系统消息':
        this.speakSys(message);
        break;
      default:
        this.speakOther(message);
    }
  };
  speakAlarm = message => {
    const device =
      message?.infoContent?.title
杨思琦's avatar
杨思琦 committed
452
        ?.replace(/_/g, ',')
崔佳豪's avatar
崔佳豪 committed
453 454 455 456 457 458
        ?.split(' ')
        ?.splice(1)
        ?.join(' ') ?? ''; // 报警设备:“【阈值报警】 二供泵房 | 光谷物联港”
    const alarmType = message?.infoContent?.alarmType; // 报警类型:“状态报警”
    const content = message?.infoContent?.alarmContent; // 报警内容:“出水超压报警”
    const alarmValue = message?.infoContent?.alarmValue ?? ''; // 报警值
459 460
    const level = message?.infoLevel !== '1' ? '紧急报警,紧急报警,紧急报警:' : ''
    let msg = `${level}${device},${alarmType},${content},报警值:${isString(alarmValue) ? replaceSpeak(alarmValue) :'' },请注意!!!`;
崔佳豪's avatar
崔佳豪 committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    this.speak(msg);
  };
  speakCase = message => {
    const caseType = message?.infoContent?.caseType;
    const flowName = message?.infoContent?.flowName;
    const nodeName = message?.infoContent?.nodeName;
    const flowNo = message?.infoContent?.flowNo;
    const content = message?.infoContent?.content;
    let msg = `您有新的${caseType}${flowName},${nodeName},${flowNo ? '工单编号:' + flowNo : ''},${content}`;

    this.speak(msg);
  };
  speakSys = message => {
    const noticeType = message?.infoContent?.noticeType ?? '系统通知';
    const noticeTitle = message?.infoContent?.noticeTitle ?? '';
    const noticeContent = message?.infoContent?.noticeContent ?? '';
    let msg = `${noticeType}:${noticeTitle},${noticeContent}`;

    this.speak(msg);
  };
  speakOther = message => {
    // 类型区分‘全员公告’ 还是 ‘个人消息’
483 484
    const type = message.title ?? (isEmpty(message.tousers) || parseMessage.tousers === '' ? '公告' : '消息');
    
崔佳豪's avatar
崔佳豪 committed
485 486
    // 构建语音消息内容
    const { infoContent, time } = message;
487 488 489
    let content = replaceSpeak((infoContent?.content ?? message.defaultContent ?? '').replace(/\\n/g, ','));
    const text = content.substring(0, content.lastIndexOf(',')).replace(':', ',');
    let msg = `您有新的${type}${text !== '' ? text : content} 时间:${time}`;
崔佳豪's avatar
崔佳豪 committed
490 491 492

    this.speak(msg);
  };
杨思琦's avatar
杨思琦 committed
493
  speakDefault = message => {
杨思琦's avatar
杨思琦 committed
494
    let msg = replaceSpeak(`${message.title},${message.title}: ${message.infoContent ?? ''}`)
杨思琦's avatar
杨思琦 committed
495 496
    this.speak(msg);
  };
杨思琦's avatar
杨思琦 committed
497 498 499
  speakWebVoice = message => {
    const { webVoice } = message;
    if(typeof webVoice === "string") {
杨思琦's avatar
杨思琦 committed
500
      this.speak(replaceSpeak(webVoice.replace(/_/g, ',')));
杨思琦's avatar
杨思琦 committed
501 502
    }
  }
崔佳豪's avatar
崔佳豪 committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
  speak = msg => {
    if (!this.speakState) return;
    this.speakState.text = msg;
    window.speechSynthesis.cancel();
    window.speechSynthesis.speak(this.speakState);
  };

  /**
   * 实时消息特殊处理
   * @param {*} message
   * @returns
   */
  noticeHandler = message => {
    if (!message) return;
    if (message.infoLevel === SYS_LEVEL) {
      this.renderSysNoticePlatform(message); // 显示系统弹窗
      return;
    }
    if (message.infoLevel === VIDEO_LEVEL) {
      this.renderPopVideo(message); // 显示视频弹窗
      return;
    }
杨思琦's avatar
杨思琦 committed
525
    if (message.infoLevel === PLATFORM_LEVEL) {
杨思琦's avatar
杨思琦 committed
526 527 528
      this.renderPopPlatform(message); // 显示报警弹窗
      return;
    }
崔佳豪's avatar
崔佳豪 committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  };

  renderPopPlatform(message) {
    this.renderPlatformElement = this.renderPlatform && this.renderPlatform(message);
  }

  renderSysNoticePlatform(message) {
    this.renderSysPlatform && this.renderSysPlatform(message);
  }

  renderPopVideo(message) {
    this.renderVideoElement = this.renderVideo && this.renderVideo(message);
  }


  // 工具类
  async loadHisMessages(pageIndex, pageSize) {
    const self = this;
    return api
      .getInformationInfo({
        userID: self.getUserInfo().OID,
        pageIndex,
        pageSize,
        'request.preventCache': Date.now(),
      })
      .then(res => {
        if (res && res.code === 0) {
          const data = res.data || {};
          const result = {
            totalCount: data.totalCount,
            messages: (Array.isArray(data.list) ? data.list : []).map(item =>
              createMessageFromHis(item, { version: this.getMessageVersion() }),
            ),
          };
          self.messageCache.totalCount = result.totalCount;
          (result.messages || []).forEach(message => {
            const index = self.messageCache.messages.findIndex(item => item.id === message.id);
            index === -1 && self.messageCache.messages.push(message);
          });
          if (self.messageCache.totalCount > self.messageCache.messages.length && data.list.length === 0) {
            // 服务端返回总数还有,但是查不到数据了,前端修正服务端返回的总数
            self.messageCache.totalCount = self.messageCache.messages.length;
          }
          self.publish(NEW_MESSAGE, self.messageCache);
          console.log('render');
          const selectData = result.messages.find(item => {
            return item.infoLevel == 6;
          });
          if (selectData) {
            self.renderSysNoticePlatform(selectData);
          }

          return Promise.resolve(result);
        }
      });
  }

  // 1.0报警消息格式解析播报
  parseScadaMessage(message) {
    const data = message.messContent.split('\\n');
    const last = data[2];
    const alarmType = data[0].split('】')[0].split('【')[1];
    const sensor = data[1];
    const station = data[0].split('】')[1];
    let lastValue = last;
    if (last.includes(' / ')) {
      lastValue = '';
      const alaVal = last.split(' / ');
      let newVal = 0;
      let setVal = 0;
      let unit = '';
      if (alaVal[0].includes(' ')) {
        newVal = alaVal[0].split(' ')[0] * 1;
        setVal = alaVal[1].split(' ')[0] * 1;
        unit = alaVal[0].split(' ')[1];
      } else {
        newVal = alaVal[0] * 1;
        setVal = alaVal[1] * 1;
      }
      lastValue = Math.abs(setVal - newVal).toFixed(2) + unit;
    } else {
      lastValue = `,报警实时值${lastValue}`;
    }
    let msg = `紧急报警:${station},${alarmType},${sensor}${lastValue},请注意!!!`;
    for (let i = 0; i < 3; i++) {
      msg += msg;
    }
    const state = new window.SpeechSynthesisUtterance(msg);
    state.lang = 'zh';
    state.rate = 1;
    window.speechSynthesis.cancel();
    window.speechSynthesis.speak(state);
  }
}

export default Notifier;