¶Ô±ÈÐÂÎļþ |
| | |
| | | //package com.billion.main.tcp; |
| | | // |
| | | //import cn.hutool.core.collection.CollUtil; |
| | | //import cn.hutool.core.util.ObjUtil; |
| | | //import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | //import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | //import com.billion.main.da.domain.DaStationCollection; |
| | | //import com.billion.main.da.service.impl.DaStationCollectionServiceImpl; |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.springframework.beans.factory.annotation.Value; |
| | | //import org.springframework.scheduling.annotation.Async; |
| | | //import org.springframework.stereotype.Component; |
| | | // |
| | | //import javax.annotation.PostConstruct; |
| | | //import javax.annotation.Resource; |
| | | //import java.io.IOException; |
| | | //import java.io.InputStream; |
| | | //import java.io.OutputStream; |
| | | //import java.net.InetAddress; |
| | | //import java.net.ServerSocket; |
| | | //import java.net.Socket; |
| | | //import java.net.UnknownHostException; |
| | | //import java.util.Arrays; |
| | | //import java.util.List; |
| | | //import java.nio.charset.StandardCharsets; |
| | | // |
| | | ///** |
| | | // * ç®ä»è¯´æ:TCPè¿æ¥ |
| | | // * |
| | | // * @author: Eric |
| | | // * @date: 2025-03-29 13:29:01 |
| | | // * @version: 1.0 |
| | | // */ |
| | | //@Slf4j |
| | | //@Component |
| | | //public class TcpClient { |
| | | // |
| | | // private Socket socket; |
| | | // private InputStream input; |
| | | // private OutputStream output; |
| | | // |
| | | // @Value("${tcp.server.ip}") |
| | | // private String serverIp; |
| | | // |
| | | // @Value("${tcp.server.port}") |
| | | // private int serverPort; |
| | | // |
| | | // @Resource |
| | | // private DaStationCollectionServiceImpl daStationCollectionService; |
| | | // |
| | | // @PostConstruct |
| | | // public void init() throws UnknownHostException { |
| | | // |
| | | // try { |
| | | // log.info("å¼å§å»ºç«è¿æ¥"); |
| | | // // å»ºç« TCP è¿æ¥ |
| | | // socket = new Socket(serverIp, serverPort); |
| | | // int localPort = socket.getLocalPort(); |
| | | // InetAddress localAddress = socket.getLocalAddress(); |
| | | // log.info("localPort",localPort); |
| | | // log.info("localAddress",localAddress); |
| | | // int port = socket.getPort(); |
| | | // log.info("port:",port); |
| | | // input = socket.getInputStream(); |
| | | // output = socket.getOutputStream(); |
| | | // log.info("input:",input); |
| | | // log.info("output:",output); |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // |
| | | // // åéæ°æ®å°ä¸ä½æº |
| | | // public void sendData(byte[] data) throws IOException { |
| | | // output.write(data); |
| | | // output.flush(); |
| | | // } |
| | | // |
| | | // // æ¥æ¶ä¸ä½æºæ°æ®ï¼æç»çå¬ï¼ |
| | | // @Async |
| | | // public void receiveData() { |
| | | // log.info("æ¥æ¶æ°æ®å¾ªç¯"); |
| | | // byte[] buffer = new byte[1024]; |
| | | // if (ObjUtil.isNotNull(socket)){ |
| | | // while (!socket.isClosed()) { |
| | | // try { |
| | | // log.info("å¼å§æ¥æ¶æ°æ®"); |
| | | // int len = input.read(buffer); |
| | | // if (len > 0) { |
| | | // byte[] received = Arrays.copyOf(buffer, len); |
| | | // String str = new String(received, "UTF-8"); |
| | | // System.out.println(str); |
| | | // log.info("æ¥æ¶å°æ°æ®ï¼",str); |
| | | //// processData(received); // å¤çæ°æ® |
| | | // List<DaStationCollection> collect = daStationCollectionService.list(new LambdaQueryWrapper<DaStationCollection>() |
| | | // .eq(DaStationCollection::getStatus, "0")); |
| | | // if (CollUtil.isNotEmpty(collect)) { |
| | | // DaStationCollection daStationCollection = collect.get(0); |
| | | // String sfcCode = daStationCollection.getSfcCode(); |
| | | // TcpClient tcpClient = new TcpClient(); |
| | | // byte[] bytes = sfcCode.getBytes(StandardCharsets.UTF_8); |
| | | // tcpClient.sendData(bytes); |
| | | // log.info("åéæ°æ®ï¼",sfcCode); |
| | | // log.info("åéæ°æ®-byteï¼",bytes); |
| | | // //æ¸
é¤ |
| | | // daStationCollectionService.update(new LambdaUpdateWrapper<DaStationCollection>() |
| | | // .eq(DaStationCollection::getSfcCode,sfcCode) |
| | | // .set(DaStationCollection::getStatus,"1")); |
| | | // } |
| | | // } |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // closeConnection(); |
| | | // } |
| | | // } |
| | | // } |
| | | // |
| | | // } |
| | | // |
| | | //// private void processData(byte[] data) { |
| | | //// // è§£ææ°æ®ï¼å¦ Modbus TCP åè®®ï¼ |
| | | //// System.out.println("Received: " + HexUtils.bytesToHex(data)); |
| | | //// } |
| | | // |
| | | // public void closeConnection() { |
| | | // try { |
| | | // input.close(); |
| | | // output.close(); |
| | | // socket.close(); |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | //} |