|
|
@@ -14,9 +14,6 @@ import com.xunmei.common.core.enums.CategoryDataEnum;
|
|
|
import com.xunmei.common.core.util.BeanHelper;
|
|
|
import com.xunmei.common.core.utils.DateUtils;
|
|
|
import com.xunmei.common.core.utils.IDHelper;
|
|
|
-import com.xunmei.mediator.api.mapper.MediatorAlarmRuleExpressMapper;
|
|
|
-import com.xunmei.mediator.api.mapper.MediatorAlarmRuleMapper;
|
|
|
-import com.xunmei.mediator.api.mapper.MediatorAlarmSystemFieldMapper;
|
|
|
import com.xunmei.mediator.api.mapper.MediatorTimeTemplateMapper;
|
|
|
import com.xunmei.mediator.domain.dto.redis.RedisKey;
|
|
|
import com.xunmei.system.api.RemoteOrgService;
|
|
|
@@ -71,13 +68,7 @@ public class EquipmentSyncService {
|
|
|
@Autowired
|
|
|
private IMediatorProductDeviceService productDeviceService;
|
|
|
@Resource
|
|
|
- private MediatorAlarmRuleMapper alarmRuleMapper;
|
|
|
- @Resource
|
|
|
private MediatorTimeTemplateMapper timeTemplateMapper;
|
|
|
- @Resource
|
|
|
- private MediatorAlarmRuleExpressMapper alarmRuleExpressMapper;
|
|
|
- @Resource
|
|
|
- private MediatorAlarmSystemFieldMapper alarmSystemFieldMapper;
|
|
|
@Autowired
|
|
|
private IMediatorAlarmDataNewestService alarmDataNewestService;
|
|
|
@Autowired
|
|
|
@@ -380,309 +371,6 @@ public class EquipmentSyncService {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void analysisItem(MediatorDeviceData deviceData) throws ParseException {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
- SimpleDateFormat sdfa = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
- String strItems = null;
|
|
|
- if (deviceData.getMulti() == 1) {
|
|
|
- strItems = deviceData.getMultiItems();
|
|
|
-
|
|
|
- } else {
|
|
|
- strItems = deviceData.getItems();
|
|
|
-
|
|
|
- }
|
|
|
- Date time = new Date();
|
|
|
- Date beginTime = null;
|
|
|
- Date endTime = null;
|
|
|
- //获取设备
|
|
|
- List<MediatorProductDevice> productDevices = productDeviceService.findDeviceByDevice(deviceData.getDeviceId(), deviceData.getOrgId());
|
|
|
- if (productDevices == null || productDevices.size() == 0) {
|
|
|
- log.error("没有找到对应的设备");
|
|
|
- return;
|
|
|
- }
|
|
|
- MediatorProductDevice productDevice = productDevices.stream().collect(Collectors.collectingAndThen
|
|
|
- (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MediatorProductDevice::getDeviceId))), ArrayList::new)).get(0);
|
|
|
- //获取设备类型
|
|
|
- String productType = productDevice.getProductType().toString();
|
|
|
- ProductTypeEnum type = null;
|
|
|
- if (productType.equals("3712194930442240")) {
|
|
|
- //烟雾传感器
|
|
|
- type = ProductTypeEnum.SMOKE_SENSOR;
|
|
|
- }
|
|
|
- if (productType.equals("3712195941285888")) {
|
|
|
- //温湿度采集器
|
|
|
- type = ProductTypeEnum.TEMPERATURE_HUMIDITY;
|
|
|
- }
|
|
|
- if (productType.equals("3712196701503488")) {
|
|
|
- //水浸传感器
|
|
|
- type = ProductTypeEnum.FLOODING;
|
|
|
- }
|
|
|
- if (productType.equals("3712197092999168")) {
|
|
|
- //门磁、窗磁传感器(Lora无线)
|
|
|
- type = ProductTypeEnum.DOOR_SENSOR;
|
|
|
- }
|
|
|
- if (productType.equals("3712197283692544")) {
|
|
|
- //卷帘门门磁
|
|
|
- type = ProductTypeEnum.SHUTTER_DOORS;
|
|
|
- }
|
|
|
- if (productType.equals("3712197446270976")) {
|
|
|
- //地磁传感器
|
|
|
- type = ProductTypeEnum.GEOMAGNETIC;
|
|
|
- }
|
|
|
- if (productType.equals("3712197694111744")) {
|
|
|
- //燃气报警器
|
|
|
- type = ProductTypeEnum.GAS_ALARM;
|
|
|
- }
|
|
|
- if (productType.equals("3712198166114160")) {
|
|
|
- //智能电表
|
|
|
- type = ProductTypeEnum.ZNDB;
|
|
|
- }
|
|
|
- if (type == null) {
|
|
|
- log.error("没有找到对应的设备类型");
|
|
|
- return;
|
|
|
- }
|
|
|
- //根据设备id获取报警规则
|
|
|
- List<MediatorAlarmRule> ListById = alarmRuleMapper.findListByDeviceId(deviceData.getDeviceId().toString(), deviceData.getOrgId(), type.getNum());
|
|
|
- //根据设备类型获取报警规则
|
|
|
- List<MediatorAlarmRule> ListByType = alarmRuleMapper.findListByDeviceType(type.getNum());
|
|
|
- //获取改设备下的所有规则列表
|
|
|
- List<MediatorAlarmRule> listAll = new ArrayList<>();
|
|
|
- if (ListById != null && ListById.size() > 0) {
|
|
|
- listAll.addAll(ListById);
|
|
|
- }
|
|
|
- if (ListByType != null && ListByType.size() > 0) {
|
|
|
- listAll.addAll(ListByType);
|
|
|
- }
|
|
|
- if (listAll.size() == 0) {
|
|
|
- log.error("没有找到报警规则");
|
|
|
- return;
|
|
|
- }
|
|
|
- //对改设备下的所有规则进行去重
|
|
|
- List<Long> ruleIds = listAll.stream().collect(Collectors.collectingAndThen
|
|
|
- (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MediatorAlarmRule::getId))), ArrayList::new)).stream().map(MediatorAlarmRule::getId).collect(Collectors.toList());
|
|
|
-
|
|
|
- //根据规则id,和设备类型获取告警规则表达式表
|
|
|
- List<MediatorAlarmRuleExpress> expressList = alarmRuleExpressMapper.findListByRuleIdAndType(ruleIds, type.getNum());
|
|
|
- //将表达式按规则分组
|
|
|
- if (expressList == null || expressList.size() == 0) {
|
|
|
- log.error("没有找到表达式");
|
|
|
- return;
|
|
|
- }
|
|
|
- Map<Long, List<MediatorAlarmRuleExpress>> expressListMap = expressList.stream().collect(Collectors.groupingBy(MediatorAlarmRuleExpress::getRuleId));
|
|
|
-
|
|
|
- for (Long key : expressListMap.keySet()) {
|
|
|
- List<MediatorAlarmRuleExpress> expresses = expressListMap.get(key);
|
|
|
- if (expresses == null || expresses.size() == 0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- MediatorAlarmRuleExpress express = expressListMap.get(key).get(0);
|
|
|
- if (express.getIsUseWorkTemplate() == 0 && express.getTimeTemplateId() != null) {
|
|
|
- //同一规则下的表达是使用的时间模版一样的,根据时间模版的id来获取时间模版
|
|
|
- MediatorTimeTemplate timeTemplate = timeTemplateMapper.findOneTemplate(express.getTimeTemplateId());
|
|
|
- if (timeTemplate == null || timeTemplate.getTemplatecontent() == null) {
|
|
|
- log.error("没有找到时间模版");
|
|
|
- continue;
|
|
|
- }
|
|
|
- String templateContent = timeTemplate.getTemplatecontent();
|
|
|
- JSONArray jsonArray = JSONArray.parseArray(templateContent);
|
|
|
- JSONObject obj = JSON.parseObject(jsonArray.get(0).toString());
|
|
|
- String workTime = obj.get("startTime").toString();
|
|
|
- String offTime = obj.get("endTime").toString();
|
|
|
- String weekday = obj.get("weekday").toString();
|
|
|
-
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
- calendar.setTime(time);
|
|
|
- int weekIdex = calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
- if (weekday.contains(weekIdex + "")) {
|
|
|
- String timea = sdfa.format(time);
|
|
|
- beginTime = sdf.parse(timea + " " + workTime + ":00");
|
|
|
- endTime = sdf.parse(timea + " " + offTime + ":00");
|
|
|
-
|
|
|
- } else {
|
|
|
- log.error("没有找到时间模版");
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- } else if (express.getIsUseWorkTemplate() == 1) {
|
|
|
- //使用作息时间
|
|
|
- //获取今日的作息
|
|
|
- Map<String, Object> toDayworktime = alarmRuleExpressMapper.findWorkTime(new Date(), deviceData.getOrgId());
|
|
|
- if (toDayworktime == null) {
|
|
|
- log.error("今日没有作息");
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- String ymdDate = toDayworktime.get("ymdDate").toString();
|
|
|
- String workTime = toDayworktime.get("workTime").toString();
|
|
|
- String offTime = toDayworktime.get("offTime").toString();
|
|
|
- beginTime = sdf.parse(ymdDate + " " + workTime + ":00");
|
|
|
- endTime = sdf.parse(ymdDate + " " + offTime + ":00");
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- if (StringUtils.isNotEmpty(strItems)) {
|
|
|
- MediatorProductDevice device = null;
|
|
|
- List<MediatorProductDevice> devices = productDeviceService.findDeviceByDevice(deviceData.getDeviceId(), deviceData.getOrgId());
|
|
|
- if (devices != null && devices.size() > 0) {
|
|
|
- device = devices.stream().collect(Collectors.collectingAndThen
|
|
|
- (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MediatorProductDevice::getDeviceId))), ArrayList::new)).get(0);
|
|
|
- }
|
|
|
- JSONArray jsonArray = JSONArray.parseArray(strItems);
|
|
|
- List<MediatorAlarmData> appAlarmDataList = new ArrayList<>();
|
|
|
- for (MediatorAlarmRuleExpress ruleExpress : expresses) {
|
|
|
- MediatorAlarmData objectMap = analysis(jsonArray, ruleExpress, deviceData, time, beginTime, endTime, type, productDevice);
|
|
|
- if (objectMap != null /*&& !alarmCommonService.filter(objectMap)*/) {
|
|
|
- appAlarmDataList.add(objectMap);
|
|
|
- alarmDataNewestService.saveData(objectMap);
|
|
|
-
|
|
|
- try {
|
|
|
- LambdaUpdateWrapper<MediatorAlarmDataNewest> updateWrapper = Wrappers.lambdaUpdate();
|
|
|
- updateWrapper.eq(MediatorAlarmDataNewest::getDeviceId, objectMap.getDeviceId())
|
|
|
- .eq(MediatorAlarmDataNewest::getFieldcode, objectMap.getFieldCode());
|
|
|
- MediatorAlarmDataNewest alarmDataNewest = new MediatorAlarmDataNewest();
|
|
|
- BeanHelper.copyProperties(alarmDataNewest, objectMap);
|
|
|
- //记录下最新告警的id
|
|
|
- alarmDataNewest.setAlarmDataId(objectMap.getId());
|
|
|
- alarmDataNewestService.saveOrUpdate(alarmDataNewest, updateWrapper);
|
|
|
-
|
|
|
- /* TodayAlarm todayAlarm = saveTodayAlarm(objectMap, deviceData);
|
|
|
- rabbitAlarmService.sendDongHuan(todayAlarm);*/
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("告警数据转换为最新数据失败: " + e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (device != null) {
|
|
|
- //异步中不更新这两个字段
|
|
|
- device.setTimeofOnline(null);
|
|
|
- device.setCheckTime(null);
|
|
|
- if (appAlarmDataList.size() > 0) {
|
|
|
- //把对应设备修改为告警
|
|
|
- device.setDoStatus(1);
|
|
|
- } else {
|
|
|
- //把设备修改wie正常
|
|
|
- device.setDoStatus(0);
|
|
|
- }
|
|
|
- productDeviceService.saveOrUpdate(device);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private MediatorAlarmData analysis(JSONArray jsonArray, MediatorAlarmRuleExpress ruleExpress, MediatorDeviceData deviceData, Date time, Date beginTime, Date endTime, ProductTypeEnum type, MediatorProductDevice productDevice) throws ParseException {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
- boolean flag = false;
|
|
|
- for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
- JSONObject obj = JSON.parseObject(jsonArray.get(i).toString());
|
|
|
- String filedcode = alarmSystemFieldMapper.findByFieldCode(ruleExpress.getFieldcode(), obj.get("fieldId").toString());
|
|
|
- if (!StringUtils.isNotEmpty(filedcode)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- //当日该设备不再接收该类型报警
|
|
|
- String strKey = RedisKey.NOT_ALARM_AGAIN + productDevice.getDeviceId() + "_" + type.getNum() + "_" + ruleExpress.getFieldcode();
|
|
|
- /*if (Boolean.TRUE.equals(redisTemplate.hasKey(strKey))) {
|
|
|
- log.info("机构名称:{}, 设备id:{},设备名称:{} 不再接收{}类型报警", productDevice.getOrgName(), productDevice.getDeviceId(), productDevice.getDeviceName(), type.getText());
|
|
|
- continue;
|
|
|
- }*/
|
|
|
- boolean equals = Boolean.TRUE.equals(stringRedisTemplate.hasKey(strKey));
|
|
|
- if (equals) {
|
|
|
- LogUtils.STATUS_INFO_DEVICE_DATA.info("机构名称:{}, 设备id:{},设备名称:{} 不再接收{}类型报警", productDevice.getOrgName(), productDevice.getDeviceId(), productDevice.getDeviceName(), type.getText());
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- LogUtils.STATUS_INFO_DEVICE_DATA.info("redis 中不包含key :{},继续向下执行。。。。", strKey);
|
|
|
- }
|
|
|
- if (obj.getString("fieldCode").equals(filedcode)) {
|
|
|
- flag = true;
|
|
|
- ElectricityMeterAttributes attributes = ElectricityMeterAttributes.getEnumByAttrtCode(ruleExpress.getFieldcode());
|
|
|
- if (ruleExpress.getFieldcode().equals("CurrentHumidity") || ruleExpress.getFieldcode().equals("CurrentTemperature")
|
|
|
- || ObjectUtil.isNotEmpty(attributes) && ObjectUtil.isNotEmpty(attributes.getUnit())) {
|
|
|
- String value = obj.get("value").toString();
|
|
|
- String operator = ruleExpress.getOperator();
|
|
|
- if (sdf.parse(sdf.format(time)).after(beginTime) && time.before(endTime)) {
|
|
|
- String unit = "";
|
|
|
- String str = "";
|
|
|
- if (ruleExpress.getFieldcode().equals("CurrentHumidity")) {
|
|
|
- unit = "%RH";
|
|
|
- str = "湿度";
|
|
|
- }
|
|
|
- if (ruleExpress.getFieldcode().equals("CurrentTemperature")) {
|
|
|
- unit = "°C";
|
|
|
- str = "温度";
|
|
|
- }
|
|
|
- if (ruleExpress.getFieldcode().contains("METER_ATTRIBUTES")) {
|
|
|
- unit = attributes.getUnit();
|
|
|
- str = attributes.getText();
|
|
|
- }
|
|
|
- if (operator.equals("EQUALS")) {
|
|
|
- if (Double.valueOf(value).equals(Double.valueOf(ruleExpress.getValue()))) {
|
|
|
- //保存一条异常数据object异常数据对象
|
|
|
- String content = deviceData.getDeviceName() + str + value + unit;
|
|
|
- MediatorAlarmData alarm = buildDeviceAlarm(value, ruleExpress, deviceData, type, content);
|
|
|
- return alarm;
|
|
|
- } else {
|
|
|
- }
|
|
|
- }
|
|
|
- if (operator.equals("GT")) {
|
|
|
- if (Double.valueOf(value) > Double.valueOf(ruleExpress.getValue())) {
|
|
|
- //保存一条异常数据object异常数据对象
|
|
|
- String content = deviceData.getDeviceName() + str + value + unit;
|
|
|
- MediatorAlarmData alarm = buildDeviceAlarm(value, ruleExpress, deviceData, type, content);
|
|
|
- return alarm;
|
|
|
- }
|
|
|
- }
|
|
|
- if (operator.equals("GTE")) {
|
|
|
- if (Double.valueOf(value) >= Double.valueOf(ruleExpress.getValue())) {
|
|
|
- //保存一条异常数据object异常数据对象
|
|
|
- String content = deviceData.getDeviceName() + str + value + unit;
|
|
|
- MediatorAlarmData alarm = buildDeviceAlarm(value, ruleExpress, deviceData, type, content);
|
|
|
- return alarm;
|
|
|
- }
|
|
|
- }
|
|
|
- if (operator.equals("LT")) {
|
|
|
- if (Double.valueOf(value) < Double.valueOf(ruleExpress.getValue())) {
|
|
|
- //保存一条异常数据object异常数据对象
|
|
|
- String content = deviceData.getDeviceName() + str + value + unit;
|
|
|
- MediatorAlarmData alarm = buildDeviceAlarm(value, ruleExpress, deviceData, type, content);
|
|
|
- return alarm;
|
|
|
- }
|
|
|
- }
|
|
|
- if (operator.equals("LTE")) {
|
|
|
- if (Double.valueOf(value) <= Double.valueOf(ruleExpress.getValue())) {
|
|
|
- //保存一条异常数据object异常数据对象
|
|
|
- String content = deviceData.getDeviceName() + str + value + unit;
|
|
|
- MediatorAlarmData alarm = buildDeviceAlarm(value, ruleExpress, deviceData, type, content);
|
|
|
- return alarm;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- String value = obj.get("value").toString();
|
|
|
- if (ruleExpress.getFieldcode().contains("METER_ATTRIBUTES")) {
|
|
|
- Double key = Double.parseDouble(obj.getString("value"));
|
|
|
- value = key.intValue() + "";
|
|
|
- }
|
|
|
- if (sdf.parse(sdf.format(time)).after(beginTime) && time.before(endTime)) {
|
|
|
- if (value.equals(ruleExpress.getValue()) || value.equals(ruleExpress.getValueText())) {
|
|
|
- //保存一条异常数据object异常数据对象
|
|
|
- String content = deviceData.getDeviceName() + ruleExpress.getValueText();
|
|
|
- MediatorAlarmData alarm = buildDeviceAlarm(value, ruleExpress, deviceData, type, content);
|
|
|
- return alarm;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!flag) {
|
|
|
- log.error("设备【" + deviceData.getDeviceName() + "】未找到报警关键字");
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
private MediatorAlarmData buildDeviceAlarm(String value, MediatorAlarmRuleExpress ruleExpress, MediatorDeviceData deviceData, ProductTypeEnum type, String content) throws ParseException {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|