| | |
| | | /* |
| | | * 离线设备状态码 |
| | | * */ |
| | | public static final String DEVICE_STATE_OFFLINE = "4"; |
| | | public static final String DEVICE_STATE_OFFLINE = "0"; |
| | | |
| | | /* |
| | | * 臭氧code |
| | |
| | | public static final String SENSOR_KEY = "sensor"; |
| | | |
| | | /* |
| | | * 设备信息前缀 |
| | | * redis中设备信息key |
| | | * */ |
| | | public static final String DEVICE = "device_"; |
| | | public static final String DEVICE = "device"; |
| | | |
| | | /* |
| | | * 存储设备显示单位,报警等级以及单位转换公式 |
| | |
| | | public static final String ADJUST = "adjust_"; |
| | | |
| | | /* |
| | | * 设备实时数据 |
| | | * redis中设备实时数据key |
| | | * */ |
| | | public static final String DEVICE_DATA = "data_"; |
| | | public static final String DEVICE_DATA = "data"; |
| | | |
| | | /* |
| | | * 设备实时数据 |
| | | * redis中aqi数据key |
| | | * */ |
| | | public static final String AQI_DATA = "aqi_"; |
| | | public static final String AQI_DATA = "aqi_data"; |
| | | |
| | | |
| | | } |
| | |
| | | int n = data.size(); |
| | | Collections.sort(data); |
| | | double v = n / (100 / p); |
| | | System.out.println(n % (100 / p)); |
| | | if (n % (100 / p) == 0) { |
| | | if (v == n) { |
| | | return data.get(n - 1); |
| | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.extension.activerecord.Model; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import java.time.LocalDateTime; |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | |
| | | /** |
| | | * 创建(生产)时间 |
| | | */ |
| | | private LocalDateTime createTime; |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 更新时间 |
| | | */ |
| | | private LocalDateTime updateTime; |
| | | private Date updateTime; |
| | | |
| | | /** |
| | | * 安装时间 |
| | | */ |
| | | private LocalDateTime installTime; |
| | | private Date installTime; |
| | | |
| | | /** |
| | | * 逻辑删除,0:不删除;1:删除 |
| | |
| | | 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 java.util.Date; |
| | | import java.util.List; |
| | |
| | | //获取所有在线设备 |
| | | List<Device> devices = deviceMapper.selectList(queryWrapper); |
| | | for (Device device : devices) { |
| | | Map<String, Object> data = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DEVICE_DATA + device.getMac());; |
| | | Map<String, Object> data = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE_DATA, device.getMac()); |
| | | device.setState(Constants.DEVICE_STATE_OFFLINE); |
| | | if (data != null && data.containsKey("DataTime")) { |
| | | Date time = DateUtils.getDate((String) data.get("DataTime"), DateUtils.yyyyMMddHHmmss_EN); |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void updateDeviceState(Device device) { |
| | | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); |
| | | updateWrapper.eq("id", device.getId()).set("state", device.getState()); |
| | |
| | | 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.web.client.RestTemplate; |
| | | |
| | |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryAqi() { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | //获取aqi城市配置 |
| | |
| | | //数据存入数据库 |
| | | historyAqiMapper.insert(historyAqi); |
| | | //存入redis |
| | | redisTemplate.opsForHash().putAll(RedisConstants.AQI_DATA + cityCode, value); |
| | | redisTemplate.opsForHash().put(RedisConstants.AQI_DATA, cityCode, value); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | private HistoryHourlyService historyHourlyService; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryDaily() { |
| | | String format = DateUtils.yyyy_MM_dd_EN; |
| | | Date now = new Date(); |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryFiveMinutely() { |
| | | //时间格式化:yyyy-MM-dd HH:mm |
| | | String format = DateUtils.yyyy_MM_dd_HH_mm_EN; |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | private HistoryDailyService historyDailyService; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryMonthly() { |
| | | //开始时间,上月1号 |
| | | Date start = DateUtils.getFirstDayOfLastMonth(); |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | private HistoryDailyService historyDailyService; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryWeekly() { |
| | | //开始时间,上周一 |
| | | Date start = DateUtils.getLastWeekMonday(); |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.HistoryHourly; |
| | | import com.moral.api.mapper.HistoryHourlyMapper; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.moral.api.service.HistoryAqiService; |
| | | import com.moral.api.service.HistoryDailyService; |
| | | import com.moral.api.service.HistoryFiveMinutelyService; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.HistoryMonthlyService; |
| | | |
| | | |
| | | @Slf4j |
| | |
| | | private HistoryHourlyService historyHourlyService; |
| | | |
| | | @Autowired |
| | | private HistoryFiveMinutelyService historyFiveMinutelyService; |
| | | |
| | | @Autowired |
| | | private HistoryHourlyMapper historyHourlyMapper; |
| | | |
| | | @Autowired |
| | | private HistoryDailyService historyDailyService; |
| | | |
| | | @Autowired |
| | | private HistoryMonthlyService historyMonthlyService; |
| | | |
| | | @Autowired |
| | | private DeviceService deviceService; |
| | | |
| | | @Autowired |
| | | private HistoryAqiService historyAqiService; |
| | | |
| | | @ApiOperation(value = "job测试", notes = "job测试") |
| | | @ApiImplicitParams({ |
| | |
| | | }) |
| | | @RequestMapping(value = "jobTest", method = RequestMethod.GET) |
| | | public void jobTest() { |
| | | historyDailyService.insertHistoryDaily(); |
| | | historyAqiService.insertHistoryAqi(); |
| | | } |
| | | |
| | | |
| | |
| | | //数据校准 |
| | | data = deviceService.adjustDeviceData(data); |
| | | //存入redis |
| | | redisTemplate.opsForValue().set(RedisConstants.DEVICE_DATA + mac, data); |
| | | redisTemplate.opsForHash().put(RedisConstants.DEVICE_DATA, mac, data); |
| | | //判断并修改设备状态 |
| | | deviceService.judgeDeviceState(data); |
| | | ack.acknowledge(); |
| | |
| | | * 从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 |
| | |
| | | Object areaCode = monitorPoint.get("areaCode"); |
| | | Object cityCode = monitorPoint.get("cityCode"); |
| | | |
| | | 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); |
| | | } |
| | |
| | | } |
| | | |
| | | @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); |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | |
| | | private DeviceService deviceService; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryHourly(Map<String, Object> data) { |
| | | Map<String, Object> dataAdjust = new HashMap<>(data); |
| | | String mac = data.remove("mac").toString(); |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | |
| | | private DeviceService deviceService; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryMinutely(Map<String, Object> data) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | Map<String, Object> dataAdjust = new HashMap<>(data); |