198 lines
9.2 KiB
Java
198 lines
9.2 KiB
Java
package com.maibu.service;
|
||
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.net.URLEncoder;
|
||
import java.security.InvalidKeyException;
|
||
import java.security.NoSuchAlgorithmException;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
import javax.crypto.Mac;
|
||
import javax.crypto.spec.SecretKeySpec;
|
||
|
||
import org.apache.commons.codec.binary.Base64;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.util.CollectionUtils;
|
||
|
||
import com.dingtalk.api.DefaultDingTalkClient;
|
||
import com.dingtalk.api.DingTalkClient;
|
||
import com.dingtalk.api.request.OapiRobotSendRequest;
|
||
import com.dingtalk.api.response.OapiRobotSendResponse;
|
||
import com.maibu.core.business.AlarmOrderConfig;
|
||
import com.maibu.core.business.IOTWorkOrder;
|
||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||
import com.maibu.core.business.device.NettyDevice;
|
||
import com.maibu.core.redis.RedisCache;
|
||
import com.maibu.dto.DeviceErrorPushDTO;
|
||
import com.maibu.manager.DeviceSessionManager;
|
||
import com.maibu.memory.GlobalMemory;
|
||
import com.maibu.memory.SiteMemory;
|
||
import com.maibu.service.impl.AlarmServiceImpl;
|
||
import com.maibu.service.impl.WorkOrderServiceImpl;
|
||
import com.maibu.utils.DingTalkTemplate;
|
||
import com.maibu.utils.json.JsonUtils;
|
||
import com.taobao.api.ApiException;
|
||
|
||
import lombok.extern.slf4j.Slf4j;
|
||
|
||
@Service
|
||
@Slf4j
|
||
public class AlertPushService {
|
||
|
||
@Autowired
|
||
private DeviceSessionManager sessionManager;
|
||
|
||
@Autowired
|
||
private AlarmServiceImpl alarmService;
|
||
|
||
@Autowired
|
||
private RedisCache redisCache;
|
||
|
||
@Autowired
|
||
private WorkOrderServiceImpl wService;
|
||
|
||
public void messagePushHandler(String deviceId, List<DeviceErrorPushDTO> dtos) {
|
||
NettyDevice nettyDevice = sessionManager.getDevice(deviceId);
|
||
if (nettyDevice != null && nettyDevice.getDevice() != null) {
|
||
Long siteId = nettyDevice.getDevice().getSiteId();
|
||
Long orgId = nettyDevice.getDevice().getOrgId();
|
||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||
if (siteMemory != null) {
|
||
Integer dingTalkPush = siteMemory.getSysSite().getDingTalkPush();
|
||
Integer emailPush = siteMemory.getSysSite().getEmailPush();
|
||
Integer alertPush = siteMemory.getSysSite().getAlertPush();
|
||
String secret = siteMemory.getSysSite().getDingTalkSecret();
|
||
String token = siteMemory.getSysSite().getDingTalkToken();
|
||
dtos.forEach(dto -> {
|
||
if (!CollectionUtils.isEmpty(dto.getDetails())) {
|
||
List<AlarmMessage> newList = new ArrayList<>();
|
||
List<IOTWorkOrder> workOrders = new ArrayList<>();
|
||
dto.getDetails().forEach(detail -> {
|
||
if (dingTalkPush == 1) {
|
||
String pushKey = "alarm:" + "dingtalk:" + deviceId + ":" + detail.getNo();
|
||
Boolean exists = redisCache.hasKey(pushKey);
|
||
if (exists != null && !exists) {
|
||
pushDingTalkMessage(secret, token, DingTalkTemplate.buildText(detail));
|
||
redisCache.setCacheObject(pushKey, JsonUtils.toJsonString(detail), 10,
|
||
TimeUnit.MINUTES);
|
||
}
|
||
}
|
||
if (emailPush == 1) {
|
||
String pushKey = "alarm:" + "email:" + deviceId + ":" + detail.getNo();
|
||
Boolean exists = redisCache.hasKey(pushKey);
|
||
if (exists != null && !exists) {
|
||
pushEmailMessage(null, null);
|
||
redisCache.setCacheObject(pushKey, JsonUtils.toJsonString(detail), 10,
|
||
TimeUnit.MINUTES);
|
||
}
|
||
}
|
||
if (alertPush == 1) {
|
||
String pushKey = "alarm:" + "mqtt:" + deviceId + ":" + detail.getNo();
|
||
Boolean exists = redisCache.hasKey(pushKey);
|
||
if (exists != null && !exists) {
|
||
newList.add(detail);
|
||
redisCache.setCacheObject(pushKey, JsonUtils.toJsonString(detail), 10,
|
||
TimeUnit.MINUTES);
|
||
}
|
||
}
|
||
AlarmOrderConfig config = siteMemory.getAlarmOrderConfig(detail.getNo());
|
||
if (config != null && config.isAutoGenerateOrder()) {
|
||
IOTWorkOrder workOrder = wService.generateWorkOrders(siteId, deviceId, orgId, orgId);
|
||
workOrders.add(workOrder);
|
||
}
|
||
});
|
||
if (alertPush == 1) {
|
||
alarmService.pushAlertMes(dto.getDetails());
|
||
}
|
||
if (!CollectionUtils.isEmpty(workOrders)) {
|
||
wService.saveAll(workOrders);
|
||
}
|
||
}
|
||
});
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
public void pushDingTalkMessage(String secret, String token, String alertMessage) {
|
||
try {
|
||
Long timestamp = System.currentTimeMillis();
|
||
String stringToSign = timestamp + "\n" + secret;
|
||
Mac mac = Mac.getInstance("HmacSHA256");
|
||
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
|
||
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
|
||
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
|
||
|
||
// sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
|
||
DingTalkClient client = new DefaultDingTalkClient(
|
||
"https://oapi.dingtalk.com/robot/send?sign=" + sign + "×tamp=" + timestamp);
|
||
OapiRobotSendRequest req = new OapiRobotSendRequest();
|
||
// 定义文本内容
|
||
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
|
||
text.setContent(alertMessage);
|
||
// 定义 @ 对象
|
||
// OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
|
||
// at.setAtUserIds(Arrays.asList(USER_ID));
|
||
// 设置消息类型
|
||
req.setMsgtype("text");
|
||
req.setText(text);
|
||
// req.setAt(at);
|
||
client.execute(req, token);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
log.error("发送钉钉消息失败:{}", e.getMessage());
|
||
}
|
||
}
|
||
|
||
public void pushEmailMessage(String deviceId, String errorMessage) {
|
||
// Implement the logic to push error messages to the appropriate channels
|
||
// This could involve sending notifications, logging errors, etc.
|
||
System.out.println("Error for device " + deviceId + ": " + errorMessage);
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
try {
|
||
Long timestamp = System.currentTimeMillis();
|
||
System.out.println(timestamp);
|
||
String secret = "SEC6e538cab506df2efda5d327dfe582f8fd12ac55b77664d662031e90d2a3a3887";
|
||
String stringToSign = timestamp + "\n" + secret;
|
||
Mac mac = Mac.getInstance("HmacSHA256");
|
||
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
|
||
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
|
||
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
|
||
System.out.println(sign);
|
||
|
||
// sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
|
||
DingTalkClient client = new DefaultDingTalkClient(
|
||
"https://oapi.dingtalk.com/robot/send?sign=" + sign + "×tamp=" + timestamp);
|
||
OapiRobotSendRequest req = new OapiRobotSendRequest();
|
||
/**
|
||
* 发送文本消息
|
||
*/
|
||
// 定义文本内容
|
||
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
|
||
text.setContent("昀姐牛逼");
|
||
// 定义 @ 对象
|
||
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
|
||
// at.setAtUserIds(Arrays.asList(USER_ID));
|
||
// 设置消息类型
|
||
req.setMsgtype("text");
|
||
req.setText(text);
|
||
req.setAt(at);
|
||
OapiRobotSendResponse rsp = client.execute(req,
|
||
"8bfdf609c7edeff5f31c6a243088d2a4f6672221afc86c008ad8622156a58407");
|
||
System.out.println(rsp.getBody());
|
||
} catch (ApiException e) {
|
||
e.printStackTrace();
|
||
} catch (UnsupportedEncodingException e) {
|
||
throw new RuntimeException(e);
|
||
} catch (NoSuchAlgorithmException e) {
|
||
throw new RuntimeException(e);
|
||
} catch (InvalidKeyException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
} |