|
@@ -1,549 +0,0 @@
|
|
|
-package com.hfln.portal.infrastructure.mqtt;
|
|
|
-
|
|
|
-import cn.hfln.framework.redis.util.RedisUtil;
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
-import com.hfln.portal.common.constant.mqtt.topic.TopicConstants;
|
|
|
-import com.hfln.portal.common.constant.redis.RedisCacheConstant;
|
|
|
-import com.hfln.portal.common.dto.data.user.SendMsgUserDto;
|
|
|
-import com.hfln.portal.domain.customer.AlarmEventType;
|
|
|
-import com.hfln.portal.domain.customer.util.MsgClient;
|
|
|
-import com.hfln.portal.domain.customer.util.WxOfficeAccountClient;
|
|
|
-import com.hfln.portal.infrastructure.po.*;
|
|
|
-import com.hfln.portal.infrastructure.service.*;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.messaging.Message;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-
|
|
|
-import java.time.LocalDate;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-@Component
|
|
|
-@Slf4j
|
|
|
-public class MqttSubHandle {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private MqttClient mqttClient; // MQTT消息发布器
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DevInfoService devInfoService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private EventListService eventListService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private UserService userService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private MsgClient msgClient;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private WxRelationService wxRelationService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private WxOfficeAccountClient wxOfficeAccountClient;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RedisUtil redisUtil;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private TblTenantService tblTenantService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private AdminUserService adminUserService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DevShareService devShareService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private AlarmPlanService alarmPlanService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PersonInOutService personInOutService;
|
|
|
-
|
|
|
- /**
|
|
|
- * MQTT消息统一入口处理方法
|
|
|
- * <p>
|
|
|
- * 业务流程:
|
|
|
- * 1. 从消息头提取MQTT主题和负载
|
|
|
- * 2. 从主题路径中提取操作类型(action)
|
|
|
- * 3. 根据操作类型路由到具体的处理方法
|
|
|
- * 4. 异常情况统一捕获和日志记录
|
|
|
- *
|
|
|
- * @param message Spring Integration封装的MQTT消息对象
|
|
|
- */
|
|
|
- public void handleMessage(Message<?> message) {
|
|
|
- try {
|
|
|
- String topic = (String) message.getHeaders().get("mqtt_receivedTopic");
|
|
|
- String payload = message.getPayload().toString();
|
|
|
-
|
|
|
- if (topic == null) {
|
|
|
- log.warn("Received message without topic header");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String[] parts = topic.split("/");
|
|
|
- String action = parts[parts.length - 1];
|
|
|
-// String devId = parts[parts.length - 2];
|
|
|
-
|
|
|
- // 根据主题路由到不同的处理方法
|
|
|
- switch (action) {
|
|
|
-
|
|
|
- // 接收LAS告警事件主题消息 if里面判断新逻辑, else 保持旧逻辑
|
|
|
- case "alarm_event":
|
|
|
- subDasAlarmEvent(topic, payload);
|
|
|
- break;
|
|
|
- case "connect":
|
|
|
- subMqttClientConnect(topic, payload);
|
|
|
- break;
|
|
|
-
|
|
|
- // 接收dev 主题消息
|
|
|
- case "login":
|
|
|
- subDevLogin(topic, payload);
|
|
|
- break;
|
|
|
- case "last_will":
|
|
|
- subDevLastWill(topic, payload);
|
|
|
- break;
|
|
|
- case "presence_change":
|
|
|
- subDevPresenceChange(topic, payload);
|
|
|
- break;
|
|
|
- case "falling_event_change":
|
|
|
- subDevFallingEventChange(topic, payload);
|
|
|
- break;
|
|
|
- case "report_device_param":
|
|
|
- subDevReportDeviceParam(topic, payload);
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- log.debug("Unhandled device topic: {}", topic);
|
|
|
- break;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("Error handling device message: {}", e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void subDevReportDeviceParam(String topic, String payload) {
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- String clientId = getDevId(topic);
|
|
|
- DevInfo dev = devInfoService.queryByClientId(clientId);
|
|
|
- if (dev == null) {
|
|
|
- log.warn("Device not found for clientId: {}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject obj = JSONObject.parseObject(payload);
|
|
|
- String deviceInfoStr = obj.getString("device_info");
|
|
|
- JSONObject deviceInfo = JSONObject.parseObject(deviceInfoStr);
|
|
|
-
|
|
|
- String hardware = deviceInfo.getString("firmware");
|
|
|
- dev.setHardware(hardware);
|
|
|
- int indicatorLed = obj.getIntValue("indicator_led");
|
|
|
- dev.setStatusLight(indicatorLed == 0 ? 1 : 0);
|
|
|
- int isCeiling = obj.getIntValue("isCeiling");
|
|
|
- dev.setMountPlain(isCeiling == 1 ? DevInfo.Constants.MountPlain.CEILING : DevInfo.Constants.MountPlain.WALL);
|
|
|
-
|
|
|
- devInfoService.updateById(dev);
|
|
|
- }
|
|
|
-
|
|
|
- private void subDevFallingEventChange(String topic, String payload) {
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- String clientId = getDevId(topic);
|
|
|
- DevInfo dev = devInfoService.queryByClientId(clientId);
|
|
|
- if (dev == null) {
|
|
|
- log.warn("Device not found for clientId: {}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject obj = JSONObject.parseObject(payload);
|
|
|
- int falling = obj.getIntValue("falling");
|
|
|
- //0:no fall 1:detected 2: confirmed 3:calling
|
|
|
-
|
|
|
- // 2 有跌倒事件发生
|
|
|
- if (falling == 2) {
|
|
|
-
|
|
|
- //存储跌倒时间到数据库
|
|
|
- EventList eventListVO = new EventList();
|
|
|
- eventListVO.setDevId(dev.getDevId());
|
|
|
- eventListVO.setIsHandle(0);
|
|
|
- eventListVO.setEventType(falling);
|
|
|
- eventListVO.setTenantId(dev.getTenantId());
|
|
|
- eventListService.save(eventListVO);
|
|
|
-
|
|
|
- // 整理需要接收消息的人员列表
|
|
|
- log.info("mqttutil--有跌倒事件");
|
|
|
- List<SendMsgUserDto> sendList = new ArrayList<>();
|
|
|
- String devName = dev.getDevName();
|
|
|
- Long userId = dev.getUserId();
|
|
|
-
|
|
|
- // 需要发送提示的 有 当前设备拥有者, 被分享者, 以及 对当前设备 具有管理权限的 web管理用户
|
|
|
- // 小程序拥有者
|
|
|
- if (userId != null) {
|
|
|
- UserInfo userInfo = userService.queryById(userId);
|
|
|
- SendMsgUserDto msgUserDto = new SendMsgUserDto();
|
|
|
- msgUserDto.setUserId(userId);
|
|
|
- msgUserDto.setPhone(userInfo.getPhone());
|
|
|
- msgUserDto.setUnionId(userInfo.getUnionId());
|
|
|
- msgUserDto.setMessageFlag(true);
|
|
|
- msgUserDto.setServiceNumberFlag(true);
|
|
|
- WxRelation wxRelation = wxRelationService.queryOneByUnionId(userInfo.getUnionId());
|
|
|
- if (wxRelation != null) {
|
|
|
- msgUserDto.setFwhOpenId(wxRelation.getFwhOpenId());
|
|
|
- }
|
|
|
- sendList.add(msgUserDto);
|
|
|
-
|
|
|
- // 对于 小程序拥有者,给出全局跌倒提醒,-- 被分享者是否需要
|
|
|
- JSONObject wxMsg = new JSONObject();
|
|
|
- wxMsg.put("clientId", clientId);
|
|
|
- wxMsg.put("event", "fall_confirmed");
|
|
|
- wxMsg.put("msgType", "fall");
|
|
|
- wxMsg.put("devName", devName.toString());
|
|
|
- wxMsg.put("eventListId", String.valueOf(eventListVO.getEventListId()));
|
|
|
-
|
|
|
- log.info("发送微信跌倒主题消息:topic:{}, msg:{}", String.format(TopicConstants.TOPIC_MPS_NOTIC, RedisCacheConstant.WX_USER_PRE + userInfo.getUserId()), wxMsg.toString());
|
|
|
- mqttClient.sendMessage(String.format(TopicConstants.TOPIC_MPS_NOTIC, RedisCacheConstant.WX_USER_PRE + userInfo.getUserId()), wxMsg.toString(), 2, false);
|
|
|
-
|
|
|
- // 设备的被分享者 - 根据标志位筛选并添加到发送列表
|
|
|
- List<DevShare> shares = devShareService.queryConfirmedByDevId(dev.getDevId());
|
|
|
- if (!CollectionUtils.isEmpty(shares)) {
|
|
|
- for (DevShare share : shares) {
|
|
|
- // 发送微信跌倒主题消息
|
|
|
- log.info("发送微信跌倒主题消息:topic:{}, msg:{}", String.format(TopicConstants.TOPIC_MPS_NOTIC, RedisCacheConstant.WX_USER_PRE + share.getSharedUserId()), wxMsg.toString());
|
|
|
- mqttClient.sendMessage(String.format(TopicConstants.TOPIC_MPS_NOTIC, RedisCacheConstant.WX_USER_PRE + share.getSharedUserId()), wxMsg.toString(), 2, false);
|
|
|
-
|
|
|
- // 创建被分享者的消息用户DTO
|
|
|
- SendMsgUserDto msgUserShared = new SendMsgUserDto();
|
|
|
- msgUserShared.setUserId(share.getSharedUserId());
|
|
|
- msgUserShared.setPhone(share.getSharedPhone());
|
|
|
-
|
|
|
- // 获取完整的分享信息(包含标志位)
|
|
|
- DevShare devShare = devShareService.getById(share.getShareId());
|
|
|
- if (devShare != null) {
|
|
|
- // 将Integer类型的标志位转换为boolean类型:0-授权(true),1-拒绝(false)
|
|
|
- boolean messageFlag = devShare.getMessageFlag() != null && devShare.getMessageFlag() == 0;
|
|
|
- boolean serviceNumberFlag = devShare.getServiceNumberFlag() != null && devShare.getServiceNumberFlag() == 0;
|
|
|
-
|
|
|
- msgUserShared.setMessageFlag(messageFlag);
|
|
|
- msgUserShared.setServiceNumberFlag(serviceNumberFlag);
|
|
|
-
|
|
|
- // 根据标志位决定是否添加到发送列表
|
|
|
- // 只有当短信通知或服务号通知任一被授权时,才添加到发送列表
|
|
|
- if (messageFlag || serviceNumberFlag) {
|
|
|
- // 获取被分享者的微信关系信息
|
|
|
- UserInfo sharedUserInfo = userService.queryById(share.getSharedUserId());
|
|
|
- if (sharedUserInfo != null) {
|
|
|
- msgUserShared.setUnionId(sharedUserInfo.getUnionId());
|
|
|
- WxRelation sharedWxRelation = wxRelationService.queryOneByUnionId(sharedUserInfo.getUnionId());
|
|
|
- if (sharedWxRelation != null) {
|
|
|
- msgUserShared.setFwhOpenId(sharedWxRelation.getFwhOpenId());
|
|
|
- }
|
|
|
- sendList.add(msgUserShared);
|
|
|
- log.info("被分享者已添加到发送列表: userId={}, messageFlag={}, serviceNumberFlag={}",
|
|
|
- share.getSharedUserId(), messageFlag, serviceNumberFlag);
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.info("被分享者拒绝所有通知: userId={}, messageFlag={}, serviceNumberFlag={}",
|
|
|
- share.getSharedUserId(), messageFlag, serviceNumberFlag);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 针对跌倒事件 发送 网页 主题消息提示
|
|
|
- String tenantName = "";
|
|
|
- // 网页告警提示 需要当前设备绑定到租户,会发送到当前租户已经登录的管理员页面
|
|
|
- if (dev.getTenantId() != null) {
|
|
|
- TblTenant tblTenant = tblTenantService.getById(dev.getTenantId());
|
|
|
- tenantName = tblTenant.getTenantName();
|
|
|
-
|
|
|
- JSONObject webMsg = new JSONObject();
|
|
|
- webMsg.put("clientId", clientId);
|
|
|
- webMsg.put("event", "fall_confirmed");
|
|
|
- webMsg.put("msgType", "fall");
|
|
|
- webMsg.put("devName", devName.toString());
|
|
|
- webMsg.put("tenantName", tenantName);
|
|
|
- webMsg.put("eventListId", String.valueOf(eventListVO.getEventListId()));
|
|
|
-
|
|
|
- // 查询当前需要发送的userId
|
|
|
- List<AdminUserInfo> adminUserInfos = adminUserService.queryByTenantIdAndUserType(dev.getTenantId(), null);
|
|
|
- if (!CollectionUtils.isEmpty(adminUserInfos)) {
|
|
|
- for (AdminUserInfo adminUserInfo : adminUserInfos) {
|
|
|
-
|
|
|
- // 判断当前用户是否登录网页
|
|
|
- if (redisUtil.sIsMember(RedisCacheConstant.MQTT_CLIENT_USERID, RedisCacheConstant.WEB_USER_PRE + adminUserInfo.getUserId())) {
|
|
|
- log.info("发送网页跌倒主题消息:topic:{}, msg:{}", String.format(TopicConstants.TOPIC_MPS_NOTIC, RedisCacheConstant.WEB_USER_PRE + adminUserInfo.getUserId()), webMsg.toString());
|
|
|
- mqttClient.sendMessage(String.format(TopicConstants.TOPIC_MPS_NOTIC, RedisCacheConstant.WEB_USER_PRE + adminUserInfo.getUserId()), webMsg.toString(), 2, false);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //3.调用 发送短信和微信公众号通知 功能
|
|
|
- sendMesAndWxService(sendList, devName, clientId);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void subDevPresenceChange(String topic, String payload) {
|
|
|
-
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- String clientId = getDevId(topic);
|
|
|
- DevInfo dev = devInfoService.queryByClientId(clientId);
|
|
|
- if (dev == null) {
|
|
|
- log.warn("Device not found for clientId: {}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
- JSONObject obj = JSONObject.parseObject(payload);
|
|
|
- if (obj.get("presence") == null) {
|
|
|
- log.warn("presence is null");
|
|
|
- return;
|
|
|
- }
|
|
|
- //0:no presense 1:presence
|
|
|
- long presence = obj.getIntValue("presence");
|
|
|
-
|
|
|
- devInfoService.update(
|
|
|
- new LambdaUpdateWrapper<DevInfo>()
|
|
|
- .eq(DevInfo::getClientId, clientId)
|
|
|
- .set(DevInfo::getExistFlag, presence == 1 ? DevInfo.Constants.ExistFlag.EXIST : DevInfo.Constants.ExistFlag.NOT_EXIST)
|
|
|
- .set(DevInfo::getPresenceChangeTime, LocalDateTime.now())
|
|
|
- );
|
|
|
-
|
|
|
-
|
|
|
- PersonInOutInfo personInOut = personInOutService.queryOneByDevId(dev.getDevId(), DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()));
|
|
|
- if (presence == 1) {
|
|
|
- if (personInOut == null) {
|
|
|
- personInOut = new PersonInOutInfo();
|
|
|
- personInOut.setTenantId(dev.getTenantId());
|
|
|
- personInOut.setDevId(dev.getDevId());
|
|
|
- personInOut.setCountDate(DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()));
|
|
|
- personInOut.setCount(1);
|
|
|
- personInOutService.save(personInOut);
|
|
|
- } else {
|
|
|
- personInOut.setCount(personInOut.getCount() + 1);
|
|
|
- personInOutService.updateById(personInOut);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private void subDevLastWill(String topic, String payload) {
|
|
|
-
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- String clientId = getDevId(topic);
|
|
|
- DevInfo dev = devInfoService.queryByClientId(clientId);
|
|
|
- if (dev == null) {
|
|
|
- log.warn("Device not found for clientId: {}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject obj = JSONObject.parseObject(payload);
|
|
|
- if (obj.get("cmd_offline") == null || obj.getIntValue("cmd_offline") != 1) {
|
|
|
- log.warn("cmd_offline is null or not 1");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- devInfoService.update(
|
|
|
- new LambdaUpdateWrapper<DevInfo>()
|
|
|
- .eq(DevInfo::getClientId, clientId)
|
|
|
- .set(DevInfo::getOnline, DevInfo.Constants.OnlineStatus.OFFLINE)
|
|
|
- .set(DevInfo::getOnoffTime, LocalDateTime.now())
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- private void subDevLogin(String topic, String payload) {
|
|
|
-
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- String clientId = getDevId(topic);
|
|
|
- DevInfo dev = devInfoService.queryByClientId(clientId);
|
|
|
- if (dev == null) {
|
|
|
- log.warn("Device not found for clientId: {}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- devInfoService.update(
|
|
|
- new LambdaUpdateWrapper<DevInfo>()
|
|
|
- .eq(DevInfo::getClientId, clientId)
|
|
|
- .set(DevInfo::getOnline, DevInfo.Constants.OnlineStatus.ONLINE)
|
|
|
- .set(DevInfo::getExistFlag, DevInfo.Constants.ExistFlag.EXIST)
|
|
|
- .set(DevInfo::getOnoffTime, LocalDateTime.now())
|
|
|
- .set(DevInfo::getPresenceChangeTime, LocalDateTime.now())
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 推送设备转移消息给转移对象
|
|
|
- */
|
|
|
- public void pushDeviceTransferMsg(DevInfo devInfo, UserInfo targetUser) {
|
|
|
-
|
|
|
- // 根据 unionId 查询服务号关系
|
|
|
- List<WxRelation> wxRelations = wxRelationService.queryByUnionId(targetUser.getUnionId());
|
|
|
- String fwhOpenId = "";
|
|
|
- if (wxRelations != null && !wxRelations.isEmpty()) {
|
|
|
- fwhOpenId = wxRelations.get(0).getFwhOpenId();
|
|
|
- }
|
|
|
-
|
|
|
- // 日志打印
|
|
|
- log.info("mqttutil--当前useropenid={}, fwhopenId={}", targetUser.getOpenid(), fwhOpenId);
|
|
|
- log.info("发送微信公众号信息:devName={}, phoneNumber={}, fwhOpenId={}", devInfo.getDevName(), targetUser.getPhone(), fwhOpenId);
|
|
|
-
|
|
|
- // 服务号推送消息
|
|
|
- wxOfficeAccountClient.sendMsg(devInfo.getClientId(), devInfo.getDevName(), targetUser.getPhone(), fwhOpenId, "设备运行检测到设备转移,请前往小程序查看");
|
|
|
-
|
|
|
- log.info("发送微信公众号信息完毕");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送短信和微信公众号通知
|
|
|
- *
|
|
|
- * @param sendList 需要发送通知的用户列表
|
|
|
- * @param devName 设备名称
|
|
|
- * @param devId 设备ID
|
|
|
- */
|
|
|
- private void sendMesAndWxService(List<SendMsgUserDto> sendList, String devName, String devId) {
|
|
|
- if (CollectionUtils.isEmpty(sendList)) {
|
|
|
- log.info("没有需要发送通知的用户");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- for (SendMsgUserDto sendDto : sendList) {
|
|
|
- try {
|
|
|
- log.info("mqttutil--开始处理用户通知: userId={}, messageFlag={}, serviceNumberFlag={}",
|
|
|
- sendDto.getUserId(), sendDto.isMessageFlag(), sendDto.isServiceNumberFlag());
|
|
|
-
|
|
|
- // 根据短信标志位决定是否发送短信
|
|
|
- if (sendDto.isMessageFlag()) {
|
|
|
- try {
|
|
|
- log.info("mqttutil--开始发送跌倒短信: phone={}, dev_name={}", sendDto.getPhone(), devName);
|
|
|
- msgClient.sendNotifyMsg(sendDto.getPhone(), devName);
|
|
|
- log.info("mqttUtil--短信发送完成");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送短信失败: userId={}, phone={}, error={}",
|
|
|
- sendDto.getUserId(), sendDto.getPhone(), e.getMessage(), e);
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.info("用户拒绝短信通知: userId={}", sendDto.getUserId());
|
|
|
- }
|
|
|
-
|
|
|
- // 根据服务号标志位决定是否发送微信公众号消息
|
|
|
- if (sendDto.isServiceNumberFlag() && StringUtils.isNotBlank(sendDto.getFwhOpenId())) {
|
|
|
- try {
|
|
|
- log.info("发送微信公众号信息:devName={}, phoneNo={}, fwhOpenId={}",
|
|
|
- devName, sendDto.getPhone(), sendDto.getFwhOpenId());
|
|
|
- // 发送微信公众号消息
|
|
|
- wxOfficeAccountClient.sendMsg(devId, devName, sendDto.getPhone(),
|
|
|
- sendDto.getFwhOpenId(), "设备检测到跌倒,请前往小程序查看详细信息");
|
|
|
- log.info("微信公众号消息发送完成");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送微信公众号消息失败: userId={}, fwhOpenId={}, error={}",
|
|
|
- sendDto.getUserId(), sendDto.getFwhOpenId(), e.getMessage(), e);
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.info("用户拒绝服务号通知或未绑定微信: userId={}, serviceNumberFlag={}, fwhOpenId={}",
|
|
|
- sendDto.getUserId(), sendDto.isServiceNumberFlag(), sendDto.getFwhOpenId());
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("处理用户通知异常: userId={}, error={}",
|
|
|
- sendDto.getUserId(), e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public void subDasAlarmEvent(String topic, String payload) {
|
|
|
-
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- // 1.判断是否为新逻辑 从LAS获取
|
|
|
-
|
|
|
- JSONObject obj = JSONObject.parseObject(payload);
|
|
|
- String eventType = obj.getString("event_type");
|
|
|
- // 1.1 判断 消息类型是否符合 起夜异常 or 异常滞留
|
|
|
- if (AlarmEventType.isTargetEvent(eventType)) {
|
|
|
- String clientId = obj.getString("dev_id");
|
|
|
- String uuid = obj.getString("plan_uuid");
|
|
|
-
|
|
|
- // 1.2 判断告警计划是否开启服务号推送
|
|
|
- AlarmPlan alarmPlan = alarmPlanService.findByUuid(uuid);
|
|
|
- if (alarmPlan.getLinkagePushWechatService() == 0) {
|
|
|
- log.info("服务号通知发送失败,当前设备告警计划未开启服务号推送: clientId={}, uuid={}", clientId, uuid);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 1.3 查找设备信息,获取主绑人的userId
|
|
|
- DevInfo dev = devInfoService.queryByClientId(clientId);
|
|
|
-
|
|
|
- if (dev == null) {
|
|
|
- log.info("服务号通知发送失败,当前设备不存在: clientId={}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- Long userId = dev.getUserId();
|
|
|
- String devName = dev.getDevName();
|
|
|
- String devId = dev.getClientId();
|
|
|
-
|
|
|
-
|
|
|
- // 1.4 如果userId为空,则没有绑定人
|
|
|
- if (userId == null) {
|
|
|
- log.info("服务号通知发送失败,设备未绑定用户: clientId={}", clientId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 1.5 查询主绑人信息
|
|
|
- UserInfo user = userService.queryById(userId);
|
|
|
-
|
|
|
- // 1.6 获取用户微信服务号OpenId
|
|
|
- WxRelation wxRelations = wxRelationService.queryOneByUnionId(user.getUnionId());
|
|
|
-
|
|
|
- if (wxRelations == null) {
|
|
|
- log.info("服务号通知发送失败,用户未绑定微信服务号: userId={}", userId);
|
|
|
- return;
|
|
|
- }
|
|
|
- String fwhOpenId = wxRelations.getFwhOpenId();
|
|
|
-
|
|
|
- log.info("mqttutil--当前useropenid=" + user.getOpenid() + ", fwhopenId=" + fwhOpenId);
|
|
|
- log.info("发送微信公众号信息:devName=" + devName + ", phoneNo=" + user.getPhone() + "fwhOpenId=" + fwhOpenId);
|
|
|
-
|
|
|
- // 1.7 发送微信公告号消息
|
|
|
- wxOfficeAccountClient.sendMsg(devId, devName, user.getPhone(), fwhOpenId, "设备运行检测到告警异常,请前往小程序查看");
|
|
|
- log.info("发送微信公众号消息发完了");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void subMqttClientConnect(String topic, String payload) {
|
|
|
-
|
|
|
- log.info("Received device message: topic={}, payload={}", topic, payload);
|
|
|
- if (StringUtils.isEmpty(payload)) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject obj = JSONObject.parseObject(payload);
|
|
|
- String userId = obj.getString("userId");
|
|
|
- String deviceType = obj.getString("deviceType");
|
|
|
- String msgType = obj.getString("msgType");
|
|
|
-
|
|
|
- if (StringUtils.isBlank(userId) || StringUtils.isBlank(deviceType) || StringUtils.isBlank(msgType)) {
|
|
|
- log.error("userId or deviceType or msgType is null");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (msgType.equals("connect")) {
|
|
|
- redisUtil.sAdd(RedisCacheConstant.MQTT_CLIENT_USERID, deviceType + "_" + userId);
|
|
|
- } else if (msgType.equals("disconnect")) {
|
|
|
- redisUtil.sRemove(RedisCacheConstant.MQTT_CLIENT_USERID, deviceType + "_" + userId);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String getDevId(String topic) {
|
|
|
- String[] split = topic.split("/");
|
|
|
- return split[split.length - 2];
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|