| | |
| | | package com.zy.core.task; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | |
| | | import com.zy.asrs.entity.DeviceDataLog; |
| | | import com.zy.asrs.service.DeviceConfigService; |
| | | import com.zy.asrs.service.DeviceDataLogService; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Set; |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class DeviceLogScheduler { |
| | | |
| | | @Autowired |
| | | private DeviceConfigService deviceConfigService; |
| | | @Autowired |
| | | private DeviceDataLogService deviceDataLogService; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | @Scheduled(cron = "0/3 * * * * ? ") |
| | | public void execute() { |
| | | // List<DeviceConfig> shuttleList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>() |
| | | // .eq("device_type", String.valueOf(SlaveType.Shuttle))); |
| | | // for (DeviceConfig deviceConfig : shuttleList) { |
| | | // ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, deviceConfig.getDeviceNo()); |
| | | // if(shuttleThread == null){ |
| | | // continue; |
| | | // } |
| | | // |
| | | // ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); |
| | | // if (shuttleProtocol == null) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // if (System.currentTimeMillis() - shuttleProtocol.getDeviceDataLog() > 500) { |
| | | // if (shuttleThread.getOriginDeviceData() != null) { |
| | | // //采集时间超过5s,保存一次数据记录 |
| | | // |
| | | // //离线不做日志存储 |
| | | // if (shuttleProtocol.getProtocolStatusType().equals(ShuttleProtocolStatusType.OFFLINE)) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // //保存数据记录 |
| | | // DeviceDataLog deviceDataLog = new DeviceDataLog(); |
| | | // deviceDataLog.setOriginData(JSON.toJSONString(shuttleThread.getOriginDeviceData())); |
| | | // deviceDataLog.setWcsData(JSON.toJSONString(shuttleProtocol)); |
| | | // deviceDataLog.setType(String.valueOf(SlaveType.Shuttle)); |
| | | // deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo()); |
| | | // deviceDataLog.setCreateTime(new Date()); |
| | | // deviceDataLogService.insert(deviceDataLog); |
| | | // |
| | | // //更新采集时间 |
| | | // shuttleThread.updateDeviceDataLogTime(System.currentTimeMillis()); |
| | | // } |
| | | // } |
| | | // } |
| | | int maxCount = 100; |
| | | Set<String> keys = redisUtil.scanKeys(RedisKeyType.DEVICE_LOG_KEY.key, maxCount); |
| | | if (keys == null || keys.isEmpty()) { |
| | | return; |
| | | } |
| | | List<Object> values = redisUtil.multiGet(keys); |
| | | List<DeviceDataLog> list = new ArrayList<>(); |
| | | for (Object object : values) { |
| | | if (object instanceof DeviceDataLog) { |
| | | list.add((DeviceDataLog) object); |
| | | } |
| | | } |
| | | if (!list.isEmpty()) { |
| | | if (deviceDataLogService.saveBatch(list)) { |
| | | redisUtil.del(keys.toArray(new String[0])); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |