|
|
|
package com.luhui.deviceinfo.sync.service;
|
|
|
|
|
|
|
|
import cn.hutool.db.nosql.redis.RedisDS;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class JedisService {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger("redis-log");
|
|
|
|
|
|
|
|
private static final RedisDS deviceInfoRedis = RedisDS.create();
|
|
|
|
private static final RedisDS deviceHostRedis = RedisDS.create("devicehost");
|
|
|
|
|
|
|
|
private static void log(String action, Map<String, String> kvMap) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("{\"tag\":\"jedis\",\"action\":\"").append(action).append("\"");
|
|
|
|
for (Map.Entry<String, String> entry : kvMap.entrySet()) {
|
|
|
|
sb.append(",\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
|
|
|
|
}
|
|
|
|
sb.append("}");
|
|
|
|
logger.info(sb.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<String, String> getDevicHost(String imei) {
|
|
|
|
Map<String, String> result = null;
|
|
|
|
try (Jedis jedis = deviceHostRedis.getJedis()) {
|
|
|
|
result = jedis.hgetAll(imei);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("getDevicHost_error", error);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<String, String> getDevicInfo(String deviceInfoId) {
|
|
|
|
Map<String, String> result = null;
|
|
|
|
try (Jedis jedis = deviceInfoRedis.getJedis()) {
|
|
|
|
result = jedis.hgetAll(deviceInfoId);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("deviceInfoId", deviceInfoId);
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("getDevicInfo_error", error);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDevicInfo(String deviceInfoId, String attribute, String value) {
|
|
|
|
if (value == null) {
|
|
|
|
throw new IllegalArgumentException("Value cannot be null");
|
|
|
|
}
|
|
|
|
try (Jedis jedis = deviceInfoRedis.getJedis()) {
|
|
|
|
jedis.hset(deviceInfoId, attribute, value);
|
|
|
|
Map<String, String> info = new HashMap<String, String>();
|
|
|
|
info.put("deviceInfoId", deviceInfoId);
|
|
|
|
info.put("attribute", attribute);
|
|
|
|
info.put("value", value);
|
|
|
|
log("setDevicInfo", info);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("deviceInfoId", deviceInfoId);
|
|
|
|
error.put("attribute", attribute);
|
|
|
|
error.put("value", value);
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("setDevicInfo_error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDevicHost(String imei, String attribute, String value) {
|
|
|
|
if (value == null) {
|
|
|
|
throw new IllegalArgumentException("Value cannot be null");
|
|
|
|
}
|
|
|
|
try (Jedis jedis = deviceHostRedis.getJedis()) {
|
|
|
|
jedis.hset(imei, attribute, value);
|
|
|
|
Map<String, String> info = new HashMap<String, String>();
|
|
|
|
info.put("imei", imei);
|
|
|
|
info.put("attribute", attribute);
|
|
|
|
info.put("value", value);
|
|
|
|
log("setDevicHost", info);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("attribute", attribute);
|
|
|
|
error.put("value", value);
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("setDevicHost_error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDevicHostBatch(String imei, Map<String, String> fields) {
|
|
|
|
if (fields == null || fields.containsValue(null)) {
|
|
|
|
throw new IllegalArgumentException("Fields cannot be null or contain null values");
|
|
|
|
}
|
|
|
|
try (Jedis jedis = deviceHostRedis.getJedis()) {
|
|
|
|
jedis.hmset(imei, fields);
|
|
|
|
Map<String, String> info = new HashMap<String, String>();
|
|
|
|
info.put("imei", imei);
|
|
|
|
info.put("fieldsCount", String.valueOf(fields.size()));
|
|
|
|
log("setDevicHostBatch", info);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("setDevicHostBatch_error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDevicInfoBatchWithTTL(String imei, Map<String, String> fields, int ttlSeconds) {
|
|
|
|
if (fields == null ) {
|
|
|
|
throw new IllegalArgumentException("Fields cannot be null or contain null values");
|
|
|
|
}
|
|
|
|
if(fields.containsValue(null))
|
|
|
|
{
|
|
|
|
// 删除值为空的属性
|
|
|
|
fields.entrySet().removeIf(entry -> entry.getValue() == null || entry.getValue().isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
try (Jedis jedis = deviceInfoRedis.getJedis()) {
|
|
|
|
jedis.hmset(imei, fields);
|
|
|
|
if (ttlSeconds > 0) {
|
|
|
|
jedis.expire(imei, ttlSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map<String, String> info = new HashMap<String, String>();
|
|
|
|
// info.put("imei", imei);
|
|
|
|
// info.put("fieldsCount", String.valueOf(fields.size()));
|
|
|
|
// info.put("ttl", String.valueOf(ttlSeconds));
|
|
|
|
// log("setDevicHostBatchWithTTL", info);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("fieldsCount", String.valueOf(fields.size()));
|
|
|
|
error.put("ttl", String.valueOf(ttlSeconds));
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("setDevicHostBatchWithTTL_error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDevicHostBatchWithTTL(String imei, Map<String, String> fields, int ttlSeconds) {
|
|
|
|
if (fields == null ) {
|
|
|
|
throw new IllegalArgumentException("Fields cannot be null or contain null values");
|
|
|
|
}
|
|
|
|
if(fields.containsValue(null))
|
|
|
|
{
|
|
|
|
// 删除值为空的属性
|
|
|
|
fields.entrySet().removeIf(entry -> entry.getValue() == null || entry.getValue().isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
try (Jedis jedis = deviceHostRedis.getJedis()) {
|
|
|
|
jedis.hmset(imei, fields);
|
|
|
|
if (ttlSeconds > 0) {
|
|
|
|
jedis.expire(imei, ttlSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map<String, String> info = new HashMap<String, String>();
|
|
|
|
// info.put("imei", imei);
|
|
|
|
// info.put("fieldsCount", String.valueOf(fields.size()));
|
|
|
|
// info.put("ttl", String.valueOf(ttlSeconds));
|
|
|
|
// log("setDevicHostBatchWithTTL", info);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("fieldsCount", String.valueOf(fields.size()));
|
|
|
|
error.put("ttl", String.valueOf(ttlSeconds));
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("setDevicHostBatchWithTTL_error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDevicHostWithTTL(String imei, String attribute, String value, int ttlSeconds) {
|
|
|
|
if (value == null) {
|
|
|
|
throw new IllegalArgumentException("Value cannot be null");
|
|
|
|
}
|
|
|
|
try (Jedis jedis = deviceHostRedis.getJedis()) {
|
|
|
|
jedis.hset(imei, attribute, value);
|
|
|
|
jedis.expire(imei, ttlSeconds);
|
|
|
|
Map<String, String> info = new HashMap<String, String>();
|
|
|
|
info.put("imei", imei);
|
|
|
|
info.put("attribute", attribute);
|
|
|
|
info.put("value", value);
|
|
|
|
info.put("ttl", String.valueOf(ttlSeconds));
|
|
|
|
log("setDevicHostWithTTL", info);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("attribute", attribute);
|
|
|
|
error.put("value", value);
|
|
|
|
error.put("ttl", String.valueOf(ttlSeconds));
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("setDevicHostWithTTL_error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean devicHostExists(String imei) {
|
|
|
|
boolean exists = false;
|
|
|
|
try (Jedis jedis = deviceHostRedis.getJedis()) {
|
|
|
|
exists = jedis.exists(imei);
|
|
|
|
Map<String, String> info = new HashMap<String, String>();
|
|
|
|
info.put("imei", imei);
|
|
|
|
info.put("exists", String.valueOf(exists));
|
|
|
|
log("devicHostExists", info);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Map<String, String> error = new HashMap<String, String>();
|
|
|
|
error.put("imei", imei);
|
|
|
|
error.put("error", e.getMessage());
|
|
|
|
log("devicHostExists_error", error);
|
|
|
|
}
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Map<String, String> fields = new HashMap<String, String>();
|
|
|
|
fields.put("type", "MODEL-X");
|
|
|
|
fields.put("status", "online");
|
|
|
|
fields.put("alarm", "000");
|
|
|
|
setDevicHostBatch("test:123", fields);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|