Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
pnoker
2021-12-07 21:45:22 +08:00
11 changed files with 238 additions and 100 deletions
@@ -106,7 +106,7 @@ public class TokenServiceImpl implements TokenService {
@Override
public boolean cancelToken(String username) {
redisUtil.removeKey(Common.Cache.USER + Common.Cache.TOKEN + Common.Cache.SEPARATOR + username);
redisUtil.deleteKey(Common.Cache.USER + Common.Cache.TOKEN + Common.Cache.SEPARATOR + username);
return true;
}
@@ -134,7 +134,7 @@ public class PointValueServiceImpl implements PointValueService {
String prefix = Common.Cache.REAL_TIME_VALUE_KEY_PREFIX + deviceId + "_";
List<String> keys = listR.getData().stream().map(point -> prefix + point.getId()).collect(Collectors.toList());
if (keys.size() > 0) {
List<PointValue> pointValues = redisUtil.getKeys(keys, PointValue.class);
List<PointValue> pointValues = redisUtil.getKey(keys, PointValue.class);
pointValues = pointValues.stream().filter(Objects::nonNull).map(pointValue -> pointValue.setTimeOut(null).setTimeUnit(null)).collect(Collectors.toList());
if (pointValues.size() > 0) {
return pointValues;
@@ -318,7 +318,15 @@ public class PointValueServiceImpl implements PointValueService {
* @param pointValues Point Value Array
*/
private void savePointValuesToRedis(final List<PointValue> pointValues) {
pointValues.forEach(this::savePointValueToRedis);
Map<String, Object> valueMap = new HashMap<>(16);
Map<String, Long> expireMap = new HashMap<>(16);
for (PointValue pointValue : pointValues) {
String pointIdKey = pointValue.getPointId() != null ? String.valueOf(pointValue.getPointId()) : Common.Cache.ASTERISK;
String key = Common.Cache.REAL_TIME_VALUE_KEY_PREFIX + pointValue.getDeviceId() + Common.Cache.DOT + pointIdKey;
valueMap.put(key, pointValue);
expireMap.put(key, pointValue.getTimeUnit().toMillis(pointValue.getTimeOut()));
}
redisUtil.setKey(valueMap, expireMap);
}
private PointValue latestPointValue(Long deviceId, Long pointId) {
@@ -15,12 +15,15 @@ package com.dc3.common.utils;
import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
@@ -33,22 +36,6 @@ public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
/**
* 判断 Key 是否存在
*
* @param key String key
* @return boolean
*/
public boolean hasKey(String key) {
try {
Boolean hasKey = redisTemplate.hasKey(key);
return null != hasKey ? hasKey : false;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return false;
}
/**
* 添加 Key 缓存
*
@@ -63,36 +50,6 @@ public class RedisUtil {
}
}
/**
* 批量添加 Key 缓存
*
* @param key String key
* @param value Object
*/
public void setKeys(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 批量添加 Key 缓存,并设置失效时间
*
* @param key String key
* @param value Object
* @param time Time
* @param unit TimeUnit
*/
public void setKeys(String key, Object value, long time, TimeUnit unit) {
try {
redisTemplate.opsForValue().set(key, value, time, unit);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 添加 Key 缓存,并设置失效时间
*
@@ -109,6 +66,34 @@ public class RedisUtil {
}
}
/**
* 批量添加 Key 缓存
*
* @param valuesMap Map<String, Object>
*/
public void setKey(Map<String, Object> valuesMap) {
try {
redisTemplate.opsForValue().multiSet(valuesMap);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 批量添加 Key 缓存,并设置失效时间
*
* @param valueMap Map<String, Object>
* @param expireMillis Map<String, Long>
*/
public void setKey(Map<String, Object> valueMap, Map<String, Long> expireMillis) {
try {
redisTemplate.opsForValue().multiSet(valueMap);
setExpire(expireMillis);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 获取 Key 缓存
*
@@ -127,13 +112,13 @@ public class RedisUtil {
}
/**
* 获取 Keys 缓存
* 批量获取 Key 缓存
*
* @param keys String key array
* @param <T> T
* @return T Array
*/
public <T> List<T> getKeys(List<String> keys, Class<T> type) {
public <T> List<T> getKey(List<String> keys, Class<T> type) {
try {
List<Object> objects = redisTemplate.opsForValue().multiGet(keys);
return Convert.toList(type, objects);
@@ -144,24 +129,49 @@ public class RedisUtil {
}
/**
* 删除 Keys 缓存
* 删除 Key 缓存
*
* @param key Key
*/
public void deleteKey(String key) {
try {
redisTemplate.delete(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 批量删除 Key 缓存
*
* @param keys Key Array
*/
public void removeKey(String... keys) {
if (null != keys && keys.length > 0) {
public void deleteKey(List<String> keys) {
if (null != keys && keys.size() > 0) {
try {
if (keys.length == 1) {
redisTemplate.delete(keys[0]);
} else {
redisTemplate.delete(Arrays.asList(keys));
}
redisTemplate.delete(keys);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
/**
* 判断 Key 是否存在
*
* @param key String key
* @return boolean
*/
public boolean hasKey(String key) {
try {
Boolean hasKey = redisTemplate.hasKey(key);
return null != hasKey ? hasKey : false;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return false;
}
/**
* 指定键值失效时间
*
@@ -179,6 +189,47 @@ public class RedisUtil {
}
}
/**
* 批量指定键值失效时间
*
* @param expireMillis Map<String, Long>
*/
public void setExpire(Map<String, Long> expireMillis) {
try {
if (null != expireMillis && expireMillis.size() > 0) {
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.execute((RedisCallback<Object>) connection -> {
expireMillis.forEach((key, expire) -> {
byte[] serialize = stringRedisSerializer.serialize(key);
if (null != serialize) {
connection.pExpire(serialize, expire);
}
});
return null;
});
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 指定键值在指定时间失效
*
* @param key String key
* @param date Date
*/
public void setExpireAt(String key, Date date) {
try {
Date current = new Date();
if (date.getTime() >= current.getTime()) {
redisTemplate.expireAt(key, date);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 获取 Key 失效时间
*
@@ -195,4 +246,5 @@ public class RedisUtil {
}
return 0L;
}
}
@@ -28,7 +28,7 @@ import java.util.Date;
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Plcs7PointVariable {
public class PlcS7PointVariable {
private int dbNum;
private int byteOffset;
private int bitOffset;
@@ -36,7 +36,7 @@ public class Plcs7PointVariable {
private S7Type type;
private Class<?> fieldType;
public Plcs7PointVariable(int dbNum, int byteOffset, int bitOffset, int size, String s7Type) {
public PlcS7PointVariable(int dbNum, int byteOffset, int bitOffset, int size, String s7Type) {
this.dbNum = dbNum;
this.byteOffset = byteOffset;
this.bitOffset = bitOffset;
@@ -22,17 +22,19 @@ import com.dc3.common.model.Point;
import com.dc3.common.sdk.bean.driver.DriverContext;
import com.dc3.common.sdk.service.DriverCustomService;
import com.dc3.common.sdk.service.DriverService;
import com.dc3.driver.bean.Plcs7PointVariable;
import com.dc3.driver.bean.PlcS7PointVariable;
import com.github.s7connector.api.S7Connector;
import com.github.s7connector.api.S7Serializer;
import com.github.s7connector.api.factory.S7ConnectorFactory;
import com.github.s7connector.api.factory.S7SerializerFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.dc3.common.sdk.util.DriverUtils.attribute;
import static com.dc3.common.sdk.util.DriverUtils.value;
@@ -52,7 +54,13 @@ public class DriverCustomServiceImpl implements DriverCustomService {
/**
* Plc Connector Map
*/
private volatile Map<Long, S7Connector> s7ConnectorMap;
private volatile Map<Long, MyS7Connector> s7ConnectorMap;
@Data
private static class MyS7Connector {
private ReentrantReadWriteLock lock;
private S7Connector connector;
}
@Override
public void initial() {
@@ -62,18 +70,38 @@ public class DriverCustomServiceImpl implements DriverCustomService {
@Override
public String read(Map<String, AttributeInfo> driverInfo, Map<String, AttributeInfo> pointInfo, Device device, Point point) throws Exception {
log.debug("Plc S7 Read, device: {}, point: {}", JSON.toJSONString(device), JSON.toJSONString(point));
S7Serializer serializer = getS7Serializer(device.getId(), driverInfo);
Plcs7PointVariable plcs7PointVariable = getPointVariable(pointInfo, point.getType());
return String.valueOf(serializer.dispense(plcs7PointVariable));
MyS7Connector myS7Connector = getS7Connector(device.getId(), driverInfo);
myS7Connector.lock.writeLock().lock();
S7Serializer serializer = S7SerializerFactory.buildSerializer(myS7Connector.getConnector());
PlcS7PointVariable plcs7PointVariable = getPointVariable(pointInfo, point.getType());
try {
return String.valueOf(serializer.dispense(plcs7PointVariable));
} catch (Exception e) {
log.error("Plc S7 Read Error: {}", e.getMessage());
return "nil";
} finally {
myS7Connector.lock.writeLock().unlock();
}
}
@Override
public Boolean write(Map<String, AttributeInfo> driverInfo, Map<String, AttributeInfo> pointInfo, Device device, AttributeInfo value) throws Exception {
log.debug("Plc S7 Read, device: {}, value: {}", JSON.toJSONString(device), JSON.toJSONString(value));
S7Serializer serializer = getS7Serializer(device.getId(), driverInfo);
Plcs7PointVariable plcs7PointVariable = getPointVariable(pointInfo, value.getType());
store(serializer, plcs7PointVariable, value.getType(), value.getValue());
return true;
log.debug("Plc S7 Write, device: {}, value: {}", JSON.toJSONString(device), JSON.toJSONString(value));
MyS7Connector myS7Connector = getS7Connector(device.getId(), driverInfo);
myS7Connector.lock.writeLock().lock();
S7Serializer serializer = S7SerializerFactory.buildSerializer(myS7Connector.getConnector());
PlcS7PointVariable plcs7PointVariable = getPointVariable(pointInfo, value.getType());
try {
store(serializer, plcs7PointVariable, value.getType(), value.getValue());
return true;
} catch (Exception e) {
log.error("Plc S7 Write Error: {}", e.getMessage());
return false;
} finally {
myS7Connector.lock.writeLock().unlock();
}
}
@Override
@@ -101,21 +129,25 @@ public class DriverCustomServiceImpl implements DriverCustomService {
* @param driverInfo DeviceInfo Map
* @return S7Serializer
*/
private S7Serializer getS7Serializer(Long deviceId, Map<String, AttributeInfo> driverInfo) {
S7Connector s7Connector = s7ConnectorMap.get(deviceId);
if (null == s7Connector) {
private MyS7Connector getS7Connector(Long deviceId, Map<String, AttributeInfo> driverInfo) {
MyS7Connector myS7Connector = s7ConnectorMap.get(deviceId);
if (null == myS7Connector) {
myS7Connector = new MyS7Connector();
log.debug("Plc S7 Connection Info {}", JSON.toJSONString(driverInfo));
try {
s7Connector = S7ConnectorFactory.buildTCPConnector().withHost(attribute(driverInfo, "host")).withPort(attribute(driverInfo, "port")).build();
S7Connector s7Connector = S7ConnectorFactory.buildTCPConnector()
.withHost(attribute(driverInfo, "host"))
.withPort(attribute(driverInfo, "port"))
.build();
myS7Connector.setLock(new ReentrantReadWriteLock());
myS7Connector.setConnector(s7Connector);
} catch (Exception e) {
throw new ServiceException("new s7connector fail" + e.getMessage());
}
s7ConnectorMap.put(deviceId, myS7Connector);
}
if (null != s7Connector) {
s7ConnectorMap.put(deviceId, s7Connector);
return S7SerializerFactory.buildSerializer(s7Connector);
}
throw new ServiceException("new s7connector fail");
return myS7Connector;
}
/**
@@ -124,43 +156,43 @@ public class DriverCustomServiceImpl implements DriverCustomService {
* @param pointInfo PointInfo Map
* @return Plcs7PointVariable
*/
private Plcs7PointVariable getPointVariable(Map<String, AttributeInfo> pointInfo, String type) {
private PlcS7PointVariable getPointVariable(Map<String, AttributeInfo> pointInfo, String type) {
log.debug("Plc S7 Point Info {}", JSON.toJSONString(pointInfo));
return new Plcs7PointVariable(attribute(pointInfo, "dbNum"), attribute(pointInfo, "byteOffset"), attribute(pointInfo, "bitOffset"), attribute(pointInfo, "blockSize"), type);
return new PlcS7PointVariable(attribute(pointInfo, "dbNum"), attribute(pointInfo, "byteOffset"), attribute(pointInfo, "bitOffset"), attribute(pointInfo, "blockSize"), type);
}
/**
* 向 Plc S7 写数据
*
* @param serializer S7Serializer
* @param plcs7PointVariable Plcs7PointVariable
* @param plcS7PointVariable Plcs7PointVariable
* @param type Value Type
* @param value String Value
*/
private void store(S7Serializer serializer, Plcs7PointVariable plcs7PointVariable, String type, String value) {
private void store(S7Serializer serializer, PlcS7PointVariable plcS7PointVariable, String type, String value) {
switch (type.toLowerCase()) {
case Common.ValueType.INT:
int intValue = value(type, value);
serializer.store(intValue, plcs7PointVariable.getDbNum(), plcs7PointVariable.getByteOffset());
serializer.store(intValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset());
break;
case Common.ValueType.LONG:
long longValue = value(type, value);
serializer.store(longValue, plcs7PointVariable.getDbNum(), plcs7PointVariable.getByteOffset());
serializer.store(longValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset());
break;
case Common.ValueType.FLOAT:
float floatValue = value(type, value);
serializer.store(floatValue, plcs7PointVariable.getDbNum(), plcs7PointVariable.getByteOffset());
serializer.store(floatValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset());
break;
case Common.ValueType.DOUBLE:
double doubleValue = value(type, value);
serializer.store(doubleValue, plcs7PointVariable.getDbNum(), plcs7PointVariable.getByteOffset());
serializer.store(doubleValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset());
break;
case Common.ValueType.BOOLEAN:
boolean booleanValue = value(type, value);
serializer.store(booleanValue, plcs7PointVariable.getDbNum(), plcs7PointVariable.getByteOffset());
serializer.store(booleanValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset());
break;
case Common.ValueType.STRING:
serializer.store(value, plcs7PointVariable.getDbNum(), plcs7PointVariable.getByteOffset());
serializer.store(value, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset());
break;
default:
break;
@@ -12,7 +12,7 @@
*/
package com.github.s7connector.api;
import com.dc3.driver.bean.Plcs7PointVariable;
import com.dc3.driver.bean.PlcS7PointVariable;
import com.github.s7connector.exception.S7Exception;
/**
@@ -53,7 +53,7 @@ public interface S7Serializer {
* @return
* @throws S7Exception the s7 exception
*/
Object dispense(Plcs7PointVariable plcs7PointVariable) throws S7Exception;
Object dispense(PlcS7PointVariable plcs7PointVariable) throws S7Exception;
/**
* Stores an Object to the Datablock.
@@ -12,7 +12,7 @@
*/
package com.github.s7connector.impl.serializer;
import com.dc3.driver.bean.Plcs7PointVariable;
import com.dc3.driver.bean.PlcS7PointVariable;
import com.github.s7connector.api.DaveArea;
import com.github.s7connector.api.S7Connector;
import com.github.s7connector.api.S7Serializer;
@@ -30,7 +30,7 @@ import java.lang.reflect.Array;
@Slf4j
public final class S7SerializerImpl implements S7Serializer {
public static Object extractBytes(Plcs7PointVariable plcs7PointVariable, final byte[] buffer, final int byteOffset) {
public static Object extractBytes(PlcS7PointVariable plcs7PointVariable, final byte[] buffer, final int byteOffset) {
try {
final BeanEntry entry = BeanParser.parse(plcs7PointVariable);
return entry.serializer.extract(entry.type, buffer, entry.byteOffset + byteOffset, entry.bitOffset);
@@ -171,7 +171,7 @@ public final class S7SerializerImpl implements S7Serializer {
* add by pnoker
*/
@Override
public Object dispense(Plcs7PointVariable plcs7PointVariable) throws S7Exception {
public Object dispense(PlcS7PointVariable plcs7PointVariable) throws S7Exception {
try {
final byte[] buffer = this.connector.read(DaveArea.DB, plcs7PointVariable.getDbNum(), plcs7PointVariable.getSize(), plcs7PointVariable.getByteOffset());
return extractBytes(plcs7PointVariable, buffer, 0);
@@ -12,7 +12,7 @@
*/
package com.github.s7connector.impl.serializer.parser;
import com.dc3.driver.bean.Plcs7PointVariable;
import com.dc3.driver.bean.PlcS7PointVariable;
import com.github.s7connector.api.S7Serializable;
import com.github.s7connector.api.S7Type;
import com.github.s7connector.api.annotation.S7Variable;
@@ -49,7 +49,7 @@ public final class BeanParser {
}
public static BeanEntry parse(Plcs7PointVariable plcs7PointVariable) throws Exception {
public static BeanEntry parse(PlcS7PointVariable plcs7PointVariable) throws Exception {
final BeanEntry entry = new BeanEntry();
entry.byteOffset = plcs7PointVariable.getByteOffset();
entry.bitOffset = plcs7PointVariable.getBitOffset();
+25
View File
@@ -0,0 +1,25 @@
#
# Copyright 2016-2021 Pnoker. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
FROM registry.cn-beijing.aliyuncs.com/dc3/influxdb:2.1.1
MAINTAINER pnoker <pnokers.icloud.com>
ENV DOCKER_INFLUXDB_INIT_MODE setup
ENV DOCKER_INFLUXDB_INIT_USERNAME dc3
ENV DOCKER_INFLUXDB_INIT_PASSWORD dc3dc3dc3
ENV DOCKER_INFLUXDB_INIT_ORG dc3
ENV DOCKER_INFLUXDB_INIT_BUCKET dc3
ENV DOCKER_INFLUXDB_INIT_ADMIN_TOKEN ka6go4w3NdcbjQJSyrCKHjTI12kEbR6u
+1 -2
View File
@@ -14,8 +14,7 @@
-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
+22
View File
@@ -504,6 +504,25 @@ services:
aliases:
- dc3-opentsdb
influxdb:
build:
context: ./dependencies/influxdb/
dockerfile: ./Dockerfile
image: pnoker/dc3-influxdb:1.0.0
restart: always
ports:
- 8086:8086
container_name: dc3-influxdb
hostname: dc3-influxdb
volumes:
- influxdb:/var/lib/influxdb
- influxdb2:/var/lib/influxdb2
- influxdb2_etc:/etc/influxdb2
networks:
dc3net:
aliases:
- dc3-influxdb
redis:
build:
context: ./dependencies/redis/
@@ -669,6 +688,9 @@ volumes:
opentsdb_data:
opentsdb_tmp:
opentsdb_plugins:
influxdb:
influxdb2:
influxdb2_etc:
portainer:
networks: