正在显示
56 个修改的文件
包含
1610 行增加
和
239 行删除
| @@ -106,7 +106,7 @@ public class ByteUtil { | @@ -106,7 +106,7 @@ public class ByteUtil { | ||
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | public static void main(String[] args) { | 108 | public static void main(String[] args) { |
| 109 | - System.out.println(ByteUtil.toHexString( ByteUtil.intToBytesASC(2011239256,4))); | 109 | + System.out.println(ByteUtil.bytesToLongDESC(new byte[]{(byte)0x09,(byte) 0xF7},0,2)); |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | /** | 112 | /** |
| @@ -216,4 +216,57 @@ public class ByteUtil { | @@ -216,4 +216,57 @@ public class ByteUtil { | ||
| 216 | return value; | 216 | return value; |
| 217 | } | 217 | } |
| 218 | 218 | ||
| 219 | + /** | ||
| 220 | + * 获取字节里面指定字节位的值 | ||
| 221 | + * @param b 字节 | ||
| 222 | + * @param position 字节位编号(从0开始计数) | ||
| 223 | + * @return | ||
| 224 | + */ | ||
| 225 | + public static int getBitValue(byte b, int position) { | ||
| 226 | + // 检查位置是否有效 | ||
| 227 | + if (position < 0 || position >= 8) { | ||
| 228 | + throw new IllegalArgumentException("Position must be between 0 and 7 (inclusive)."); | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + // 将1左移position位,以便创建一个只有指定位为1的掩码 | ||
| 232 | + int mask = 1 << position; | ||
| 233 | + | ||
| 234 | + // 将字节与掩码进行位与运算,如果指定位为1,则结果不为0 | ||
| 235 | + int result = b & mask; | ||
| 236 | + | ||
| 237 | + // 如果结果为0,说明指定位是0;否则,指定位是1 | ||
| 238 | + return result != 0 ? 1 : 0; | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + /** | ||
| 242 | + * 获取字节里面指定多个字节位的值 | ||
| 243 | + * @param b 字节 | ||
| 244 | + * @param startBit 开始位的位置(从0开始计数) | ||
| 245 | + * @param numberOfBits 需要获取的位的数量 | ||
| 246 | + * @return | ||
| 247 | + */ | ||
| 248 | + public static int getBitsValue(byte b, int startBit, int numberOfBits) { | ||
| 249 | + // 检查参数的有效性 | ||
| 250 | + if (startBit < 0 || startBit >= 8 || numberOfBits <= 0 || startBit + numberOfBits > 8) { | ||
| 251 | + throw new IllegalArgumentException("Invalid bit range."); | ||
| 252 | + } | ||
| 253 | + | ||
| 254 | + // 将字节转换为无符号整数,以便进行位操作 | ||
| 255 | + int unsignedByte = b & 0xFF; | ||
| 256 | + | ||
| 257 | + // 计算掩码,用于提取指定位 | ||
| 258 | + int mask = (1 << numberOfBits) - 1; | ||
| 259 | + | ||
| 260 | + // 将掩码左移,以便对齐到正确的位位置 | ||
| 261 | + mask <<= startBit; | ||
| 262 | + | ||
| 263 | + // 应用掩码,提取指定位 | ||
| 264 | + int bits = unsignedByte & mask; | ||
| 265 | + | ||
| 266 | + // 右移结果,以便将提取的位放在最低有效位上 | ||
| 267 | + bits >>= startBit; | ||
| 268 | + | ||
| 269 | + return bits; | ||
| 270 | + } | ||
| 271 | + | ||
| 219 | } | 272 | } |
| @@ -6,6 +6,8 @@ import org.springframework.beans.BeansException; | @@ -6,6 +6,8 @@ import org.springframework.beans.BeansException; | ||
| 6 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; | 6 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 7 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | 7 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| 8 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | 8 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 9 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
| 10 | +import org.springframework.beans.support.ResourceEditorRegistrar; | ||
| 9 | import org.springframework.context.ApplicationContext; | 11 | import org.springframework.context.ApplicationContext; |
| 10 | import org.springframework.context.ApplicationContextAware; | 12 | import org.springframework.context.ApplicationContextAware; |
| 11 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
| @@ -143,4 +145,33 @@ public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationC | @@ -143,4 +145,33 @@ public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationC | ||
| 143 | final String[] activeProfiles = getActiveProfiles(); | 145 | final String[] activeProfiles = getActiveProfiles(); |
| 144 | return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; | 146 | return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; |
| 145 | } | 147 | } |
| 148 | + public static void registerBean(String name, Object beanInstance) { | ||
| 149 | + String beanName = name==null?beanInstance.getClass().getSimpleName():name; | ||
| 150 | + if (containsBean(beanName)) | ||
| 151 | + { | ||
| 152 | + return; | ||
| 153 | + } | ||
| 154 | + beanFactory.registerSingleton(beanName, beanInstance); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + /** | ||
| 158 | + * 删除指定名称的bean | ||
| 159 | + * | ||
| 160 | + * @param name | ||
| 161 | + * @return boolean | ||
| 162 | + */ | ||
| 163 | + public static boolean deleteBean(String name) | ||
| 164 | + { | ||
| 165 | + if (containsBean(name)) | ||
| 166 | + { | ||
| 167 | + beanFactory.destroyBean(name, getBean(name)); | ||
| 168 | + return true; | ||
| 169 | + } | ||
| 170 | + return false; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + public static boolean isSpringBean() | ||
| 174 | + { | ||
| 175 | + return null != beanFactory; | ||
| 176 | + } | ||
| 146 | } | 177 | } |
| @@ -42,7 +42,7 @@ public class EnumModelOutput extends ThingsModelItemBase<String> | @@ -42,7 +42,7 @@ public class EnumModelOutput extends ThingsModelItemBase<String> | ||
| 42 | { | 42 | { |
| 43 | return null; | 43 | return null; |
| 44 | } | 44 | } |
| 45 | - return getValue()+""; | 45 | + return getValue().toString(); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | 48 |
| @@ -67,24 +67,24 @@ public class IotProduct implements Serializable | @@ -67,24 +67,24 @@ public class IotProduct implements Serializable | ||
| 67 | private String sync_db; | 67 | private String sync_db; |
| 68 | 68 | ||
| 69 | @ApiModelProperty("解析服务") | 69 | @ApiModelProperty("解析服务") |
| 70 | - private String analysis_clas; // varchar(100) DEFAULT 'com.zhonglai.luhui.device.protocol.factory.analysis.DefaultProtocolParserFactoryImpl' COMMENT '解析服务', | 70 | + private Integer analysis_clas; // varchar(100) DEFAULT 'com.zhonglai.luhui.device.protocol.factory.analysis.DefaultProtocolParserFactoryImpl' COMMENT '解析服务', |
| 71 | 71 | ||
| 72 | @ApiModelProperty("清洗服务") | 72 | @ApiModelProperty("清洗服务") |
| 73 | - private String purification_clas; // varchar(100) DEFAULT 'com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl' COMMENT '清洗服务', | 73 | + private Integer purification_clas; // varchar(100) DEFAULT 'com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl' COMMENT '清洗服务', |
| 74 | 74 | ||
| 75 | - public String getPurification_clas() { | 75 | + public Integer getPurification_clas() { |
| 76 | return purification_clas; | 76 | return purification_clas; |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | - public void setPurification_clas(String purification_clas) { | 79 | + public void setPurification_clas(Integer purification_clas) { |
| 80 | this.purification_clas = purification_clas; | 80 | this.purification_clas = purification_clas; |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | - public String getAnalysis_clas() { | 83 | + public Integer getAnalysis_clas() { |
| 84 | return analysis_clas; | 84 | return analysis_clas; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | - public void setAnalysis_clas(String analysis_clas) { | 87 | + public void setAnalysis_clas(Integer analysis_clas) { |
| 88 | this.analysis_clas = analysis_clas; | 88 | this.analysis_clas = analysis_clas; |
| 89 | } | 89 | } |
| 90 | 90 |
lh-jar/lh-jar-device-service/src/main/java/com/zhonglai/luhui/device/domain/IotProtocolClass.java
0 → 100644
| 1 | +package com.zhonglai.luhui.device.domain; | ||
| 2 | + | ||
| 3 | +import com.ruoyi.common.annotation.PublicSQLConfig; | ||
| 4 | +import io.swagger.annotations.ApiModel; | ||
| 5 | +import io.swagger.annotations.ApiModelProperty; | ||
| 6 | + | ||
| 7 | +import java.io.Serializable; | ||
| 8 | + | ||
| 9 | +@ApiModel("产品解析插件包") | ||
| 10 | +public class IotProtocolClass implements Serializable { | ||
| 11 | + @PublicSQLConfig(isSelect=false) | ||
| 12 | + private static final long serialVersionUID = 1L; | ||
| 13 | + @ApiModelProperty("主键id") | ||
| 14 | + private Integer id; | ||
| 15 | + | ||
| 16 | + @ApiModelProperty("创建时间") | ||
| 17 | + private String create_time; | ||
| 18 | + | ||
| 19 | + @ApiModelProperty("类型(1解析协议,2清洗协议,3数据库同步服务)") | ||
| 20 | + private Integer type; | ||
| 21 | + | ||
| 22 | + @ApiModelProperty("名称") | ||
| 23 | + private String name; | ||
| 24 | + | ||
| 25 | + @ApiModelProperty("模型举例") | ||
| 26 | + private String case_model; | ||
| 27 | + | ||
| 28 | + @ApiModelProperty("类名") | ||
| 29 | + private String classname; | ||
| 30 | + | ||
| 31 | + public Integer getId() { | ||
| 32 | + return id; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public void setId(Integer id) { | ||
| 36 | + this.id = id; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public String getCreate_time() { | ||
| 40 | + return create_time; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public void setCreate_time(String create_time) { | ||
| 44 | + this.create_time = create_time; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public Integer getType() { | ||
| 48 | + return type; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + public void setType(Integer type) { | ||
| 52 | + this.type = type; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public String getName() { | ||
| 56 | + return name; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + public void setName(String name) { | ||
| 60 | + this.name = name; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + public String getCase_model() { | ||
| 64 | + return case_model; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public void setCase_model(String case_model) { | ||
| 68 | + this.case_model = case_model; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + public String getClassname() { | ||
| 72 | + return classname; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + public void setClassname(String classname) { | ||
| 76 | + this.classname = classname; | ||
| 77 | + } | ||
| 78 | +} |
| 1 | +package com.zhonglai.luhui.admin.controller.data; | ||
| 2 | + | ||
| 3 | +import com.ruoyi.common.core.domain.AjaxResult; | ||
| 4 | +import com.zhonglai.luhui.dao.service.PublicService; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.comm.util.TableUtil; | ||
| 6 | +import io.swagger.annotations.Api; | ||
| 7 | +import io.swagger.annotations.ApiImplicitParam; | ||
| 8 | +import io.swagger.annotations.ApiImplicitParams; | ||
| 9 | +import io.swagger.annotations.ApiOperation; | ||
| 10 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 11 | +import org.springframework.web.bind.annotation.GetMapping; | ||
| 12 | +import org.springframework.web.bind.annotation.PathVariable; | ||
| 13 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 14 | +import org.springframework.web.bind.annotation.RestController; | ||
| 15 | + | ||
| 16 | +import java.util.List; | ||
| 17 | +import java.util.Map; | ||
| 18 | + | ||
| 19 | +@Api(tags = "地图轨迹") | ||
| 20 | +@RestController | ||
| 21 | +@RequestMapping("/data/atlasTrajectory") | ||
| 22 | +public class AtlasTrajectoryController { | ||
| 23 | + @Autowired | ||
| 24 | + private PublicService publicService; | ||
| 25 | + | ||
| 26 | + @ApiOperation("根据时间测试经纬度轨迹") | ||
| 27 | + @ApiImplicitParams({ | ||
| 28 | + @ApiImplicitParam(value = "设备id",name = "deviceInfoId"), | ||
| 29 | + @ApiImplicitParam(value = "经度数据类型",name = "latType"), | ||
| 30 | + @ApiImplicitParam(value = "纬度数据类型",name = "lngType"), | ||
| 31 | + @ApiImplicitParam(value = "日期(2024-06-05)",name = "day"), | ||
| 32 | + @ApiImplicitParam(value = "开始时间(时间戳)",name = "startTime"), | ||
| 33 | + @ApiImplicitParam(value = "结束时间(时间戳)",name = "endTime") | ||
| 34 | + }) | ||
| 35 | + @GetMapping(value = "/oneDay/{deviceInfiId}/{day}") | ||
| 36 | + public AjaxResult oneDay(@PathVariable String deviceInfoId,@PathVariable String day,String latType,String lngType,Integer startTime,Integer endTime) | ||
| 37 | + { | ||
| 38 | + String table = TableUtil.getTableName(day,"ly_sensor_data","device_sensor_data",3); | ||
| 39 | + List<Map<String,Object>> list = publicService.getObjectListBySQL("select creat_time,data_value from `"+table+"` where device_info_id='"+deviceInfoId+"' and data_type='"+latType+"'"); | ||
| 40 | + return AjaxResult.success(list); | ||
| 41 | + } | ||
| 42 | +} |
lh-modules/lh-admin/src/main/java/com/zhonglai/luhui/admin/controller/data/SenserDataController.java
0 → 100644
| 1 | +package com.zhonglai.luhui.admin.controller.data; | ||
| 2 | + | ||
| 3 | +import com.ruoyi.common.core.domain.AjaxResult; | ||
| 4 | +import com.zhonglai.luhui.dao.service.PublicService; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.comm.util.TableUtil; | ||
| 6 | +import com.zhonglai.luhui.sys.service.ISysDictTypeService; | ||
| 7 | +import io.swagger.annotations.Api; | ||
| 8 | +import io.swagger.annotations.ApiImplicitParam; | ||
| 9 | +import io.swagger.annotations.ApiImplicitParams; | ||
| 10 | +import io.swagger.annotations.ApiOperation; | ||
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 12 | +import org.springframework.security.access.prepost.PreAuthorize; | ||
| 13 | +import org.springframework.web.bind.annotation.GetMapping; | ||
| 14 | +import org.springframework.web.bind.annotation.PathVariable; | ||
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 16 | +import org.springframework.web.bind.annotation.RestController; | ||
| 17 | + | ||
| 18 | +import java.util.List; | ||
| 19 | +import java.util.Map; | ||
| 20 | + | ||
| 21 | +@Api(tags = "传感器数据管理") | ||
| 22 | +@RestController | ||
| 23 | +@RequestMapping("/data/senserData") | ||
| 24 | +public class SenserDataController { | ||
| 25 | + @Autowired | ||
| 26 | + private PublicService publicService; | ||
| 27 | + | ||
| 28 | + @ApiOperation("获取一天的原始数据") | ||
| 29 | + @ApiImplicitParams({ | ||
| 30 | + @ApiImplicitParam(value = "设备id",name = "deviceInfoId"), | ||
| 31 | + @ApiImplicitParam(value = "数据类型",name = "type"), | ||
| 32 | + @ApiImplicitParam(value = "日期(2024-06-05)",name = "day") | ||
| 33 | + }) | ||
| 34 | + @GetMapping(value = "/oneDay/{deviceInfiId}/{type}/{day}") | ||
| 35 | + public AjaxResult oneDay(@PathVariable String deviceInfoId, @PathVariable String type, @PathVariable String day) | ||
| 36 | + { | ||
| 37 | + String table = TableUtil.getTableName(day,"ly_sensor_data","device_sensor_data",3); | ||
| 38 | + List<Map<String,Object>> list = publicService.getObjectListBySQL("select creat_time,data_value from `"+table+"` where device_info_id='"+deviceInfoId+"' and data_type='"+type+"'"); | ||
| 39 | + return AjaxResult.success(list); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | +} |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | + <modelVersion>4.0.0</modelVersion> | ||
| 6 | + <parent> | ||
| 7 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | + <artifactId>lh-modules</artifactId> | ||
| 9 | + <version>1.0-SNAPSHOT</version> | ||
| 10 | + </parent> | ||
| 11 | + | ||
| 12 | + <artifactId>lh-device-operation-service</artifactId> | ||
| 13 | + | ||
| 14 | + <properties> | ||
| 15 | + <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | + <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | + </properties> | ||
| 19 | + | ||
| 20 | + <description> | ||
| 21 | + 设备操作服务 | ||
| 22 | + </description> | ||
| 23 | + | ||
| 24 | + <dependencies> | ||
| 25 | + <!-- spring-boot-devtools --> | ||
| 26 | + <dependency> | ||
| 27 | + <groupId>org.springframework.boot</groupId> | ||
| 28 | + <artifactId>spring-boot-devtools</artifactId> | ||
| 29 | + <optional>true</optional> <!-- 表示依赖不会传递 --> | ||
| 30 | + </dependency> | ||
| 31 | + <!-- SpringBoot Web容器 --> | ||
| 32 | + <dependency> | ||
| 33 | + <groupId>org.springframework.boot</groupId> | ||
| 34 | + <artifactId>spring-boot-starter-web</artifactId> | ||
| 35 | + <exclusions></exclusions> | ||
| 36 | + </dependency> | ||
| 37 | + <!-- Spring框架基本的核心工具 --> | ||
| 38 | + <dependency> | ||
| 39 | + <groupId>org.springframework</groupId> | ||
| 40 | + <artifactId>spring-context-support</artifactId> | ||
| 41 | + </dependency> | ||
| 42 | + | ||
| 43 | + <dependency> | ||
| 44 | + <groupId>org.apache.rocketmq</groupId> | ||
| 45 | + <artifactId>rocketmq-spring-boot-starter</artifactId> | ||
| 46 | + </dependency> | ||
| 47 | + | ||
| 48 | + <dependency> | ||
| 49 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 50 | + <artifactId>lh-jar-device-service</artifactId> | ||
| 51 | + </dependency> | ||
| 52 | + <dependency> | ||
| 53 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 54 | + <artifactId>lh-device-protocol-factory</artifactId> | ||
| 55 | + </dependency> | ||
| 56 | + </dependencies> | ||
| 57 | +</project> |
| 1 | +package com.zhonglai.luhui.device.operation.service; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import org.slf4j.Logger; | ||
| 5 | +import org.slf4j.LoggerFactory; | ||
| 6 | +import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| 7 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
| 8 | +import org.springframework.boot.builder.SpringApplicationBuilder; | ||
| 9 | +import org.springframework.context.annotation.ComponentScan; | ||
| 10 | + | ||
| 11 | +@ComponentScan({ | ||
| 12 | + "com.zhonglai.luhui.device.operation.service.clien", | ||
| 13 | +}) | ||
| 14 | +@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) | ||
| 15 | +public class DeviceOperationServiceApplication { | ||
| 16 | + private static Logger log = LoggerFactory.getLogger(DeviceOperationServiceApplication.class); | ||
| 17 | + | ||
| 18 | + public static void main(String[] args) { | ||
| 19 | + log.info("启动服务"); | ||
| 20 | + SpringApplicationBuilder builder = new SpringApplicationBuilder(DeviceOperationServiceApplication.class); | ||
| 21 | + builder.run( args); | ||
| 22 | + } | ||
| 23 | +} |
| 1 | +package com.zhonglai.luhui.device.operation.service.clien; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 4 | +import com.zhonglai.luhui.device.analysis.dto.Message; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.dto.MessageCode; | ||
| 6 | +import org.apache.rocketmq.common.message.MessageExt; | ||
| 7 | +import org.apache.rocketmq.spring.annotation.MessageModel; | ||
| 8 | +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; | ||
| 9 | +import org.apache.rocketmq.spring.annotation.SelectorType; | ||
| 10 | +import org.apache.rocketmq.spring.core.RocketMQReplyListener; | ||
| 11 | +import org.slf4j.Logger; | ||
| 12 | +import org.slf4j.LoggerFactory; | ||
| 13 | +import org.springframework.stereotype.Service; | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +@Service | ||
| 17 | +@RocketMQMessageListener(consumerGroup = "${rocketmq.producer.group:}", topic = "${rocketmq.producer.send-topic:}",selectorType = SelectorType.TAG,selectorExpression = "${rocketmq.producer.send-tags:}",messageModel = MessageModel.BROADCASTING) | ||
| 18 | +public class RocketMqService implements RocketMQReplyListener<MessageExt, Message> { | ||
| 19 | + private static final Logger log = LoggerFactory.getLogger(RocketMqService.class); | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + @Override | ||
| 23 | + public Message onMessage(MessageExt messageExt) { | ||
| 24 | + log.info("监听到消息{}",messageExt); | ||
| 25 | +// String clint = MessageUtil.getReplyToClient(messageExt); | ||
| 26 | + | ||
| 27 | + String str = new String(messageExt.getBody()); | ||
| 28 | + log.info("消息body{}",str); | ||
| 29 | +// DeviceCommandApi deviceCommandApi = JSON.parseObject(str, DeviceCommandApi.class); | ||
| 30 | +// try { | ||
| 31 | +// Message message = deviceCommandApi.invokeApi(deviceService); | ||
| 32 | +// log.info("{} 指令执行完以后返回的结果{}",deviceCommandApi.getDeviceCommandApiParameter().getClient_id(),message); | ||
| 33 | +// return message; | ||
| 34 | +// } catch (Exception e) { | ||
| 35 | +// log.error("执行异常",e); | ||
| 36 | +// } | ||
| 37 | + return new Message(MessageCode.DEFAULT_FAIL_CODE,"服务器玩脱了"); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + | ||
| 41 | +} |
| 1 | +##服务器配置 | ||
| 2 | +server: | ||
| 3 | + tomcat: | ||
| 4 | + uri-encoding: UTF-8 | ||
| 5 | + port: 4887 | ||
| 6 | + servlet: | ||
| 7 | + context-path: / | ||
| 8 | + | ||
| 9 | +spring: | ||
| 10 | + messages: | ||
| 11 | + encoding: UTF-8 | ||
| 12 | + mvc: | ||
| 13 | + #出现错误时, 直接抛出异常 | ||
| 14 | + throw-exception-if-no-handler-found: true | ||
| 15 | + jackson: | ||
| 16 | + date-format: yyyy-MM-dd HH:mm:ss | ||
| 17 | + time-zone: GMT+8 | ||
| 18 | + default-property-inclusion: non_null | ||
| 19 | + | ||
| 20 | +mqtt: | ||
| 21 | + #链接地址 | ||
| 22 | + broker: tcp://175.24.61.68:1883 | ||
| 23 | + #唯一标识 | ||
| 24 | + clientId: ${random.uuid} | ||
| 25 | + topicconfig: "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}" | ||
| 26 | + username: sysuser | ||
| 27 | + password: "!@#1qaz" | ||
| 28 | + client: | ||
| 29 | + #客户端操作时间 | ||
| 30 | + operationTime: 10 | ||
| 31 | + | ||
| 32 | +# NameServer地址 | ||
| 33 | +rocketmq: | ||
| 34 | + name-server: 47.115.144.179:9876 | ||
| 35 | + # 默认的消息组 | ||
| 36 | + producer: | ||
| 37 | + group: lh-device-operation-service | ||
| 38 | + send-message-timeout: 30000 | ||
| 39 | + send-topic: lh-device-operation-service | ||
| 40 | + send-tags: 1 |
| 1 | +<configuration> | ||
| 2 | + <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| 3 | + <file>logs/output.log</file> | ||
| 4 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
| 5 | + <fileNamePattern>logs/output.%d{yyyy-MM-dd}.log</fileNamePattern> | ||
| 6 | + <maxHistory>5</maxHistory> | ||
| 7 | + </rollingPolicy> | ||
| 8 | + <encoder> | ||
| 9 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
| 10 | + </encoder> | ||
| 11 | + </appender> | ||
| 12 | + | ||
| 13 | + <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
| 14 | + <encoder> | ||
| 15 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
| 16 | + </encoder> | ||
| 17 | + </appender> | ||
| 18 | + | ||
| 19 | + <appender name="myDataAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| 20 | + <file>logs/myData/myData.log</file> | ||
| 21 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
| 22 | + <fileNamePattern>logs/myData/myData.%d{yyyy-MM-dd}.log</fileNamePattern> | ||
| 23 | + <maxHistory>5</maxHistory> | ||
| 24 | + </rollingPolicy > | ||
| 25 | + <encoder> | ||
| 26 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern> | ||
| 27 | + </encoder> | ||
| 28 | + </appender> | ||
| 29 | + | ||
| 30 | + <!-- Logger "com.example.first" 的配置 --> | ||
| 31 | + <logger name="myDatalog" level="info" additivity="false"> | ||
| 32 | + <appender-ref ref="myDataAppender" /> | ||
| 33 | + </logger> | ||
| 34 | + | ||
| 35 | + <root level="info"> | ||
| 36 | + <appender-ref ref="FILE" /> | ||
| 37 | + <appender-ref ref="CONSOLE" /> | ||
| 38 | + </root> | ||
| 39 | +</configuration> |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + | ||
| 3 | +<assembly> | ||
| 4 | + <id>bin</id> | ||
| 5 | + <!-- 最终打包成一个用于发布的zip文件 --> | ||
| 6 | + <formats> | ||
| 7 | + <format>zip</format> | ||
| 8 | + </formats> | ||
| 9 | + | ||
| 10 | + <!-- Adds dependencies to zip package under lib directory --> | ||
| 11 | + <dependencySets> | ||
| 12 | + <dependencySet> | ||
| 13 | + <!-- | ||
| 14 | + 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 | ||
| 15 | + --> | ||
| 16 | + <useProjectArtifact>false</useProjectArtifact> | ||
| 17 | + <outputDirectory>lib</outputDirectory> | ||
| 18 | + <unpack>false</unpack> | ||
| 19 | + </dependencySet> | ||
| 20 | + </dependencySets> | ||
| 21 | + | ||
| 22 | + <fileSets> | ||
| 23 | + <!-- 把项目相关的说明文件,打包进zip文件的根目录 --> | ||
| 24 | + <fileSet> | ||
| 25 | + <directory>${project.basedir}</directory> | ||
| 26 | + <outputDirectory>/</outputDirectory> | ||
| 27 | + <includes> | ||
| 28 | + <include>README*</include> | ||
| 29 | + <include>LICENSE*</include> | ||
| 30 | + <include>NOTICE*</include> | ||
| 31 | + </includes> | ||
| 32 | + </fileSet> | ||
| 33 | + | ||
| 34 | + <!-- 把项目的配置文件,打包进zip文件的config目录 --> | ||
| 35 | + <fileSet> | ||
| 36 | + <directory>${project.basedir}\src\main\resources\configs</directory> | ||
| 37 | + <outputDirectory>../configs</outputDirectory> | ||
| 38 | + <includes> | ||
| 39 | + <include>*.properties</include> | ||
| 40 | + </includes> | ||
| 41 | + </fileSet> | ||
| 42 | + | ||
| 43 | + <!-- 把项目的配置文件,提出来 --> | ||
| 44 | + <fileSet> | ||
| 45 | + <directory>${project.basedir}\src\main\resources</directory> | ||
| 46 | + <outputDirectory>/</outputDirectory> | ||
| 47 | + <includes> | ||
| 48 | + <include>*.properties</include> | ||
| 49 | + <include>*.yml</include> | ||
| 50 | + </includes> | ||
| 51 | + </fileSet> | ||
| 52 | + | ||
| 53 | + <!-- 把项目的脚本文件目录( src/main/scripts )中的启动脚本文件,打包进zip文件的跟目录 --> | ||
| 54 | + <fileSet> | ||
| 55 | + <directory>${project.basedir}\bin</directory> | ||
| 56 | + <outputDirectory></outputDirectory> | ||
| 57 | + <includes> | ||
| 58 | + <include>start.*</include> | ||
| 59 | + <include>stop.*</include> | ||
| 60 | + </includes> | ||
| 61 | + </fileSet> | ||
| 62 | + | ||
| 63 | + <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 --> | ||
| 64 | + <fileSet> | ||
| 65 | + <directory>${project.build.directory}</directory> | ||
| 66 | + <outputDirectory></outputDirectory> | ||
| 67 | + <includes> | ||
| 68 | + <include>*.jar</include> | ||
| 69 | + </includes> | ||
| 70 | + </fileSet> | ||
| 71 | + </fileSets> | ||
| 72 | +</assembly> |
| 1 | package com.zhonglai.luhui.device.protocol.http.analysis.analysis; | 1 | package com.zhonglai.luhui.device.protocol.http.analysis.analysis; |
| 2 | 2 | ||
| 3 | -import com.alibaba.fastjson.JSONObject; | ||
| 4 | import com.google.gson.JsonObject; | 3 | import com.google.gson.JsonObject; |
| 5 | -import com.ruoyi.common.utils.GsonConstructor; | ||
| 6 | import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | 4 | import com.zhonglai.luhui.device.analysis.comm.factory.Topic; |
| 7 | import com.zhonglai.luhui.device.analysis.util.TopicUtil; | 5 | import com.zhonglai.luhui.device.analysis.util.TopicUtil; |
| 8 | import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | 6 | import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; |
| 9 | import org.springframework.stereotype.Service; | 7 | import org.springframework.stereotype.Service; |
| 10 | 8 | ||
| 11 | -import java.util.Map; | ||
| 12 | - | ||
| 13 | -@Service | ||
| 14 | public class ProtocolParserServiceImpl implements ProtocolParserFactory<JsonObject> { | 9 | public class ProtocolParserServiceImpl implements ProtocolParserFactory<JsonObject> { |
| 15 | private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{imei}}/{{topicType}}"; | 10 | private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{imei}}/{{topicType}}"; |
| 16 | 11 |
| 1 | -package com.zhonglai.luhui.device.protocol.http.analysis.purification; | ||
| 2 | - | ||
| 3 | -import com.google.gson.JsonObject; | ||
| 4 | -import com.ruoyi.common.utils.DateUtils; | ||
| 5 | -import com.ruoyi.common.utils.GsonConstructor; | ||
| 6 | -import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | ||
| 7 | -import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; | ||
| 8 | -import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto; | ||
| 9 | -import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; | ||
| 10 | -import com.zhonglai.luhui.device.protocol.factory.purification.ProtocolPurificationFactory; | ||
| 11 | -import org.springframework.stereotype.Service; | ||
| 12 | - | ||
| 13 | -import java.util.ArrayList; | ||
| 14 | -import java.util.List; | ||
| 15 | -import java.util.Map; | ||
| 16 | - | ||
| 17 | -@Service("http_purification") | ||
| 18 | -public class ProtocolPurificationServiceImpl implements ProtocolPurificationFactory { | ||
| 19 | - | ||
| 20 | - @Override | ||
| 21 | - public ProtocolPurificationModel purification(Integer product_id, Topic topic, JsonObject analysisObject ) { | ||
| 22 | - return null; | ||
| 23 | - } | ||
| 24 | -} |
| 1 | -package com.zhonglai.luhui.device.protocol.http.analysis.service; | ||
| 2 | - | ||
| 3 | -import com.zhonglai.luhui.device.analysis.comm.dao.BaseDao; | ||
| 4 | -import com.zhonglai.luhui.device.domain.IotDevice; | ||
| 5 | -import com.zhonglai.luhui.device.protocol.http.analysis.service.db.LsyDBFactoryImp; | ||
| 6 | -import org.springframework.stereotype.Service; | ||
| 7 | - | ||
| 8 | -import java.util.List; | ||
| 9 | -import java.util.Map; | ||
| 10 | - | ||
| 11 | -@Service | ||
| 12 | -public class LsyDbService { | ||
| 13 | - private BaseDao lsy_baseDao = new BaseDao(new LsyDBFactoryImp()); | ||
| 14 | - | ||
| 15 | - public IotDevice getLsyDeviceById(String id) | ||
| 16 | - { | ||
| 17 | - List<Map<String,Object>> list = lsy_baseDao.findListBysql("SELECT order_dtu_imei,device_type FROM `order_dtu` WHERE order_dtu_imei='"+id+"'"); | ||
| 18 | - if(null != list && list.size() != 0) | ||
| 19 | - { | ||
| 20 | - Map<String,Object> map = list.get(0); | ||
| 21 | - IotDevice iotDevice = new IotDevice(); | ||
| 22 | - iotDevice.setClient_id(id); | ||
| 23 | - iotDevice.setMqtt_username(map.get("device_type")+""); | ||
| 24 | - return iotDevice; | ||
| 25 | - } | ||
| 26 | - return null; | ||
| 27 | - } | ||
| 28 | -} |
| 1 | -package com.zhonglai.luhui.device.protocol.http.analysis.service.db; | ||
| 2 | - | ||
| 3 | -import com.zhonglai.luhui.device.analysis.comm.dao.DBFactory; | ||
| 4 | -import org.apache.commons.dbcp.BasicDataSourceFactory; | ||
| 5 | - | ||
| 6 | -import javax.sql.DataSource; | ||
| 7 | -import java.io.FileInputStream; | ||
| 8 | -import java.util.Properties; | ||
| 9 | - | ||
| 10 | -public class LsyDBFactoryImp implements DBFactory { | ||
| 11 | - private static DataSource ds = null; | ||
| 12 | - static { | ||
| 13 | - try { | ||
| 14 | - if(null==ds ) | ||
| 15 | - { | ||
| 16 | - String dbPath = System.getProperty("dbPath"); | ||
| 17 | - String path = null != dbPath?dbPath:System.getProperty("user.dir")+"/configs/"; | ||
| 18 | - Properties p = new Properties(); | ||
| 19 | - System.out.println("》》》》》》》》》》》》》数据库配置文件地址:"+path+"lsy_dbcpconfig.properties"); | ||
| 20 | - p.load(new FileInputStream(path+"lsy_dbcpconfig.properties")); | ||
| 21 | -// p.load(DBFactory.class | ||
| 22 | -// .getClassLoader().getResourceAsStream("configs/dbcpconfig.properties")); | ||
| 23 | - ds = BasicDataSourceFactory.createDataSource(p); | ||
| 24 | - } | ||
| 25 | - } catch (Exception e) { | ||
| 26 | - e.printStackTrace(); | ||
| 27 | - } | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - @Override | ||
| 31 | - public DataSource getDataSource() { | ||
| 32 | - return ds; | ||
| 33 | - } | ||
| 34 | -} |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | + <modelVersion>4.0.0</modelVersion> | ||
| 6 | + <parent> | ||
| 7 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | + <artifactId>lh-device-protocol-parser</artifactId> | ||
| 9 | + <version>1.0-SNAPSHOT</version> | ||
| 10 | + </parent> | ||
| 11 | + | ||
| 12 | + <artifactId>lh-device-modbus</artifactId> | ||
| 13 | + | ||
| 14 | + <properties> | ||
| 15 | + <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | + <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | + </properties> | ||
| 19 | + | ||
| 20 | + <dependencies> | ||
| 21 | + <dependency> | ||
| 22 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 23 | + <artifactId>lh-device-protocol-factory</artifactId> | ||
| 24 | + </dependency> | ||
| 25 | + </dependencies> | ||
| 26 | +</project> |
| 1 | +package com.zhonglai.luhui.device.protocol.modbus.analysis; | ||
| 2 | + | ||
| 3 | +import com.google.gson.JsonObject; | ||
| 4 | +import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.util.TopicUtil; | ||
| 6 | +import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | ||
| 7 | +import com.zhonglai.luhui.device.protocol.modbus.dto.ModbusDto; | ||
| 8 | + | ||
| 9 | +public class ModbusProtocolParserFactoryImpl implements ProtocolParserFactory<byte[]> { | ||
| 10 | + private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{imei}}/{{topicType}}"; | ||
| 11 | + | ||
| 12 | + @Override | ||
| 13 | + public Topic analysisTopic(String topicStr) { | ||
| 14 | + Topic topic = TopicUtil.initTopicFromModelStr(topicStr,topicModel); | ||
| 15 | + return topic; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public JsonObject analysisPayload(Topic topic, byte[] payload) { | ||
| 20 | + ModbusDto modbusDto = new ModbusDto(payload); | ||
| 21 | + String senserNumber = "1"; | ||
| 22 | + modbusDto.getCommdcode(); | ||
| 23 | + return null; | ||
| 24 | + } | ||
| 25 | +} |
| 1 | +package com.zhonglai.luhui.device.protocol.modbus.dto; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 5 | +import com.ruoyi.common.utils.ByteUtil; | ||
| 6 | +import lombok.Data; | ||
| 7 | +import org.apache.commons.lang3.ArrayUtils; | ||
| 8 | + | ||
| 9 | +import java.io.Serializable; | ||
| 10 | +import java.util.HashMap; | ||
| 11 | +import java.util.Map; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * Modbus协议 | ||
| 15 | + */ | ||
| 16 | +@Data | ||
| 17 | +public class ModbusDto implements Serializable { | ||
| 18 | + private static final long serialVersionUID = -6008279428004571734L; | ||
| 19 | + private String hstr; | ||
| 20 | + protected Integer address; //地址位 | ||
| 21 | + protected Integer commdcode; //功能码 | ||
| 22 | + protected byte[] data; //数据 | ||
| 23 | + protected String crc; //16CRC 码 | ||
| 24 | + | ||
| 25 | + public ModbusDto(Integer address, Integer commdcode, byte[] data) | ||
| 26 | + { | ||
| 27 | + toModbusDto(address,commdcode,data); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + protected void toModbusDto(Integer address,Integer commdcode,byte[] data) | ||
| 31 | + { | ||
| 32 | + this.address = address; | ||
| 33 | + this.commdcode = commdcode; | ||
| 34 | + this.data = data; | ||
| 35 | + | ||
| 36 | + byte[] heardbyte = new byte[2]; | ||
| 37 | + heardbyte[0] = address.byteValue(); | ||
| 38 | + heardbyte[1] = commdcode.byteValue(); | ||
| 39 | + byte[] notlrcdata = ArrayUtils.addAll(heardbyte,data); | ||
| 40 | + this.crc = generateLRC(notlrcdata); | ||
| 41 | + this.hstr = ByteUtil.toHexString(notlrcdata)+this.crc; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public ModbusDto() | ||
| 45 | + { | ||
| 46 | + | ||
| 47 | + } | ||
| 48 | + public ModbusDto(byte[] bytes) | ||
| 49 | + { | ||
| 50 | + this.hstr = ByteUtil.toHexString(bytes); | ||
| 51 | + byte[] headAndData = ArrayUtils.subarray(bytes,0,bytes.length-2); | ||
| 52 | + byte[] crcByte = ArrayUtils.subarray(bytes,bytes.length-2,bytes.length); | ||
| 53 | + this.crc = ByteUtil.toHexString(crcByte).toUpperCase(); | ||
| 54 | + // 校验CRC | ||
| 55 | + if (!ByteUtil.getCRC16(headAndData).equals(crc)) { | ||
| 56 | + System.out.println("CRC校验失败"); | ||
| 57 | + return ; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + this.address = Math.toIntExact(ByteUtil.bytesToLongDESC(headAndData, 0, 1)); | ||
| 61 | + this.commdcode = Math.toIntExact(ByteUtil.bytesToLongDESC(headAndData, 1, 1)); | ||
| 62 | + this.data = ArrayUtils.subarray(headAndData,2,headAndData.length);; | ||
| 63 | + | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + public ModbusDto(String str) { | ||
| 67 | + new ModbusDto(ByteUtil.hexStringToByte(str)); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + | ||
| 71 | + public byte[] generateCommd() | ||
| 72 | + { | ||
| 73 | + return ByteUtil.hexStringToByte(hstr); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + // 计算CRC校验码 | ||
| 77 | + public static String generateLRC(byte[] data) | ||
| 78 | + { | ||
| 79 | + return ByteUtil.getCRC16(data); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + public static void main(String[] args) { | ||
| 83 | + String hexData = "01 03 00 0D 00 01".replace(" ",""); | ||
| 84 | + String crc = ByteUtil.getCRC16(ByteUtil.hexStringToByte(hexData)); | ||
| 85 | + System.out.println(crc); | ||
| 86 | + | ||
| 87 | + Map<Integer,String> map = new HashMap<>(); | ||
| 88 | + map.put(1,"ssss"); | ||
| 89 | + map.put(2,"eae"); | ||
| 90 | + try { | ||
| 91 | + System.out.println(new ObjectMapper().writeValueAsString(map)); | ||
| 92 | + } catch (JsonProcessingException e) { | ||
| 93 | + throw new RuntimeException(e); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | +} |
| 1 | package com.zhonglai.luhui.device.protocol.factory; | 1 | package com.zhonglai.luhui.device.protocol.factory; |
| 2 | 2 | ||
| 3 | import com.google.gson.JsonObject; | 3 | import com.google.gson.JsonObject; |
| 4 | +import com.ruoyi.common.utils.StringUtils; | ||
| 4 | import com.ruoyi.common.utils.spring.SpringUtils; | 5 | import com.ruoyi.common.utils.spring.SpringUtils; |
| 5 | import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | 6 | import com.zhonglai.luhui.device.analysis.comm.factory.Topic; |
| 6 | import com.zhonglai.luhui.device.analysis.util.TopicUtil; | 7 | import com.zhonglai.luhui.device.analysis.util.TopicUtil; |
| 8 | +import com.zhonglai.luhui.device.domain.IotProduct; | ||
| 9 | +import com.zhonglai.luhui.device.protocol.factory.analysis.DefaultProtocolParserFactoryImpl; | ||
| 7 | import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | 10 | import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; |
| 8 | import com.zhonglai.luhui.device.protocol.factory.comm.DataLogType; | 11 | import com.zhonglai.luhui.device.protocol.factory.comm.DataLogType; |
| 9 | import com.zhonglai.luhui.device.protocol.factory.comm.DeviceDataLog; | 12 | import com.zhonglai.luhui.device.protocol.factory.comm.DeviceDataLog; |
| 13 | +import com.zhonglai.luhui.device.protocol.factory.config.PluginsClassLoader; | ||
| 10 | import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; | 14 | import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; |
| 15 | +import com.zhonglai.luhui.device.protocol.factory.plugins.InitPlugins; | ||
| 16 | +import com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl; | ||
| 11 | import com.zhonglai.luhui.device.protocol.factory.purification.ProtocolPurificationFactory; | 17 | import com.zhonglai.luhui.device.protocol.factory.purification.ProtocolPurificationFactory; |
| 18 | +import com.zhonglai.luhui.device.protocol.factory.service.PersistenceDBService; | ||
| 12 | import org.slf4j.Logger; | 19 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; | 20 | import org.slf4j.LoggerFactory; |
| 14 | import org.springframework.beans.factory.annotation.Autowired; | 21 | import org.springframework.beans.factory.annotation.Autowired; |
| @@ -21,6 +28,8 @@ import java.util.Map; | @@ -21,6 +28,8 @@ import java.util.Map; | ||
| 21 | */ | 28 | */ |
| 22 | @Component | 29 | @Component |
| 23 | public class ProtocolParserAndPurificationFactory<T> { | 30 | public class ProtocolParserAndPurificationFactory<T> { |
| 31 | + @Autowired | ||
| 32 | + protected PersistenceDBService persistenceDBService; | ||
| 24 | 33 | ||
| 25 | /** | 34 | /** |
| 26 | * 根据topic对payload数据进行解析和清洗 | 35 | * 根据topic对payload数据进行解析和清洗 |
| @@ -28,14 +37,13 @@ public class ProtocolParserAndPurificationFactory<T> { | @@ -28,14 +37,13 @@ public class ProtocolParserAndPurificationFactory<T> { | ||
| 28 | * @param payload 数据信息 | 37 | * @param payload 数据信息 |
| 29 | * @return | 38 | * @return |
| 30 | */ | 39 | */ |
| 31 | - public ProtocolPurificationModel analysisAndPurification(Integer product_id,String topicStr, T payload ,String analysis_clas,String purification_clas) throws ClassNotFoundException { | 40 | + public ProtocolPurificationModel analysisAndPurification(IotProduct iotProduct, String topicStr, T payload ) throws InstantiationException, IllegalAccessException { |
| 32 | Topic baseTopic = TopicUtil.initTopicFromModelStr(topicStr,"/{{roleid}}/{{username}}"); //我们定义的topic | 41 | Topic baseTopic = TopicUtil.initTopicFromModelStr(topicStr,"/{{roleid}}/{{username}}"); //我们定义的topic |
| 33 | 42 | ||
| 34 | - | ||
| 35 | //根据产品类型找到对应的解析服务 | 43 | //根据产品类型找到对应的解析服务 |
| 36 | - ProtocolParserFactory protocolParserFactory = (ProtocolParserFactory) SpringUtils.getBean(Class.forName(analysis_clas)); | 44 | + ProtocolParserFactory protocolParserFactory = getProtocolParserFactory(persistenceDBService.getClassnameFromIotProtocolClassId(iotProduct.getAnalysis_clas())); |
| 37 | //根据产品类型找到对应的清洗服务 | 45 | //根据产品类型找到对应的清洗服务 |
| 38 | - ProtocolPurificationFactory protocolPurificationFactory = (ProtocolPurificationFactory) SpringUtils.getBean(Class.forName(purification_clas)); | 46 | + ProtocolPurificationFactory protocolPurificationFactory =getProtocolPurificationFactory(persistenceDBService.getClassnameFromIotProtocolClassId(iotProduct.getPurification_clas())); |
| 39 | 47 | ||
| 40 | if(null != protocolParserFactory && null != protocolPurificationFactory) //需要解析服务和清除服务都存在业务才能进行下去 | 48 | if(null != protocolParserFactory && null != protocolPurificationFactory) //需要解析服务和清除服务都存在业务才能进行下去 |
| 41 | { | 49 | { |
| @@ -45,7 +53,7 @@ public class ProtocolParserAndPurificationFactory<T> { | @@ -45,7 +53,7 @@ public class ProtocolParserAndPurificationFactory<T> { | ||
| 45 | JsonObject jsonObject = protocolParserFactory.analysisPayload(topic,payload); | 53 | JsonObject jsonObject = protocolParserFactory.analysisPayload(topic,payload); |
| 46 | 54 | ||
| 47 | //通过模式对解析结果进行清洗,获得到的数据就是业务数据 | 55 | //通过模式对解析结果进行清洗,获得到的数据就是业务数据 |
| 48 | - ProtocolPurificationModel protocolPurificationModel = protocolPurificationFactory.purification(product_id,topic,jsonObject); | 56 | + ProtocolPurificationModel protocolPurificationModel = protocolPurificationFactory.purification(iotProduct.getId(),topic,jsonObject); |
| 49 | return protocolPurificationModel; | 57 | return protocolPurificationModel; |
| 50 | 58 | ||
| 51 | } | 59 | } |
| @@ -54,4 +62,47 @@ public class ProtocolParserAndPurificationFactory<T> { | @@ -54,4 +62,47 @@ public class ProtocolParserAndPurificationFactory<T> { | ||
| 54 | return null; | 62 | return null; |
| 55 | } | 63 | } |
| 56 | 64 | ||
| 65 | + /** | ||
| 66 | + * 获取解析服务 | ||
| 67 | + * @param analysis_clas | ||
| 68 | + * @return | ||
| 69 | + * @throws InstantiationException | ||
| 70 | + * @throws IllegalAccessException | ||
| 71 | + */ | ||
| 72 | + private ProtocolParserFactory getProtocolParserFactory(String analysis_clas) throws InstantiationException, IllegalAccessException { | ||
| 73 | + if(StringUtils.isEmpty(analysis_clas)) | ||
| 74 | + { | ||
| 75 | + return new DefaultProtocolParserFactoryImpl(); | ||
| 76 | + } | ||
| 77 | + ProtocolParserFactory protocolParserFactory = PluginsClassLoader.getJarClass(ProtocolParserFactory.class,analysis_clas); | ||
| 78 | + if(null == protocolParserFactory) | ||
| 79 | + { | ||
| 80 | + System.out.println("没有找到解析服务使用默认的"); | ||
| 81 | + protocolParserFactory = new DefaultProtocolParserFactoryImpl(); | ||
| 82 | + } | ||
| 83 | + return protocolParserFactory; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * 获取清洗服务 | ||
| 88 | + * @param purification_clas | ||
| 89 | + * @return | ||
| 90 | + * @throws InstantiationException | ||
| 91 | + * @throws IllegalAccessException | ||
| 92 | + */ | ||
| 93 | + | ||
| 94 | + private ProtocolPurificationFactory getProtocolPurificationFactory(String purification_clas) throws InstantiationException, IllegalAccessException { | ||
| 95 | + if(StringUtils.isEmpty(purification_clas)) | ||
| 96 | + { | ||
| 97 | + return new DefaultProtocolPurificationFactoryImpl(); | ||
| 98 | + } | ||
| 99 | + ProtocolPurificationFactory protocolPurificationFactory = PluginsClassLoader.getJarClass(ProtocolPurificationFactory.class,purification_clas); | ||
| 100 | + if(null == protocolPurificationFactory) | ||
| 101 | + { | ||
| 102 | + protocolPurificationFactory = new DefaultProtocolPurificationFactoryImpl(); | ||
| 103 | + } | ||
| 104 | + return protocolPurificationFactory; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + | ||
| 57 | } | 108 | } |
| @@ -6,13 +6,11 @@ import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | @@ -6,13 +6,11 @@ import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | ||
| 6 | import com.zhonglai.luhui.device.analysis.util.TopicUtil; | 6 | import com.zhonglai.luhui.device.analysis.util.TopicUtil; |
| 7 | import org.springframework.stereotype.Service; | 7 | import org.springframework.stereotype.Service; |
| 8 | 8 | ||
| 9 | -@Service | ||
| 10 | public class DefaultProtocolParserFactoryImpl implements ProtocolParserFactory<byte[]>{ | 9 | public class DefaultProtocolParserFactoryImpl implements ProtocolParserFactory<byte[]>{ |
| 11 | - private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{imei}}/{{topicType}}"; | 10 | + private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}"; |
| 12 | 11 | ||
| 13 | @Override | 12 | @Override |
| 14 | public Topic analysisTopic(String topicStr) { | 13 | public Topic analysisTopic(String topicStr) { |
| 15 | - // /13/jiulin/476210165B365166812345678Userdata/Json/476210165B365166812345678/pub_data | ||
| 16 | Topic topic = TopicUtil.initTopicFromModelStr(topicStr,topicModel); | 14 | Topic topic = TopicUtil.initTopicFromModelStr(topicStr,topicModel); |
| 17 | return topic; | 15 | return topic; |
| 18 | } | 16 | } |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.config; | ||
| 2 | + | ||
| 3 | +import com.ruoyi.common.utils.StringUtils; | ||
| 4 | +import com.ruoyi.common.utils.spring.SpringUtils; | ||
| 5 | +import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | ||
| 6 | +import com.zhonglai.luhui.device.protocol.factory.plugins.InitPlugins; | ||
| 7 | +import org.springframework.stereotype.Component; | ||
| 8 | +import org.springframework.stereotype.Service; | ||
| 9 | +import org.springframework.util.FileCopyUtils; | ||
| 10 | + | ||
| 11 | +import java.io.File; | ||
| 12 | +import java.io.IOException; | ||
| 13 | +import java.lang.reflect.InvocationTargetException; | ||
| 14 | +import java.lang.reflect.Modifier; | ||
| 15 | +import java.net.MalformedURLException; | ||
| 16 | +import java.net.URL; | ||
| 17 | +import java.net.URLClassLoader; | ||
| 18 | +import java.util.Enumeration; | ||
| 19 | +import java.util.HashMap; | ||
| 20 | +import java.util.List; | ||
| 21 | +import java.util.Map; | ||
| 22 | +import java.util.jar.JarEntry; | ||
| 23 | +import java.util.jar.JarFile; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * 自定义的类加载器 | ||
| 27 | + */ | ||
| 28 | +public class PluginsClassLoader extends URLClassLoader { | ||
| 29 | + /** | ||
| 30 | + * 存放类 | ||
| 31 | + */ | ||
| 32 | + private static Map<String,PluginsClassLoader> jarMap = new HashMap<>(); | ||
| 33 | + | ||
| 34 | + private static Map<String,Class<?>> classMap = new HashMap<>(); | ||
| 35 | + | ||
| 36 | + private PluginsClassLoader(URL[] urls) { | ||
| 37 | + super(urls,PluginsClassLoader.class.getClassLoader()); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * 卸载jar | ||
| 42 | + * @throws IOException | ||
| 43 | + */ | ||
| 44 | + private static void unloadJar(String... filePaths) throws IOException { | ||
| 45 | + for (String filePath:filePaths) | ||
| 46 | + { | ||
| 47 | + String key = InitPlugins.toJarPath(filePath); | ||
| 48 | + if(jarMap.containsKey(key)) | ||
| 49 | + { | ||
| 50 | + PluginsClassLoader pluginsClassLoader = jarMap.get(key); | ||
| 51 | + jarMap.remove(key); | ||
| 52 | + pluginsClassLoader.close(); | ||
| 53 | + } | ||
| 54 | + File file = new File(key); | ||
| 55 | + if(file.exists()) | ||
| 56 | + { | ||
| 57 | + file.delete(); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * 更新jar | ||
| 66 | + * @throws IOException | ||
| 67 | + */ | ||
| 68 | + public static void uploadJar(File... filePaths) { | ||
| 69 | + try { | ||
| 70 | + for (File file:filePaths) | ||
| 71 | + { | ||
| 72 | + String filePath = file.getAbsolutePath(); | ||
| 73 | + unloadJar(filePath); | ||
| 74 | + | ||
| 75 | + String key = InitPlugins.toJarPath(filePath); | ||
| 76 | + | ||
| 77 | + FileCopyUtils.copy(new File(filePath),new File(key)); | ||
| 78 | + | ||
| 79 | + PluginsClassLoader pluginsClassLoader = new PluginsClassLoader(new URL[]{new URL("file:"+key)}); | ||
| 80 | + jarMap.put(key,pluginsClassLoader); | ||
| 81 | + | ||
| 82 | + laodJar(new File(key),pluginsClassLoader); | ||
| 83 | + } | ||
| 84 | + } catch (IOException e) { | ||
| 85 | + throw new RuntimeException(e); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + | ||
| 91 | + | ||
| 92 | + private static void laodJar(File jarfile,ClassLoader classLoader) | ||
| 93 | + { | ||
| 94 | + JarFile jarFile = null; | ||
| 95 | + try { | ||
| 96 | + jarFile = new JarFile(jarfile); | ||
| 97 | + Enumeration<JarEntry> entries = jarFile.entries(); | ||
| 98 | + while (entries.hasMoreElements()) | ||
| 99 | + { | ||
| 100 | + JarEntry jarEntry = entries.nextElement(); | ||
| 101 | + String entryName = jarEntry.getName(); | ||
| 102 | + if (entryName != null && entryName.endsWith(".class")) { | ||
| 103 | + entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf(".")); | ||
| 104 | + Class<?> clas = classLoader.loadClass(entryName); | ||
| 105 | + System.out.println(ProtocolParserFactory.class.isAssignableFrom(clas));; | ||
| 106 | + if(SpringUtils.isSpringBean()) | ||
| 107 | + { | ||
| 108 | + Object object = loadBean(clas); | ||
| 109 | + } | ||
| 110 | + classMap.put(entryName,clas); | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + } catch (IOException e) { | ||
| 114 | + throw new RuntimeException(e); | ||
| 115 | + } catch (ClassNotFoundException e) { | ||
| 116 | + throw new RuntimeException(e); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + private static <T> T loadBean(Class<T> clas) | ||
| 122 | + { | ||
| 123 | + if(isInstantiable(clas)) | ||
| 124 | + { | ||
| 125 | + try { | ||
| 126 | + Object object = clas.newInstance(); | ||
| 127 | + if(clas.isAnnotationPresent(Component.class) || clas.isAnnotationPresent(Service.class)) | ||
| 128 | + { | ||
| 129 | + SpringUtils.registerBean(null,object); | ||
| 130 | + return SpringUtils.getBean(clas); | ||
| 131 | + }else { | ||
| 132 | + return (T) object; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + } catch (InstantiationException e) { | ||
| 136 | + throw new RuntimeException(e); | ||
| 137 | + } catch (IllegalAccessException e) { | ||
| 138 | + throw new RuntimeException(e); | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + return null; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + private static boolean isInstantiable(Class<?> clazz) { | ||
| 145 | + // 检查类是否是抽象类或接口 | ||
| 146 | + if (Modifier.isAbstract(clazz.getModifiers()) || clazz.isInterface()) { | ||
| 147 | + return false; | ||
| 148 | + } | ||
| 149 | + // 尝试查找并访问默认的无参构造器 | ||
| 150 | + try { | ||
| 151 | + clazz.getDeclaredConstructor((Class[]) null); | ||
| 152 | + return true; | ||
| 153 | + } catch (NoSuchMethodException e) { | ||
| 154 | + return false; | ||
| 155 | + } | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + public static <T> T getJarClass(Class<T> tClass,String classname) { | ||
| 159 | + Class clazz = classMap.get(classname); | ||
| 160 | + if (null != clazz) | ||
| 161 | + { | ||
| 162 | + System.out.println("接口的类加载器:"+tClass.getClassLoader()); | ||
| 163 | + System.out.println("实现类的类加载器:"+clazz.getClassLoader()); | ||
| 164 | + try { | ||
| 165 | + if (tClass.isAssignableFrom(clazz) && !clazz.equals(tClass)) { | ||
| 166 | + return tClass.cast(clazz.getDeclaredConstructor().newInstance()); | ||
| 167 | + }else if(tClass.isAssignableFrom(clazz) && clazz.equals(tClass)) | ||
| 168 | + { | ||
| 169 | + return tClass.newInstance(); | ||
| 170 | + } | ||
| 171 | + } catch (InstantiationException e) { | ||
| 172 | + throw new RuntimeException(e); | ||
| 173 | + } catch (IllegalAccessException e) { | ||
| 174 | + throw new RuntimeException(e); | ||
| 175 | + } catch (InvocationTargetException e) { | ||
| 176 | + throw new RuntimeException(e); | ||
| 177 | + } catch (NoSuchMethodException e) { | ||
| 178 | + throw new RuntimeException(e); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + } | ||
| 182 | + return null; | ||
| 183 | + } | ||
| 184 | +} |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.control; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.device.analysis.comm.clien.ClienConnection; | ||
| 4 | +import com.zhonglai.luhui.device.analysis.comm.clien.impl.ClienConnectionImpl; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.dto.Message; | ||
| 6 | +import com.zhonglai.luhui.device.analysis.dto.MessageCode; | ||
| 7 | +import net.jodah.expiringmap.ExpirationListener; | ||
| 8 | +import net.jodah.expiringmap.ExpirationPolicy; | ||
| 9 | +import net.jodah.expiringmap.ExpiringMap; | ||
| 10 | +import org.apache.rocketmq.common.message.MessageExt; | ||
| 11 | +import org.apache.rocketmq.spring.core.RocketMQReplyListener; | ||
| 12 | +import org.slf4j.Logger; | ||
| 13 | +import org.slf4j.LoggerFactory; | ||
| 14 | + | ||
| 15 | +import java.util.concurrent.TimeUnit; | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * 控制工厂 | ||
| 19 | + */ | ||
| 20 | +public abstract class ControlFactory implements RocketMQReplyListener<MessageExt, Message> { | ||
| 21 | + private static final Logger log = LoggerFactory.getLogger(ControlFactory.class); | ||
| 22 | + | ||
| 23 | + protected static ExpiringMap<String, ClienConnection> clienConnectionMap = ExpiringMap.builder().maxSize(20000).expiration(15, TimeUnit.SECONDS) | ||
| 24 | + .asyncExpirationListener(new ExpirationListener<String, ClienConnection>() { | ||
| 25 | + @Override | ||
| 26 | + public void expired(String s, ClienConnection clienConnection) { | ||
| 27 | + log.info("{} 通道消失了>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",s); | ||
| 28 | + clienConnection.close(); | ||
| 29 | + } | ||
| 30 | + }) | ||
| 31 | + .expirationPolicy(ExpirationPolicy.CREATED).build(); | ||
| 32 | + | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + public Message onMessage(MessageExt message) { | ||
| 36 | + log.info("监听到消息{}",message); | ||
| 37 | + String str = new String(message.getBody()); | ||
| 38 | + log.info("消息body{}",str); | ||
| 39 | + try { | ||
| 40 | + return sendMessage(str); | ||
| 41 | + } catch (InterruptedException e) { | ||
| 42 | + log.error("消息发送异常",e); | ||
| 43 | + return new Message(MessageCode.DEFAULT_FAIL_CODE,"系统异常,请联系管理员"); | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + private Message sendMessage(String str) throws InterruptedException { | ||
| 48 | + //设置通知渠道 | ||
| 49 | + ClienConnection clienConnection = new ClienConnectionImpl(); | ||
| 50 | + String key = analysisMessageKey(); | ||
| 51 | + log.info("设置通知渠道 {} {}",key,str); | ||
| 52 | + | ||
| 53 | + if(clienConnectionMap.containsKey(key)) | ||
| 54 | + { | ||
| 55 | + return new Message(MessageCode.DEFAULT_FAIL_CODE,"有人正在操作,请稍后尝试"); | ||
| 56 | + } | ||
| 57 | + clienConnectionMap.put(key,clienConnection); | ||
| 58 | + if(transmitMessage(str)) | ||
| 59 | + { | ||
| 60 | + synchronized(clienConnection) | ||
| 61 | + { | ||
| 62 | + log.info("{}等待通知",key); | ||
| 63 | + clienConnection.wait(getOperationTime()+3000l); | ||
| 64 | + } | ||
| 65 | + //清楚通道 | ||
| 66 | + clienConnectionMap.remove(key); | ||
| 67 | + log.info("{}收到通知{}",key,clienConnection.getReplyMessage().getMessage()); | ||
| 68 | + Message message = clienConnection.getReplyMessage(); | ||
| 69 | + log.info("{}返回通知{}",key,message); | ||
| 70 | + return message; | ||
| 71 | + } | ||
| 72 | + return new Message(MessageCode.DEFAULT_FAIL_CODE,"消息转发失败,联系管理员"); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * 获取消息key,一般使用imei号 | ||
| 77 | + * @return | ||
| 78 | + */ | ||
| 79 | + protected abstract String analysisMessageKey(); | ||
| 80 | + | ||
| 81 | + /** | ||
| 82 | + * 转发消息 | ||
| 83 | + * @param str | ||
| 84 | + * @return | ||
| 85 | + */ | ||
| 86 | + protected abstract Boolean transmitMessage(String str); | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * 客户端操作等待时间,推荐10000 | ||
| 90 | + * @return | ||
| 91 | + */ | ||
| 92 | + protected abstract long getOperationTime(); //客户端操作时间 | ||
| 93 | +} |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.control.clien; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import com.zhonglai.luhui.device.analysis.comm.dto.ApiClientRePlyDto; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.dto.Message; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 客户端链接 | ||
| 9 | + */ | ||
| 10 | +public interface ClienConnection { | ||
| 11 | + public void close(); | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * 回复 | ||
| 15 | + */ | ||
| 16 | + public void reply(ApiClientRePlyDto apiClientRePlyDto); | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * 回复 | ||
| 20 | + */ | ||
| 21 | + public Message getReplyMessage(); | ||
| 22 | +} |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.control.clien.impl; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import com.zhonglai.luhui.device.analysis.comm.dto.ApiClientRePlyDto; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.dto.Message; | ||
| 6 | +import com.zhonglai.luhui.device.analysis.dto.MessageCode; | ||
| 7 | +import com.zhonglai.luhui.device.analysis.dto.MessageCodeType; | ||
| 8 | +import com.zhonglai.luhui.device.protocol.factory.control.clien.ClienConnection; | ||
| 9 | + | ||
| 10 | +public class ClienConnectionImpl implements ClienConnection { | ||
| 11 | + private Message message = new Message(); | ||
| 12 | + | ||
| 13 | + @Override | ||
| 14 | + public void close() { | ||
| 15 | + this.message.setCode(MessageCode.DEFAULT_FAIL_CODE); | ||
| 16 | + this.message.setMessage("链接超时关闭"); | ||
| 17 | + this.notify(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + @Override | ||
| 21 | + public void reply(ApiClientRePlyDto apiClientRePlyDto) { | ||
| 22 | + apiClientRePlyDto.setReplyMessage(message); | ||
| 23 | + this.notify(); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public Message getReplyMessage() { | ||
| 28 | + return message; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public ClienConnectionImpl setCode(MessageCodeType code) | ||
| 32 | + { | ||
| 33 | + this.message.setCode(code); | ||
| 34 | + return this; | ||
| 35 | + } | ||
| 36 | + public ClienConnectionImpl setData(Object data) | ||
| 37 | + { | ||
| 38 | + this.message.setData(data); | ||
| 39 | + return this; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + public ClienConnectionImpl setMessage(String message) | ||
| 43 | + { | ||
| 44 | + this.message.setMessage(message); | ||
| 45 | + return this; | ||
| 46 | + } | ||
| 47 | +} |
| @@ -21,21 +21,23 @@ public class AnalysisDto { | @@ -21,21 +21,23 @@ public class AnalysisDto { | ||
| 21 | return this; | 21 | return this; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | - public ParserDeviceHostDto toParserDeviceHostDto(String id) | 24 | + public ParserDeviceHostDto toParserDeviceHostDto(String id,Integer time) |
| 25 | { | 25 | { |
| 26 | ParserDeviceHostDto parserDeviceHostDto = new ParserDeviceHostDto(); | 26 | ParserDeviceHostDto parserDeviceHostDto = new ParserDeviceHostDto(); |
| 27 | parserDeviceHostDto.setData(things_model_value); | 27 | parserDeviceHostDto.setData(things_model_value); |
| 28 | parserDeviceHostDto.setConfig(things_model_config); | 28 | parserDeviceHostDto.setConfig(things_model_config); |
| 29 | parserDeviceHostDto.setId(id); | 29 | parserDeviceHostDto.setId(id); |
| 30 | + parserDeviceHostDto.setUpdateTime(time); | ||
| 30 | return parserDeviceHostDto; | 31 | return parserDeviceHostDto; |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | - public ParserDeviceInfoDto toParserDeviceInfoDto(String id) | 34 | + public ParserDeviceInfoDto toParserDeviceInfoDto(String id,Integer time) |
| 34 | { | 35 | { |
| 35 | ParserDeviceInfoDto parserDeviceInfoDto = new ParserDeviceInfoDto(); | 36 | ParserDeviceInfoDto parserDeviceInfoDto = new ParserDeviceInfoDto(); |
| 36 | parserDeviceInfoDto.setData(things_model_value); | 37 | parserDeviceInfoDto.setData(things_model_value); |
| 37 | parserDeviceInfoDto.setConfig(things_model_config); | 38 | parserDeviceInfoDto.setConfig(things_model_config); |
| 38 | parserDeviceInfoDto.setId(id); | 39 | parserDeviceInfoDto.setId(id); |
| 40 | + parserDeviceInfoDto.setUpdateTime(time); | ||
| 39 | return parserDeviceInfoDto; | 41 | return parserDeviceInfoDto; |
| 40 | } | 42 | } |
| 41 | } | 43 | } |
| 1 | package com.zhonglai.luhui.device.protocol.factory.dto; | 1 | package com.zhonglai.luhui.device.protocol.factory.dto; |
| 2 | 2 | ||
| 3 | import com.google.gson.JsonObject; | 3 | import com.google.gson.JsonObject; |
| 4 | +import com.zhonglai.luhui.device.domain.IotProduct; | ||
| 4 | import io.swagger.annotations.ApiModelProperty; | 5 | import io.swagger.annotations.ApiModelProperty; |
| 5 | import lombok.Data; | 6 | import lombok.Data; |
| 6 | 7 | ||
| @@ -30,28 +31,6 @@ public class ParserDeviceHostDto { | @@ -30,28 +31,6 @@ public class ParserDeviceHostDto { | ||
| 30 | * 数据类型 | 31 | * 数据类型 |
| 31 | */ | 32 | */ |
| 32 | private String device_type; | 33 | private String device_type; |
| 33 | - /** | ||
| 34 | - *同步数据的 | ||
| 35 | - */ | ||
| 36 | - private String sync_db; | ||
| 37 | - /** | ||
| 38 | - *是否同步数据 | ||
| 39 | - */ | ||
| 40 | - private boolean is_sync_db; | ||
| 41 | - /** | ||
| 42 | - *产品id | ||
| 43 | - */ | ||
| 44 | - private Integer product_id; | ||
| 45 | - | ||
| 46 | - /** | ||
| 47 | - * 解析服务 | ||
| 48 | - */ | ||
| 49 | - private String analysis_clas; | ||
| 50 | - | ||
| 51 | - /** | ||
| 52 | - * 清洗服务 | ||
| 53 | - */ | ||
| 54 | - private String purification_clas; | ||
| 55 | 34 | ||
| 56 | /** | 35 | /** |
| 57 | * 设备摘要,格式{{"name":"device"},{"chip":"esp8266"}} | 36 | * 设备摘要,格式{{"name":"device"},{"chip":"esp8266"}} |
| @@ -61,4 +40,6 @@ public class ParserDeviceHostDto { | @@ -61,4 +40,6 @@ public class ParserDeviceHostDto { | ||
| 61 | private boolean is_config_up; | 40 | private boolean is_config_up; |
| 62 | private boolean is_data_up; | 41 | private boolean is_data_up; |
| 63 | private boolean is_summary_up; | 42 | private boolean is_summary_up; |
| 43 | + | ||
| 44 | + private IotProduct iotProduct; | ||
| 64 | } | 45 | } |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.plugins; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | ||
| 4 | +import com.zhonglai.luhui.device.protocol.factory.config.PluginsClassLoader; | ||
| 5 | +import org.springframework.context.event.ContextRefreshedEvent; | ||
| 6 | +import org.springframework.context.event.EventListener; | ||
| 7 | +import org.springframework.stereotype.Component; | ||
| 8 | + | ||
| 9 | +import javax.annotation.PostConstruct; | ||
| 10 | +import javax.annotation.PreDestroy; | ||
| 11 | +import java.io.File; | ||
| 12 | +import java.io.IOException; | ||
| 13 | +import java.nio.file.*; | ||
| 14 | +import java.util.ArrayList; | ||
| 15 | +import java.util.List; | ||
| 16 | + | ||
| 17 | +@Component | ||
| 18 | +public class FileChangeListener { | ||
| 19 | + private WatchService watchService; | ||
| 20 | + private Thread watcherThread; | ||
| 21 | + private volatile boolean running = true; | ||
| 22 | + | ||
| 23 | + public void startFileWatcher() throws IOException { | ||
| 24 | + //初始化插件 | ||
| 25 | + InitPlugins.init(); | ||
| 26 | + | ||
| 27 | + //注册文件监听 | ||
| 28 | + registerListener(); | ||
| 29 | + | ||
| 30 | + //启动监听线程 | ||
| 31 | + startThread(); | ||
| 32 | + | ||
| 33 | + System.out.println("启动成功"); | ||
| 34 | + ProtocolParserFactory protocolParserFactory = PluginsClassLoader.getJarClass(ProtocolParserFactory.class,"com.zhonglai.luhui.device.protocol.http.analysis.analysis.ProtocolParserServiceImpl"); | ||
| 35 | + System.out.println(protocolParserFactory.analysisTopic("/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{imei}}/{{topicType}}")); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + @EventListener(ContextRefreshedEvent.class) | ||
| 40 | + public void onApplicationEvent(ContextRefreshedEvent event) throws IOException { | ||
| 41 | + startFileWatcher(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + private void registerListener() throws IOException { | ||
| 46 | + // 创建WatchService,它是对操作系统的文件监视器的封装,相对之前,不需要遍历文件目录,效率要高很多 | ||
| 47 | + watchService = FileSystems.getDefault().newWatchService(); | ||
| 48 | + // 注册指定目录使用的监听器,监视目录下文件的变化; | ||
| 49 | + // PS:Path必须是目录,不能是文件; | ||
| 50 | + // StandardWatchEventKinds.ENTRY_MODIFY,表示监视文件的修改事件 | ||
| 51 | + Path path = Paths.get(InitPlugins.getPluginsPath()); | ||
| 52 | + path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + private void startThread() | ||
| 56 | + { | ||
| 57 | + // 创建并启动监听线程 | ||
| 58 | + watcherThread = new Thread(this::watchFileChanges); | ||
| 59 | + watcherThread.start(); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + private void watchFileChanges() { | ||
| 63 | + while (running) { | ||
| 64 | + try { | ||
| 65 | + // 获取目录的变化: | ||
| 66 | + // take()是一个阻塞方法,会等待监视器发出的信号才返回。 | ||
| 67 | + // 还可以使用watcher.poll()方法,非阻塞方法,会立即返回当时监视器中是否有信号。 | ||
| 68 | + // 返回结果WatchKey,是一个单例对象,与前面的register方法返回的实例是同一个; | ||
| 69 | + WatchKey key = watchService.take(); | ||
| 70 | + // 处理文件变化事件: | ||
| 71 | + // key.pollEvents()用于获取文件变化事件,只能获取一次,不能重复获取,类似队列的形式。 | ||
| 72 | + for (WatchEvent<?> event : key.pollEvents()) { | ||
| 73 | + // event.kind():事件类型 | ||
| 74 | + if (event.kind() == StandardWatchEventKinds.OVERFLOW) { | ||
| 75 | + //事件可能lost or discarded | ||
| 76 | + continue; | ||
| 77 | + } | ||
| 78 | + // 返回触发事件的文件或目录的路径(相对路径) | ||
| 79 | + Path fileName = (Path) event.context(); | ||
| 80 | + System.out.println("文件更新: " + fileName); | ||
| 81 | + PluginsClassLoader.uploadJar(new File(key.watchable().toString()+"/"+fileName)); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + // 每次调用WatchService的take()或poll()方法时需要通过本方法重置 | ||
| 85 | + if (!key.reset()) { | ||
| 86 | + break; | ||
| 87 | + } | ||
| 88 | + } catch (Exception e) { | ||
| 89 | + e.printStackTrace(); | ||
| 90 | + // 如果有必要,可以在这里优雅地处理异常,比如重新注册WatchKey | ||
| 91 | + break; | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + @PreDestroy | ||
| 97 | + public void stopFileWatcher() { | ||
| 98 | + running = false; // 设置标志以停止线程 | ||
| 99 | + | ||
| 100 | + // 等待线程完成(可选,但可能不总是需要的,取决于你的需求) | ||
| 101 | + try { | ||
| 102 | + watcherThread.join(); | ||
| 103 | + } catch (InterruptedException e) { | ||
| 104 | + e.printStackTrace(); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + // 关闭WatchService | ||
| 108 | + try { | ||
| 109 | + if (watchService != null) { | ||
| 110 | + watchService.close(); | ||
| 111 | + } | ||
| 112 | + } catch (IOException e) { | ||
| 113 | + e.printStackTrace(); | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + public static void main(String[] args) throws IOException { | ||
| 118 | + FileChangeListener fileChangeListener = new FileChangeListener(); | ||
| 119 | + fileChangeListener.startFileWatcher(); | ||
| 120 | + System.out.println("启动成功"); | ||
| 121 | + } | ||
| 122 | +} |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.plugins; | ||
| 2 | + | ||
| 3 | +import com.ruoyi.common.utils.StringUtils; | ||
| 4 | +import com.ruoyi.common.utils.spring.SpringUtils; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | ||
| 6 | +import com.zhonglai.luhui.device.protocol.factory.analysis.DefaultProtocolParserFactoryImpl; | ||
| 7 | +import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | ||
| 8 | +import com.zhonglai.luhui.device.protocol.factory.config.PluginsClassLoader; | ||
| 9 | +import com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl; | ||
| 10 | +import io.swagger.v3.oas.annotations.servers.Server; | ||
| 11 | +import org.apache.commons.lang3.ArrayUtils; | ||
| 12 | +import org.springframework.stereotype.Component; | ||
| 13 | +import org.springframework.stereotype.Service; | ||
| 14 | +import org.springframework.util.FileCopyUtils; | ||
| 15 | + | ||
| 16 | +import javax.annotation.PostConstruct; | ||
| 17 | +import java.io.File; | ||
| 18 | +import java.io.FileFilter; | ||
| 19 | +import java.io.IOException; | ||
| 20 | +import java.lang.reflect.InvocationTargetException; | ||
| 21 | +import java.lang.reflect.Method; | ||
| 22 | +import java.lang.reflect.Modifier; | ||
| 23 | +import java.net.MalformedURLException; | ||
| 24 | +import java.net.URL; | ||
| 25 | +import java.net.URLClassLoader; | ||
| 26 | +import java.util.*; | ||
| 27 | +import java.util.jar.JarEntry; | ||
| 28 | +import java.util.jar.JarFile; | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +public class InitPlugins { | ||
| 32 | + | ||
| 33 | + private static final String pluginsPath = "plugins"; | ||
| 34 | + private static final String pluginsJarPath = "pluginsjar"; | ||
| 35 | + | ||
| 36 | + | ||
| 37 | + public static void init() | ||
| 38 | + { | ||
| 39 | +// //加载默认的class | ||
| 40 | +// //默认协议解析 | ||
| 41 | +// classMap.put("com.zhonglai.luhui.device.protocol.factory.analysis.DefaultProtocolParserFactoryImpl", DefaultProtocolParserFactoryImpl.class); | ||
| 42 | +// //默认协议清洗 | ||
| 43 | +// classMap.put("com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl", DefaultProtocolPurificationFactoryImpl.class); | ||
| 44 | +// | ||
| 45 | +// File[] files = getJarFiles(getPluginsPath()); | ||
| 46 | +// loaderJar(files); | ||
| 47 | + File[] files = getJarFiles(getPluginsPath()); | ||
| 48 | + PluginsClassLoader.uploadJar(files); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + public static String getPluginsPath() | ||
| 52 | + { | ||
| 53 | + String jarFilePath = System.getProperty("jarFilePath"); | ||
| 54 | + String path = null != jarFilePath?jarFilePath:System.getProperty("user.dir")+"/"+pluginsPath; | ||
| 55 | + return path; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + public static String getPluginsJarPath() | ||
| 59 | + { | ||
| 60 | + String path = System.getProperty("user.dir")+"/"+pluginsJarPath; | ||
| 61 | + return path; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + public static File[] getJarFiles(String jarFilePath) | ||
| 66 | + { | ||
| 67 | + File file = new File(jarFilePath); | ||
| 68 | + if(file.exists() && file.isDirectory()) | ||
| 69 | + { | ||
| 70 | + | ||
| 71 | + File[] files = file.listFiles((dir, name) -> { | ||
| 72 | + if(name.endsWith(".jar")) | ||
| 73 | + { | ||
| 74 | + return true; | ||
| 75 | + } | ||
| 76 | + return false; | ||
| 77 | + }); | ||
| 78 | + return files; | ||
| 79 | + } | ||
| 80 | + return null; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + public static void main(String[] args) throws MalformedURLException { | ||
| 84 | + File[] files = getJarFiles(getPluginsPath()); | ||
| 85 | + PluginsClassLoader.uploadJar(files); | ||
| 86 | +// String jarFilePath = "E:\\work\\idea\\Luhui\\lh-modules\\lh-device-protocol-parser\\lh-device-xinjie\\target"; | ||
| 87 | +// File[] files = getJarFiles(jarFilePath); | ||
| 88 | +// loaderJar(files); | ||
| 89 | +// try { | ||
| 90 | +// ProtocolParserFactory protocolParserFactory = getJarClass(ProtocolParserFactory.class,"com.zhonglai.luhui.device.protocol.xinjie.analysis.ProtocolParserServiceImpl"); | ||
| 91 | +// Topic topic = protocolParserFactory.analysisTopic("/13/jiulin/476210165B365166812345678Userdata/Json/476210165B365166812345678/pub_data"); | ||
| 92 | +// System.out.println(topic); | ||
| 93 | +// } catch (InstantiationException e) { | ||
| 94 | +// throw new RuntimeException(e); | ||
| 95 | +// } catch (IllegalAccessException e) { | ||
| 96 | +// throw new RuntimeException(e); | ||
| 97 | +// } | ||
| 98 | + | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + | ||
| 102 | +// public static <T> T getJarClass(Class<T> clazz, String className) { | ||
| 103 | +// try { | ||
| 104 | +// Class<?> loadedClass = Class.forName(className); | ||
| 105 | +// System.out.println("接口的类加载器:"+clazz.getClassLoader()); | ||
| 106 | +// System.out.println("实现类的类加载器:"+loadedClass.getClassLoader()); | ||
| 107 | +// if (clazz.isAssignableFrom(loadedClass)) { | ||
| 108 | +// return clazz.cast(loadedClass.getDeclaredConstructor().newInstance()); | ||
| 109 | +// } | ||
| 110 | +// } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { | ||
| 111 | +// // Handle exceptions here | ||
| 112 | +// } | ||
| 113 | +// return null; | ||
| 114 | +// } | ||
| 115 | + | ||
| 116 | + public static String toJarPath(String filePath) | ||
| 117 | + { | ||
| 118 | + return filePath.replace(pluginsPath,pluginsJarPath); | ||
| 119 | + } | ||
| 120 | +} |
| 1 | package com.zhonglai.luhui.device.protocol.factory.purification; | 1 | package com.zhonglai.luhui.device.protocol.factory.purification; |
| 2 | 2 | ||
| 3 | -import com.alibaba.fastjson.JSON; | ||
| 4 | import com.alibaba.fastjson.JSONObject; | 3 | import com.alibaba.fastjson.JSONObject; |
| 5 | import com.google.gson.JsonElement; | 4 | import com.google.gson.JsonElement; |
| 6 | import com.google.gson.JsonObject; | 5 | import com.google.gson.JsonObject; |
| 7 | -import com.ruoyi.common.utils.DateUtils; | ||
| 8 | import com.ruoyi.common.utils.GsonConstructor; | 6 | import com.ruoyi.common.utils.GsonConstructor; |
| 9 | -import com.ruoyi.common.utils.StringUtils; | 7 | +import com.ruoyi.common.utils.spring.SpringUtils; |
| 10 | import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; | 8 | import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; |
| 11 | -import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelBase; | ||
| 12 | import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelDataTypeEnum; | 9 | import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelDataTypeEnum; |
| 13 | import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelItemBase; | 10 | import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelItemBase; |
| 14 | import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | 11 | import com.zhonglai.luhui.device.analysis.comm.factory.Topic; |
| 12 | +import com.zhonglai.luhui.device.analysis.comm.util.DateUtils; | ||
| 15 | import com.zhonglai.luhui.device.domain.IotThingsModel; | 13 | import com.zhonglai.luhui.device.domain.IotThingsModel; |
| 16 | import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisDto; | 14 | import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisDto; |
| 17 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; | 15 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; |
| 18 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto; | 16 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto; |
| 19 | import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; | 17 | import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; |
| 20 | import com.zhonglai.luhui.device.protocol.factory.service.IotThingsModelService; | 18 | import com.zhonglai.luhui.device.protocol.factory.service.IotThingsModelService; |
| 21 | -import com.zhonglai.luhui.device.protocol.factory.service.PersistenceDBService; | ||
| 22 | import org.apache.commons.lang3.EnumUtils; | 19 | import org.apache.commons.lang3.EnumUtils; |
| 23 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 24 | -import org.springframework.stereotype.Service; | ||
| 25 | 20 | ||
| 26 | import java.util.ArrayList; | 21 | import java.util.ArrayList; |
| 27 | import java.util.List; | 22 | import java.util.List; |
| 28 | -import java.util.Map; | ||
| 29 | 23 | ||
| 30 | -@Service("default_purification") | 24 | +/** |
| 25 | + * 默认的清洗服务 | ||
| 26 | + */ | ||
| 31 | public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificationFactory{ | 27 | public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificationFactory{ |
| 32 | - @Autowired | ||
| 33 | - private IotThingsModelService iotThingsModelService; | ||
| 34 | 28 | ||
| 35 | @Override | 29 | @Override |
| 36 | public ProtocolPurificationModel purification(Integer product_id,Topic topic, JsonObject protocolParserModel ) { | 30 | public ProtocolPurificationModel purification(Integer product_id,Topic topic, JsonObject protocolParserModel ) { |
| @@ -40,7 +34,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -40,7 +34,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 40 | ParserDeviceHostDto parserDeviceHostDto = analysisHost(product_id,topic,protocolParserModel,protocolPurificationModel.getDeviceSensorDataList()); | 34 | ParserDeviceHostDto parserDeviceHostDto = analysisHost(product_id,topic,protocolParserModel,protocolPurificationModel.getDeviceSensorDataList()); |
| 41 | protocolPurificationModel.setParserDeviceHostDto(parserDeviceHostDto); | 35 | protocolPurificationModel.setParserDeviceHostDto(parserDeviceHostDto); |
| 42 | 36 | ||
| 43 | - List<ParserDeviceInfoDto> parserDeviceInfoDtoList = analysisDeviceInfo(product_id,topic,protocolParserModel,protocolPurificationModel.getDeviceSensorDataList()); | 37 | + List<ParserDeviceInfoDto> parserDeviceInfoDtoList = analysisDeviceInfo(parserDeviceHostDto.getUpdateTime(),product_id,topic,protocolParserModel,protocolPurificationModel.getDeviceSensorDataList()); |
| 44 | protocolPurificationModel.setParserDeviceInfoDtoList(parserDeviceInfoDtoList); | 38 | protocolPurificationModel.setParserDeviceInfoDtoList(parserDeviceInfoDtoList); |
| 45 | return protocolPurificationModel; | 39 | return protocolPurificationModel; |
| 46 | } | 40 | } |
| @@ -59,10 +53,11 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -59,10 +53,11 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 59 | summary = jsonObjectData.get("summary").getAsJsonObject(); | 53 | summary = jsonObjectData.get("summary").getAsJsonObject(); |
| 60 | jsonObjectData.remove("summary"); | 54 | jsonObjectData.remove("summary"); |
| 61 | } | 55 | } |
| 56 | + int time = DateUtils.getNowTimeMilly(); | ||
| 62 | ParserDeviceHostDto parserDeviceHostDto = null; | 57 | ParserDeviceHostDto parserDeviceHostDto = null; |
| 63 | if(null != jsonObjectData && jsonObjectData.size() != 0) | 58 | if(null != jsonObjectData && jsonObjectData.size() != 0) |
| 64 | { | 59 | { |
| 65 | - parserDeviceHostDto = analysisJsonData(product_id,topic,jsonObjectData).pushDeviceSensorData(deviceSensorDataList).toParserDeviceHostDto(topic.getClientid()); | 60 | + parserDeviceHostDto = analysisJsonData(time,"0",product_id,topic,jsonObjectData).pushDeviceSensorData(deviceSensorDataList).toParserDeviceHostDto(topic.getClientid(),time); |
| 66 | } | 61 | } |
| 67 | 62 | ||
| 68 | if(null != summary && summary.size() != 0) | 63 | if(null != summary && summary.size() != 0) |
| @@ -74,12 +69,13 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -74,12 +69,13 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 74 | } | 69 | } |
| 75 | parserDeviceHostDto.setSummary(summary); | 70 | parserDeviceHostDto.setSummary(summary); |
| 76 | } | 71 | } |
| 72 | + parserDeviceHostDto.setUpdateTime(time); | ||
| 77 | return parserDeviceHostDto; | 73 | return parserDeviceHostDto; |
| 78 | } | 74 | } |
| 79 | return null; | 75 | return null; |
| 80 | } | 76 | } |
| 81 | 77 | ||
| 82 | - private List<ParserDeviceInfoDto> analysisDeviceInfo(Integer product_id,Topic topic, JsonObject data,List<DeviceSensorData> deviceSensorDataList) | 78 | + private List<ParserDeviceInfoDto> analysisDeviceInfo(Integer time,Integer product_id,Topic topic, JsonObject data,List<DeviceSensorData> deviceSensorDataList) |
| 83 | { | 79 | { |
| 84 | List<ParserDeviceInfoDto> list = new ArrayList<>(); | 80 | List<ParserDeviceInfoDto> list = new ArrayList<>(); |
| 85 | 81 | ||
| @@ -87,7 +83,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -87,7 +83,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 87 | { | 83 | { |
| 88 | if(!key.equals("0") && data.get(key).isJsonObject()) | 84 | if(!key.equals("0") && data.get(key).isJsonObject()) |
| 89 | { | 85 | { |
| 90 | - ParserDeviceInfoDto parserDeviceInfoDto = analysisJsonData(product_id,topic,data.get(key).getAsJsonObject()).pushDeviceSensorData(deviceSensorDataList).toParserDeviceInfoDto(topic.getClientid()+"_"+key); | 86 | + ParserDeviceInfoDto parserDeviceInfoDto = analysisJsonData(time,key,product_id,topic,data.get(key).getAsJsonObject()).pushDeviceSensorData(deviceSensorDataList).toParserDeviceInfoDto(topic.getClientid()+"_"+key,time); |
| 91 | list.add(parserDeviceInfoDto); | 87 | list.add(parserDeviceInfoDto); |
| 92 | } | 88 | } |
| 93 | } | 89 | } |
| @@ -95,7 +91,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -95,7 +91,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 95 | return list; | 91 | return list; |
| 96 | } | 92 | } |
| 97 | 93 | ||
| 98 | - private AnalysisDto analysisJsonData(Integer product_id,Topic topic, JsonObject jsonObjectData ) | 94 | + private AnalysisDto analysisJsonData(Integer time,String sensorNumber,Integer product_id,Topic topic, JsonObject jsonObjectData ) |
| 99 | { | 95 | { |
| 100 | if(null != jsonObjectData && jsonObjectData.size() !=0) | 96 | if(null != jsonObjectData && jsonObjectData.size() !=0) |
| 101 | { | 97 | { |
| @@ -108,7 +104,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -108,7 +104,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 108 | analysisDto.setThings_model_config(things_model_config); | 104 | analysisDto.setThings_model_config(things_model_config); |
| 109 | for (String key: jsonObjectData.keySet()) | 105 | for (String key: jsonObjectData.keySet()) |
| 110 | { | 106 | { |
| 111 | - IotThingsModel thingsModel = iotThingsModelService.getIotThingsModel(product_id, key); | 107 | + IotThingsModel thingsModel = SpringUtils.getBean(IotThingsModelService.class).getIotThingsModel(product_id, key); |
| 112 | 108 | ||
| 113 | JsonElement jsonElement = jsonObjectData.get(key); | 109 | JsonElement jsonElement = jsonObjectData.get(key); |
| 114 | if(null != jsonElement && !jsonElement.isJsonNull() ) | 110 | if(null != jsonElement && !jsonElement.isJsonNull() ) |
| @@ -145,7 +141,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -145,7 +141,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 145 | deviceSensorDataList = new ArrayList<>(); | 141 | deviceSensorDataList = new ArrayList<>(); |
| 146 | analysisDto.setDeviceSensorDataList(deviceSensorDataList); | 142 | analysisDto.setDeviceSensorDataList(deviceSensorDataList); |
| 147 | } | 143 | } |
| 148 | - deviceSensorDataList.add(getDeviceSensorData(topic,thingsModel,thingsModelItemBase)); | 144 | + deviceSensorDataList.add(getDeviceSensorData(time,sensorNumber,topic,thingsModel,thingsModelItemBase)); |
| 149 | } | 145 | } |
| 150 | } | 146 | } |
| 151 | } | 147 | } |
| @@ -155,9 +151,6 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -155,9 +151,6 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 155 | return null; | 151 | return null; |
| 156 | } | 152 | } |
| 157 | 153 | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | private ThingsModelItemBase getThingsModelItemBase(IotThingsModel thingsModel,JsonElement jsonElement) | 154 | private ThingsModelItemBase getThingsModelItemBase(IotThingsModel thingsModel,JsonElement jsonElement) |
| 162 | { | 155 | { |
| 163 | String data_type = thingsModel.getData_type().toUpperCase(); | 156 | String data_type = thingsModel.getData_type().toUpperCase(); |
| @@ -168,14 +161,19 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | @@ -168,14 +161,19 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat | ||
| 168 | return ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, jsonElement); | 161 | return ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, jsonElement); |
| 169 | } | 162 | } |
| 170 | 163 | ||
| 171 | - private DeviceSensorData getDeviceSensorData(Topic topic,IotThingsModel thingsModel,ThingsModelItemBase thingsModelItemBase) | 164 | + private DeviceSensorData getDeviceSensorData(Integer time,String sensorNumber,Topic topic,IotThingsModel thingsModel,ThingsModelItemBase thingsModelItemBase) |
| 172 | { | 165 | { |
| 173 | DeviceSensorData sensorData = new DeviceSensorData(); | 166 | DeviceSensorData sensorData = new DeviceSensorData(); |
| 174 | sensorData.setDataType(thingsModel.getIdentifier()); | 167 | sensorData.setDataType(thingsModel.getIdentifier()); |
| 175 | sensorData.setDataValue(thingsModelItemBase.getSaveView()); | 168 | sensorData.setDataValue(thingsModelItemBase.getSaveView()); |
| 176 | - sensorData.setCreatTime(com.zhonglai.luhui.device.analysis.comm.util.DateUtils.getNowTimeMilly()); | 169 | + sensorData.setCreatTime(time); |
| 177 | sensorData.setDeviceModel(topic.getUsername()); | 170 | sensorData.setDeviceModel(topic.getUsername()); |
| 178 | - sensorData.setDeviceInfoId(topic.getClientid()); | 171 | + if("0".equals(sensorNumber)) |
| 172 | + { | ||
| 173 | + sensorData.setDeviceInfoId(topic.getClientid()); | ||
| 174 | + }else { | ||
| 175 | + sensorData.setDeviceInfoId(topic.getClientid()+"_"+sensorNumber); | ||
| 176 | + } | ||
| 179 | return sensorData; | 177 | return sensorData; |
| 180 | 178 | ||
| 181 | } | 179 | } |
| 1 | package com.zhonglai.luhui.device.protocol.factory.service; | 1 | package com.zhonglai.luhui.device.protocol.factory.service; |
| 2 | 2 | ||
| 3 | import com.zhonglai.luhui.device.protocol.factory.ProtocolParserAndPurificationFactory; | 3 | import com.zhonglai.luhui.device.protocol.factory.ProtocolParserAndPurificationFactory; |
| 4 | +import com.zhonglai.luhui.device.protocol.factory.control.ControlFactory; | ||
| 4 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; | 5 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; |
| 5 | import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; | 6 | import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; |
| 7 | +import com.zhonglai.luhui.device.protocol.factory.sync.ProtocolSyncFactory; | ||
| 6 | import org.slf4j.Logger; | 8 | import org.slf4j.Logger; |
| 7 | import org.slf4j.LoggerFactory; | 9 | import org.slf4j.LoggerFactory; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| @@ -35,10 +37,11 @@ public abstract class BaseCallback<T> { | @@ -35,10 +37,11 @@ public abstract class BaseCallback<T> { | ||
| 35 | @Autowired | 37 | @Autowired |
| 36 | protected PersistenceDBService persistenceDBService; | 38 | protected PersistenceDBService persistenceDBService; |
| 37 | 39 | ||
| 40 | + | ||
| 38 | /** | 41 | /** |
| 39 | * 数据处理的工作流 | 42 | * 数据处理的工作流 |
| 40 | */ | 43 | */ |
| 41 | - protected void messageArrived(String imei,String s,T payload) throws ClassNotFoundException { | 44 | + protected void messageArrived(String imei,String s,T payload) throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
| 42 | //判断网关是否存在 | 45 | //判断网关是否存在 |
| 43 | ParserDeviceHostDto oldparserDeviceHostDto = persistenceDBService.getOldParserDeviceHostDto(imei); | 46 | ParserDeviceHostDto oldparserDeviceHostDto = persistenceDBService.getOldParserDeviceHostDto(imei); |
| 44 | if(null == oldparserDeviceHostDto) | 47 | if(null == oldparserDeviceHostDto) |
| @@ -48,7 +51,7 @@ public abstract class BaseCallback<T> { | @@ -48,7 +51,7 @@ public abstract class BaseCallback<T> { | ||
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | //解析和清洗body | 53 | //解析和清洗body |
| 51 | - ProtocolPurificationModel protocolPurificationModel = protocolParserAndPurificationFactory.analysisAndPurification( oldparserDeviceHostDto.getProduct_id(),s,payload,oldparserDeviceHostDto.getAnalysis_clas(),oldparserDeviceHostDto.getPurification_clas()); | 54 | + ProtocolPurificationModel protocolPurificationModel = protocolParserAndPurificationFactory.analysisAndPurification( oldparserDeviceHostDto.getIotProduct(),s,payload); |
| 52 | 55 | ||
| 53 | //缓存更新 | 56 | //缓存更新 |
| 54 | int i = deviceCashUpService.upProtocolPurificationModel(protocolPurificationModel,oldparserDeviceHostDto); | 57 | int i = deviceCashUpService.upProtocolPurificationModel(protocolPurificationModel,oldparserDeviceHostDto); |
| @@ -59,5 +62,9 @@ public abstract class BaseCallback<T> { | @@ -59,5 +62,9 @@ public abstract class BaseCallback<T> { | ||
| 59 | //同步 | 62 | //同步 |
| 60 | persistenceDBService.syncDB(protocolPurificationModel); | 63 | persistenceDBService.syncDB(protocolPurificationModel); |
| 61 | 64 | ||
| 65 | + //返回 | ||
| 66 | + | ||
| 62 | } | 67 | } |
| 68 | + | ||
| 69 | + | ||
| 63 | } | 70 | } |
| @@ -6,8 +6,10 @@ import com.ruoyi.common.utils.StringUtils; | @@ -6,8 +6,10 @@ import com.ruoyi.common.utils.StringUtils; | ||
| 6 | import com.ruoyi.common.utils.spring.SpringUtils; | 6 | import com.ruoyi.common.utils.spring.SpringUtils; |
| 7 | import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; | 7 | import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; |
| 8 | import com.zhonglai.luhui.device.analysis.comm.dto.LogDeviceOperation; | 8 | import com.zhonglai.luhui.device.analysis.comm.dto.LogDeviceOperation; |
| 9 | +import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelDataTypeEnum; | ||
| 9 | import com.zhonglai.luhui.device.domain.IotDevice; | 10 | import com.zhonglai.luhui.device.domain.IotDevice; |
| 10 | import com.zhonglai.luhui.device.domain.IotProduct; | 11 | import com.zhonglai.luhui.device.domain.IotProduct; |
| 12 | +import com.zhonglai.luhui.device.domain.IotProtocolClass; | ||
| 11 | import com.zhonglai.luhui.device.domain.IotThingsModel; | 13 | import com.zhonglai.luhui.device.domain.IotThingsModel; |
| 12 | import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach; | 14 | import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach; |
| 13 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; | 15 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; |
| @@ -27,7 +29,7 @@ import java.util.Map; | @@ -27,7 +29,7 @@ import java.util.Map; | ||
| 27 | */ | 29 | */ |
| 28 | @Service | 30 | @Service |
| 29 | public class PersistenceDBService { | 31 | public class PersistenceDBService { |
| 30 | - | 32 | + private static Map<Integer, IotProtocolClass> iotProtocolClassMap = new HashMap<>(); |
| 31 | @Autowired | 33 | @Autowired |
| 32 | private DefaultDbService defaultDbService; | 34 | private DefaultDbService defaultDbService; |
| 33 | 35 | ||
| @@ -60,9 +62,9 @@ public class PersistenceDBService { | @@ -60,9 +62,9 @@ public class PersistenceDBService { | ||
| 60 | public void syncDB(ProtocolPurificationModel protocolPurificationModel) throws ClassNotFoundException { | 62 | public void syncDB(ProtocolPurificationModel protocolPurificationModel) throws ClassNotFoundException { |
| 61 | //主机 | 63 | //主机 |
| 62 | ParserDeviceHostDto parserDeviceHostDto = protocolPurificationModel.getParserDeviceHostDto(); | 64 | ParserDeviceHostDto parserDeviceHostDto = protocolPurificationModel.getParserDeviceHostDto(); |
| 63 | - if (parserDeviceHostDto.is_sync_db() && StringUtils.isNotEmpty(parserDeviceHostDto.getSync_db())) | 65 | + if (parserDeviceHostDto.getIotProduct().getIs_sync_db()==1 && StringUtils.isNotEmpty(parserDeviceHostDto.getIotProduct().getSync_db())) |
| 64 | { | 66 | { |
| 65 | - ProtocolSyncFactory protocolSyncFactory = getProtocolSyncService(parserDeviceHostDto.getSync_db()); | 67 | + ProtocolSyncFactory protocolSyncFactory = getProtocolSyncService(parserDeviceHostDto.getIotProduct().getSync_db()); |
| 66 | if(null != protocolSyncFactory) | 68 | if(null != protocolSyncFactory) |
| 67 | { | 69 | { |
| 68 | protocolSyncFactory.updateParserDeviceHostDto(parserDeviceHostDto); | 70 | protocolSyncFactory.updateParserDeviceHostDto(parserDeviceHostDto); |
| @@ -198,27 +200,29 @@ public class PersistenceDBService { | @@ -198,27 +200,29 @@ public class PersistenceDBService { | ||
| 198 | 200 | ||
| 199 | oldParserDeviceHostDto.setDevice_life(iotDevice.getDevice_life()); | 201 | oldParserDeviceHostDto.setDevice_life(iotDevice.getDevice_life()); |
| 200 | oldParserDeviceHostDto.setDevice_type(iotDevice.getMqtt_username()); | 202 | oldParserDeviceHostDto.setDevice_type(iotDevice.getMqtt_username()); |
| 201 | - oldParserDeviceHostDto.set_sync_db(iotProduct.getIs_sync_db()==1); | ||
| 202 | - oldParserDeviceHostDto.setSync_db(iotProduct.getSync_db()); | ||
| 203 | - oldParserDeviceHostDto.setProduct_id(iotDevice.getProduct_id()); | ||
| 204 | - | ||
| 205 | - oldParserDeviceHostDto.setAnalysis_clas(iotProduct.getAnalysis_clas()); | ||
| 206 | - oldParserDeviceHostDto.setPurification_clas(iotProduct.getPurification_clas()); | ||
| 207 | - //设置同步设备 | ||
| 208 | -// if("1".equals(iotProduct.getIs_sync_db()) && StringUtils.isNotEmpty(iotProduct.getSync_db())) | ||
| 209 | -// { | ||
| 210 | -// ParserDeviceHostDto parserDeviceHostDto = getSyncDeviceById(iotDevice.getClient_id(),iotProduct.getSync_db()); | ||
| 211 | -// if(null != parserDeviceHostDto) | ||
| 212 | -// { | ||
| 213 | -// if(StringUtils.isNull(parserDeviceHostDto.getDevice_life())) | ||
| 214 | -// { | ||
| 215 | -// parserDeviceHostDto.setDevice_life(iotDevice.getDevice_life()); | ||
| 216 | -// } | ||
| 217 | -// parserDeviceHostDto.setId(iotProduct.getSync_db()+"|"+parserDeviceHostDto.getId()); | ||
| 218 | -// DeviceCach.putDeviceHost(parserDeviceHostDto,parserDeviceHostDto.getDevice_life()); | ||
| 219 | -// } | ||
| 220 | -// } | 203 | + oldParserDeviceHostDto.setIotProduct(iotProduct); |
| 221 | } | 204 | } |
| 222 | return oldParserDeviceHostDto; | 205 | return oldParserDeviceHostDto; |
| 223 | } | 206 | } |
| 207 | + | ||
| 208 | + private IotProtocolClass getIotProtocolClass(Integer id) | ||
| 209 | + { | ||
| 210 | + if(!iotProtocolClassMap.containsKey(id)) | ||
| 211 | + { | ||
| 212 | + IotProtocolClass iotProtocolClass = defaultDbService.getIotProtocolClass(id); | ||
| 213 | + iotProtocolClass.setCase_model(null); | ||
| 214 | + iotProtocolClassMap.put(id,iotProtocolClass); | ||
| 215 | + } | ||
| 216 | + return iotProtocolClassMap.get(id); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + /** | ||
| 220 | + * 获取产品解析插件包类名 | ||
| 221 | + * @param id | ||
| 222 | + * @return | ||
| 223 | + */ | ||
| 224 | + public String getClassnameFromIotProtocolClassId(Integer id) | ||
| 225 | + { | ||
| 226 | + return getIotProtocolClass(id).getClassname(); | ||
| 227 | + } | ||
| 224 | } | 228 | } |
| @@ -8,10 +8,7 @@ import com.zhonglai.luhui.device.analysis.comm.dao.BaseDao; | @@ -8,10 +8,7 @@ import com.zhonglai.luhui.device.analysis.comm.dao.BaseDao; | ||
| 8 | import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; | 8 | import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; |
| 9 | import com.zhonglai.luhui.device.analysis.comm.dto.LogDeviceOperation; | 9 | import com.zhonglai.luhui.device.analysis.comm.dto.LogDeviceOperation; |
| 10 | import com.zhonglai.luhui.device.analysis.comm.dto.TableGenerateSqlEnum; | 10 | import com.zhonglai.luhui.device.analysis.comm.dto.TableGenerateSqlEnum; |
| 11 | -import com.zhonglai.luhui.device.domain.IotDevice; | ||
| 12 | -import com.zhonglai.luhui.device.domain.IotProduct; | ||
| 13 | -import com.zhonglai.luhui.device.domain.IotTerminal; | ||
| 14 | -import com.zhonglai.luhui.device.domain.IotThingsModel; | 11 | +import com.zhonglai.luhui.device.domain.*; |
| 15 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; | 12 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; |
| 16 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto; | 13 | import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto; |
| 17 | import org.springframework.stereotype.Service; | 14 | import org.springframework.stereotype.Service; |
| @@ -39,6 +36,11 @@ public class DefaultDbService { | @@ -39,6 +36,11 @@ public class DefaultDbService { | ||
| 39 | return rlist; | 36 | return rlist; |
| 40 | } | 37 | } |
| 41 | 38 | ||
| 39 | + public IotProtocolClass getIotProtocolClass(Integer id) | ||
| 40 | + { | ||
| 41 | + return (IotProtocolClass) baseDao.get(IotProtocolClass.class,"id="+id+"","`mqtt_broker`.`iot_protocol_class`"); | ||
| 42 | + } | ||
| 43 | + | ||
| 42 | public IotDevice getDeviceById(String id) | 44 | public IotDevice getDeviceById(String id) |
| 43 | { | 45 | { |
| 44 | return (IotDevice) baseDao.get(IotDevice.class,"client_id='"+id+"'","`mqtt_broker`.`iot_device`"); | 46 | return (IotDevice) baseDao.get(IotDevice.class,"client_id='"+id+"'","`mqtt_broker`.`iot_device`"); |
| @@ -67,25 +69,6 @@ public class DefaultDbService { | @@ -67,25 +69,6 @@ public class DefaultDbService { | ||
| 67 | } | 69 | } |
| 68 | 70 | ||
| 69 | 71 | ||
| 70 | - public ParserDeviceHostDto getParserDeviceHostDtoFromDb(String id) { | ||
| 71 | - IotDevice iotDevice = getDeviceById(id); | ||
| 72 | - ParserDeviceHostDto parserDeviceHostDto = new ParserDeviceHostDto(); | ||
| 73 | - parserDeviceHostDto.setId(id); | ||
| 74 | - if(StringUtils.isNotEmpty(iotDevice.getSummary())) | ||
| 75 | - { | ||
| 76 | - parserDeviceHostDto.setData(GsonConstructor.get().fromJson(iotDevice.getSummary(),JsonObject.class)); | ||
| 77 | - } | ||
| 78 | - parserDeviceHostDto.setUpdateTime(iotDevice.getData_update_time()); | ||
| 79 | - parserDeviceHostDto.setDevice_life(iotDevice.getDevice_life()); | ||
| 80 | - parserDeviceHostDto.setDevice_type(iotDevice.getMqtt_username()); | ||
| 81 | - | ||
| 82 | - IotProduct iotProduct = getIotProductById(iotDevice.getProduct_id()); | ||
| 83 | - | ||
| 84 | - parserDeviceHostDto.setSync_db(iotProduct.getSync_db()); | ||
| 85 | - parserDeviceHostDto.setProduct_id(iotDevice.getProduct_id()); | ||
| 86 | - return parserDeviceHostDto; | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | public ParserDeviceInfoDto getParserDeviceInfoDtoFromDb(String id,String name, ParserDeviceHostDto oldparserDeviceHostDto) { | 72 | public ParserDeviceInfoDto getParserDeviceInfoDtoFromDb(String id,String name, ParserDeviceHostDto oldparserDeviceHostDto) { |
| 90 | IotTerminal iotTerminal = (IotTerminal) baseDao.get(IotTerminal.class,id); | 73 | IotTerminal iotTerminal = (IotTerminal) baseDao.get(IotTerminal.class,id); |
| 91 | if(null == iotTerminal) | 74 | if(null == iotTerminal) |
| @@ -95,7 +78,7 @@ public class DefaultDbService { | @@ -95,7 +78,7 @@ public class DefaultDbService { | ||
| 95 | iotTerminal.setDevice_id(oldparserDeviceHostDto.getId()); | 78 | iotTerminal.setDevice_id(oldparserDeviceHostDto.getId()); |
| 96 | iotTerminal.setMqtt_username(oldparserDeviceHostDto.getDevice_type()); | 79 | iotTerminal.setMqtt_username(oldparserDeviceHostDto.getDevice_type()); |
| 97 | iotTerminal.setCreate_time(oldparserDeviceHostDto.getUpdateTime()); | 80 | iotTerminal.setCreate_time(oldparserDeviceHostDto.getUpdateTime()); |
| 98 | - iotTerminal.setProduct_id(oldparserDeviceHostDto.getProduct_id()); | 81 | + iotTerminal.setProduct_id(oldparserDeviceHostDto.getIotProduct().getId()); |
| 99 | if(StringUtils.isNotEmpty(name)) | 82 | if(StringUtils.isNotEmpty(name)) |
| 100 | { | 83 | { |
| 101 | iotTerminal.setName(name); | 84 | iotTerminal.setName(name); |
| 1 | +package com.zhonglai.luhui.device.protocol.factory.sync; | ||
| 2 | + | ||
| 3 | +import com.ruoyi.common.utils.StringUtils; | ||
| 4 | +import com.zhonglai.luhui.device.analysis.comm.dao.BaseDao; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData; | ||
| 6 | +import com.zhonglai.luhui.device.analysis.comm.dto.LogDeviceOperation; | ||
| 7 | +import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; | ||
| 8 | +import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto; | ||
| 9 | +import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel; | ||
| 10 | +import org.springframework.stereotype.Component; | ||
| 11 | +import org.springframework.stereotype.Service; | ||
| 12 | + | ||
| 13 | +import java.util.List; | ||
| 14 | + | ||
| 15 | +@Service | ||
| 16 | +public class DefaultProtocolSyncFactoryImpl implements ProtocolSyncFactory{ | ||
| 17 | + private BaseDao lsy_baseDao = new BaseDao(); | ||
| 18 | + @Override | ||
| 19 | + public void updateParserDeviceHostDto(ParserDeviceHostDto parserDeviceHostDto) { | ||
| 20 | + | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + @Override | ||
| 24 | + public void updateParserDeviceInfoDtoList(List<ParserDeviceInfoDto> parserDeviceInfoDtoList) { | ||
| 25 | + | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + @Override | ||
| 29 | + public void updateDeviceSensorDataList(List<DeviceSensorData> deviceSensorDataList) { | ||
| 30 | + | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + @Override | ||
| 34 | + public void updateLogDeviceOperationList(List<LogDeviceOperation> logDeviceOperationList) { | ||
| 35 | + | ||
| 36 | + } | ||
| 37 | +} |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | + <modelVersion>4.0.0</modelVersion> | ||
| 6 | + <parent> | ||
| 7 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | + <artifactId>lh-device-protocol-parser</artifactId> | ||
| 9 | + <version>1.0-SNAPSHOT</version> | ||
| 10 | + </parent> | ||
| 11 | + | ||
| 12 | + <artifactId>lh-device-uyu</artifactId> | ||
| 13 | + | ||
| 14 | + <properties> | ||
| 15 | + <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | + <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | + </properties> | ||
| 19 | + | ||
| 20 | + <dependencies> | ||
| 21 | + <dependency> | ||
| 22 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 23 | + <artifactId>lh-device-protocol-factory</artifactId> | ||
| 24 | + </dependency> | ||
| 25 | + </dependencies> | ||
| 26 | +</project> |
| 1 | +package com.zhonglai.luhui.device.protocol.uyu.analysis; | ||
| 2 | + | ||
| 3 | +import com.google.gson.JsonObject; | ||
| 4 | +import com.ruoyi.common.utils.ByteUtil; | ||
| 5 | +import com.zhonglai.luhui.device.analysis.comm.factory.Topic; | ||
| 6 | +import com.zhonglai.luhui.device.analysis.util.TopicUtil; | ||
| 7 | +import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory; | ||
| 8 | + | ||
| 9 | +public class UyuProtocolParserFactoryImpl implements ProtocolParserFactory<byte[]> { | ||
| 10 | + private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{imei}}/{{topicType}}"; | ||
| 11 | + @Override | ||
| 12 | + public Topic analysisTopic(String topicStr) { | ||
| 13 | + Topic topic = TopicUtil.initTopicFromModelStr(topicStr,topicModel); | ||
| 14 | + return topic; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + @Override | ||
| 18 | + public JsonObject analysisPayload(Topic topic, byte[] payload) { | ||
| 19 | + JsonObject jsonObject = new JsonObject(); | ||
| 20 | + int addres = 1; //应该用第3字节:地址(目前定为0x80),但是下位机没有用到 我们现在也不用就写死 | ||
| 21 | + JsonObject senserData = new JsonObject(); | ||
| 22 | + jsonObject.add(addres+"",senserData); | ||
| 23 | + | ||
| 24 | + byte b5 = payload[4]; | ||
| 25 | + int dangwei = ByteUtil.getBitsValue(b5,0,3); | ||
| 26 | + int kaiguan = ByteUtil.getBitValue(b5,7); | ||
| 27 | + | ||
| 28 | + int zhuanshu = new Long(ByteUtil.bytesToLongDESC(payload,5,2)).intValue(); | ||
| 29 | + int dianliu = new Long(ByteUtil.bytesToLongDESC(payload,7,2)).intValue(); | ||
| 30 | + int dianya = new Long(ByteUtil.bytesToLongDESC(payload,9,2)).intValue(); | ||
| 31 | + int gonglv = new Long(ByteUtil.bytesToLongDESC(payload,11,2)).intValue(); | ||
| 32 | + | ||
| 33 | + //第14字节:故障码 (转成2进制对应下表) | ||
| 34 | + byte b14 = payload[13]; | ||
| 35 | + int qianyabaoh = ByteUtil.getBitValue(b14,0); | ||
| 36 | + int duanlubaohu = ByteUtil.getBitValue(b14,1); | ||
| 37 | + int qidongyichangbaohu = ByteUtil.getBitValue(b14,2); | ||
| 38 | + int guowenbaohu = ByteUtil.getBitValue(b14,2); | ||
| 39 | + int qiexiangbaohu = ByteUtil.getBitValue(b14,2); | ||
| 40 | + | ||
| 41 | + jsonObject.addProperty("106",dangwei); | ||
| 42 | + jsonObject.addProperty("3",kaiguan); | ||
| 43 | + jsonObject.addProperty("67",zhuanshu); | ||
| 44 | + jsonObject.addProperty("61",dianliu); | ||
| 45 | + jsonObject.addProperty("57",dianya); | ||
| 46 | + jsonObject.addProperty("59",gonglv); | ||
| 47 | + jsonObject.addProperty("107",qianyabaoh); | ||
| 48 | + jsonObject.addProperty("108",duanlubaohu); | ||
| 49 | + jsonObject.addProperty("109",qidongyichangbaohu); | ||
| 50 | + jsonObject.addProperty("110",guowenbaohu); | ||
| 51 | + jsonObject.addProperty("111",qiexiangbaohu); | ||
| 52 | + return jsonObject; | ||
| 53 | + } | ||
| 54 | +} |
| 1 | +package com.zhonglai.luhui.device.protocol.uyu.control; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.device.protocol.factory.control.ControlFactory; | ||
| 4 | +import org.apache.rocketmq.spring.annotation.MessageModel; | ||
| 5 | +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; | ||
| 6 | +import org.apache.rocketmq.spring.annotation.SelectorType; | ||
| 7 | +import org.springframework.stereotype.Component; | ||
| 8 | + | ||
| 9 | +@Component | ||
| 10 | +@RocketMQMessageListener(consumerGroup = "lh-device-uyu", topic = "control-device-uyu",selectorType = SelectorType.TAG,selectorExpression = "1",messageModel = MessageModel.BROADCASTING) | ||
| 11 | +public class UyuControlFactoryImpl extends ControlFactory { | ||
| 12 | + | ||
| 13 | + @Override | ||
| 14 | + protected String analysisMessageKey() { | ||
| 15 | + return null; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + protected Boolean transmitMessage(String str) { | ||
| 20 | + return null; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + @Override | ||
| 24 | + protected long getOperationTime() { | ||
| 25 | + return 10000; | ||
| 26 | + } | ||
| 27 | +} |
lh-modules/lh-device-protocol-parser/lh-device-uyu/src/main/resources/单路变频增氧机指令协议V3.0_240226.docx
0 → 100644
不能预览此文件类型
| @@ -16,12 +16,12 @@ import com.zhonglai.luhui.device.protocol.xinjie.analysis.topic.WriteData; | @@ -16,12 +16,12 @@ import com.zhonglai.luhui.device.protocol.xinjie.analysis.topic.WriteData; | ||
| 16 | import com.zhonglai.luhui.device.protocol.xinjie.analysis.topic.WriteReply; | 16 | import com.zhonglai.luhui.device.protocol.xinjie.analysis.topic.WriteReply; |
| 17 | import com.zhonglai.luhui.device.protocol.xinjie.dto.Variant; | 17 | import com.zhonglai.luhui.device.protocol.xinjie.dto.Variant; |
| 18 | import com.zhonglai.luhui.device.protocol.xinjie.dto.XinJieDto; | 18 | import com.zhonglai.luhui.device.protocol.xinjie.dto.XinJieDto; |
| 19 | +import org.springframework.stereotype.Component; | ||
| 19 | import org.springframework.stereotype.Service; | 20 | import org.springframework.stereotype.Service; |
| 20 | 21 | ||
| 21 | import java.util.ArrayList; | 22 | import java.util.ArrayList; |
| 22 | import java.util.List; | 23 | import java.util.List; |
| 23 | 24 | ||
| 24 | -@Service | ||
| 25 | public class ProtocolParserServiceImpl implements ProtocolParserFactory<byte[]> { | 25 | public class ProtocolParserServiceImpl implements ProtocolParserFactory<byte[]> { |
| 26 | private static final String topicModel = "/{{roleid}}/{{username}}/{{password}}/{{payloadtype}}/{{clientid}}/{{topicType}}"; | 26 | private static final String topicModel = "/{{roleid}}/{{username}}/{{password}}/{{payloadtype}}/{{clientid}}/{{topicType}}"; |
| 27 | 27 |
| @@ -17,7 +17,6 @@ import org.springframework.stereotype.Service; | @@ -17,7 +17,6 @@ import org.springframework.stereotype.Service; | ||
| 17 | 17 | ||
| 18 | import java.util.List; | 18 | import java.util.List; |
| 19 | 19 | ||
| 20 | -@Service | ||
| 21 | public class LsyProtocolSyncFactoryImpl implements ProtocolSyncFactory { | 20 | public class LsyProtocolSyncFactoryImpl implements ProtocolSyncFactory { |
| 22 | 21 | ||
| 23 | private BaseDao lsy_baseDao = new BaseDao(new LsyDBFactoryImp()); | 22 | private BaseDao lsy_baseDao = new BaseDao(new LsyDBFactoryImp()); |
| @@ -24,6 +24,8 @@ | @@ -24,6 +24,8 @@ | ||
| 24 | <module>lh-device-xinjie</module> | 24 | <module>lh-device-xinjie</module> |
| 25 | <module>lh-device-protocol-factory</module> | 25 | <module>lh-device-protocol-factory</module> |
| 26 | <module>lh-device-http-public</module> | 26 | <module>lh-device-http-public</module> |
| 27 | + <module>lh-device-modbus</module> | ||
| 28 | + <module>lh-device-uyu</module> | ||
| 27 | </modules> | 29 | </modules> |
| 28 | <description> | 30 | <description> |
| 29 | 设备协议解析工厂 | 31 | 设备协议解析工厂 |
| @@ -116,10 +116,6 @@ | @@ -116,10 +116,6 @@ | ||
| 116 | <groupId>com.zhonglai.luhui</groupId> | 116 | <groupId>com.zhonglai.luhui</groupId> |
| 117 | <artifactId>lh-device-protocol-factory</artifactId> | 117 | <artifactId>lh-device-protocol-factory</artifactId> |
| 118 | </dependency> | 118 | </dependency> |
| 119 | - <dependency> | ||
| 120 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 121 | - <artifactId>lh-device-http-public</artifactId> | ||
| 122 | - </dependency> | ||
| 123 | </dependencies> | 119 | </dependencies> |
| 124 | 120 | ||
| 125 | <build> | 121 | <build> |
| @@ -36,6 +36,7 @@ public class ShutdownManager | @@ -36,6 +36,7 @@ public class ShutdownManager | ||
| 36 | { | 36 | { |
| 37 | logger.info("====关闭后台任务任务线程池===="); | 37 | logger.info("====关闭后台任务任务线程池===="); |
| 38 | asyncManager.shutdown(); | 38 | asyncManager.shutdown(); |
| 39 | + logger.info("====关闭成功===="); | ||
| 39 | } | 40 | } |
| 40 | catch (Exception e) | 41 | catch (Exception e) |
| 41 | { | 42 | { |
| @@ -28,8 +28,8 @@ public class HttpCallback extends BaseCallback<JsonObject> { | @@ -28,8 +28,8 @@ public class HttpCallback extends BaseCallback<JsonObject> { | ||
| 28 | { | 28 | { |
| 29 | try { | 29 | try { |
| 30 | HttpCallback.super.messageArrived(imei,"/"+role+"/"+username+"/"+imei+"/Json",data); | 30 | HttpCallback.super.messageArrived(imei,"/"+role+"/"+username+"/"+imei+"/Json",data); |
| 31 | - } catch (ClassNotFoundException e) { | ||
| 32 | - throw new RuntimeException(e); | 31 | + } catch (Exception e) { |
| 32 | + log.error(imei+"解析数据失败",e); | ||
| 33 | } | 33 | } |
| 34 | // asyncManager.execute(new TimerTask() { | 34 | // asyncManager.execute(new TimerTask() { |
| 35 | // @Override | 35 | // @Override |
| @@ -141,10 +141,6 @@ | @@ -141,10 +141,6 @@ | ||
| 141 | </dependency> | 141 | </dependency> |
| 142 | 142 | ||
| 143 | <dependency> | 143 | <dependency> |
| 144 | - <groupId>org.apache.rocketmq</groupId> | ||
| 145 | - <artifactId>rocketmq-spring-boot-starter</artifactId> | ||
| 146 | - </dependency> | ||
| 147 | - <dependency> | ||
| 148 | <groupId>com.zhonglai.luhui</groupId> | 144 | <groupId>com.zhonglai.luhui</groupId> |
| 149 | <artifactId>lh-jar-device-service</artifactId> | 145 | <artifactId>lh-jar-device-service</artifactId> |
| 150 | </dependency> | 146 | </dependency> |
| @@ -152,10 +148,15 @@ | @@ -152,10 +148,15 @@ | ||
| 152 | <groupId>com.zhonglai.luhui</groupId> | 148 | <groupId>com.zhonglai.luhui</groupId> |
| 153 | <artifactId>lh-device-protocol-factory</artifactId> | 149 | <artifactId>lh-device-protocol-factory</artifactId> |
| 154 | </dependency> | 150 | </dependency> |
| 151 | +<!-- <dependency>--> | ||
| 152 | +<!-- <groupId>com.zhonglai.luhui</groupId>--> | ||
| 153 | +<!-- <artifactId>lh-device-xinjie</artifactId>--> | ||
| 154 | +<!-- </dependency>--> | ||
| 155 | <dependency> | 155 | <dependency> |
| 156 | <groupId>com.zhonglai.luhui</groupId> | 156 | <groupId>com.zhonglai.luhui</groupId> |
| 157 | - <artifactId>lh-device-xinjie</artifactId> | 157 | + <artifactId>lh-device-modbus</artifactId> |
| 158 | </dependency> | 158 | </dependency> |
| 159 | + | ||
| 159 | </dependencies> | 160 | </dependencies> |
| 160 | 161 | ||
| 161 | <build> | 162 | <build> |
| @@ -31,8 +31,23 @@ public class MqttServiceListenApplication { | @@ -31,8 +31,23 @@ public class MqttServiceListenApplication { | ||
| 31 | 31 | ||
| 32 | public static void main(String[] args) { | 32 | public static void main(String[] args) { |
| 33 | log.info("启动服务"); | 33 | log.info("启动服务"); |
| 34 | + System.setProperty("RunInIDEA",checkRunInIDEA()); | ||
| 35 | + | ||
| 34 | SpringApplicationBuilder builder = new SpringApplicationBuilder(MqttServiceListenApplication.class); | 36 | SpringApplicationBuilder builder = new SpringApplicationBuilder(MqttServiceListenApplication.class); |
| 35 | builder.run( args); | 37 | builder.run( args); |
| 36 | } | 38 | } |
| 37 | 39 | ||
| 40 | + /** | ||
| 41 | + * 判断是否是idea里面启动 | ||
| 42 | + * @return true:是 false:否 | ||
| 43 | + */ | ||
| 44 | + private static String checkRunInIDEA() { | ||
| 45 | + try { | ||
| 46 | + Class.forName("com.intellij.rt.execution.application.AppMainV2"); | ||
| 47 | + return "1"; | ||
| 48 | + } catch (ClassNotFoundException ignored) { | ||
| 49 | + return null; | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + | ||
| 38 | } | 53 | } |
| 1 | package com.zhonglai.luhui.mqtt.service.proxy.comm.service; | 1 | package com.zhonglai.luhui.mqtt.service.proxy.comm.service; |
| 2 | 2 | ||
| 3 | +import com.ruoyi.common.utils.StringUtils; | ||
| 3 | import com.zhonglai.luhui.device.analysis.comm.config.SysParameter; | 4 | import com.zhonglai.luhui.device.analysis.comm.config.SysParameter; |
| 4 | import com.zhonglai.luhui.device.protocol.factory.service.impl.DefaultDbService; | 5 | import com.zhonglai.luhui.device.protocol.factory.service.impl.DefaultDbService; |
| 5 | import org.eclipse.paho.client.mqttv3.MqttClient; | 6 | import org.eclipse.paho.client.mqttv3.MqttClient; |
| @@ -13,6 +14,8 @@ import org.springframework.beans.factory.annotation.Value; | @@ -13,6 +14,8 @@ import org.springframework.beans.factory.annotation.Value; | ||
| 13 | import org.springframework.stereotype.Service; | 14 | import org.springframework.stereotype.Service; |
| 14 | 15 | ||
| 15 | import javax.annotation.PostConstruct; | 16 | import javax.annotation.PostConstruct; |
| 17 | +import javax.annotation.PreDestroy; | ||
| 18 | +import java.util.ArrayList; | ||
| 16 | import java.util.List; | 19 | import java.util.List; |
| 17 | import java.util.concurrent.ExecutorService; | 20 | import java.util.concurrent.ExecutorService; |
| 18 | import java.util.concurrent.LinkedBlockingQueue; | 21 | import java.util.concurrent.LinkedBlockingQueue; |
| @@ -34,13 +37,6 @@ public class TerminalService { | @@ -34,13 +37,6 @@ public class TerminalService { | ||
| 34 | @Autowired | 37 | @Autowired |
| 35 | private SysParameter sysParameter; | 38 | private SysParameter sysParameter; |
| 36 | 39 | ||
| 37 | - | ||
| 38 | - //业务处理异步线程池,线程池参数可以根据您的业务特点调整,或者您也可以用其他异步方式处理接收到的消息。 | ||
| 39 | - private final static ExecutorService executorService = new ThreadPoolExecutor( | ||
| 40 | - Runtime.getRuntime().availableProcessors(), | ||
| 41 | - Runtime.getRuntime().availableProcessors() * 2, 60, TimeUnit.SECONDS, | ||
| 42 | - new LinkedBlockingQueue<>(50000)); | ||
| 43 | - | ||
| 44 | @Value("${mqtt.broker}") | 40 | @Value("${mqtt.broker}") |
| 45 | private String broker; | 41 | private String broker; |
| 46 | @Value("${mqtt.clientId}") | 42 | @Value("${mqtt.clientId}") |
| @@ -49,6 +45,8 @@ public class TerminalService { | @@ -49,6 +45,8 @@ public class TerminalService { | ||
| 49 | private String username; | 45 | private String username; |
| 50 | @Value("${mqtt.password}") | 46 | @Value("${mqtt.password}") |
| 51 | private String password; | 47 | private String password; |
| 48 | + @Value("${mqtt.textTopic:null}") | ||
| 49 | + private String textTopic; | ||
| 52 | 50 | ||
| 53 | private MqttClient mqttclient; | 51 | private MqttClient mqttclient; |
| 54 | 52 | ||
| @@ -84,6 +82,12 @@ public class TerminalService { | @@ -84,6 +82,12 @@ public class TerminalService { | ||
| 84 | 82 | ||
| 85 | public List<String> getCompletionTopics() | 83 | public List<String> getCompletionTopics() |
| 86 | { | 84 | { |
| 85 | + if ("1".equals(System.getProperty("RunInIDEA")) && StringUtils.isNotEmpty(textTopic)) | ||
| 86 | + { | ||
| 87 | + List<String> list = new ArrayList<>(); | ||
| 88 | + list.add(textTopic); | ||
| 89 | + return list; | ||
| 90 | + } | ||
| 87 | return dbService.getTopicFromRole(); | 91 | return dbService.getTopicFromRole(); |
| 88 | } | 92 | } |
| 89 | 93 | ||
| @@ -99,4 +103,13 @@ public class TerminalService { | @@ -99,4 +103,13 @@ public class TerminalService { | ||
| 99 | log.info("-----------订阅{}成功--------------------",topics); | 103 | log.info("-----------订阅{}成功--------------------",topics); |
| 100 | 104 | ||
| 101 | } | 105 | } |
| 106 | + | ||
| 107 | + @PreDestroy | ||
| 108 | + public void stop() throws MqttException { | ||
| 109 | + if(null != mqttclient) | ||
| 110 | + { | ||
| 111 | + mqttclient.disconnect(); | ||
| 112 | + mqttclient.close(); | ||
| 113 | + } | ||
| 114 | + } | ||
| 102 | } | 115 | } |
| 1 | ## /{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/ 由代理服务器做权限控制,终端不需要传递 | 1 | ## /{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/ 由代理服务器做权限控制,终端不需要传递 |
| 2 | 2 | ||
| 3 | ## 终端订阅 | 3 | ## 终端订阅 |
| 4 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/PUT/+ 写数据,需要返回执行结果 | ||
| 5 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/GET_REQ/+ 获取数据的返回结果 | ||
| 6 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/READ/+ 读数据,需要返回执行结果 | 4 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/PUT/+ 写数据,需要返回执行结果 |
| 5 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/GET_REQ/+ 获取数据的返回结果 | ||
| 6 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/READ/+ 读数据,需要返回执行结果 | ||
| 7 | 7 | ||
| 8 | ## 终端发布 | 8 | ## 终端发布 |
| 9 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/ALL_POST/+ 全量上报数据,不需要返回 | ||
| 10 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/ADD_POST/+ 增量上报数据,不需要返回 | ||
| 11 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/PUT_REQ/+ 写数据的执行结果 | ||
| 12 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/GET/+ 获取数据 | ||
| 13 | -/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/READ_REQ/+ 读数据的执行结果 | 9 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/ALL_POST/+ 全量上报数据,不需要返回 |
| 10 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/ADD_POST/+ 增量上报数据,不需要返回 | ||
| 11 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}//PUT_REQ/+ 写数据的执行结果 | ||
| 12 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/GET/+ 获取数据 | ||
| 13 | +/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/READ_REQ/+ 读数据的执行结果 | ||
| 14 | 14 | ||
| 15 | ##代理通知 | 15 | ##代理通知 |
| 16 | /${{roleid}}/${{username}}/${{clientid}}/online 上下线通知 | 16 | /${{roleid}}/${{username}}/${{clientid}}/online 上下线通知 |
| @@ -12,6 +12,7 @@ import org.springframework.context.annotation.ComponentScan; | @@ -12,6 +12,7 @@ import org.springframework.context.annotation.ComponentScan; | ||
| 12 | 12 | ||
| 13 | @ComponentScan(basePackages = { | 13 | @ComponentScan(basePackages = { |
| 14 | "com.zhonglai.luhui.device.analysis", | 14 | "com.zhonglai.luhui.device.analysis", |
| 15 | + "com.zhonglai.luhui.redis.service", | ||
| 15 | "com.zhonglai.luhui.mqtt.service", | 16 | "com.zhonglai.luhui.mqtt.service", |
| 16 | "com.zhonglai.luhui.mqtt.comm.service", | 17 | "com.zhonglai.luhui.mqtt.comm.service", |
| 17 | "com.zhonglai.luhui.mqtt.comm.rocketMq", | 18 | "com.zhonglai.luhui.mqtt.comm.rocketMq", |
lh-modules/lh-mqtt-service/src/main/java/com/zhonglai/luhui/mqtt/comm/service/MqttDeviceService.java
| @@ -147,7 +147,7 @@ public class MqttDeviceService extends DeviceService{ | @@ -147,7 +147,7 @@ public class MqttDeviceService extends DeviceService{ | ||
| 147 | ThingsModelItemBase thingsModelItemBase = ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel,gsonobject.get(skey)); | 147 | ThingsModelItemBase thingsModelItemBase = ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel,gsonobject.get(skey)); |
| 148 | 148 | ||
| 149 | jsonObject.put(skey,thingsModelItemBase.getCmdView(object)); | 149 | jsonObject.put(skey,thingsModelItemBase.getCmdView(object)); |
| 150 | - thingsModelItemBase.setValue(thingsModelItemBase.getCmdView(object)); | 150 | +// thingsModelItemBase.setValue(thingsModelItemBase.getCmdView(object)); |
| 151 | 151 | ||
| 152 | String id = clienid+"_"+key; | 152 | String id = clienid+"_"+key; |
| 153 | logDeviceOperationList.add(dviceLogService.newLogDeviceOperation(id,thingsModelItemBase.getSaveView(),null,"远程控制"+thingsModelItemBase.getName()+"为"+thingsModelItemBase.getView(),null)); | 153 | logDeviceOperationList.add(dviceLogService.newLogDeviceOperation(id,thingsModelItemBase.getSaveView(),null,"远程控制"+thingsModelItemBase.getName()+"为"+thingsModelItemBase.getView(),null)); |
| @@ -2,6 +2,9 @@ package com.zhonglai.luhui.mqtt.service.topic; | @@ -2,6 +2,9 @@ package com.zhonglai.luhui.mqtt.service.topic; | ||
| 2 | 2 | ||
| 3 | import com.alibaba.fastjson.JSON; | 3 | import com.alibaba.fastjson.JSON; |
| 4 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.google.gson.JsonElement; | ||
| 6 | +import com.ruoyi.common.utils.GsonConstructor; | ||
| 7 | +import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelItemBase; | ||
| 5 | import com.zhonglai.luhui.device.analysis.comm.service.BusinessDataUpdateService; | 8 | import com.zhonglai.luhui.device.analysis.comm.service.BusinessDataUpdateService; |
| 6 | import com.zhonglai.luhui.device.analysis.dto.topic.AddPostDto; | 9 | import com.zhonglai.luhui.device.analysis.dto.topic.AddPostDto; |
| 7 | import com.zhonglai.luhui.device.domain.IotThingsModel; | 10 | import com.zhonglai.luhui.device.domain.IotThingsModel; |
| @@ -71,11 +74,13 @@ public class ReadReqTopic implements BusinessAgreement<ReadReqDto> { | @@ -71,11 +74,13 @@ public class ReadReqTopic implements BusinessAgreement<ReadReqDto> { | ||
| 71 | { | 74 | { |
| 72 | data_type = ThingsModelDataTypeEnum.STRING.name(); | 75 | data_type = ThingsModelDataTypeEnum.STRING.name(); |
| 73 | } | 76 | } |
| 74 | - Class<ThingsModelBase> aClass = Enum.valueOf(ThingsModelDataTypeEnum.class,data_type).getaClass(); | ||
| 75 | - ThingsModelBase thingsModelBase = JSON.parseObject(thingsModel.getSpecs(),aClass); | ||
| 76 | - thingsModelBase.conversionThingsModel(thingsModel); | 77 | + ThingsModelBase thingsModelBase = ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class)); |
| 78 | +// Class<ThingsModelBase> aClass = Enum.valueOf(ThingsModelDataTypeEnum.class,data_type).getaClass(); | ||
| 79 | +// ThingsModelBase thingsModelBase = JSON.parseObject(thingsModel.getSpecs(),aClass); | ||
| 80 | +// thingsModelBase.conversionThingsModel(thingsModel); | ||
| 81 | +// | ||
| 82 | +// thingsModelBase.addValue(jsData.get(key)); | ||
| 77 | 83 | ||
| 78 | - thingsModelBase.addValue(jsData.get(key)); | ||
| 79 | jsData.put(key,thingsModelBase); | 84 | jsData.put(key,thingsModelBase); |
| 80 | } | 85 | } |
| 81 | vjsonObject.put(vkey,jsData); | 86 | vjsonObject.put(vkey,jsData); |
| @@ -49,7 +49,7 @@ mqtt: | @@ -49,7 +49,7 @@ mqtt: | ||
| 49 | mqtt_usernames: 6_WP | 49 | mqtt_usernames: 6_WP |
| 50 | #订阅的topic | 50 | #订阅的topic |
| 51 | topics: ADD_POST,ALL_POST,DB_TOPIC_DISTRIBUTE,GET/+,online,PUT_REQ/+,READ_REQ/+ | 51 | topics: ADD_POST,ALL_POST,DB_TOPIC_DISTRIBUTE,GET/+,online,PUT_REQ/+,READ_REQ/+ |
| 52 | - sub_clientid: '+' | 52 | + sub_clientid: '863482065281251' |
| 53 | topicconfig: "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}" | 53 | topicconfig: "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}" |
| 54 | top_return_map: '{"PUT":"PUT_REQ","READ":"READ_REQ"}' | 54 | top_return_map: '{"PUT":"PUT_REQ","READ":"READ_REQ"}' |
| 55 | username: sysuser | 55 | username: sysuser |
| @@ -31,6 +31,7 @@ | @@ -31,6 +31,7 @@ | ||
| 31 | <module>lh-http-service-proxy</module> | 31 | <module>lh-http-service-proxy</module> |
| 32 | <module>lh-mqtt-service-listen</module> | 32 | <module>lh-mqtt-service-listen</module> |
| 33 | <module>lh-device-protocol-parser</module> | 33 | <module>lh-device-protocol-parser</module> |
| 34 | + <module>lh-device-operation-service</module> | ||
| 34 | </modules> | 35 | </modules> |
| 35 | 36 | ||
| 36 | <properties> | 37 | <properties> |
| @@ -373,6 +373,12 @@ | @@ -373,6 +373,12 @@ | ||
| 373 | </dependency> | 373 | </dependency> |
| 374 | 374 | ||
| 375 | <dependency> | 375 | <dependency> |
| 376 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 377 | + <artifactId>lh-device-modbus</artifactId> | ||
| 378 | + <version>${ruoyi.version}</version> | ||
| 379 | + </dependency> | ||
| 380 | + | ||
| 381 | + <dependency> | ||
| 376 | <groupId>com.zhonglai</groupId> | 382 | <groupId>com.zhonglai</groupId> |
| 377 | <artifactId>ServiceDao</artifactId> | 383 | <artifactId>ServiceDao</artifactId> |
| 378 | <version>1.4.3</version> | 384 | <version>1.4.3</version> |
-
请 注册 或 登录 后发表评论