From 60a87368dfd99d4b6abb102cfcbaedd0da0a1561 Mon Sep 17 00:00:00 2001 From: pnoker Date: Fri, 28 Feb 2020 22:47:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A9=B1=E5=8A=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/pnoker/common/constant/Common.java | 22 ++-- .../pnoker/common/sdk/bean/DriverContext.java | 27 +---- .../common/sdk/service/DriverService.java | 6 +- .../impl/DriverCommandServiceImpl.java | 114 +++++++++++++----- .../service/impl/DriverCommonServiceImpl.java | 41 ++++--- .../pnoker/common/sdk/util/DriverUtils.java | 71 ----------- .../com/pnoker/common/sdk/util/Utils.java | 71 +++++++++++ .../service/impl/DriverServiceImpl.java | 12 +- 8 files changed, 205 insertions(+), 159 deletions(-) delete mode 100644 dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/DriverUtils.java create mode 100644 dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/Utils.java diff --git a/dc3-common/dc3-base/src/main/java/com/pnoker/common/constant/Common.java b/dc3-common/dc3-base/src/main/java/com/pnoker/common/constant/Common.java index ebfef1245..70e984b50 100644 --- a/dc3-common/dc3-base/src/main/java/com/pnoker/common/constant/Common.java +++ b/dc3-common/dc3-base/src/main/java/com/pnoker/common/constant/Common.java @@ -62,21 +62,15 @@ public interface Common { } /** - * 驱动 SDK Job Bean 前缀 和 三个固定调度任务 + * 数据类型 */ - interface Sdk { - String JOB_PREFIX = "com.pnoker.common.sdk.quartz.job."; - String CUSTOMIZER_JOB = "自定义调度任务"; - String READ_JOB = "定时采集任务"; - } - - /** - * 属性常量 - */ - interface Property { - short READ_ONLY = 0; - short WRITE_ONLY = 1; - short READ_WRITE = 2; + interface ValueType { + String STRING = "string"; + String INT = "int"; + String DOUBLE = "double"; + String FLOAT = "float"; + String LONG = "long"; + String BOOLEAN = "boolean"; } /** diff --git a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/bean/DriverContext.java b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/bean/DriverContext.java index 4c5d255c3..f73cdd001 100644 --- a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/bean/DriverContext.java +++ b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/bean/DriverContext.java @@ -2,6 +2,7 @@ package com.pnoker.common.sdk.bean; import com.pnoker.common.model.Device; import com.pnoker.common.model.Point; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -11,6 +12,7 @@ import java.util.Map; * @author pnoker */ @Slf4j +@Getter @Component public class DriverContext { @@ -41,51 +43,26 @@ public class DriverContext { */ private volatile Map>> pointInfoMap; - - public synchronized long getDriverId() { - return driverId; - } - public synchronized void setDriverId(long driverId) { this.driverId = driverId; } - public synchronized Map> getDriverInfoMap() { - return driverInfoMap; - } - public synchronized void setDriverInfoMap(Map> driverInfoMap) { this.driverInfoMap = driverInfoMap; } - public synchronized Map getDeviceIdMap() { - return deviceIdMap; - } - public synchronized void setDeviceIdMap(Map deviceIdMap) { this.deviceIdMap = deviceIdMap; } - public synchronized Map getDeviceCodeMap() { - return deviceCodeMap; - } - public synchronized void setDeviceCodeMap(Map deviceCodeMap) { this.deviceCodeMap = deviceCodeMap; } - public synchronized Map> getPointMap() { - return pointMap; - } - public synchronized void setPointMap(Map> pointMap) { this.pointMap = pointMap; } - public synchronized Map>> getPointInfoMap() { - return pointInfoMap; - } - public synchronized void setPointInfoMap(Map>> pointInfoMap) { this.pointInfoMap = pointInfoMap; } diff --git a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/DriverService.java b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/DriverService.java index c1c6e58a9..bccfaa348 100644 --- a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/DriverService.java +++ b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/DriverService.java @@ -16,6 +16,8 @@ package com.pnoker.common.sdk.service; +import com.pnoker.common.model.Device; +import com.pnoker.common.model.Point; import com.pnoker.common.sdk.bean.AttributeInfo; import java.util.Map; @@ -36,9 +38,11 @@ public interface DriverService { * * @param driverInfo * @param pointInfo + * @param device + * @param point * @return */ - String read(Map driverInfo, Map pointInfo); + String read(Map driverInfo, Map pointInfo, Device device, Point point); /** * 写操作 diff --git a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommandServiceImpl.java b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommandServiceImpl.java index f71799258..bdb338f4e 100644 --- a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommandServiceImpl.java +++ b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommandServiceImpl.java @@ -1,6 +1,8 @@ package com.pnoker.common.sdk.service.impl; +import cn.hutool.core.convert.Convert; import com.pnoker.common.bean.driver.PointValue; +import com.pnoker.common.constant.Common; import com.pnoker.common.exception.ServiceException; import com.pnoker.common.model.Device; import com.pnoker.common.model.Point; @@ -9,7 +11,6 @@ import com.pnoker.common.sdk.bean.DriverContext; import com.pnoker.common.sdk.service.DriverCommandService; import com.pnoker.common.sdk.service.DriverService; import com.pnoker.common.sdk.service.message.DriverMessageSender; -import com.pnoker.common.sdk.util.DriverUtils; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -33,9 +34,9 @@ public class DriverCommandServiceImpl implements DriverCommandService { @Override @SneakyThrows public PointValue read(Long deviceId, Long pointId) { - String rawValue = driverService.read(getDriverInfo(deviceId), getPointInfo(deviceId, pointId)); - Point point = getPoint(deviceId, pointId); - PointValue pointValue = new PointValue(deviceId, pointId, rawValue, DriverUtils.processValue(rawValue, point)); + Device device = getDevice(deviceId); + String rawValue = driverService.read(getDriverInfo(device), getPointInfo(deviceId, pointId), device, getPoint(device, pointId)); + PointValue pointValue = new PointValue(deviceId, pointId, rawValue, processValue(rawValue, getPoint(device, pointId))); driverMessageSender.driverSender(pointValue); return pointValue; } @@ -43,28 +44,58 @@ public class DriverCommandServiceImpl implements DriverCommandService { @Override @SneakyThrows public Boolean write(Long deviceId, Long pointId, String value) { - return driverService.write(getDriverInfo(deviceId), getPointInfo(deviceId, pointId), - new AttributeInfo(value, getPoint(deviceId, pointId).getType())); + Device device = getDevice(deviceId); + return driverService.write(getDriverInfo(device), getPointInfo(deviceId, pointId), + new AttributeInfo(value, getPoint(device, pointId).getType())); + } + + /** + * 获取设备 + * + * @param deviceId + * @return + */ + private Device getDevice(Long deviceId) { + Device device = driverContext.getDeviceIdMap().get(deviceId); + if (null == device) { + throw new ServiceException("device does not exist"); + } + return device; } /** * 获取 驱动信息 * - * @param deviceId + * @param device * @return */ - private Map getDriverInfo(Long deviceId) { - Device device = driverContext.getDeviceIdMap().get(deviceId); - if (null == device) { - throw new ServiceException("device does not exist"); - } + private Map getDriverInfo(Device device) { Map infoMap = driverContext.getDriverInfoMap().get(device.getProfileId()); if (null == infoMap) { - throw new ServiceException("device driver info does not exist"); + throw new ServiceException("device profile driver info does not exist"); } return infoMap; } + /** + * 获取位号 + * + * @param device + * @param pointId + * @return + */ + private Point getPoint(Device device, Long pointId) { + Map pointMap = driverContext.getPointMap().get(device.getProfileId()); + if (null == pointMap) { + throw new ServiceException("device profile does not exist"); + } + Point point = pointMap.get(pointId); + if (null == point) { + throw new ServiceException("device point does not exist"); + } + return point; + } + /** * 获取位号信息 * @@ -73,31 +104,58 @@ public class DriverCommandServiceImpl implements DriverCommandService { * @return */ private Map getPointInfo(Long deviceId, Long pointId) { - Map infoMap = driverContext.getPointInfoMap().get(deviceId).get(pointId); + Map> tmpMap = driverContext.getPointInfoMap().get(deviceId); + if (null == tmpMap) { + throw new ServiceException("device point info does not exist"); + } + Map infoMap = tmpMap.get(pointId); if (null == infoMap) { throw new ServiceException("device point info does not exist"); } return infoMap; } - /** - * 获取设备 - * - * @param deviceId - * @return - */ - private Device getDevice(Long deviceId) { - return driverContext.getDeviceIdMap().get(deviceId); - } /** - * 获取位号 + * 处理数值 * - * @param deviceId - * @param pointId + * @param value + * @param point point.type : string/int/double/float/long/boolean * @return */ - private Point getPoint(Long deviceId, Long pointId) { - return driverContext.getPointMap().get(getDevice(deviceId).getProfileId()).get(pointId); + public String processValue(String value, Point point) { + value = value.trim(); + switch (point.getType()) { + case Common.ValueType.STRING: + break; + case Common.ValueType.INT: + case Common.ValueType.LONG: + try { + value = String.format("%.0f", + (Convert.convert(Double.class, value) + point.getBase()) * point.getMultiple()); + } catch (Exception e) { + log.warn(e.getMessage()); + } + break; + case Common.ValueType.DOUBLE: + case Common.ValueType.FLOAT: + try { + value = String.format(point.getFormat(), + (Convert.convert(Double.class, value) + point.getBase()) * point.getMultiple()); + } catch (Exception e) { + log.warn(e.getMessage()); + } + break; + case Common.ValueType.BOOLEAN: + try { + value = String.valueOf(Boolean.parseBoolean(value)); + } catch (Exception e) { + log.warn(e.getMessage()); + } + break; + default: + throw new ServiceException("invalid device point value type"); + } + return value; } } diff --git a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommonServiceImpl.java b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommonServiceImpl.java index 4c5cec257..6775d819d 100644 --- a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommonServiceImpl.java +++ b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/service/impl/DriverCommonServiceImpl.java @@ -39,6 +39,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.net.InetAddress; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -185,6 +186,9 @@ public class DriverCommonServiceImpl implements DriverCommonService { if (rp.isOk()) { PointInfo info = rp.getData(); PointAttribute attribute = this.pointAttributeMap.get(info.getPointAttributeId()); + if (null == driverContext.getPointInfoMap().get(info.getDeviceId())) { + driverContext.getPointInfoMap().put(info.getDeviceId(), new ConcurrentHashMap<>(16)); + } Map> map = driverContext.getPointInfoMap().get(info.getDeviceId()); if (null == map.get(info.getPointId())) { map.put(info.getPointId(), new ConcurrentHashMap<>(16)); @@ -195,7 +199,7 @@ public class DriverCommonServiceImpl implements DriverCommonService { @Override public void deletePointInfo(Long pointId, Long attributeId, Long deviceId) { - String attributeName = this.pointAttributeMap.get(deviceId).getName(); + String attributeName = this.pointAttributeMap.get(attributeId).getName(); driverContext.getPointInfoMap().get(deviceId).get(pointId).entrySet().removeIf(next -> next.getKey().equals(attributeName)); driverContext.getPointInfoMap().get(deviceId).entrySet().removeIf(next -> next.getValue().size() < 1); } @@ -222,6 +226,22 @@ public class DriverCommonServiceImpl implements DriverCommonService { return registerDriver() && registerDriverAttribute() && registerPointAttribute(); } + /** + * 加载数据 + */ + public void loadData() { + log.info("driver initial basic data ……"); + List profileList = getProfileList(driverContext.getDriverId()); + this.driverAttributeMap = getDriverAttributeMap(driverContext.getDriverId()); + this.pointAttributeMap = getPointAttributeMap(driverContext.getDriverId()); + + driverContext.setDriverInfoMap(getDriverInfoMap(profileList)); + getDeviceMap(profileList); + driverContext.setPointMap(getPointMap(profileList)); + driverContext.setPointInfoMap(getPointInfoMap(driverContext.getDeviceIdMap())); + log.info("driver initial basic data is complete"); + } + public String getHost() { try { InetAddress address = InetAddress.getLocalHost(); @@ -376,21 +396,6 @@ public class DriverCommonServiceImpl implements DriverCommonService { return true; } - /** - * 加载数据 - */ - public void loadData() { - log.info("driver initial basic data ……"); - List profileList = getProfileList(driverContext.getDriverId()); - this.driverAttributeMap = getDriverAttributeMap(driverContext.getDriverId()); - driverContext.setDriverInfoMap(getDriverInfoMap(profileList)); - getDeviceMap(profileList); - driverContext.setPointMap(getPointMap(profileList)); - this.pointAttributeMap = getPointAttributeMap(driverContext.getDriverId()); - driverContext.setPointInfoMap(getPointInfoMap(driverContext.getDeviceIdMap())); - log.info("driver initial basic data is complete"); - } - /** * 获取驱动 driver 配置 * @@ -398,7 +403,7 @@ public class DriverCommonServiceImpl implements DriverCommonService { * @return */ public Map getDriverAttributeMap(long driverId) { - Map infoMap = new ConcurrentHashMap<>(16); + Map infoMap = new HashMap<>(16); DriverAttributeDto connectInfoDto = new DriverAttributeDto(); connectInfoDto.setPage(new Pages().setSize(-1L)).setDriverId(driverId); R> rp = driverAttributeClient.list(connectInfoDto); @@ -417,7 +422,7 @@ public class DriverCommonServiceImpl implements DriverCommonService { * @return */ public Map getPointAttributeMap(long driverId) { - Map infoMap = new ConcurrentHashMap<>(16); + Map infoMap = new HashMap<>(16); PointAttributeDto pointAttributeDto = new PointAttributeDto(); pointAttributeDto.setPage(new Pages().setSize(-1L)).setDriverId(driverId); R> rp = pointAttributeClient.list(pointAttributeDto); diff --git a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/DriverUtils.java b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/DriverUtils.java deleted file mode 100644 index fd17aee38..000000000 --- a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/DriverUtils.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.pnoker.common.sdk.util; - -import com.pnoker.common.exception.ServiceException; -import com.pnoker.common.model.Point; -import lombok.extern.slf4j.Slf4j; - -import java.math.BigDecimal; - -/** - * @author pnoker - */ -@Slf4j -public class DriverUtils { - /** - * 参数类型 - */ - public interface ValueType { - String STRING = "string"; - String INT = "int"; - String DOUBLE = "double"; - String FLOAT = "float"; - String LONG = "long"; - String BOOLEAN = "boolean"; - } - - /** - * 处理数值 - * - * @param value - * @param point point.type : string/int/double/float/long/boolean - * @return - */ - public static String processValue(String value, Point point) { - value = value.trim(); - switch (point.getType()) { - case ValueType.STRING: - break; - case ValueType.INT: - case ValueType.LONG: - try { - value = String.format("%.0f", (getDoubleValue(value) + point.getBase()) * point.getMultiple()); - } catch (Exception e) { - log.error(e.getMessage()); - } - break; - case ValueType.DOUBLE: - case ValueType.FLOAT: - try { - value = String.format(point.getFormat(), (getDoubleValue(value) + point.getBase()) * point.getMultiple()); - } catch (Exception e) { - log.error(e.getMessage()); - } - break; - case ValueType.BOOLEAN: - try { - value = String.valueOf(Boolean.parseBoolean(value)); - } catch (Exception e) { - log.error(e.getMessage()); - } - break; - default: - throw new ServiceException("invalid point value type"); - } - return value; - } - - private static double getDoubleValue(String value) { - BigDecimal decimal = new BigDecimal(value); - return decimal.doubleValue(); - } -} diff --git a/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/Utils.java b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/Utils.java new file mode 100644 index 000000000..2597f81e2 --- /dev/null +++ b/dc3-common/dc3-sdk/src/main/java/com/pnoker/common/sdk/util/Utils.java @@ -0,0 +1,71 @@ +package com.pnoker.common.sdk.util; + +import cn.hutool.core.convert.Convert; +import com.pnoker.common.constant.Common; +import com.pnoker.common.sdk.bean.AttributeInfo; +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; + +/** + * @author pnoker + */ +@Slf4j +public class Utils { + + /** + * 获取 属性值 + * + * @param infoMap + * @param attribute + * @param + * @return + */ + public static T attribute(Map infoMap, String attribute) { + return convertValue(infoMap.get(attribute).getType(), infoMap.get(attribute).getValue()); + } + + /** + * 通过类型转换数据 + * + * @param type + * @param value + * @param + * @return + */ + public static T convertValue(String type, String value) { + return Convert.convertByClassName(getTypeClassName(type), value); + } + + /** + * 获取基本类型 Class Name + * + * @param type + * @return + */ + private static String getTypeClassName(String type) { + String className = "java.lang.String"; + switch (type.toLowerCase()) { + case Common.ValueType.INT: + className = "java.lang.Integer"; + break; + case Common.ValueType.DOUBLE: + className = "java.lang.Double"; + break; + case Common.ValueType.FLOAT: + className = "java.lang.Float"; + break; + case Common.ValueType.LONG: + className = "java.lang.Long"; + break; + case Common.ValueType.BOOLEAN: + className = "java.lang.Boolean"; + break; + default: + break; + } + return className; + } + + +} diff --git a/dc3-driver/dc3-driver-virtual/src/main/java/com/pnoker/driver/service/impl/DriverServiceImpl.java b/dc3-driver/dc3-driver-virtual/src/main/java/com/pnoker/driver/service/impl/DriverServiceImpl.java index 4d0867ec0..1fa52eb40 100644 --- a/dc3-driver/dc3-driver-virtual/src/main/java/com/pnoker/driver/service/impl/DriverServiceImpl.java +++ b/dc3-driver/dc3-driver-virtual/src/main/java/com/pnoker/driver/service/impl/DriverServiceImpl.java @@ -1,6 +1,8 @@ package com.pnoker.driver.service.impl; import cn.hutool.core.util.RandomUtil; +import com.pnoker.common.model.Device; +import com.pnoker.common.model.Point; import com.pnoker.common.sdk.bean.AttributeInfo; import com.pnoker.common.sdk.service.DriverService; import lombok.extern.slf4j.Slf4j; @@ -8,6 +10,8 @@ import org.springframework.stereotype.Service; import java.util.Map; +import static com.pnoker.common.sdk.util.Utils.attribute; + /** * @author pnoker */ @@ -21,8 +25,12 @@ public class DriverServiceImpl implements DriverService { } @Override - public String read(Map driverInfo, Map pointInfo) { - String value = String.valueOf(RandomUtil.randomDouble()); + public String read(Map driverInfo, Map pointInfo, Device device, Point point) { + String host = attribute(driverInfo, "host"); + Integer port = attribute(driverInfo, "port"); + String tag = attribute(pointInfo, "tag"); + String value = String.valueOf(RandomUtil.randomDouble(100)); + log.info("driverInfo:{},{},pointInfo:{},device:{}.{},point:{}.{},value:{}", host, port, tag, device.getId(), device.getName(), point.getId(), point.getName(), value); return value; }