From 1a1dd17436547d46795e6d3fa5147a5a01b16f56 Mon Sep 17 00:00:00 2001
From: yj <913944315@qq.com>
Date: Wed, 3 Jul 2024 15:30:32 +0800
Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E5=B1=B1=E7=AB=99=E5=B0=9D=E8=AF=95?=
=?UTF-8?q?=E7=94=A8=E7=A8=8B=E5=BA=8F=E9=87=87=E9=9B=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
device_gather/pom.xml | 169 +++
.../xr/device/DeviceGatherApplication.java | 19 +
.../common/configvalue/StaticPropProxy.java | 11 +
.../common/configvalue/StaticProperties.java | 26 +
.../xr/device/common/utils/CharsetKit.java | 85 ++
.../com/xr/device/common/utils/Convert.java | 1000 +++++++++++++++++
.../com/xr/device/common/utils/Files.java | 435 +++++++
.../xr/device/common/utils/ModbusUtils.java | 167 +++
.../device/common/utils/PythonExecutor.java | 205 ++++
.../xr/device/common/utils/SpringUtils.java | 151 +++
.../device/common/utils/StaticPropUtil.java | 26 +
.../xr/device/common/utils/StrFormatter.java | 90 ++
.../xr/device/common/utils/StringUtils.java | 607 ++++++++++
.../xr/device/common/utils/UploadUtil.java | 187 +++
.../device/common/utils/ValueFormatUtil.java | 80 ++
.../xr/device/model/entity/DeviceCamera.java | 126 +++
.../model/entity/FocalLengthConfig.java | 141 +++
.../xr/device/model/entity/MeterConfig.java | 183 +++
.../model/entity/MeterInitialization.java | 50 +
.../model/entity/MeterReadingRecord.java | 97 ++
.../com/xr/device/model/entity/MeterType.java | 86 ++
.../model/mapper/DeviceCameraMapper.java | 20 +
.../model/mapper/FocalLengthConfigMapper.java | 20 +
.../model/mapper/MeterConfigMapper.java | 24 +
.../mapper/MeterInitializationMapper.java | 20 +
.../mapper/MeterReadingRecordMapper.java | 20 +
.../device/model/mapper/MeterTypeMapper.java | 20 +
.../model/service/DeviceCameraService.java | 13 +
.../service/FocalLengthConfigService.java | 13 +
.../model/service/MeterConfigService.java | 17 +
.../service/MeterInitializationService.java | 13 +
.../service/MeterReadingRecordService.java | 13 +
.../model/service/MeterTypeService.java | 13 +
.../service/impl/DeviceCameraServiceImpl.java | 24 +
.../impl/FocalLengthConfigServiceImpl.java | 24 +
.../service/impl/MeterConfigServiceImpl.java | 30 +
.../impl/MeterInitializationServiceImpl.java | 24 +
.../impl/MeterReadingRecordServiceImpl.java | 24 +
.../service/impl/MeterTypeServiceImpl.java | 24 +
.../xr/device/schedule/GetMeterSchedule.java | 204 ++++
.../src/main/resources/application-dev.yml | 80 ++
.../src/main/resources/application-prod.yml | 80 ++
.../src/main/resources/application.yml | 82 ++
device_gather/src/main/resources/banner.txt | 23 +
device_gather/src/main/resources/logback.xml | 51 +
.../resources/mapper/DeviceCameraMapper.xml | 40 +
.../mapper/FocalLengthConfigMapper.xml | 43 +
.../resources/mapper/MeterConfigMapper.xml | 59 +
.../mapper/MeterInitializationMapper.xml | 20 +
.../mapper/MeterReadingRecordMapper.xml | 32 +
.../main/resources/mapper/MeterTypeMapper.xml | 30 +
51 files changed, 5041 insertions(+)
create mode 100644 device_gather/pom.xml
create mode 100644 device_gather/src/main/java/com/xr/device/DeviceGatherApplication.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/configvalue/StaticPropProxy.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/configvalue/StaticProperties.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/CharsetKit.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/Convert.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/Files.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/ModbusUtils.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/PythonExecutor.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/SpringUtils.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/StaticPropUtil.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/StrFormatter.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/StringUtils.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/UploadUtil.java
create mode 100644 device_gather/src/main/java/com/xr/device/common/utils/ValueFormatUtil.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/entity/DeviceCamera.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/entity/FocalLengthConfig.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/entity/MeterConfig.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/entity/MeterInitialization.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/entity/MeterReadingRecord.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/entity/MeterType.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/mapper/DeviceCameraMapper.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/mapper/FocalLengthConfigMapper.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/mapper/MeterConfigMapper.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/mapper/MeterInitializationMapper.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/mapper/MeterReadingRecordMapper.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/mapper/MeterTypeMapper.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/DeviceCameraService.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/FocalLengthConfigService.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/MeterConfigService.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/MeterInitializationService.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/MeterReadingRecordService.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/MeterTypeService.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/impl/DeviceCameraServiceImpl.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/impl/FocalLengthConfigServiceImpl.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/impl/MeterConfigServiceImpl.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/impl/MeterInitializationServiceImpl.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/impl/MeterReadingRecordServiceImpl.java
create mode 100644 device_gather/src/main/java/com/xr/device/model/service/impl/MeterTypeServiceImpl.java
create mode 100644 device_gather/src/main/java/com/xr/device/schedule/GetMeterSchedule.java
create mode 100644 device_gather/src/main/resources/application-dev.yml
create mode 100644 device_gather/src/main/resources/application-prod.yml
create mode 100644 device_gather/src/main/resources/application.yml
create mode 100644 device_gather/src/main/resources/banner.txt
create mode 100644 device_gather/src/main/resources/logback.xml
create mode 100644 device_gather/src/main/resources/mapper/DeviceCameraMapper.xml
create mode 100644 device_gather/src/main/resources/mapper/FocalLengthConfigMapper.xml
create mode 100644 device_gather/src/main/resources/mapper/MeterConfigMapper.xml
create mode 100644 device_gather/src/main/resources/mapper/MeterInitializationMapper.xml
create mode 100644 device_gather/src/main/resources/mapper/MeterReadingRecordMapper.xml
create mode 100644 device_gather/src/main/resources/mapper/MeterTypeMapper.xml
diff --git a/device_gather/pom.xml b/device_gather/pom.xml
new file mode 100644
index 0000000..7388ac8
--- /dev/null
+++ b/device_gather/pom.xml
@@ -0,0 +1,169 @@
+
+
+ 4.0.0
+
+ com.xr.device_net
+ device_car
+ 1.0.1
+
+ device_gather
+ ${project.artifactId}
+
+ 1.8
+
+
+
+
+ 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/
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-tomcat
+
+
+ org.freemarker
+ freemarker
+ 2.3.28
+
+
+ mysql
+ mysql-connector-java
+ 8.0.27
+
+
+ com.baomidou
+ mybatis-plus-boot-starter
+ 3.4.1
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-eureka-client
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
+
+ org.springframework.boot
+ spring-boot-starter-websocket
+
+
+ com.alibaba
+ fastjson
+ 1.2.78
+ compile
+
+
+ commons-discovery
+ commons-discovery
+ 0.2
+
+
+ commons-io
+ commons-io
+ 2.6
+
+
+ com.baomidou
+ mybatis-plus-generator
+ 3.4.1
+
+
+ com.baomidou
+ dynamic-datasource-spring-boot-starter
+ 3.4.1
+
+
+ io.github.java-native
+ jssc
+ 2.9.4
+
+
+ com.infiniteautomation
+ modbus4j
+ 3.0.4
+
+
+ org.rxtx
+ rxtx
+ 2.1.7
+
+
+ org.scream3r
+ jssc
+ 2.8.0
+
+
+ org.bouncycastle
+ bcprov-jdk15on
+ 1.59
+
+
+ com.xr
+ onvif-hk
+ 3.1.0
+
+
+
+
+
+
+ src/main/resources
+ false
+
+ **/*
+ **/*.fxml
+ **/fxml/*.fxml
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ 2.0.8.RELEASE
+
+
+
+ repackage
+
+
+
+
+
+
+
+
diff --git a/device_gather/src/main/java/com/xr/device/DeviceGatherApplication.java b/device_gather/src/main/java/com/xr/device/DeviceGatherApplication.java
new file mode 100644
index 0000000..26f8c70
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/DeviceGatherApplication.java
@@ -0,0 +1,19 @@
+package com.xr.device;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+@EnableScheduling//开启定时任务
+@SpringBootApplication
+@EnableDiscoveryClient
+@EnableFeignClients
+public class DeviceGatherApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DeviceGatherApplication.class, args);
+ }
+
+}
diff --git a/device_gather/src/main/java/com/xr/device/common/configvalue/StaticPropProxy.java b/device_gather/src/main/java/com/xr/device/common/configvalue/StaticPropProxy.java
new file mode 100644
index 0000000..5285d81
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/configvalue/StaticPropProxy.java
@@ -0,0 +1,11 @@
+package com.xr.device.common.configvalue;
+
+import com.xr.device.common.utils.StaticPropUtil;
+import org.springframework.stereotype.Component;
+
+@Component
+public class StaticPropProxy {
+ public StaticPropProxy(StaticProperties staticProperties){
+ StaticPropUtil.initDingDingProp(staticProperties);
+ }
+}
diff --git a/device_gather/src/main/java/com/xr/device/common/configvalue/StaticProperties.java b/device_gather/src/main/java/com/xr/device/common/configvalue/StaticProperties.java
new file mode 100644
index 0000000..d45a945
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/configvalue/StaticProperties.java
@@ -0,0 +1,26 @@
+package com.xr.device.common.configvalue;
+
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+@Data
+public class StaticProperties {
+
+ @Value("${upload.img.url}")
+ private String imgUrl;
+
+ @Value("${upload.img.path}")
+ private String imgPath;
+
+ @Value("${python.path}")
+ private String pythonPath;
+
+ @Value("${python.modelPath}")
+ private String modelPath;
+
+ @Value("${station.id}")
+ private Integer stationId;
+
+}
diff --git a/device_gather/src/main/java/com/xr/device/common/utils/CharsetKit.java b/device_gather/src/main/java/com/xr/device/common/utils/CharsetKit.java
new file mode 100644
index 0000000..f8fccbd
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/utils/CharsetKit.java
@@ -0,0 +1,85 @@
+package com.xr.device.common.utils;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 字符集工具类
+ *
+ * @author
+ */
+public class CharsetKit
+{
+ /** ISO-8859-1 */
+ public static final String ISO_8859_1 = "ISO-8859-1";
+ /** UTF-8 */
+ public static final String UTF_8 = "UTF-8";
+ /** GBK */
+ public static final String GBK = "GBK";
+
+ /** ISO-8859-1 */
+ public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1);
+ /** UTF-8 */
+ public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);
+ /** GBK */
+ public static final Charset CHARSET_GBK = Charset.forName(GBK);
+
+ /**
+ * 转换为Charset对象
+ *
+ * @param charset 字符集,为空则返回默认字符集
+ * @return Charset
+ */
+ public static Charset charset(String charset)
+ {
+ return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
+ }
+
+ /**
+ * 转换字符串的字符集编码
+ *
+ * @param source 字符串
+ * @param srcCharset 源字符集,默认ISO-8859-1
+ * @param destCharset 目标字符集,默认UTF-8
+ * @return 转换后的字符集
+ */
+ public static String convert(String source, String srcCharset, String destCharset)
+ {
+ return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
+ }
+
+ /**
+ * 转换字符串的字符集编码
+ *
+ * @param source 字符串
+ * @param srcCharset 源字符集,默认ISO-8859-1
+ * @param destCharset 目标字符集,默认UTF-8
+ * @return 转换后的字符集
+ */
+ public static String convert(String source, Charset srcCharset, Charset destCharset)
+ {
+ if (null == srcCharset)
+ {
+ srcCharset = StandardCharsets.ISO_8859_1;
+ }
+
+ if (null == destCharset)
+ {
+ destCharset = StandardCharsets.UTF_8;
+ }
+
+ if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
+ {
+ return source;
+ }
+ return new String(source.getBytes(srcCharset), destCharset);
+ }
+
+ /**
+ * @return 系统字符集编码
+ */
+ public static String systemCharset()
+ {
+ return Charset.defaultCharset().name();
+ }
+}
diff --git a/device_gather/src/main/java/com/xr/device/common/utils/Convert.java b/device_gather/src/main/java/com/xr/device/common/utils/Convert.java
new file mode 100644
index 0000000..3853bda
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/utils/Convert.java
@@ -0,0 +1,1000 @@
+package com.xr.device.common.utils;
+
+import org.apache.commons.lang3.ArrayUtils;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.text.NumberFormat;
+import java.util.Set;
+
+/**
+ * 类型转换器
+ *
+ * @author ruoyi
+ */
+public class Convert
+{
+ /**
+ * 转换为字符串
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static String toStr(Object value, String defaultValue)
+ {
+ if (null == value)
+ {
+ return defaultValue;
+ }
+ if (value instanceof String)
+ {
+ return (String) value;
+ }
+ return value.toString();
+ }
+
+ /**
+ * 转换为字符串
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static String toStr(Object value)
+ {
+ return toStr(value, null);
+ }
+
+ /**
+ * 转换为字符
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Character toChar(Object value, Character defaultValue)
+ {
+ if (null == value)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Character)
+ {
+ return (Character) value;
+ }
+
+ final String valueStr = toStr(value, null);
+ return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0);
+ }
+
+ /**
+ * 转换为字符
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Character toChar(Object value)
+ {
+ return toChar(value, null);
+ }
+
+ /**
+ * 转换为byte
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Byte toByte(Object value, Byte defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Byte)
+ {
+ return (Byte) value;
+ }
+ if (value instanceof Number)
+ {
+ return ((Number) value).byteValue();
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return Byte.parseByte(valueStr);
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为byte
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Byte toByte(Object value)
+ {
+ return toByte(value, null);
+ }
+
+ /**
+ * 转换为Short
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Short toShort(Object value, Short defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Short)
+ {
+ return (Short) value;
+ }
+ if (value instanceof Number)
+ {
+ return ((Number) value).shortValue();
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return Short.parseShort(valueStr.trim());
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为Short
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Short toShort(Object value)
+ {
+ return toShort(value, null);
+ }
+
+ /**
+ * 转换为Number
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Number toNumber(Object value, Number defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Number)
+ {
+ return (Number) value;
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return NumberFormat.getInstance().parse(valueStr);
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为Number
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Number toNumber(Object value)
+ {
+ return toNumber(value, null);
+ }
+
+ /**
+ * 转换为int
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Integer toInt(Object value, Integer defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Integer)
+ {
+ return (Integer) value;
+ }
+ if (value instanceof Number)
+ {
+ return ((Number) value).intValue();
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return Integer.parseInt(valueStr.trim());
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为int
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Integer toInt(Object value)
+ {
+ return toInt(value, null);
+ }
+
+ /**
+ * 转换为Integer数组
+ *
+ * @param str 被转换的值
+ * @return 结果
+ */
+ public static Integer[] toIntArray(String str)
+ {
+ return toIntArray(",", str);
+ }
+
+ /**
+ * 转换为Long数组
+ *
+ * @param str 被转换的值
+ * @return 结果
+ */
+ public static Long[] toLongArray(String str)
+ {
+ return toLongArray(",", str);
+ }
+
+ /**
+ * 转换为Integer数组
+ *
+ * @param split 分隔符
+ * @param split 被转换的值
+ * @return 结果
+ */
+ public static Integer[] toIntArray(String split, String str)
+ {
+ if (StringUtils.isEmpty(str))
+ {
+ return new Integer[] {};
+ }
+ String[] arr = str.split(split);
+ final Integer[] ints = new Integer[arr.length];
+ for (int i = 0; i < arr.length; i++)
+ {
+ final Integer v = toInt(arr[i], 0);
+ ints[i] = v;
+ }
+ return ints;
+ }
+
+ /**
+ * 转换为Long数组
+ *
+ * @param split 分隔符
+ * @param str 被转换的值
+ * @return 结果
+ */
+ public static Long[] toLongArray(String split, String str)
+ {
+ if (StringUtils.isEmpty(str))
+ {
+ return new Long[] {};
+ }
+ String[] arr = str.split(split);
+ final Long[] longs = new Long[arr.length];
+ for (int i = 0; i < arr.length; i++)
+ {
+ final Long v = toLong(arr[i], null);
+ longs[i] = v;
+ }
+ return longs;
+ }
+
+ /**
+ * 转换为String数组
+ *
+ * @param str 被转换的值
+ * @return 结果
+ */
+ public static String[] toStrArray(String str)
+ {
+ return toStrArray(",", str);
+ }
+
+ /**
+ * 转换为String数组
+ *
+ * @param split 分隔符
+ * @param split 被转换的值
+ * @return 结果
+ */
+ public static String[] toStrArray(String split, String str)
+ {
+ return str.split(split);
+ }
+
+ /**
+ * 转换为long
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Long toLong(Object value, Long defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Long)
+ {
+ return (Long) value;
+ }
+ if (value instanceof Number)
+ {
+ return ((Number) value).longValue();
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ // 支持科学计数法
+ return new BigDecimal(valueStr.trim()).longValue();
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为long
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Long toLong(Object value)
+ {
+ return toLong(value, null);
+ }
+
+ /**
+ * 转换为double
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Double toDouble(Object value, Double defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Double)
+ {
+ return (Double) value;
+ }
+ if (value instanceof Number)
+ {
+ return ((Number) value).doubleValue();
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ // 支持科学计数法
+ return new BigDecimal(valueStr.trim()).doubleValue();
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为double
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Double toDouble(Object value)
+ {
+ return toDouble(value, null);
+ }
+
+ /**
+ * 转换为Float
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Float toFloat(Object value, Float defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Float)
+ {
+ return (Float) value;
+ }
+ if (value instanceof Number)
+ {
+ return ((Number) value).floatValue();
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return Float.parseFloat(valueStr.trim());
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为Float
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Float toFloat(Object value)
+ {
+ return toFloat(value, null);
+ }
+
+ /**
+ * 转换为boolean
+ * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static Boolean toBool(Object value, Boolean defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof Boolean)
+ {
+ return (Boolean) value;
+ }
+ String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ valueStr = valueStr.trim().toLowerCase();
+ switch (valueStr)
+ {
+ case "true":
+ case "yes":
+ case "ok":
+ case "1":
+ return true;
+ case "false":
+ case "no":
+ case "0":
+ return false;
+ default:
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为boolean
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static Boolean toBool(Object value)
+ {
+ return toBool(value, null);
+ }
+
+ /**
+ * 转换为Enum对象
+ * 如果给定的值为空,或者转换失败,返回默认值
+ *
+ * @param clazz Enum的Class
+ * @param value 值
+ * @param defaultValue 默认值
+ * @return Enum
+ */
+ public static > E toEnum(Class clazz, Object value, E defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (clazz.isAssignableFrom(value.getClass()))
+ {
+ @SuppressWarnings("unchecked")
+ E myE = (E) value;
+ return myE;
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return Enum.valueOf(clazz, valueStr);
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为Enum对象
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ *
+ * @param clazz Enum的Class
+ * @param value 值
+ * @return Enum
+ */
+ public static > E toEnum(Class clazz, Object value)
+ {
+ return toEnum(clazz, value, null);
+ }
+
+ /**
+ * 转换为BigInteger
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static BigInteger toBigInteger(Object value, BigInteger defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof BigInteger)
+ {
+ return (BigInteger) value;
+ }
+ if (value instanceof Long)
+ {
+ return BigInteger.valueOf((Long) value);
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return new BigInteger(valueStr);
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为BigInteger
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static BigInteger toBigInteger(Object value)
+ {
+ return toBigInteger(value, null);
+ }
+
+ /**
+ * 转换为BigDecimal
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @param defaultValue 转换错误时的默认值
+ * @return 结果
+ */
+ public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)
+ {
+ if (value == null)
+ {
+ return defaultValue;
+ }
+ if (value instanceof BigDecimal)
+ {
+ return (BigDecimal) value;
+ }
+ if (value instanceof Long)
+ {
+ return new BigDecimal((Long) value);
+ }
+ if (value instanceof Double)
+ {
+ return BigDecimal.valueOf((Double) value);
+ }
+ if (value instanceof Integer)
+ {
+ return new BigDecimal((Integer) value);
+ }
+ final String valueStr = toStr(value, null);
+ if (StringUtils.isEmpty(valueStr))
+ {
+ return defaultValue;
+ }
+ try
+ {
+ return new BigDecimal(valueStr);
+ }
+ catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 转换为BigDecimal
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错
+ *
+ * @param value 被转换的值
+ * @return 结果
+ */
+ public static BigDecimal toBigDecimal(Object value)
+ {
+ return toBigDecimal(value, null);
+ }
+
+ /**
+ * 将对象转为字符串
+ * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
+ *
+ * @param obj 对象
+ * @return 字符串
+ */
+ public static String utf8Str(Object obj)
+ {
+ return str(obj, CharsetKit.CHARSET_UTF_8);
+ }
+
+ /**
+ * 将对象转为字符串
+ * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
+ *
+ * @param obj 对象
+ * @param charsetName 字符集
+ * @return 字符串
+ */
+ public static String str(Object obj, String charsetName)
+ {
+ return str(obj, Charset.forName(charsetName));
+ }
+
+ /**
+ * 将对象转为字符串
+ * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
+ *
+ * @param obj 对象
+ * @param charset 字符集
+ * @return 字符串
+ */
+ public static String str(Object obj, Charset charset)
+ {
+ if (null == obj)
+ {
+ return null;
+ }
+
+ if (obj instanceof String)
+ {
+ return (String) obj;
+ }
+ else if (obj instanceof byte[])
+ {
+ return str((byte[]) obj, charset);
+ }
+ else if (obj instanceof Byte[])
+ {
+ byte[] bytes = ArrayUtils.toPrimitive((Byte[]) obj);
+ return str(bytes, charset);
+ }
+ else if (obj instanceof ByteBuffer)
+ {
+ return str((ByteBuffer) obj, charset);
+ }
+ return obj.toString();
+ }
+
+ /**
+ * 将byte数组转为字符串
+ *
+ * @param bytes byte数组
+ * @param charset 字符集
+ * @return 字符串
+ */
+ public static String str(byte[] bytes, String charset)
+ {
+ return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
+ }
+
+ /**
+ * 解码字节码
+ *
+ * @param data 字符串
+ * @param charset 字符集,如果此字段为空,则解码的结果取决于平台
+ * @return 解码后的字符串
+ */
+ public static String str(byte[] data, Charset charset)
+ {
+ if (data == null)
+ {
+ return null;
+ }
+
+ if (null == charset)
+ {
+ return new String(data);
+ }
+ return new String(data, charset);
+ }
+
+ /**
+ * 将编码的byteBuffer数据转换为字符串
+ *
+ * @param data 数据
+ * @param charset 字符集,如果为空使用当前系统字符集
+ * @return 字符串
+ */
+ public static String str(ByteBuffer data, String charset)
+ {
+ if (data == null)
+ {
+ return null;
+ }
+
+ return str(data, Charset.forName(charset));
+ }
+
+ /**
+ * 将编码的byteBuffer数据转换为字符串
+ *
+ * @param data 数据
+ * @param charset 字符集,如果为空使用当前系统字符集
+ * @return 字符串
+ */
+ public static String str(ByteBuffer data, Charset charset)
+ {
+ if (null == charset)
+ {
+ charset = Charset.defaultCharset();
+ }
+ return charset.decode(data).toString();
+ }
+
+ // ----------------------------------------------------------------------- 全角半角转换
+ /**
+ * 半角转全角
+ *
+ * @param input String.
+ * @return 全角字符串.
+ */
+ public static String toSBC(String input)
+ {
+ return toSBC(input, null);
+ }
+
+ /**
+ * 半角转全角
+ *
+ * @param input String
+ * @param notConvertSet 不替换的字符集合
+ * @return 全角字符串.
+ */
+ public static String toSBC(String input, Set notConvertSet)
+ {
+ char[] c = input.toCharArray();
+ for (int i = 0; i < c.length; i++)
+ {
+ if (null != notConvertSet && notConvertSet.contains(c[i]))
+ {
+ // 跳过不替换的字符
+ continue;
+ }
+
+ if (c[i] == ' ')
+ {
+ c[i] = '\u3000';
+ }
+ else if (c[i] < '\177')
+ {
+ c[i] = (char) (c[i] + 65248);
+
+ }
+ }
+ return new String(c);
+ }
+
+ /**
+ * 全角转半角
+ *
+ * @param input String.
+ * @return 半角字符串
+ */
+ public static String toDBC(String input)
+ {
+ return toDBC(input, null);
+ }
+
+ /**
+ * 替换全角为半角
+ *
+ * @param text 文本
+ * @param notConvertSet 不替换的字符集合
+ * @return 替换后的字符
+ */
+ public static String toDBC(String text, Set notConvertSet)
+ {
+ char[] c = text.toCharArray();
+ for (int i = 0; i < c.length; i++)
+ {
+ if (null != notConvertSet && notConvertSet.contains(c[i]))
+ {
+ // 跳过不替换的字符
+ continue;
+ }
+
+ if (c[i] == '\u3000')
+ {
+ c[i] = ' ';
+ }
+ else if (c[i] > '\uFF00' && c[i] < '\uFF5F')
+ {
+ c[i] = (char) (c[i] - 65248);
+ }
+ }
+ String returnString = new String(c);
+
+ return returnString;
+ }
+
+ /**
+ * 数字金额大写转换 先写个完整的然后将如零拾替换成零
+ *
+ * @param n 数字
+ * @return 中文大写数字
+ */
+ public static String digitUppercase(double n)
+ {
+ String[] fraction = { "角", "分" };
+ String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
+ String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
+
+ String head = n < 0 ? "负" : "";
+ n = Math.abs(n);
+
+ String s = "";
+ for (int i = 0; i < fraction.length; i++)
+ {
+ s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
+ }
+ if (s.length() < 1)
+ {
+ s = "整";
+ }
+ int integerPart = (int) Math.floor(n);
+
+ for (int i = 0; i < unit[0].length && integerPart > 0; i++)
+ {
+ String p = "";
+ for (int j = 0; j < unit[1].length && n > 0; j++)
+ {
+ p = digit[integerPart % 10] + unit[1][j] + p;
+ integerPart = integerPart / 10;
+ }
+ s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
+ }
+ return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
+ }
+}
diff --git a/device_gather/src/main/java/com/xr/device/common/utils/Files.java b/device_gather/src/main/java/com/xr/device/common/utils/Files.java
new file mode 100644
index 0000000..8c1c161
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/utils/Files.java
@@ -0,0 +1,435 @@
+package com.xr.device.common.utils;
+
+import org.apache.commons.io.FileUtils;
+import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Encoder;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.net.Authenticator;
+import java.net.PasswordAuthentication;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
+import java.util.Base64;
+import java.util.Date;
+
+public class Files {
+
+ /**
+ * 根据文件路径获取文件字节流
+ * @return
+ * filePath 文件路径
+ * @throws IOException
+ */
+ public static byte[] toByteArray(String filePath) throws IOException {
+ File f = new File(filePath);
+ if (!f.exists()) {
+ throw new FileNotFoundException("文件不存在");
+ }
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
+ BufferedInputStream in = null;
+ try {
+ in = new BufferedInputStream(new FileInputStream(f));
+ int buf_size = 1024;
+ byte[] buffer = new byte[buf_size];
+ int len = 0;
+ while (-1 != (len = in.read(buffer, 0, buf_size))) {
+ bos.write(buffer, 0, len);
+ }
+ return bos.toByteArray();
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw e;
+ } finally {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ bos.close();
+ }
+ }
+ public static File multipartFileToFile(MultipartFile file) throws Exception {
+ File toFile = null;
+ if (file.equals("") || file.getSize() <= 0) {
+ file = null;
+ } else {
+ InputStream ins = null;
+ ins = file.getInputStream();
+ toFile = new File(file.getOriginalFilename());
+ inputStreamToFile(ins, toFile);
+ ins.close();
+ }
+ return toFile;
+
+ }
+ public static BufferedImage bytesTobufferedImage(byte[] bytes){
+ BufferedImage image = null;
+
+ try {
+ // 利用ByteArrayInputStream将字节数据转换成InputStream
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+
+ // 使用ImageIO读取InputStream中的数据转换为BufferedImage
+ image = ImageIO.read(bais);
+
+ // 关闭ByteArrayInputStream
+ bais.close();
+ // 此时,image就是转换后的BufferedImage对象,可以进行显示或其他处理
+ // 例如显示图像:
+ // ImageIcon icon=new ImageIcon(image);
+ // JOptionPane.showMessageDialog(null, icon);
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return image;
+ }
+
+ public static String uploadImage(BufferedImage bufferedImage, Integer configId) throws Exception {
+ // 构建完整的路径
+ String fileName = configId+new Date().getTime()+".jpg";
+ SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
+ String date = sim.format(new Date());
+ String dates[] = date.split("-");
+
+ String year = dates[0];
+ String month = dates[1];
+ String day = dates[2];
+ //String fullPath = StaticPropUtil.imgPath + configId + File.separator + year + File.separator + month + File.separator + day + File.separator + fileName;
+ String fullPath = StaticPropUtil.imgPath + File.separator + fileName;
+ // 创建目录结构
+ //Path directoryPath = Paths.get(StaticPropUtil.imgPath, configId.toString(), year, month, day);
+ Path directoryPath = Paths.get(StaticPropUtil.imgPath);
+ if (!java.nio.file.Files.exists(directoryPath)) {
+ java.nio.file.Files.createDirectories(directoryPath);
+ }
+
+ // 创建文件并写入图像
+ File file = new File(fullPath);
+ ImageIO.write(bufferedImage, "jpg", file);
+
+ // 构建URL
+ String url = StaticPropUtil.imgUrl + configId + "/" + year + "/"+ month + "/" + day + "/" + fileName;
+ return url;
+ }
+
+
+
+ private static void inputStreamToFile(InputStream ins, File file) {
+ try {
+ OutputStream os = new FileOutputStream(file);
+ int bytesRead = 0;
+ byte[] buffer = new byte[8192];
+ while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+ os.close();
+ ins.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static BufferedImage urlByImage(String url,String username,String password) throws IOException {
+ BufferedImage bimg=null;
+ try {
+ // 设置身份验证
+ Authenticator.setDefault(new Authenticator() {
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(username, password.toCharArray());
+ }
+ });
+
+ // 创建URL对象并打开连接
+ URL uri = new URL(url);
+ URLConnection connection = uri.openConnection();
+
+ // 获取输入流
+ InputStream inputStream = connection.getInputStream();
+ bimg= ImageIO.read(inputStream);
+ // 关闭流
+ inputStream.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return bimg;
+ }
+
+ public static BufferedImage urlByImage(String url) throws IOException {
+ URL urlfile = new URL(url);
+ InputStream is2 = urlfile.openStream();
+ BufferedImage uImg= ImageIO.read(is2);
+ return uImg;
+ }
+
+ public static String TransformPhotoToBase64Data(String path){
+ Base64.Encoder encoder= Base64.getEncoder(); //获取Base64编码器
+ byte [] ImgContainer = null ; //数据集缓存器
+ FileInputStream fileInputStream = null; //文件输入流
+ try {
+ System.out.println(path);
+ File file=new File(path);
+ fileInputStream = new FileInputStream(file); //到指定路径寻找文件
+ ImgContainer = new byte[fileInputStream.available()]; //设置图片字节数据缓冲区大小
+ fileInputStream.read(ImgContainer); //将数据流中的图片数据读进缓冲区
+ String Base64ImgData =encoder.encodeToString(ImgContainer); //将图片编码转换成Base64格式的数据集
+ fileInputStream.close(); //关闭数据流
+ return Base64ImgData; //将缓冲区数据转换成字符数据返回
+ } catch (FileNotFoundException e) {
+ return "找不到指定文件!";
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return "null";
+ }
+
+ public static String getBase64String(MultipartFile multiPartFile) throws IOException {
+ String baseStr = null;
+
+ //把MultipartFile转化为File
+ File file = new File(multiPartFile.getOriginalFilename());
+ FileUtils.copyInputStreamToFile(multiPartFile.getInputStream(), file);
+
+ /* System.out.println("文件名:"+file.getOriginalFilename( ));
+ System.out.println("文件类型:"+file.getContentType());
+ System.out.println("文件大小: "+file.getSize( ));
+ String dateDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
+ ClassPathResource PATHS = new ClassPathResource("static/files/");
+ File paths=new File(PATHS.getPath()+file.getOriginalFilename());
+ if (paths.getParentFile() != null && !paths.getParentFile().exists()) {
+ System.out.println("创建父路径");
+ paths.getParentFile().mkdirs();
+ }
+ com.xr.projsystem.common.utils.Base64Utils.decodeFile(
+ com.xr.projsystem.common.utils.Base64Utils.encode(file.getBytes()),paths);
+ mapData.put("url",serverConfig.getUrl()+paths.getPath().substring(paths.getPath().indexOf("files")));
+ System.out.println(serverConfig.getUrl()+paths.getPath().substring(paths.getPath().indexOf("files")));*/
+ try {//file转base64
+ FileInputStream inputStream = new FileInputStream(file);
+ byte[] buffer = new byte[(int) file.length()];
+ inputStream.read(buffer);
+ inputStream.close();
+ baseStr = new BASE64Encoder().encode(buffer);
+
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ //删除临时文件
+ if (file.exists()) {
+ file.delete();
+ }
+ baseStr = baseStr.replaceAll("\r\n", "");
+ return baseStr;
+ }
+
+
+ public static String BufferedImageToBase64(BufferedImage bufferedImage) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
+ try {
+ ImageIO.write(bufferedImage, "png", baos);//写入流中
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ byte[] bytes = baos.toByteArray();//转换成字节
+ BASE64Encoder encoder = new BASE64Encoder();
+ String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
+ png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
+ System.out.println("值为:" + "data:image/jpg;base64," + png_base64);
+ return "data:image/jpg;base64," + png_base64;
+ }
+
+ public static String ImageToBase64(BufferedImage bufferedImage) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
+ try {
+ ImageIO.write(bufferedImage, "png", baos);//写入流中
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ byte[] bytes = baos.toByteArray();//转换成字节
+ BASE64Encoder encoder = new BASE64Encoder();
+ String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
+ png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
+ return png_base64;
+ }
+
+ /** *//**
+ * 旋转图片为指定角度
+ *
+ * @param file
+ * 目标图像
+ * @param degree
+ * 旋转角度
+ * @return
+ */
+ public static BufferedImage rotateImage(File file,
+ int degree) throws Exception{
+// if(degree<0){
+// degree=Math.abs(degree);
+// }else{
+// degree=0-degree;
+// }
+ BufferedImage bufferedimage = ImageIO.read(file);
+ int w= bufferedimage.getWidth();// 得到图片宽度。
+ int h= bufferedimage.getHeight();// 得到图片高度。
+ int type= bufferedimage.getColorModel().getTransparency();// 得到图片透明度。
+ BufferedImage img;// 空的图片。
+ Graphics2D graphics2d;// 空的画笔。
+ (graphics2d= (img= new BufferedImage(w, h, type))
+ .createGraphics()).setRenderingHint(
+ RenderingHints.KEY_INTERPOLATION,
+ RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ graphics2d.rotate(Math.toRadians(degree),w/2,h/2);// 旋转,degree是整型,度数,比如垂直90度。
+ graphics2d.drawImage(bufferedimage, 0, 0, null);// 从bufferedimagecopy图片至img,0,0是img的坐标。
+ graphics2d.dispose();
+ return img;// 返回复制好的图片,原图片依然没有变,没有旋转,下次还可以使用。
+ }
+
+ public static BufferedImage rotateImage(BufferedImage bufferedimage,int degree){
+ int w= bufferedimage.getWidth();// 得到图片宽度。
+ int h= bufferedimage.getHeight();// 得到图片高度。
+ int type= bufferedimage.getColorModel().getTransparency();// 得到图片透明度。
+ BufferedImage img;// 空的图片。
+ Graphics2D graphics2d;// 空的画笔。
+ (graphics2d= (img= new BufferedImage(w, h, type))
+ .createGraphics()).setRenderingHint(
+ RenderingHints.KEY_INTERPOLATION,
+ RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);// 旋转,degree是整型,度数,比如垂直90度。
+ graphics2d.drawImage(bufferedimage, 0, 0, null);// 从bufferedimagecopy图片至img,0,0是img的坐标。
+ graphics2d.dispose();
+ return img;// 返回复制好的图片,原图片依然没有变,没有旋转,下次还可以使用。
+ }
+
+ public static BufferedImage scaledImage(BufferedImage bufferedimage,Double scaled){
+ int width =bufferedimage.getWidth();
+ int height = bufferedimage.getHeight();
+ return new BufferedImage(Double.valueOf(scaled*width).intValue(),Double.valueOf(scaled*height).intValue(),BufferedImage.TYPE_INT_RGB);
+ }
+
+ public static byte[] bufferedImageToByte(BufferedImage bufferedImage) throws IOException{
+ ByteArrayOutputStream os =new ByteArrayOutputStream();
+ ImageIO.write(bufferedImage,"jpg",os);
+ return os.toByteArray();
+ }
+
+
+ public static void savePathForImage(BufferedImage bufferedImage,String path){
+
+ File file=new File(path);
+ // 创建文件输出流
+ try {
+ ImageIO.write(bufferedImage, "png", file);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static String uploadImageForUrl(BufferedImage bufferedImage,String imgPath,String rqImg) throws IOException {
+ File file =new File(imgPath+rqImg);
+ if(!file.getParentFile().exists()){
+ file.getParentFile().mkdirs();
+ }
+ ImageIO.write(bufferedImage,"jpg",file);
+ rqImg=rqImg.replaceAll("\\\\","/");
+ return StaticPropUtil.imgUrl+rqImg;
+ }
+
+ public static String uploadImageForPath(BufferedImage bufferedImage,String imgPath,String rqImg) throws IOException {
+ File file =new File(imgPath+rqImg);
+ if(!file.getParentFile().exists()){
+ file.getParentFile().mkdirs();
+ }
+ ImageIO.write(bufferedImage,"jpg",file);
+ rqImg=(imgPath+rqImg).replace("\\","\\\\");
+ return rqImg;
+ }
+
+ public static BufferedImage drawRectangleAndText(BufferedImage image, double x1, double y1,double x2, double wid, double hei, String text1, String text2) {
+ // 创建一个Graphics2D对象
+ Graphics2D g2d = image.createGraphics();
+
+
+ // 设置抗锯齿
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+
+
+ // 创建一个Rectangle2D.Double对象
+ Rectangle2D.Double rect = new Rectangle2D.Double(x1, y1, wid, hei);
+
+ // 绘制红色框
+ g2d.setColor(Color.RED);
+ g2d.draw(rect);
+ // 设置粗线条
+ g2d.setStroke(new BasicStroke(3.0f * 30)); // 设置线条宽度为3.0f
+
+ // 设置字体和颜色
+ g2d.setFont(new Font("微软雅黑", Font.BOLD, 38)); // 使用支持中文的字体,增加字体大小
+
+ // 获取当前时间
+ String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
+ // 获取文本的宽度和高度
+ FontMetrics fm = g2d.getFontMetrics();
+
+ // 绘制当前时间的背景矩形
+ int padding = 20;
+ int currentTimeWidth = fm.stringWidth(currentTime);
+ int currentTimeHeight = fm.getHeight();
+ int rectPadding = 5; // 背景矩形内边距
+ g2d.setColor(new Color(0, 0, 0, 50)); // 黑色半透明背景
+ g2d.fillRect(padding, padding, currentTimeWidth + 2 * rectPadding, currentTimeHeight + 2 * rectPadding);
+
+ // 绘制当前时间在左上角,内边距为20px
+ g2d.setColor(Color.WHITE);
+ int currentTimeX = padding + rectPadding + (currentTimeWidth + rectPadding - currentTimeWidth) / 2;
+ int currentTimeY = padding + rectPadding + fm.getAscent();
+ g2d.drawString(currentTime, currentTimeX, currentTimeY);
+
+ // 绘制文本2的背景矩形
+ int text2Width = fm.stringWidth(text2);
+ int text2Height = fm.getHeight();
+ g2d.setColor(new Color(0, 0, 0, 50)); // 黑色半透明背景
+ g2d.fillRect(padding, padding + currentTimeHeight + rectPadding * 2, text2Width + 2 * rectPadding, text2Height + 2 * rectPadding);
+
+ // 绘制文本2在时间的下面,内边距为20px
+ g2d.setColor(Color.WHITE);
+ int text2X = padding + rectPadding + (text2Width + rectPadding - text2Width) / 2;
+ int text2Y = padding + currentTimeHeight + rectPadding * 3 + fm.getAscent();
+ g2d.drawString(text2, text2X, text2Y);
+
+ // 计算文本1的右上角位置
+ int text1Width = fm.stringWidth(text1);
+ int text1X = (int) (x1 + wid); // 框的右上角位置
+ int text1Y = (int) (y1 + fm.getAscent()); // 框的右上角紧挨着框
+ g2d.setColor(Color.RED);
+
+ g2d.drawString(text1, text1X, text1Y);
+
+
+ // 释放资源
+ g2d.dispose();
+
+ return image;
+ }
+
+ public static void delFile(String url){
+ String filePath = url.replace(StaticPropUtil.imgUrl,StaticPropUtil.imgPath).replace("/","\\");
+ File file = new File(filePath);
+ if(file.exists()){
+ file.delete();
+ }
+ }
+
+}
diff --git a/device_gather/src/main/java/com/xr/device/common/utils/ModbusUtils.java b/device_gather/src/main/java/com/xr/device/common/utils/ModbusUtils.java
new file mode 100644
index 0000000..7bd0a16
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/utils/ModbusUtils.java
@@ -0,0 +1,167 @@
+package com.xr.device.common.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.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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static java.lang.Thread.sleep;
+
+public class ModbusUtils {
+ private static Logger log = LoggerFactory.getLogger(ModbusUtils.class);
+ private static Map map=new HashMap<>();
+ /**
+ * 工厂。
+ */
+ static ModbusFactory modbusFactory;
+
+ static {
+ if (modbusFactory == null) {
+ modbusFactory = new ModbusFactory();
+ }
+ }
+
+ /**
+ * 获取master
+ *
+ * @return
+ * @throws ModbusInitException
+ */
+ public static ModbusMaster getMaster(String host, int port) throws ModbusInitException {
+ IpParameters params = new IpParameters();
+ params.setHost(host);
+ params.setPort(port);
+ //
+ // modbusFactory.createRtuMaster(wapper); //RTU 协议
+ // modbusFactory.createUdpMaster(params);//UDP 协议
+ // modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
+ ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
+ master.init();
+
+ return master;
+ }
+
+ public static ModbusMaster getRtuIpMaster(String host, int port) throws ModbusInitException {
+ IpParameters params = new IpParameters();
+ params.setHost(host);
+ params.setPort(port);
+ params.setEncapsulated(true);
+ ModbusMaster master = modbusFactory.createTcpMaster(params, false);
+ try {
+ //设置超时时间
+ master.setTimeout(2000);
+ //设置重连次数
+ master.setRetries(2);
+ //初始化
+ master.init();
+ } catch (ModbusInitException e) {
+ e.printStackTrace();
+ }
+ return master;
+ }
+
+ /**
+ * 读取[01 Coil Status 0x]类型 开关数据
+ *
+ * @param slaveId 主机地址
+ * @param offset 寄存器地址
+ * @return 读取值
+ * @throws ModbusTransportException 异常
+ * @throws ErrorResponseException 异常
+ * @throws ModbusInitException 异常
+ */
+ public static Boolean readCoilStatus(ModbusMaster master, int slaveId, int offset)
+ throws ModbusTransportException, ErrorResponseException, ModbusInitException {
+ // 01 Coil Status
+ BaseLocator loc = BaseLocator.coilStatus(slaveId, offset);
+ Boolean value = master.getValue(loc);
+ return value;
+ }
+ /**
+ * 读取[03 Holding Register类型 2x]模拟量数据
+ *
+ * @param slaveId 主机地址
+ * @param offset 寄存器地址
+ * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
+ * @return
+ * @throws ModbusTransportException 异常
+ * @throws ErrorResponseException 异常
+ * @throws ModbusInitException 异常
+ */
+ public static Number readHoldingRegister(ModbusMaster master, int slaveId, int offset, int dataType)
+ throws ModbusTransportException, ErrorResponseException, ModbusInitException {
+ // 03 Holding Register类型数据读取
+ BaseLocator loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
+ Number value = master.getValue(loc);
+ return value;
+ }
+
+ /**
+ * 读取[04 Input Registers 3x]类型 模拟量数据
+ *
+ * @param slaveId 主机地址
+ * @param offset 寄存器地址
+ * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
+ * @return 返回结果
+ * @throws ModbusTransportException 异常
+ * @throws ErrorResponseException 异常
+ * @throws ModbusInitException 异常
+ */
+ public static Number readInputRegisters(ModbusMaster master, int slaveId, int offset, int dataType)
+ throws ModbusTransportException, ErrorResponseException, ModbusInitException {
+ // 04 Input Registers类型数据读取
+ BaseLocator loc = BaseLocator.inputRegister(slaveId, offset, dataType);
+ Number value = master.getValue(loc);
+ return value;
+ }
+
+ /**
+ * 批量读取使用寄存器数据
+ * @param master ModbusMaster对象
+ * @param batchRead 批量读取集合
+ * @throws ModbusTransportException
+ * @throws ErrorResponseException
+ * @throws ModbusInitException
+ */
+ public static BatchResults batchRead(ModbusMaster master, BatchRead batchRead)throws Exception {
+ try {
+ batchRead.setContiguousRequests(false);
+ BatchResults results = master.send(batchRead);
+ return results;
+ } catch (Exception e) {
+ log.error("批量读取使用寄存器数据出现异常"+e);
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static ModbusMaster getModbusMaster(String portName){
+ return map.get(portName);
+ }
+
+
+ public static void main(String[] args) {
+ try{
+ ModbusMaster modbusMaster= ModbusUtils.getMaster("192.168.1.105",1502);
+ while (true){
+ Number n = ModbusUtils.readHoldingRegister(modbusMaster, 1, 0, 4);
+ System.out.println(n);
+ sleep(1000);
+ }
+
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/device_gather/src/main/java/com/xr/device/common/utils/PythonExecutor.java b/device_gather/src/main/java/com/xr/device/common/utils/PythonExecutor.java
new file mode 100644
index 0000000..e43330c
--- /dev/null
+++ b/device_gather/src/main/java/com/xr/device/common/utils/PythonExecutor.java
@@ -0,0 +1,205 @@
+package com.xr.device.common.utils;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.xr.device.model.entity.MeterInitialization;
+import com.xr.device.model.service.MeterInitializationService;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class PythonExecutor {
+
+
+ public static void main(String[] args) throws Exception {
+ try {
+ String pythonPath = StaticPropUtil.pythonPath;
+
+ ProcessBuilder processBuilder = new ProcessBuilder(pythonPath);
+ // 启动子进程
+ Process process = processBuilder.start();
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
+ bw.write("import sys\n");
+ bw.write("sys.path.append(\"D:\\smartGrid\\smartGrid\\models\")\n");
+ bw.write("import indicatorLightStatus as ils\n");
+ bw.write("import switchRecognition as sr\n");
+ bw.write("import liquidLevel as ll\n");
+ bw.write("liquid_level_image_path = 'D:\\smartGrid\\smartGrid\\weights\\DIGITAL_METER (41).JPG'\n");
+ bw.write("ll.calculate_rgb_min_max([[112,\"D:\\smartGrid\\smartGrid\\weights\\DIGITAL_METER (41).JPG\"]])\n");
+ bw.write("print(\"Liquid level weights initialized.\")\n");
+ bw.write("liquid_level_image_path = 'D:\\smartGrid\\smartGrid\\weights\\DIGITAL_METER (10).JPG'\n");
+ bw.write("liquid_level = ll.calculate_liquid_level([112,liquid_level_image_path])\n");
+ bw.write("print(f\"Liquid level: {liquid_level}\")\n");
+
+ bw.close();
+
+
+ // 获取子进程的输出流
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+ String line;
+ // 读取输出
+ while ((line = reader.readLine()) != null) {
+ System.out.println(line);
+ }
+
+ // 等待进程执行完成
+ int exitCode = process.waitFor();
+ System.out.println("Exit Code: " + exitCode);
+
+ } catch (IOException | InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ private static boolean meterInit(String code) throws Exception {
+ String pythonPath = StaticPropUtil.pythonPath;
+ String modelPath = StaticPropUtil.modelPath;
+
+ ProcessBuilder processBuilder = new ProcessBuilder(pythonPath);
+ // 启动子进程
+ Process process = processBuilder.start();
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
+ bw.write("import sys\n");
+ String imports = "sys.path.append(\"%s\")\n";
+ bw.write(String.format(imports, modelPath));
+ bw.write("import indicatorLightStatus as ils\n");
+ bw.write("import switchRecognition as sr\n");
+ bw.write("import liquidLevel as ll\n");
+ bw.write(code);
+ bw.write("print(\"success\")\n");
+ bw.close();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+ List list = new ArrayList<>();
+ String line;
+ // 读取输出
+ while ((line = reader.readLine()) != null) {
+ list.add(line);
+ }
+
+ // 等待进程执行完成
+ int exitCode = process.waitFor();
+ System.out.println("Exit Code: " + exitCode);
+ if (list.contains("success")) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * 初始化算法
+ * id 表计配置ID
+ * sfType 4 灯光 3 开关 5 液位计
+ */
+ public static boolean meterInit(Integer id, String sfType) throws Exception {
+ MeterInitializationService meterInitializationService = SpringUtils.getBean(MeterInitializationService.class);
+ QueryWrapper query = new QueryWrapper<>();
+ query.eq("meter_id", id);
+ query.orderByAsc("serial");
+ List list = meterInitializationService.list(query);
+ List path = list.stream().map(n -> n.getImgAddress()).collect(Collectors.toList());
+ String ph = "";
+ for (String str : path) {
+ str = str.replace("\\", "\\\\");
+ ph += "\"" + str + "\"" + ",";
+ }
+ ph = ph.substring(0, ph.length() - 1);
+ String code = "";
+ if (sfType.equals("4")) {
+ List> list1 = new ArrayList<>();
+ List