| | |
| | | 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.plugins.pagination.Page; |
| | | import com.googlecode.aviator.AviatorEvaluator; |
| | | import com.googlecode.aviator.Expression; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.*; |
| | | import com.moral.api.pojo.vo.device.DeviceVO; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | |
| | | 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 org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | |
| | | 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.util.stream.Collectors; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | |
| | | * @since 2021-05-11 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService { |
| | | |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private VersionSensorUnitMapper versionSensorUnitMapper; |
| | | |
| | | |
| | | @Autowired |
| | | private AdjustDataUtils adjustDataUtils; |
| | | |
| | | /* |
| | | * 从redis获取设备信息 |
| | | * */ |
| | | private Map<String, Object> getDeviceInfoFromRedis(String mac) { |
| | | Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DEVICE + mac); |
| | | Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForValue().get(getDeviceKey(mac)); |
| | | return deviceInfo; |
| | | } |
| | | |
| | |
| | | */ |
| | | private String getDeviceKey(String mac) { |
| | | return keysConnect(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | /* |
| | | * 从redis获取报警级别 |
| | | * */ |
| | | private Map<String, Object> getOrgAlarmConfigFromRedis(String orId) { |
| | | return null; |
| | | } |
| | | |
| | | //redis key前缀 |
| | |
| | | @Override |
| | | @Transactional |
| | | public void delete(Integer deviceId) { |
| | | Device device = deviceMapper.selectById(deviceId); |
| | | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); |
| | | updateWrapper.eq("id", deviceId).set("is_delete", Constants.DELETE); |
| | | deviceMapper.update(null, updateWrapper); |
| | | Device device = deviceMapper.selectById(deviceId); |
| | | String mac = device.getMac(); |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | //维护组织型号关系表 |
| | | Integer versionId = device.getDeviceVersionId(); |
| | | Integer orgId = device.getOrganizationId(); |
| | | deleteOrganizationUnitAlarm(orgId,versionId); |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | |
| | | queryWrapper.eq("monitor_point_id", mpId); |
| | | } |
| | | |
| | | //设备名模糊查询 |
| | | if (name != null) { |
| | | queryWrapper.like("name", name); |
| | | } |
| | | |
| | | //mac模糊查询 |
| | | if (mac != null) { |
| | | queryWrapper.like("mac", mac); |
| | | } |
| | | |
| | | |
| | | //排序参数,默认create_time降序 |
| | | if (order != null && orderType != null) { |
| | |
| | | result.put("item", items); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public Map<String, Object> selectDeviceInfoById(Integer deviceId) { |
| | |
| | | MonitorPoint monitorPoint = device.getMonitorPoint(); |
| | | mpInfo.put("id", monitorPoint.getId()); |
| | | mpInfo.put("name", monitorPoint.getName()); |
| | | mpInfo.put("areaCode", monitorPoint.getAreaCode()); |
| | | mpInfo.put("cityCode", monitorPoint.getCityCode()); |
| | | mpInfo.put("provinceCode", monitorPoint.getProvinceCode()); |
| | | deviceInfo.put("monitorPoint", mpInfo); |
| | | |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData, Map<String, Object> deviceInfo) { |
| | | return null; |
| | | public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData) { |
| | | return adjustDataUtils.adjust(deviceData); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> judgeDeviceState(Map<String, Object> deviceData, Map<String, Object> deviceInfo) { |
| | | return null; |
| | | public void judgeDeviceState(Map<String, Object> deviceData) { |
| | | String mac = deviceData.remove("mac").toString(); |
| | | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | | Version version = device.getVersion(); |
| | | List<Sensor> sensors = version.getSensors(); |
| | | Expression expression; |
| | | int state = 1; |
| | | for (Sensor sensor : sensors) { |
| | | //因子报警等级 |
| | | String alarmLevel = sensor.getAlarmLevel(); |
| | | if (StringUtils.isEmpty(alarmLevel)) { |
| | | continue; |
| | | } |
| | | List<Double> 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; |
| | | } |
| | | } |
| | | //修改设备状态 |
| | | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); |
| | | updateWrapper.eq("id", device.getId()).set("state", state); |
| | | deviceMapper.update(null, updateWrapper); |
| | | } |
| | | |
| | | //根据因子值判断状态 |
| | | private int judgeState(List<Double> levels, Double data) { |
| | | int state = 1; |
| | | for (int i = levels.size() - 1; i >= 0; i--) { |
| | | Double level = levels.get(i); |
| | | if (data >= level) { |
| | | state = i + 2; |
| | | break; |
| | | } |
| | | } |
| | | return state; |
| | | } |
| | | |
| | | private void insertOrganizationUnitAlarm(Integer orgId,Integer versionId){ |