package com.isu.gaswellwatch.modbus.data;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.isu.gaswellwatch.entity.Response;
import com.isu.gaswellwatch.modbus.data.listener.Queues;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author 王仕龙
* 2024/11/23 11:55
*/
@Slf4j
@Component
@SuppressWarnings("all")
public class Redis2DBPersistenceService {
public static final String DEFAULT_DATA_TABLE = "t_device_data_";
private static final String DEVICE_INFO_SQL = "SELECT d.*, dp.code as modbus_device_product_code from device d " + "join dictionary dp on dp.id = d.product where id = ";
private static final String EXISTS_TABLE_SQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA" + " IN ('gaswellwatch', 'gas_well_watch') AND TABLE_NAME='$TableName$'";
@Resource
private JdbcTemplate jdbcTemplate;
@Resource(name = "stringRedisTemplate")
private RedisTemplate redisTemplate;
@Resource
private RabbitTemplate rabbitTemplate;
@Resource
private Map persistenceHandlerMap;
private final RestTemplate restTemplate = new RestTemplate();
@Scheduled(cron = "20/30 * * * * ? ")
public void write() {
Map idGatewayMappingMap = getOnlineGateway();
HashOperations operations = this.redisTemplate.opsForHash();
try (Cursor cursor = this.redisTemplate.scan(ScanOptions.scanOptions().match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
Long deviceId;
String cacheKey, tableName;
Map deviceMap;
PersistenceHandler persistenceHandler;
List