From 5d6dd5dbefbe0a7e70a05e244e87715144cb8cb3 Mon Sep 17 00:00:00 2001 From: ccongli <1441652193@qq.com> Date: Wed, 16 Aug 2023 16:04:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E9=87=87=E4=B8=9A=E5=8A=A1=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=BC=80=E5=8F=91v1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 44 +++- .../org/cjyx/chankosc/common/ComRunner.java | 34 ++- .../org/cjyx/chankosc/common/ComTest.java | 50 +++- .../org/cjyx/chankosc/common/DataParser.java | 15 +- .../chankosc/config/BeanConfiguration.java | 68 ++++++ .../config/createBeanConfiguration.java | 16 -- .../org/cjyx/chankosc/entity/ChConfig.java | 30 +++ .../cjyx/chankosc/mapper/ChConfigMapper.java | 9 + .../service/chconfig/ChConfigServiceImpl.java | 10 + .../service/chconfig/IChConfigService.java | 7 + .../{ => chdevice}/ChDeviceServiceImpl.java | 2 +- .../{ => chdevice}/IChDeviceService.java | 2 +- .../cjyx/chankosc/utils/Modbus4jUtils.java | 217 ++++++++++++++++++ .../chankosc/utils/Rs485DataListener.java | 14 +- src/main/resources/application.yml | 10 + .../chankosc/ChankoScApplicationTests.java | 54 ++++- 16 files changed, 546 insertions(+), 36 deletions(-) create mode 100644 src/main/java/org/cjyx/chankosc/config/BeanConfiguration.java delete mode 100644 src/main/java/org/cjyx/chankosc/config/createBeanConfiguration.java create mode 100644 src/main/java/org/cjyx/chankosc/entity/ChConfig.java create mode 100644 src/main/java/org/cjyx/chankosc/mapper/ChConfigMapper.java create mode 100644 src/main/java/org/cjyx/chankosc/service/chconfig/ChConfigServiceImpl.java create mode 100644 src/main/java/org/cjyx/chankosc/service/chconfig/IChConfigService.java rename src/main/java/org/cjyx/chankosc/service/{ => chdevice}/ChDeviceServiceImpl.java (88%) rename src/main/java/org/cjyx/chankosc/service/{ => chdevice}/IChDeviceService.java (79%) create mode 100644 src/main/java/org/cjyx/chankosc/utils/Modbus4jUtils.java diff --git a/pom.xml b/pom.xml index 9568d2b..5b24a41 100644 --- a/pom.xml +++ b/pom.xml @@ -38,13 +38,20 @@ test - + com.fazecast jSerialComm [2.0.0,3.0.0) + + + com.infiniteautomation + modbus4j + 3.1.0 + + com.alibaba fastjson @@ -62,8 +69,14 @@ commons-lang3 - + + + org.springframework.boot + spring-boot-starter-data-redis + + + @@ -108,4 +121,31 @@ + + + + + false + + + true + + ias-snapshots + Infinite Automation Snapshot Repository + https://maven.mangoautomation.net/repository/ias-snapshot/ + + + + true + + + false + + ias-releases + Infinite Automation Release Repository + https://maven.mangoautomation.net/repository/ias-release/ + + + + diff --git a/src/main/java/org/cjyx/chankosc/common/ComRunner.java b/src/main/java/org/cjyx/chankosc/common/ComRunner.java index 84a6798..b481e3a 100644 --- a/src/main/java/org/cjyx/chankosc/common/ComRunner.java +++ b/src/main/java/org/cjyx/chankosc/common/ComRunner.java @@ -2,14 +2,19 @@ package org.cjyx.chankosc.common; import com.fazecast.jSerialComm.SerialPort; import com.fazecast.jSerialComm.SerialPortDataListener; +import org.cjyx.chankosc.entity.ChConfig; import org.cjyx.chankosc.utils.Rs485DataListener; import org.cjyx.chankosc.utils.Rs485Handler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Component; import javax.annotation.PreDestroy; +import java.util.Map; @Component public class ComRunner implements ApplicationRunner { @@ -24,7 +29,17 @@ public class ComRunner implements ApplicationRunner { // private Rs485Handler handler2; @Autowired - SerialPortDataListener dataListener; + private SerialPortDataListener dataListener; + + @Autowired + private Map deviceConfigs; + + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private HashOperations hashOperations; + @Override public void run(ApplicationArguments args) throws Exception { @@ -35,8 +50,25 @@ public class ComRunner implements ApplicationRunner { handler.openPort(); // handler.listen(new Rs485DataListener(handler)); handler.listen(dataListener); + this.loadConfigs(); + } + + private void loadConfigs() { + redisTemplate.delete("sc0"); + redisTemplate.delete("sc1"); + redisTemplate.delete("sc2"); + redisTemplate.delete("sc3"); + + for(Map.Entry entry : deviceConfigs.entrySet()) { + ChConfig config = entry.getValue(); + String key = "sc" + config.getAddrNo(); + hashOperations.put(key, config.getChannel(), config.getDefData()); + } + + System.out.println("Redis配置初始化完成。。。"); } + @PreDestroy public void stop() { System.out.println("服务关闭,执行清理操作..."); diff --git a/src/main/java/org/cjyx/chankosc/common/ComTest.java b/src/main/java/org/cjyx/chankosc/common/ComTest.java index 08fa726..5201472 100644 --- a/src/main/java/org/cjyx/chankosc/common/ComTest.java +++ b/src/main/java/org/cjyx/chankosc/common/ComTest.java @@ -1,6 +1,7 @@ package org.cjyx.chankosc.common; import com.fazecast.jSerialComm.SerialPort; +import com.sun.istack.internal.NotNull; import org.cjyx.chankosc.utils.Rs485DataListener; import org.cjyx.chankosc.utils.Rs485Handler; @@ -10,15 +11,48 @@ import java.util.List; public class ComTest { public static void main(String[] args) { - SerialPort com3 = SerialPort.getCommPort("COM3"); - Rs485Handler handler = new Rs485Handler(); - handler.setSerialPort(com3); - handler.openPort(); - handler.listen(new Rs485DataListener(handler)); +// SerialPort com3 = SerialPort.getCommPort("COM3"); +// Rs485Handler handler = new Rs485Handler(); +// handler.setSerialPort(com3); +// handler.openPort(); +// handler.listen(new Rs485DataListener(handler)); // handler.closePort(); -// byte[] bytes = new byte[]{126, 0, 5, 0, 0, 5, 0, 126, 126, 1, 0, 1, 0, 2, 0, 126, 126, 0, 5, 0, 0, 5, 0, 126, 126, 1, 0, 1, 0, 2, 0, 126, 126, 0, 5, 0, 0, 5, 2, 0, 126, 126, 1, 0, 1, 0, 2, 0, 126, 126, 0, 5, 0, 0, 5, 0, 1,126}; -// DataParser parser = new DataParser(); -// parser.parseData(bytes); + byte[] bytes = new byte[]{ + 126, 0, 96, 98, 8, -54, 0, 126, + 126, 1, -125, 47, 0, -77, 0, 126, + 126, 2, 23, 124, 88, -19, 0, 126, + 126, 3, 0, -112, 0, -109, 0, 126 + }; + DataParser parser = new DataParser(); + parser.parseData(bytes); + } + + + /* + * byte转bit + * @description: 字节转比特 + * 1Byte=8bits + * @date: 2022/4/1 10:43 + * @param: b 字节 + * @return: java.lang.String 二进制字符串(8位0和1) + */ + public static String fromByte(byte b) { + return "" + (byte) ((b >> 7) & 0x1) + + (byte) ((b >> 6) & 0x1) + + (byte) ((b >> 5) & 0x1) + + (byte) ((b >> 4) & 0x1) + + (byte) ((b >> 3) & 0x1) + + (byte) ((b >> 2) & 0x1) + + (byte) ((b >> 1) & 0x1) + + (byte) ((b) & 0x1); + } + + public static String formatBinary(byte b) { + StringBuilder sb = new StringBuilder(); + for(int i = 7; i >=0; i--) { + sb.append((byte) ((b >> i) & 0x1)); + } + return sb.toString(); } } diff --git a/src/main/java/org/cjyx/chankosc/common/DataParser.java b/src/main/java/org/cjyx/chankosc/common/DataParser.java index 2249a0f..108a022 100644 --- a/src/main/java/org/cjyx/chankosc/common/DataParser.java +++ b/src/main/java/org/cjyx/chankosc/common/DataParser.java @@ -91,13 +91,18 @@ public class DataParser { return address + ch1_8 + ch9_16 + ch17_24; } - /** - * 格式化二进制字符串 + /* + * byte转bit + * @description: 字节转比特 + * 1Byte=8bits */ - private static String formatBinary(int number) { - String binary = String.format("%8s", Integer.toBinaryString(number)).replace(' ','0'); + public static String formatBinary(byte b) { + StringBuilder sb = new StringBuilder(); + for(int i = 7; i >=0; i--) { + sb.append((byte) ((b >> i) & 0x1)); + } // 小端模式(低字节在前、高字节在后) - return new StringBuilder(binary).reverse().toString(); + return sb.reverse().toString(); } diff --git a/src/main/java/org/cjyx/chankosc/config/BeanConfiguration.java b/src/main/java/org/cjyx/chankosc/config/BeanConfiguration.java new file mode 100644 index 0000000..c481576 --- /dev/null +++ b/src/main/java/org/cjyx/chankosc/config/BeanConfiguration.java @@ -0,0 +1,68 @@ +package org.cjyx.chankosc.config; + + +import org.cjyx.chankosc.entity.ChConfig; +import org.cjyx.chankosc.service.chconfig.IChConfigService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.*; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Configuration +public class BeanConfiguration { + + /** + * 传感器配置列表 + */ + @Bean + public Map deviceConfigs(IChConfigService chConfigService) { + List configs = chConfigService.list(); + Map configMap = configs.stream().collect( + Collectors.toMap(c -> (c.getAddrNo() + "-" + c.getChannel()), v -> v, (k1, k2) -> k1)); + return configMap; + } + + /** + * Redis配置 + */ + @Autowired + private RedisConnectionFactory factory; + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashValueSerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + redisTemplate.setConnectionFactory(factory); + return redisTemplate; + } + + @Bean + public HashOperations hashOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForHash(); + } + + @Bean + public ValueOperations valueOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForValue(); + } + + @Bean + public ListOperations listOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForList(); + } + + @Bean + public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForZSet(); + } + +} diff --git a/src/main/java/org/cjyx/chankosc/config/createBeanConfiguration.java b/src/main/java/org/cjyx/chankosc/config/createBeanConfiguration.java deleted file mode 100644 index 98f728c..0000000 --- a/src/main/java/org/cjyx/chankosc/config/createBeanConfiguration.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.cjyx.chankosc.config; - - -import org.cjyx.chankosc.utils.Rs485DataListener; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class createBeanConfiguration { - -// @Bean -// public Rs485DataListener dataListener() { -// return new Rs485DataListener(); -// } - -} diff --git a/src/main/java/org/cjyx/chankosc/entity/ChConfig.java b/src/main/java/org/cjyx/chankosc/entity/ChConfig.java new file mode 100644 index 0000000..088cf1d --- /dev/null +++ b/src/main/java/org/cjyx/chankosc/entity/ChConfig.java @@ -0,0 +1,30 @@ +package org.cjyx.chankosc.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +@TableName("config") +public class ChConfig { + + @TableId(type = IdType.AUTO) + private Integer id; + + @TableField("addrno") + private String addrNo; // 地址编号 + + private String channel; // 通道编号 + + @TableField("defdata") + private String defData; // 初始默认值 + + private int status; // 状态 + + @TableField("pointno") + private int pointNo; // 展台点位编号 + + private String comment; +} diff --git a/src/main/java/org/cjyx/chankosc/mapper/ChConfigMapper.java b/src/main/java/org/cjyx/chankosc/mapper/ChConfigMapper.java new file mode 100644 index 0000000..8f51332 --- /dev/null +++ b/src/main/java/org/cjyx/chankosc/mapper/ChConfigMapper.java @@ -0,0 +1,9 @@ +package org.cjyx.chankosc.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.cjyx.chankosc.entity.ChConfig; + +@Mapper +public interface ChConfigMapper extends BaseMapper { +} diff --git a/src/main/java/org/cjyx/chankosc/service/chconfig/ChConfigServiceImpl.java b/src/main/java/org/cjyx/chankosc/service/chconfig/ChConfigServiceImpl.java new file mode 100644 index 0000000..a3ad4b7 --- /dev/null +++ b/src/main/java/org/cjyx/chankosc/service/chconfig/ChConfigServiceImpl.java @@ -0,0 +1,10 @@ +package org.cjyx.chankosc.service.chconfig; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.cjyx.chankosc.entity.ChConfig; +import org.cjyx.chankosc.mapper.ChConfigMapper; +import org.springframework.stereotype.Service; + +@Service +public class ChConfigServiceImpl extends ServiceImpl implements IChConfigService { +} diff --git a/src/main/java/org/cjyx/chankosc/service/chconfig/IChConfigService.java b/src/main/java/org/cjyx/chankosc/service/chconfig/IChConfigService.java new file mode 100644 index 0000000..4f6071c --- /dev/null +++ b/src/main/java/org/cjyx/chankosc/service/chconfig/IChConfigService.java @@ -0,0 +1,7 @@ +package org.cjyx.chankosc.service.chconfig; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.cjyx.chankosc.entity.ChConfig; + +public interface IChConfigService extends IService { +} diff --git a/src/main/java/org/cjyx/chankosc/service/ChDeviceServiceImpl.java b/src/main/java/org/cjyx/chankosc/service/chdevice/ChDeviceServiceImpl.java similarity index 88% rename from src/main/java/org/cjyx/chankosc/service/ChDeviceServiceImpl.java rename to src/main/java/org/cjyx/chankosc/service/chdevice/ChDeviceServiceImpl.java index b58bf58..5d726cf 100644 --- a/src/main/java/org/cjyx/chankosc/service/ChDeviceServiceImpl.java +++ b/src/main/java/org/cjyx/chankosc/service/chdevice/ChDeviceServiceImpl.java @@ -1,4 +1,4 @@ -package org.cjyx.chankosc.service; +package org.cjyx.chankosc.service.chdevice; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.cjyx.chankosc.entity.ChDevice; diff --git a/src/main/java/org/cjyx/chankosc/service/IChDeviceService.java b/src/main/java/org/cjyx/chankosc/service/chdevice/IChDeviceService.java similarity index 79% rename from src/main/java/org/cjyx/chankosc/service/IChDeviceService.java rename to src/main/java/org/cjyx/chankosc/service/chdevice/IChDeviceService.java index 838c5b0..c868eb8 100644 --- a/src/main/java/org/cjyx/chankosc/service/IChDeviceService.java +++ b/src/main/java/org/cjyx/chankosc/service/chdevice/IChDeviceService.java @@ -1,4 +1,4 @@ -package org.cjyx.chankosc.service; +package org.cjyx.chankosc.service.chdevice; import com.baomidou.mybatisplus.extension.service.IService; import org.cjyx.chankosc.entity.ChDevice; diff --git a/src/main/java/org/cjyx/chankosc/utils/Modbus4jUtils.java b/src/main/java/org/cjyx/chankosc/utils/Modbus4jUtils.java new file mode 100644 index 0000000..06e599e --- /dev/null +++ b/src/main/java/org/cjyx/chankosc/utils/Modbus4jUtils.java @@ -0,0 +1,217 @@ +package org.cjyx.chankosc.utils; + +import com.serotonin.modbus4j.BatchRead; +import com.serotonin.modbus4j.BatchResults; +import com.serotonin.modbus4j.ModbusFactory; +import com.serotonin.modbus4j.ModbusMaster; +import com.serotonin.modbus4j.code.DataType; +import com.serotonin.modbus4j.exception.ErrorResponseException; +import com.serotonin.modbus4j.exception.ModbusInitException; +import com.serotonin.modbus4j.exception.ModbusTransportException; +import com.serotonin.modbus4j.ip.IpParameters; +import com.serotonin.modbus4j.locator.BaseLocator; + +/** + * modbus通讯工具类,采用modbus4j实现 + * + * @author lxq + * @dependencies modbus4j-3.0.3.jar + * @website https://github.com/infiniteautomation/modbus4j + */ +public class Modbus4jUtils { + /** + * 工厂。 + */ + static ModbusFactory modbusFactory; + static { + if (modbusFactory == null) { + modbusFactory = new ModbusFactory(); + } + } + + /** + * 获取master + * + * @return + * @throws ModbusInitException + */ + public static ModbusMaster getMaster() throws ModbusInitException { + IpParameters params = new IpParameters(); + params.setHost("localhost"); + params.setPort(502); + + // modbusFactory.createRtuMaster(wapper); //RTU 协议 + // modbusFactory.createUdpMaster(params);//UDP 协议 + // modbusFactory.createAsciiMaster(wrapper);//ASCII 协议 + ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议 + master.init(); + + return master; + } + + /** + * 读取[01 Coil Status 0x]类型 开关数据 + * + * @param slaveId + * slaveId + * @param offset + * 位置 + * @return 读取值 + * @throws ModbusTransportException + * 异常 + * @throws ErrorResponseException + * 异常 + * @throws ModbusInitException + * 异常 + */ + public static Boolean readCoilStatus(int slaveId, int offset) + throws ModbusTransportException, ErrorResponseException, ModbusInitException { + // 01 Coil Status + BaseLocator loc = BaseLocator.coilStatus(slaveId, offset); + Boolean value = getMaster().getValue(loc); + return value; + } + + /** + * 读取[02 Input Status 1x]类型 开关数据 + * + * @param slaveId + * @param offset + * @return + * @throws ModbusTransportException + * @throws ErrorResponseException + * @throws ModbusInitException + */ + public static Boolean readInputStatus(int slaveId, int offset) + throws ModbusTransportException, ErrorResponseException, ModbusInitException { + // 02 Input Status + BaseLocator loc = BaseLocator.inputStatus(slaveId, offset); + Boolean value = getMaster().getValue(loc); + return value; + } + + /** + * 读取[03 Holding Register类型 2x]模拟量数据 + * + * @param slaveId + * slave Id + * @param offset + * 位置 + * @param dataType + * 数据类型,来自com.serotonin.modbus4j.code.DataType + * @return + * @throws ModbusTransportException + * 异常 + * @throws ErrorResponseException + * 异常 + * @throws ModbusInitException + * 异常 + */ + public static Number readHoldingRegister(int slaveId, int offset, int dataType) + throws ModbusTransportException, ErrorResponseException, ModbusInitException { + // 03 Holding Register类型数据读取 + BaseLocator loc = BaseLocator.holdingRegister(slaveId, offset, dataType); + Number value = getMaster().getValue(loc); + return value; + } + + /** + * 读取[04 Input Registers 3x]类型 模拟量数据 + * + * @param slaveId + * slaveId + * @param offset + * 位置 + * @param dataType + * 数据类型,来自com.serotonin.modbus4j.code.DataType + * @return 返回结果 + * @throws ModbusTransportException + * 异常 + * @throws ErrorResponseException + * 异常 + * @throws ModbusInitException + * 异常 + */ + public static Number readInputRegisters(int slaveId, int offset, int dataType) + throws ModbusTransportException, ErrorResponseException, ModbusInitException { + // 04 Input Registers类型数据读取 + BaseLocator loc = BaseLocator.inputRegister(slaveId, offset, dataType); + Number value = getMaster().getValue(loc); + return value; + } + + /** + * 批量读取使用方法 + * + * @throws ModbusTransportException + * @throws ErrorResponseException + * @throws ModbusInitException + */ + public static void batchRead() throws ModbusTransportException, ErrorResponseException, ModbusInitException { + + BatchRead batch = new BatchRead(); + + batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.FOUR_BYTE_FLOAT)); + batch.addLocator(1, BaseLocator.inputStatus(1, 0)); + + ModbusMaster master = getMaster(); + + batch.setContiguousRequests(false); + BatchResults results = master.send(batch); + System.out.println(results.getValue(0)); + System.out.println(results.getValue(1)); + } + + /** + * 测试 + * + * @param args + */ + public static void main(String[] args) { + try { + // 01 读输出线圈 +// Boolean x00001 = readCoilStatus(1, 0); +// Boolean x00003 = readCoilStatus(1, 2); +// Boolean x00007 = readCoilStatus(1, 6); +// System.out.println("0x0001:" + x00001); +// System.out.println("0x0003:" + x00003); +// System.out.println("0x0007:" + x00007); +// +// // 02 读输入线圈 +// Boolean x10001 = readInputStatus(1, 0); +// Boolean x10003 = readInputStatus(1, 2); +// Boolean x10007 = readInputStatus(1, 6); +// System.out.println("1x0001:" + x10001); +// System.out.println("1x0003:" + x10003); +// System.out.println("1x0007:" + x10007); +// +// // 03 读保持寄存器 +// Number x40002 = readHoldingRegister(1, 1, DataType.FOUR_BYTE_INT_SIGNED); +// Number x40005 = readHoldingRegister(1, 4, DataType.EIGHT_BYTE_INT_SIGNED); +// Number x40010 = readHoldingRegister(1, 9, DataType.TWO_BYTE_INT_SIGNED); +// System.out.println("4x0002:" + x40002); +// System.out.println("4x0005:" + x40005); +// System.out.println("4x0010:" + x40010); +// +// // 04 读输入寄存器 +// Number x30001 = readInputRegisters(1, 0, DataType.EIGHT_BYTE_FLOAT); +// Number x30005 = readInputRegisters(1, 4, DataType.TWO_BYTE_INT_UNSIGNED); +// Number x30006 = readInputRegisters(1, 5, DataType.TWO_BYTE_INT_SIGNED); +// Number x30008 = readInputRegisters(1, 7, DataType.FOUR_BYTE_FLOAT); +// System.out.println("x30001:" + x30001); +// System.out.println("x30005:" + x30005); +// System.out.println("x30006:" + x30006); +// System.out.println("x30008:" + x30008); + + // 读二进制位 + Number xl30001 = readInputRegisters(1, 0, DataType.EIGHT_BYTE_FLOAT); + System.out.println("xl30001: " + xl30001); + +// // 批量读取 +// batchRead(); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/org/cjyx/chankosc/utils/Rs485DataListener.java b/src/main/java/org/cjyx/chankosc/utils/Rs485DataListener.java index 5fe1dc9..7442bad 100644 --- a/src/main/java/org/cjyx/chankosc/utils/Rs485DataListener.java +++ b/src/main/java/org/cjyx/chankosc/utils/Rs485DataListener.java @@ -5,8 +5,9 @@ import com.fazecast.jSerialComm.SerialPortDataListener; import com.fazecast.jSerialComm.SerialPortEvent; import org.cjyx.chankosc.common.DataParser; import org.cjyx.chankosc.entity.ChDevice; -import org.cjyx.chankosc.service.IChDeviceService; +import org.cjyx.chankosc.service.chdevice.IChDeviceService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.HashOperations; import org.springframework.stereotype.Component; import java.util.Collections; @@ -27,6 +28,9 @@ public class Rs485DataListener implements SerialPortDataListener { @Autowired private IChDeviceService chDeviceService; + @Autowired + private HashOperations hashOperations; + // 注册要监听的事件类型 @Override public int getListeningEvents() { @@ -52,7 +56,15 @@ public class Rs485DataListener implements SerialPortDataListener { byte[] AllBytes = handler.readData(); List chDevices = DataParser.parseData(AllBytes); Collections.reverse(chDevices); + // mysql chDeviceService.saveBatch(chDevices); + // redis + for(ChDevice chdevice : chDevices) { + String addrNo = "sc" + chdevice.getAddress(); + String channel = chdevice.getChannel(); + String data = chdevice.getData(); + hashOperations.put(addrNo, channel, data); + } } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 87d036f..4c0d0af 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,6 +9,16 @@ spring: password: 123456 url: jdbc:mysql://localhost:3306/chankosc?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver + redis: + host: localhost + port: 6379 + client-type: lettuce + timeout: 20000 + lettuce: + max-idle: 6 #最大空闲数 + max-active: 10 #最大连接数 + min-idle: 2 #最小空闲数 + # mybatis-plus配置 mybatis-plus: diff --git a/src/test/java/org/cjyx/chankosc/ChankoScApplicationTests.java b/src/test/java/org/cjyx/chankosc/ChankoScApplicationTests.java index f082245..007cfc4 100644 --- a/src/test/java/org/cjyx/chankosc/ChankoScApplicationTests.java +++ b/src/test/java/org/cjyx/chankosc/ChankoScApplicationTests.java @@ -1,10 +1,20 @@ package org.cjyx.chankosc; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import org.cjyx.chankosc.entity.ChConfig; import org.cjyx.chankosc.entity.ChDevice; -import org.cjyx.chankosc.service.IChDeviceService; +import org.cjyx.chankosc.service.chconfig.IChConfigService; +import org.cjyx.chankosc.service.chdevice.IChDeviceService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; + +import java.sql.Wrapper; +import java.util.List; +import java.util.Map; @SpringBootTest class ChankoScApplicationTests { @@ -12,6 +22,12 @@ class ChankoScApplicationTests { @Autowired private IChDeviceService chDeviceService; + @Autowired + private IChConfigService chConfigService; + + @Autowired + RedisTemplate redisTemplate; + @Test void contextLoads() { ChDevice chDevice = new ChDevice(); @@ -22,4 +38,40 @@ class ChankoScApplicationTests { chDeviceService.save(chDevice); } + @Autowired + private ValueOperations valueOperations; + + @Test + void test01() { +// ValueOperations vps = redisTemplate.opsForValue(); +// vps.set("testKey", "testValue"); +// String str = (String) vps.get("testKey"); + valueOperations.set("sss","123456"); + String str = valueOperations.get("sss"); + System.out.println(str); + } + + @Test + void test02 () { + HashOperations hps = redisTemplate.opsForHash(); + hps.put("testKeyH", "testField", "testValue"); + String str = (String) hps.get("testKeyH", "testField"); + System.out.println(str); + } + + @Autowired + private Map deviceConfigs; + + + + @Test + void test03() { +// List configs = chConfigService.list(); +// QueryWrapper wrapper = new QueryWrapper<>(); +// wrapper.eq("status",3); +// List configs = chConfigService.list(wrapper); +// System.out.println(configs); + System.out.println(deviceConfigs); + } + }