| | |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | |
| | | import com.moral.api.service.SysDictDataService; |
| | | import com.moral.api.util.CacheUtils; |
| | | import com.moral.api.util.AdjustDataUtils; |
| | | import com.moral.api.util.LogUtils; |
| | |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | @Autowired |
| | | private SysDictDataService sysDictDataService; |
| | | |
| | | @Autowired |
| | | private LogUtils logUtils; |
| | | |
| | | @Autowired |
| | |
| | | private VersionSensorUnitMapper versionSensorUnitMapper; |
| | | |
| | | @Autowired |
| | | private SpecialDeviceMapper specialDeviceMapper; |
| | | |
| | | @Autowired |
| | | private AdjustDataUtils adjustDataUtils; |
| | | |
| | | /* |
| | | * 从redis获取设备信息 |
| | | * */ |
| | | private Map<String, Object> getDeviceInfoFromRedis(String mac) { |
| | | return (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DEVICE + mac); |
| | | return (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | /* |
| | | * 设备信息存入redis |
| | | */ |
| | | private void setDeviceInfoToRedis(String mac, Map<String, Object> deviceInfo) { |
| | | redisTemplate.opsForValue().set(RedisConstants.DEVICE + mac, deviceInfo); |
| | | redisTemplate.opsForHash().put(RedisConstants.DEVICE, mac, deviceInfo); |
| | | } |
| | | |
| | | /* |
| | | * 从redis删除设备信息 |
| | | */ |
| | | private void delDeviceInfoFromRedis(String mac) { |
| | | redisTemplate.delete(RedisConstants.DEVICE + mac); |
| | | redisTemplate.opsForHash().delete(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | @Override |
| | |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("添加了设备:").append(device.getName()).append(";").append("mac:").append(mac); |
| | | content.append("添加了设备:").append(device.getName()).append(":").append("mac:").append(mac); |
| | | logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE); |
| | | } |
| | | |
| | |
| | | insertOrganizationUnitAlarm(newOrgId, newVersionId); |
| | | } |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | delDeviceInfoFromRedis(oldDevice.getMac()); |
| | | Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId); |
| | | //设备信息存入redis |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("修改了设备:").append(mac).append(":"); |
| | | content.append("修改了设备:" + oldDevice.getMac()).append("==>").append(mac).append(":"); |
| | | Field[] fields = Device.class.getDeclaredFields(); |
| | | for (Field field : fields) { |
| | | if (field.getName().equals("id")) { |
| | |
| | | Object o1 = method.invoke(oldDevice); |
| | | Object o2 = method.invoke(device); |
| | | if (o2 != null) { |
| | | content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(";"); |
| | | content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | |
| | | mpInfo.put("provinceCode", monitorPoint.getProvinceCode()); |
| | | deviceInfo.put("monitorPoint", mpInfo); |
| | | |
| | | //国控站点 |
| | | Map<String, Object> govMpInfo = new LinkedHashMap<>(); |
| | | |
| | | govMpInfo.put("guid", null); |
| | | govMpInfo.put("name", null); |
| | | if (device.getGovMonitorPoint() != null) { |
| | | govMpInfo.put("guid", device.getGovMonitorPoint().getGuid()); |
| | | govMpInfo.put("name", device.getGovMonitorPoint().getName()); |
| | | } |
| | | deviceInfo.put("govMonitorPoint", govMpInfo); |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | return deviceInfo; |
| | | } |
| | |
| | | @Override |
| | | public Map<String, Object> getDeviceByMac(String mac) { |
| | | Map<String, Object> deviceInfo = getDeviceInfoFromRedis(mac); |
| | | if (deviceInfo == null) { |
| | | if (ObjectUtils.isEmpty(deviceInfo)) { |
| | | QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("mac", mac).eq("is_delete", Constants.NOT_DELETE); |
| | | Device device = deviceMapper.selectOne(queryWrapper); |
| | |
| | | |
| | | @Override |
| | | public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData) { |
| | | String mac = deviceData.get("mac").toString(); |
| | | String mac = deviceData.remove("mac").toString(); |
| | | //从redis获取校准公式 |
| | | Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + mac); |
| | | Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac); |
| | | if (!ObjectUtils.isEmpty(adjustFormula)) { |
| | | Map<String, Object> deviceInfo = getDeviceByMac(mac); |
| | | Map<String, Object> monitorPoint = (Map<String, Object>) deviceInfo.get("monitorPoint"); |
| | | Object areaCode = monitorPoint.get("areaCode"); |
| | | Object cityCode = monitorPoint.get("cityCode"); |
| | | String areaCode = monitorPoint.get("areaCode").toString(); |
| | | String cityCode = monitorPoint.get("cityCode").toString(); |
| | | |
| | | Map<String, Object> aqiMap = redisTemplate.opsForHash().entries(RedisConstants.AQI_DATA + areaCode); |
| | | Map<String, Object> aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, areaCode); |
| | | if (ObjectUtils.isEmpty(aqiMap)) { |
| | | aqiMap = redisTemplate.opsForHash().entries(RedisConstants.AQI_DATA + cityCode); |
| | | aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, cityCode); |
| | | } |
| | | return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap); |
| | | } |
| | | deviceData.remove("DataTime"); |
| | | return deviceData; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> adjustSpecialDeviceData(Map<String, Object> deviceData) { |
| | | String mac = deviceData.remove("mac").toString(); |
| | | //从redis获取校准公式 |
| | | Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac); |
| | | if (!ObjectUtils.isEmpty(adjustFormula)) { |
| | | Organization organization = specialDeviceMapper.selectOrgByMac(mac); |
| | | String areaCode = organization.getAreaCode().toString(); |
| | | String cityCode = organization.getCityCode().toString(); |
| | | Map<String, Object> aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, areaCode); |
| | | if (ObjectUtils.isEmpty(aqiMap)) { |
| | | aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, cityCode); |
| | | } |
| | | return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap); |
| | | } |
| | | deviceData.remove("DataTime"); |
| | | return deviceData; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void judgeDeviceState(Map<String, Object> deviceData) { |
| | | String mac = deviceData.remove("mac").toString(); |
| | | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | |
| | | if (StringUtils.isEmpty(alarmLevel)) { |
| | | continue; |
| | | } |
| | | List<Double> list = JSONObject.parseObject(alarmLevel, List.class); |
| | | List<Object> list = JSONObject.parseObject(alarmLevel, List.class); |
| | | String sensorCode = sensor.getCode(); |
| | | //转换公式 |
| | | String formula = sensor.getFormula(); |
| | | //转换单位前因子值 |
| | | String sensorValue = (String) deviceData.get(sensorCode); |
| | | double value = Double.parseDouble(String.format("%.4f", sensorValue)); |
| | | double value = Double.parseDouble(sensorValue); |
| | | //单位转换 |
| | | if (formula != null) { |
| | | //转换后因子值 |
| | | sensorValue = formula.replace("{0}", sensorValue); |
| | | expression = AviatorEvaluator.compile(sensorValue); |
| | | value = Double.parseDouble(String.format("%.4f", expression.execute())); |
| | | value = Double.parseDouble(expression.execute().toString()); |
| | | } |
| | | int sensorState = judgeState(list, value); |
| | | if (sensorState > state) { |
| | |
| | | } |
| | | //修改设备状态 |
| | | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); |
| | | updateWrapper.eq("id", device.getId()).set("state", state); |
| | | updateWrapper.eq("id", device.getId()).set("state", String.valueOf(state)); |
| | | deviceMapper.update(null, updateWrapper); |
| | | } |
| | | |
| | | //根据因子值判断状态 |
| | | private int judgeState(List<Double> levels, Double data) { |
| | | private int judgeState(List<Object> levels, Double data) { |
| | | int state = 1; |
| | | for (int i = levels.size() - 1; i >= 0; i--) { |
| | | Double level = levels.get(i); |
| | | Double level = Double.parseDouble(levels.get(i).toString()); |
| | | if (data >= level) { |
| | | state = i + 2; |
| | | state = i + 1; |
| | | break; |
| | | } |
| | | } |
| | | return state; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 判断组织是否含有该设备的型号,如果没有则添加到组织型号关系表中 |
| | | * @Param: [orgId, versionId] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/6 |
| | | */ |
| | | private void insertOrganizationUnitAlarm(Integer orgId, Integer versionId) { |
| | | //查询是否含有该型号 |
| | | QueryWrapper<OrganizationUnitAlarm> queryOrganizationVersionWrapper = new QueryWrapper<>(); |
| | | queryOrganizationVersionWrapper.eq("organization_id", orgId); |
| | | queryOrganizationVersionWrapper.eq("version_id", versionId); |
| | | queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrganizationVersionWrapper); |
| | | //如果没有该型号则插入数据 |
| | | if (ObjectUtils.isEmpty(organizationUnitAlarms)) { |
| | | //查询型号的因子和单位 |
| | | QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper = new QueryWrapper<>(); |
| | | queryVersionSensorUnitWrapper.eq("version_id", versionId); |
| | | queryVersionSensorUnitWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | List<VersionSensorUnit> versionSensorUnits = versionSensorUnitMapper.selectList(queryVersionSensorUnitWrapper); |
| | | //获取所有因子的默认报警值 |
| | | Map<String, SysDictData> sensorAlarms = sysDictDataService.getDictDatasByType("defaultAlarm"); |
| | | if (!ObjectUtils.isEmpty(versionSensorUnits)) { |
| | | for (VersionSensorUnit versionSensorUnit : versionSensorUnits) { |
| | | OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm(); |
| | | String sensorCode = versionSensorUnit.getSensorCode(); |
| | | //设置默认报警值 |
| | | SysDictData alarm = sensorAlarms.get(sensorCode); |
| | | if(alarm!=null) |
| | | organizationUnitAlarm.setAlarmLevel(alarm.getDataValue()); |
| | | organizationUnitAlarm.setOrganizationId(orgId); |
| | | organizationUnitAlarm.setVersionId(versionId); |
| | | organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode()); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @Description: 删除设备后,如果该组织没有这个设备型号其他的设备了,则删除组织型号关系表中该型号的信息。 |
| | | * @Param: [orgId, versionId] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/6 |
| | | */ |
| | | private void deleteOrganizationUnitAlarm(Integer orgId, Integer versionId) { |
| | | QueryWrapper<Device> queryOrganizationVersionWrapper = new QueryWrapper<>(); |
| | | queryOrganizationVersionWrapper.eq("organization_id", orgId); |