|
|
@@ -0,0 +1,114 @@
|
|
|
+package com.zd.alg.monitor;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.zd.common.core.redis.RedisService;
|
|
|
+import com.zd.common.core.utils.HttpUtils;
|
|
|
+import com.zd.common.core.utils.StringUtils;
|
|
|
+import com.zd.laboratory.api.feign.RemoteLabHardwareService;
|
|
|
+import com.zd.model.constant.HttpStatus;
|
|
|
+import com.zd.model.domain.ResultData;
|
|
|
+import io.swagger.models.auth.In;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>硬件设备监听</p>
|
|
|
+ *
|
|
|
+ * @author linft
|
|
|
+ * @version 1.0
|
|
|
+ * @date 1/10/2023
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class HardwareDeviceMonitor {
|
|
|
+
|
|
|
+ @Value("${monitor.alert.dingTalkNotice}")
|
|
|
+ private Boolean dingTalkNotice;
|
|
|
+
|
|
|
+ @Value("${monitor.alert.dingTalkSubject}")
|
|
|
+ private String dingTalkSubject;
|
|
|
+
|
|
|
+ @Value("${monitor.alert.dingTalkUrl}")
|
|
|
+ private String dingTalkUrl;
|
|
|
+
|
|
|
+ private String redisPrefix = "HARDWARE_DEVICE_MONITOR:";
|
|
|
+
|
|
|
+ private long expire = 30L;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RemoteLabHardwareService remoteLabHardwareService;
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 */9 * * * ?")
|
|
|
+ public void checkPowerUse() {
|
|
|
+ if (dingTalkNotice != null && dingTalkNotice && StringUtils.isNotEmpty(dingTalkUrl)) {
|
|
|
+ log.info("【设备监听提醒】 开始执行检查..............");
|
|
|
+ ResultData result = remoteLabHardwareService.statistics();
|
|
|
+ if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
|
|
|
+ String jsonStr = JSONObject.toJSONString(result.getData());
|
|
|
+ JSONObject json = JSONObject.parseObject(jsonStr);
|
|
|
+ Integer offline = json.getInteger("powerSupplyOffline");
|
|
|
+ log.info("【设备监听提醒】 异常设备掉线数量:" + offline);
|
|
|
+ if (offline != null && offline > 0) {
|
|
|
+ //获取离线列表
|
|
|
+ ResultData data = remoteLabHardwareService.list("SWITCH", "OFFLINE");
|
|
|
+ //log.info("【设备监听提醒】 获取异常列表:"+JSONObject.toJSONString(data));
|
|
|
+ if (data != null && data.getCode() == HttpStatus.SUCCESS && data.getData() != null) {
|
|
|
+ //log.info("【设备监听提醒】 异常设备列表信息:" + data.getData());
|
|
|
+ String listStr = JSONObject.toJSONString(data.getData());
|
|
|
+ List<Map<String, Object>> list = JSONObject.parseObject(listStr, List.class);
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ for (Map m : list) {
|
|
|
+ String code = (String) m.get("relayCode");
|
|
|
+ if (!map.containsKey(code)) {
|
|
|
+ map.put(code, "---信息[名称" + m.get("name") + ",所在学院:" + m.get("deptName") + ",所在实验室:" + m.get("subjectName") + "]\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //封装消息
|
|
|
+ if (!map.isEmpty()) {
|
|
|
+ Set<String> code = map.keySet();
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ buffer.append("【"+dingTalkSubject+"-设备掉线提醒】离线继电器信息:\n");
|
|
|
+ boolean isSend = Boolean.FALSE;
|
|
|
+ for (String key : code) {
|
|
|
+ Integer num = redisService.getCacheObject(redisPrefix+key);
|
|
|
+ if (num != null && num > 2) {
|
|
|
+ buffer.append("继电器[" + key + "] " + map.get(key));
|
|
|
+ redisService.deleteObject(redisPrefix+key);
|
|
|
+ isSend = Boolean.TRUE;
|
|
|
+ } else {
|
|
|
+ int value = num == null ? 1:num+1;
|
|
|
+ redisService.setCacheObject(redisPrefix+key, value, expire, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //通知
|
|
|
+ if (isSend) {
|
|
|
+ String msg = "{\"msgtype\": \"text\",\"text\": {\"content\":\"" + buffer.toString() + "\"}}";
|
|
|
+ String rs = HttpUtils.sendPost(dingTalkUrl, msg, "application/json", null);
|
|
|
+ //log.info("【设备监听提醒】 钉钉通知响应:" + rs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|