| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.googlecode.aviator.AviatorEvaluator; |
| | | import com.googlecode.aviator.Expression; |
| | |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.api.service.SysDictDataService; |
| | | import com.moral.api.util.CacheUtils; |
| | | import com.moral.api.util.AdjustDataUtils; |
| | | import com.moral.api.util.LogUtils; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.ConvertUtils; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | |
| | | import java.beans.PropertyDescriptor; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.lang.reflect.Type; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | @Autowired |
| | | private LogUtils logUtils; |
| | | private SysDictDataService sysDictDataService; |
| | | |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | |
| | | private VersionSensorUnitMapper versionSensorUnitMapper; |
| | | |
| | | @Autowired |
| | | private SpecialDeviceMapper specialDeviceMapper; |
| | | |
| | | @Autowired |
| | | private AdjustDataUtils adjustDataUtils; |
| | | |
| | | @Autowired |
| | | private OrganizationService organizationService; |
| | | |
| | | /* |
| | | * 从redis获取设备信息 |
| | | * */ |
| | | private Map<String, Object> getDeviceInfoFromRedis(String mac) { |
| | | Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForValue().get(getDeviceKey(mac)); |
| | | return deviceInfo; |
| | | return (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | /* |
| | | * 设备信息存入redis |
| | | */ |
| | | private void setDeviceInfoToRedis(String mac, Map<String, Object> deviceInfo) { |
| | | redisTemplate.opsForValue().set(getDeviceKey(mac), deviceInfo); |
| | | redisTemplate.opsForHash().put(RedisConstants.DEVICE, mac, deviceInfo); |
| | | } |
| | | |
| | | /* |
| | | * 从redis删除设备信息 |
| | | */ |
| | | private void delDeviceInfoFromRedis(String mac) { |
| | | redisTemplate.delete(getDeviceKey(mac)); |
| | | } |
| | | |
| | | /* |
| | | * 获取设备信息在redis里的key |
| | | */ |
| | | private String getDeviceKey(String mac) { |
| | | return keysConnect(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | /* |
| | | * 从redis获取报警级别 |
| | | * */ |
| | | private Map<String, Object> getOrgAlarmConfigFromRedis(String orId) { |
| | | return null; |
| | | } |
| | | |
| | | //redis key前缀 |
| | | private String keysConnect(String... keys) { |
| | | StringBuilder key = new StringBuilder(keys[0]); |
| | | for (int i = 1; i < keys.length; i++) { |
| | | key.append("_"); |
| | | key.append(keys[i]); |
| | | } |
| | | return key.toString().toLowerCase(); |
| | | redisTemplate.opsForHash().delete(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | @Override |
| | |
| | | delDeviceInfoFromRedis(mac); |
| | | //设备信息存入redis |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | //刷新deviceInfo缓存 |
| | | CacheUtils.refreshDeviceAlarmInfo(); |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("添加了设备:").append(device.getName()).append(";").append("mac:").append(mac); |
| | | logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE); |
| | | content.append("添加了设备:").append(device.getName()).append(":").append("mac:").append(mac); |
| | | LogUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE); |
| | | } |
| | | |
| | | @Override |
| | |
| | | String mac = device.getMac(); |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | //维护组织型号关系表 |
| | | Integer versionId = device.getDeviceVersionId(); |
| | | Integer orgId = device.getOrganizationId(); |
| | | deleteOrganizationUnitAlarm(orgId, versionId); |
| | | //刷新deviceInfo缓存 |
| | | CacheUtils.refreshDeviceAlarmInfo(); |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("删除了设备:").append(device.getName()).append(";").append("mac:").append(mac); |
| | | logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE); |
| | | LogUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE); |
| | | } |
| | | |
| | | @Override |
| | |
| | | insertOrganizationUnitAlarm(newOrgId, newVersionId); |
| | | } |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | delDeviceInfoFromRedis(oldDevice.getMac()); |
| | | Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId); |
| | | //设备信息存入redis |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | //刷新deviceInfo缓存 |
| | | CacheUtils.refreshDeviceAlarmInfo(mac); |
| | | //操作日志记录 |
| | | 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) { |
| | |
| | | } |
| | | |
| | | } |
| | | logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE); |
| | | LogUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> selectMonitorsByOrgId(Integer orgId) { |
| | | //获取所有子组织 |
| | | List<Organization> organizations = organizationService.getAllChildrenOrganization(orgId); |
| | | List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList()); |
| | | orgIds.add(orgId); |
| | | QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.select("id", "name").eq("organization_id", orgId).eq("is_delete", Constants.NOT_DELETE); |
| | | queryWrapper.select("id", "name") |
| | | .eq("is_delete", Constants.NOT_DELETE) |
| | | .in("organization_id", orgIds); |
| | | return monitorPointMapper.selectMaps(queryWrapper); |
| | | } |
| | | |
| | |
| | | Object orgId = parameters.get("organizationId"); |
| | | Object mpId = parameters.get("monitorPointId"); |
| | | |
| | | //查询某组织下所有设备 |
| | | //查询某组织下所有设备,包括子组织 |
| | | if (orgId != null) { |
| | | queryWrapper.eq("organization_id", orgId); |
| | | //查询所有子组织 |
| | | List<Organization> organizations = organizationService.getAllChildrenOrganization(Integer.parseInt(orgId.toString())); |
| | | List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList()); |
| | | orgIds.add(Integer.parseInt(orgId.toString())); |
| | | queryWrapper.in("organization_id", orgIds); |
| | | } |
| | | |
| | | //查询某站点下所有设备 |
| | |
| | | queryWrapper.eq("monitor_point_id", mpId); |
| | | } |
| | | |
| | | //设备名模糊查询 |
| | | if (name != null) { |
| | | queryWrapper.like("name", name); |
| | | } |
| | | |
| | | //mac模糊查询 |
| | | if (mac != null) { |
| | | queryWrapper.like("mac", mac); |
| | | //设备名称或mac模糊查询 |
| | | if (name != null && mac != null) { |
| | | queryWrapper.like("name", name).or().like("mac", mac); |
| | | } |
| | | |
| | | //排序参数,默认create_time降序 |
| | |
| | | //扩展字段 |
| | | deviceInfo.put("extend", device.getExtend()); |
| | | |
| | | //乡镇街道信息 |
| | | deviceInfo.put("town", device.getTown()); |
| | | |
| | | //行业 |
| | | deviceInfo.put("profession", device.getProfession()); |
| | | deviceInfo.put("professionName", device.getProfessionName()); |
| | | deviceInfo.put("professions", device.getProfessions()); |
| | | |
| | | //工艺 |
| | | deviceInfo.put("tech", device.getTech()); |
| | |
| | | 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) { |
| | | return adjustDataUtils.adjust(deviceData); |
| | | public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData,String code) { |
| | | // String mac = deviceData.remove("mac").toString(); |
| | | String mac = deviceData.get("mac").toString(); |
| | | //从redis获取校准公式 |
| | | Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac); |
| | | if (!ObjectUtils.isEmpty(adjustFormula)) { |
| | | Map<String, Object> deviceInfo = getDeviceByMac(mac); |
| | | //获取设备绑定的国控站信息 |
| | | Map<String, Object> govMpInfo = (Map<String, Object>) deviceInfo.get("govMonitorPoint"); |
| | | Map<String, Object> aqiMap = null; |
| | | if (govMpInfo.get("guid") != null) { |
| | | aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMpInfo.get("guid").toString()); |
| | | } |
| | | return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap,code); |
| | | } |
| | | deviceData.remove("DataTime"); |
| | | return deviceData; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> adjustSpecialDeviceData(Map<String, Object> deviceData) { |
| | | // String mac = deviceData.remove("mac").toString(); |
| | | String mac = deviceData.get("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,"0"); |
| | | } |
| | | deviceData.remove("DataTime"); |
| | | return deviceData; |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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("%.3f", sensorValue)); |
| | | //单位转换 |
| | | if (formula != null) { |
| | | //转换后因子值 |
| | | sensorValue = formula.replace("{0}", sensorValue); |
| | | expression = AviatorEvaluator.compile(sensorValue); |
| | | value = Double.parseDouble(String.format("%.3f", expression.execute())); |
| | | } |
| | | int sensorState = judgeState(list, value); |
| | | if (sensorState > state) { |
| | | state = sensorState; |
| | | |
| | | //设备所传因子与配置型号因子不一致的处理逻辑 |
| | | if (deviceData.get(sensorCode) != null) { |
| | | String sensorValue = String.valueOf(deviceData.get(sensorCode)); |
| | | double value = Double.parseDouble(sensorValue); |
| | | if (formula != null) { |
| | | //转换后因子值 |
| | | sensorValue = formula.replace("{0}", sensorValue); |
| | | expression = AviatorEvaluator.compile(sensorValue); |
| | | value = Double.parseDouble(expression.execute().toString()); |
| | | } |
| | | int sensorState = judgeState(list, value); |
| | | if (sensorState > state) { |
| | | state = sensorState; |
| | | } |
| | | } |
| | | } |
| | | //修改设备状态 |
| | | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); |
| | | updateWrapper.eq("id", device.getId()).set("state", state); |
| | | deviceMapper.update(null, updateWrapper); |
| | | Device device1 = new Device(); |
| | | device1.setId(device.getId()); |
| | | device1.setState(String.valueOf(state)); |
| | | deviceMapper.updateById(device1); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> selectMonitorPiontAndDeviceByOrgId(int orgId) { |
| | | QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>(); |
| | | monitorPointQueryWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | monitorPointQueryWrapper.eq("organization_id", orgId); |
| | | List<MonitorPoint> monitorPointList = monitorPointMapper.selectList(monitorPointQueryWrapper); |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | for (MonitorPoint monitorPoint : monitorPointList) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | resultMap = JSON.parseObject(JSON.toJSONString(monitorPoint), Map.class); |
| | | int mp_id = monitorPoint.getId(); |
| | | QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>(); |
| | | deviceQueryWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | deviceQueryWrapper.eq("monitor_point_id", mp_id); |
| | | List<Device> devices = new ArrayList<>(); |
| | | devices = deviceMapper.selectList(deviceQueryWrapper); |
| | | resultMap.put("devices", devices); |
| | | resultList.add(resultMap); |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | //根据因子值判断状态 |
| | | 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; |
| | | 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); |
| | |
| | | organizationUnitAlarmMapper.update(null, deleteWrapper); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void setRedisDevice() { |
| | | List<Device> list = lambdaQuery().eq(Device::getIsDelete,0).list(); |
| | | for(Device d : list){ |
| | | redisTemplate.opsForHash().delete(RedisConstants.DEVICE, d.getMac()); |
| | | Map<String, Object> deviceInfo = selectDeviceInfoById(d.getId()); |
| | | setDeviceInfoToRedis(d.getMac(), deviceInfo); |
| | | } |
| | | } |
| | | } |