| | |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.util.DeviceExcelDTO; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.DateUtils; |
| | |
| | | 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.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | @Override |
| | | public void judgeOffLineDevice() { |
| | | QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.ne("state", Constants.DEVICE_STATE_OFFLINE); |
| | | queryWrapper.eq("state", Constants.DEVICE_STATE_OFFLINE); |
| | | queryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | //获取所有在线设备 |
| | | List<Device> devices = deviceMapper.selectList(queryWrapper); |
| | | for (Device device : devices) { |
| | | Map<String, Object> data = getDataFromRedis(device.getMac()); |
| | | Map<String, Object> data = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, device.getMac()); |
| | | device.setState(Constants.DEVICE_STATE_OFFLINE); |
| | | if (data != null && data.containsKey("DataTime")) { |
| | | long time = Long.parseLong(data.get("DataTime").toString()); |
| | | if (data != null && data.containsKey("dataTime")) { |
| | | Date time = DateUtils.getDate((String) data.get("dataTime"), DateUtils.yyyyMMddHHmmss_EN); |
| | | //超过两分钟无数据就离线 |
| | | if (DateUtils.getDateOfMin(new Date(time), 2).getTime() < new Date().getTime()) { |
| | | if (DateUtils.getDateOfMin(time, 12).getTime() > new Date().getTime()) { |
| | | device.setState("1"); |
| | | updateDeviceState(device); |
| | | } |
| | | } else { |
| | | updateDeviceState(device); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void updateDeviceState(Device device) { |
| | | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); |
| | | updateWrapper.eq("id", device.getId()).set("state", device.getState()); |
| | | deviceMapper.update(null, updateWrapper); |
| | | } |
| | | |
| | | private Map<String, Object> getDataFromRedis(String mac) { |
| | | return (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DEVICE_DATA + "_" + mac); |
| | | @Override |
| | | public List<Device> getDateByOrgId(int orgId) { |
| | | QueryWrapper<Device> wrapper_device = new QueryWrapper<>(); |
| | | wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgId); |
| | | List<Device> devices = new ArrayList<>(); |
| | | devices = deviceMapper.selectList(wrapper_device); |
| | | return devices; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<DeviceExcelDTO> ListDeviceExcel(String startTime, String endTime, int id) { |
| | | return this.baseMapper.ListDeviceExcel(startTime,id); |
| | | } |
| | | } |