From 5a1db6a5f85738d7b70ffc77cb28273aa0f7f37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAPTOP-S9HJSOEB=5C=E6=98=8A=E5=A4=A9?= Date: Tue, 24 Dec 2024 14:11:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=89=E6=BA=90=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CameraControlController.java | 2 +- .../web/controller/OrderController.java | 179 +++++++++++++++++- .../web/controller/StreetController.java | 51 +---- .../zhehekeji/web/service/CodeService.java | 175 +++++++++++++++++ .../com/zhehekeji/web/service/CronTab.java | 30 +-- .../web/service/LightSourceService.java | 139 +++++++++++++- 6 files changed, 504 insertions(+), 72 deletions(-) create mode 100644 web/src/main/java/com/zhehekeji/web/service/CodeService.java diff --git a/web/src/main/java/com/zhehekeji/web/controller/CameraControlController.java b/web/src/main/java/com/zhehekeji/web/controller/CameraControlController.java index 766b3f2..1c2b270 100644 --- a/web/src/main/java/com/zhehekeji/web/controller/CameraControlController.java +++ b/web/src/main/java/com/zhehekeji/web/controller/CameraControlController.java @@ -331,7 +331,7 @@ public class CameraControlController { @Resource private CameraService cameraService; - private void checkLogin(Integer cameraId){ + public void checkLogin(Integer cameraId){ if(CameraConnMap.getConnId(cameraId) == null){ Camera camera = cameraMapper.selectById(cameraId); cameraService.cameraLogin(camera); diff --git a/web/src/main/java/com/zhehekeji/web/controller/OrderController.java b/web/src/main/java/com/zhehekeji/web/controller/OrderController.java index f1d9cea..41cd55b 100644 --- a/web/src/main/java/com/zhehekeji/web/controller/OrderController.java +++ b/web/src/main/java/com/zhehekeji/web/controller/OrderController.java @@ -1,25 +1,42 @@ package com.zhehekeji.web.controller; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.github.pagehelper.PageInfo; +import com.sun.jna.ptr.IntByReference; +import com.zhehekeji.common.util.PathUtil; import com.zhehekeji.common.util.ValidatorUtil; import com.zhehekeji.core.pojo.Result; +import com.zhehekeji.web.config.ConfigProperties; +import com.zhehekeji.web.entity.LightSource; +import com.zhehekeji.web.entity.Street; +import com.zhehekeji.web.lib.CameraConnMap; +import com.zhehekeji.web.lib.hik.HCNetSDK; +import com.zhehekeji.web.lib.hik.HikLoginModuleImpl; +import com.zhehekeji.web.mapper.StreetMapper; import com.zhehekeji.web.pojo.OrderSaveReq; import com.zhehekeji.web.pojo.OrderSearch; import com.zhehekeji.web.pojo.OrderVO; -import com.zhehekeji.web.service.OrderService; -import com.zhehekeji.web.service.PlcCmdInfo; -import com.zhehekeji.web.service.PlcService; +import com.zhehekeji.web.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import io.swagger.models.auth.In; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.io.File; +import java.util.HashMap; +import java.util.List; + +import static com.zhehekeji.web.lib.hik.HCNetSDK.NET_DVR_GET_PTZPOS; +import static com.zhehekeji.web.lib.hik.HCNetSDK.NET_DVR_SET_PTZPOS; @Api(value = "OrderController",tags = "订单管理") @RestController(value = "OrderController") @RequestMapping("/order") +@Slf4j public class OrderController { @Resource @@ -29,6 +46,9 @@ public class OrderController { @Resource private ValidatorUtil validatorUtil; + @Resource + private CameraControlController cameracheckLogin; + @Value("${zhehe.filter.postToken}") private String postToken; @@ -84,4 +104,157 @@ public class OrderController { return Result.success(orderService.orders(orderSearch)); } + @Resource + ConfigProperties configProperties; + @Resource + CodeService codeService; + @Resource + StreetMapper streetMapper; + @Resource + LightSourceService lightSourceService; + @PostMapping("/test1/{id}") + @ApiOperation(value = "test1") + //@SessionHandler + public Result test1(@PathVariable Integer id) { + //validatorUtil.validate(orderSearch); + Street street = streetMapper.selectOne(new QueryWrapper().eq("camera1_Id", id).or().eq("camera2_Id", id)); + List lightSources = lightSourceService.getLightSourceByStreetId(street.getId()); + lightSources.forEach(lightSource -> { + lightSourceService.controllerLightSource(lightSource,1); + + }); + + //球机调用指定预置点位 + + plcService.gyrateCameraByCode(id,"E1"); + //球机拍照 + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + String path = "work\\1.jpg"; + + plcService.cameraCapture(id,false,null,path); + + //交予灵闪识别 + HashMap stringMap = new HashMap<>(); + stringMap.put("code",""); + + stringMap.put("location",""); + CodeService.readOCR("127.0.0.1",9040,stringMap); + System.out.println("stringMap: "+stringMap); + //2560,1440 + if(!"".equals(stringMap.get("location"))){ + //调整位置 + ge( id, stringMap.get("location")); + } + //球机拍照 + try { + Thread.sleep(8000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + plcService.cameraCapture(id,false,null,path); + //交予灵闪识别 + String code = CodeService.readOCR("127.0.0.1",9040,stringMap); + lightSources.forEach(lightSource -> { + lightSourceService.controllerLightSource(lightSource,0); + + }); + + return Result.success(code); + } + + @PostMapping("/test/{id}") + @ApiOperation(value = "test") + //@SessionHandler + public Result test(@PathVariable Integer id) { + //validatorUtil.validate(orderSearch); + Street street = streetMapper.selectOne(new QueryWrapper().eq("camera1_Id", id).or().eq("camera2_Id", id)); + List lightSources = lightSourceService.getLightSourceByStreetId(street.getId()); + lightSources.forEach(lightSource -> { + lightSourceService.controllerLightSource(lightSource,1); + + }); + + //球机调用指定预置点位 + + plcService.gyrateCameraByCode(id,"E1"); + //球机拍照 + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + String path = "work\\1.jpg"; + + plcService.cameraCapture(id,false,null,path); + //交予灵闪识别 + HashMap stringMap = new HashMap<>(); + stringMap.put("code",""); + + stringMap.put("location",""); + String code = CodeService.readOCR("127.0.0.1",9040,stringMap); + //2560,1440 + + return Result.success(code); + } + ////2560,1440 + void ge(int id,String location){ + String[] locations = location.split(","); + cameracheckLogin.checkLogin(id); + HCNetSDK.NET_DVR_PTZPOS struPtTZPos = new HCNetSDK.NET_DVR_PTZPOS(); + + IntByReference intByReference = new IntByReference(1); + + + boolean b_GetPTZ = HikLoginModuleImpl.hcNetsdk.NET_DVR_GetDVRConfig( CameraConnMap.getConnId(id).intValue(), NET_DVR_GET_PTZPOS, 1,struPtTZPos.getPointer(), struPtTZPos.size(),intByReference); + if (b_GetPTZ==false){ + System.out.println("获取PTZ坐标信息失败,错误码:" + HikLoginModuleImpl.hcNetsdk.NET_DVR_GetLastError()); + return; + }else { + System.out.println("获取PTZ坐标信息成功"); + + struPtTZPos.read(); + } + //水平 + int p = ((int) Math.round( Double.parseDouble(locations[0]))-(2560/2))/30; + //垂直 + int t = ((int) Math.round( Double.parseDouble(locations[1]))-(1440/2))/30; + + struPtTZPos.wAction = 1; + //水平 + + System.out.println(struPtTZPos.wPanPos); + struPtTZPos.wPanPos = (short) (struPtTZPos.wPanPos+0x10*p); + System.out.println(struPtTZPos.wPanPos); + //垂直 + System.out.println(); + System.out.println(struPtTZPos.wTiltPos); + struPtTZPos.wTiltPos = (short) (struPtTZPos.wTiltPos+0x10*t); + + System.out.println(struPtTZPos.wTiltPos); + struPtTZPos.wZoomPos = 0x0150; + struPtTZPos.write(); + boolean flag = HikLoginModuleImpl.hcNetsdk.NET_DVR_SetDVRConfig( CameraConnMap.getConnId(id).intValue(), NET_DVR_SET_PTZPOS, 1,struPtTZPos.getPointer(), struPtTZPos.size()); + if(!flag){ + System.out.println("设置PTZ坐标信息失败,错误码:" + HikLoginModuleImpl.hcNetsdk.NET_DVR_GetLastError()); + }else { + System.out.println("设置PTZ坐标信息成功 p:"+p+" t:"+t); + } + } + + public static void main(String[] args) { + + String location = "963.465209960937500,1373.154296875000000"; + + String[] locations = location.split(","); + + int p = ((int) Math.round( Double.parseDouble(locations[0]))-(2560/2))/30; + int t = ((int) Math.round( Double.parseDouble(locations[1]))-(1440/2))/32; + System.out.println(p+" "+t); + } + } diff --git a/web/src/main/java/com/zhehekeji/web/controller/StreetController.java b/web/src/main/java/com/zhehekeji/web/controller/StreetController.java index e3dbe9d..71d4194 100644 --- a/web/src/main/java/com/zhehekeji/web/controller/StreetController.java +++ b/web/src/main/java/com/zhehekeji/web/controller/StreetController.java @@ -76,27 +76,13 @@ public class StreetController { return Result.success(streetService.streetById(id)); } - - @GetMapping("/lightSource/open") @ApiOperation(value = "开启光源") public Result openLightSource(){ List lightSources = lightSourceService.getLightSource(); lightSources.forEach(lightSource -> { - JSONObject obj = JSONObject.parseObject(configProperties.getLightSource().getInfo()); - if(configProperties.getLightSource().getType() == 1){ - - JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(),lightSource.getPort()); - JYDamHelper.openDO(equip,configProperties.getLightSource().getIndex()); - //暂时关闭灯源状态查询 - /*Integer status = JYDamHelper.ReadStatus(equip,configProperties.getLightSource().getNum(),configProperties.getLightSource().getIndex()); - log.info("ip:{},status:{}",lightSource.getIp(),status);*/ - equip.DisConnect(); - - }else if(configProperties.getLightSource().getType() == 2){ - HikControlSocket.openLight(lightSource.getIp(),lightSource.getPort(),configProperties.getLightSource().getIndex(),1); - } + lightSourceService.controllerLightSource(lightSource,1); }); @@ -109,17 +95,9 @@ public class StreetController { List lightSources = lightSourceService.getLightSource(); - lightSources.forEach(lightSource -> { - JSONObject obj = JSONObject.parseObject(configProperties.getLightSource().getInfo()); - if(configProperties.getLightSource().getType() == 1){ - JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(),lightSource.getPort()); - JYDamHelper.closeDO(equip,obj.getInteger("index")); - Integer status = JYDamHelper.ReadStatus(equip,obj.getInteger("num"),obj.getInteger("index")); - log.info("ip:{},status:{}",lightSource.getIp(),status); - }else if(configProperties.getLightSource().getType() == 2){ - HikControlSocket.openLight(lightSource.getIp(),lightSource.getPort(),configProperties.getLightSource().getIndex(),0); - } + lightSources.forEach(lightSource -> { + lightSourceService.controllerLightSource(lightSource,0); }); @@ -131,15 +109,7 @@ public class StreetController { public Result openStreetLightSource(@PathVariable Integer streetId){ List lightSources = lightSourceService.getLightSourceByStreetId(streetId); lightSources.forEach(lightSource -> { - if(configProperties.getLightSource().getType() == 1){ - JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(),lightSource.getPort()); - JYDamHelper.openDO(equip,configProperties.getLightSource().getIndex()); - Integer status = JYDamHelper.ReadStatus(equip,configProperties.getLightSource().getNum(),configProperties.getLightSource().getIndex()); - log.info("ip:{},status:{}",lightSource.getIp(),status); - equip.DisConnect(); - }else if(configProperties.getLightSource().getType() == 2){ - HikControlSocket.openLight(lightSource.getIp(),lightSource.getPort(),configProperties.getLightSource().getIndex(),1); - } + lightSourceService.controllerLightSource(lightSource,1); }); @@ -151,22 +121,13 @@ public class StreetController { public Result closeStreetLightSource(@PathVariable Integer streetId){ List lightSources = lightSourceService.getLightSourceByStreetId(streetId); lightSources.forEach(lightSource -> { - if(configProperties.getLightSource().getType() == 1){ - JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(),lightSource.getPort()); - JYDamHelper.closeDO(equip,configProperties.getLightSource().getIndex()); - Integer status = JYDamHelper.ReadStatus(equip,configProperties.getLightSource().getNum(),configProperties.getLightSource().getIndex()); - log.info("ip:{},status:{}",lightSource.getIp(),status); - equip.DisConnect(); - }else if(configProperties.getLightSource().getType() == 2){ - HikControlSocket.openLight(lightSource.getIp(),lightSource.getPort(),configProperties.getLightSource().getIndex(),0); - } + lightSourceService.controllerLightSource(lightSource,0); }); + return Result.success(); } - - } diff --git a/web/src/main/java/com/zhehekeji/web/service/CodeService.java b/web/src/main/java/com/zhehekeji/web/service/CodeService.java new file mode 100644 index 0000000..c7d474c --- /dev/null +++ b/web/src/main/java/com/zhehekeji/web/service/CodeService.java @@ -0,0 +1,175 @@ +package com.zhehekeji.web.service; + +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.io.*; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@Service +public class CodeService { + + private static final Logger tcpLogger = LoggerFactory.getLogger("sick"); + public static void main(String[] args) { + Map map = new HashMap<>(); + map.put("Lotnum",""); + map.put("2",""); + String code = readOCR("127.0.0.1", 554,"",map); + System.out.println(map); + } + + /* + * 读取OCR + * @param ip + * @param port + * @param writeCmd 拍照命令 + * @param stringMap 结果集合(灵闪是一次拍照,不同作业同步返回,每个作业的配置为 参数:值;) + * @return + */ + public static String readOCR(String ip, int port, String writeCmd, Map stringMap){ + Socket socket = new Socket(); + String code = "NoRead"; + OutputStream os = null; + InputStream is = null; + try { + socket.connect(new InetSocketAddress(ip,port),3000); + socket.setSoTimeout(35000); + os = socket.getOutputStream(); + is = socket.getInputStream(); + int i = 0; + //扫描ocr + //拍照 + writeCmd(os,"start:1"); + String[] codes = read(is); + for (String c :codes) { + if (c.contains(":")) { + String[] split = code.split(":"); + if (stringMap.get(split[0]) != null && split.length > 2) { + stringMap.put(split[0], split[1]); + } + } + } + //code = read(is); + + } catch (IOException e) { + tcpLogger.error("sick time out,ip:{},info:{}",ip,e); + log.error("sick time out,ip:{},info:{}",ip,e); + }finally { + if(os != null){ + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(is != null){ + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + return code; + } + } + + /* + * 读取OCR + * @param ip + * @param port + * @param writeCmd 拍照命令 + * @param stringMap 结果集合(灵闪是一次拍照,不同作业同步返回,每个作业的配置为 参数:值;) + * @return + */ + public static String readOCR(String ip, int port,HashMap stringMap){ + + Socket socket = new Socket(); + String code = "NoRead"; + OutputStream os = null; + InputStream is = null; + try { + socket.connect(new InetSocketAddress(ip,port),3000); + socket.setSoTimeout(355000); + os = socket.getOutputStream(); + is = socket.getInputStream(); + int i = 0; + //扫描ocr + //拍照 + writeCmd(os,"start:1"); + + String[] codes = read(is); + System.out.println(codes); + for (String c :codes) { + if (c.contains(":")) { + String[] split = c.split(":"); + if (stringMap.get(split[0]) != null && split.length == 2) { + stringMap.put(split[0], split[1]); + } + } + } + + //code = read(is); + + } catch (IOException e) { + tcpLogger.error("sick time out,ip:{},info:{}",ip,e); + log.error("sick time out,ip:{},info:{}",ip,e); + }finally { + if(os != null){ + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(is != null){ + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + return stringMap.get("code"); + } + } + + private static void writeCmd(OutputStream os,String writeCmd) throws IOException { + + byte[]bytes = writeCmd.getBytes(StandardCharsets.UTF_8); + os.write(bytes); + } + + private static String[] read(InputStream inStream) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); + int c; + StringBuilder sb = new StringBuilder(); + while ((c = br.read()) != -1) { + char ch = (char) c; + + if (ch == '>') { + break; // 当读取到 "END" 时停止 + } + sb.append(ch); + + } + return sb.toString().split(";"); + } +} diff --git a/web/src/main/java/com/zhehekeji/web/service/CronTab.java b/web/src/main/java/com/zhehekeji/web/service/CronTab.java index d4fa23c..586f6d4 100644 --- a/web/src/main/java/com/zhehekeji/web/service/CronTab.java +++ b/web/src/main/java/com/zhehekeji/web/service/CronTab.java @@ -49,6 +49,9 @@ public class CronTab { private static int gByte = 1024* 1024 * 1024; + @Resource + private LightSourceService lightSourceService; + @Scheduled(cron = "0 0 0 * * ?") //@Scheduled(cron = "0 0/1 * * * *") public void file() { @@ -73,28 +76,13 @@ public class CronTab { public void lightStatus() { log.debug(" lightStatus"); long timeStamp = System.currentTimeMillis(); + List lightSources = lightSourceService.getLightSource(); - Iterator> it = lightTimeMap.entrySet().iterator(); - while (it.hasNext()){ - Map.Entry entry = it.next(); - if((BigInteger.valueOf(timeStamp).subtract(BigInteger.valueOf(entry.getValue()))).compareTo(timeDIff) > 0){ - List lightSources = lightSourceMapper.selectList(new QueryWrapper().eq("street_id",entry.getKey())); - lightSources.forEach(lightSource -> { - if(configProperties.getLightSource().getType() == 1){ - - JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(),lightSource.getPort()); - JYDamHelper.closeDO(equip,configProperties.getLightSource().getIndex()); - //不再获取灯光状态 - //Integer status = JYDamHelper.ReadStatus(equip,configProperties.getLightSource().getNum(),configProperties.getLightSource().getIndex()); - //log.info("ip:{},status:{}",lightSource.getIp(),status); - equip.DisConnect(); - }else if(configProperties.getLightSource().getType() == 2){ - HikControlSocket.openLight(lightSource.getIp(),lightSource.getPort(),configProperties.getLightSource().getIndex(),0); - } - }); - it.remove(); - } - } + + lightSources.forEach(lightSource -> { + lightSourceService.controllerLightSource(lightSource,1); + + }); } diff --git a/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java b/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java index 3f60d19..70b893d 100644 --- a/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java +++ b/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java @@ -1,19 +1,35 @@ package com.zhehekeji.web.service; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.zhehekeji.web.config.ConfigProperties; import com.zhehekeji.web.entity.LightSource; import com.zhehekeji.web.mapper.LightSourceMapper; -import org.springframework.beans.factory.annotation.Autowired; +import com.zhehekeji.web.service.damLightSource.JYDAMEquip; +import com.zhehekeji.web.service.damLightSource.JYDamHelper; +import com.zhehekeji.web.service.hikLightSource.HikControlSocket; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; @Service +@Slf4j public class LightSourceService { - @Autowired + @Resource private LightSourceMapper lightSourceMapper; + @Resource + ConfigProperties configProperties; public List getLightSource(){ return lightSourceMapper.selectByMap(new HashMap<>()); @@ -22,4 +38,123 @@ public class LightSourceService { public List getLightSourceByStreetId(Integer streetId){ return lightSourceMapper.selectList(new QueryWrapper().eq("street_id",streetId)); } + + public boolean controllerLightSource(LightSource lightSource, int status) { + JSONObject obj = JSONObject.parseObject(configProperties.getLightSource().getInfo()); + //1开灯 0关灯 + if (status == 1) { + if (configProperties.getLightSource().getType() == 1) { + + JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(), lightSource.getPort()); + JYDamHelper.openDO(equip, configProperties.getLightSource().getIndex()); + Integer stat = JYDamHelper.ReadStatus(equip, configProperties.getLightSource().getNum(), configProperties.getLightSource().getIndex()); + log.info("ip:{},status:{}", lightSource.getIp(), stat); + equip.DisConnect(); + + } else if (configProperties.getLightSource().getType() == 2) { + HikControlSocket.openLight(lightSource.getIp(), lightSource.getPort(), configProperties.getLightSource().getIndex(), 1); + } else if (configProperties.getLightSource().getType() == 3) { + relay(lightSource.getIp(), lightSource.getPort(),status); + } + } else { + if (configProperties.getLightSource().getType() == 1) { + + JYDAMEquip equip = JYDamHelper.Connect(lightSource.getIp(), lightSource.getPort()); + JYDamHelper.closeDO(equip, configProperties.getLightSource().getIndex()); + Integer stat = JYDamHelper.ReadStatus(equip, configProperties.getLightSource().getNum(), configProperties.getLightSource().getIndex()); + log.info("ip:{},status:{}", lightSource.getIp(), stat); + equip.DisConnect(); + + } else if (configProperties.getLightSource().getType() == 2) { + HikControlSocket.openLight(lightSource.getIp(), lightSource.getPort(), configProperties.getLightSource().getIndex(), 0); + } else if (configProperties.getLightSource().getType() == 3) { + relay(lightSource.getIp(), lightSource.getPort(),status); + } + } + return true; + } + /* + 本项目采用两个继电器端口 + */ + + public static void relay(String host, int port, int status) { + byte[][] data = new byte[2][]; + if (status == 1) { + //01 05 00 00 ff 00 8c 3a + data[0] = new byte[]{0x01, 0x05, 0x00, 0x00, (byte) 0xFF, 0x00, (byte) 0x8c, (byte) 0x3A}; +//网络继电器发送设置HEX 开01 05 00 01 FF 00 DD FA +//关01 05 00 01 00 00 9C 0A + data[1] = new byte[]{0x01, 0x05, 0x00, 0x01, (byte) 0xFF, 0x00, (byte) 0xdd, (byte) 0xfa}; + + } else { + //01 05 00 00 00 00 cd ca + data[0] = new byte[]{0x01, 0x05, 0x00, 0x00, (byte) 0x00, 0x00, (byte) 0xcd, (byte) 0xca}; + + data[1] = new byte[]{0x01, 0x05, 0x00, 0x01, (byte) 0x00, 0x00, (byte) 0x9c, (byte) 0x0A}; + } + + + try (Socket socket = new Socket(host, port); + DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) { + for (byte[] d :data){ + socket.setSoTimeout(1000); + // 发送数据 + outputStream.write(d); + outputStream.flush(); + } + //System.out.println("Data sent successfully."); + + } catch (IOException e) { + // System.err.println("Error sending data: " + e.getMessage()); + e.printStackTrace(); + } + } + + /** + * + * @param ip + * @param port + * @param stat 1开启,2关闭 + */ + public static void lightController(String ip ,Integer port,Integer stat){ + Socket socket = new Socket(); + OutputStream os = null; + InputStream is = null; + try { + socket.connect(new InetSocketAddress(ip,port),3000); + os = socket.getOutputStream(); + is = socket.getInputStream(); + int i = 0; + socket.setSoTimeout(1000); + String startCmd = "<1,"+stat+",post>"; + byte[]bytes = startCmd.getBytes(StandardCharsets.UTF_8); + os.write(bytes); + + + } catch (IOException e) { + + }finally { + if(os != null){ + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(is != null){ + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + } }