package com.njcn.device.pq.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.socket.SocketUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * pqs
 *
 * @author cdf
 * @date 2023/8/10
 */
@Component
@Slf4j
public class SocketClientComm {


    /**
     * 装置版本升级
     *
     * @author cdf
     * @date 2023/8/10
     */
    private void normalSocket(String ip,Integer port,String msg) {

        Socket socket = null;
        BufferedReader in = null;
        OutputStream out = null;
        try{
            socket = new Socket(ip,port);
            socket.setSoTimeout(3000);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream(),StandardCharsets.UTF_8));
            out = socket.getOutputStream();





            String expression;

            while ((expression = in.readLine())==null){break;} {
                try {
                    //System.out.println(expression);
                    JSONObject res = JSONObject.fromObject(expression);


                    if("10000".equalsIgnoreCase(res.get("resultcode").toString())){
                        //成功

                    }else {
                        //错误

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }catch(Exception e){
            e.printStackTrace();

        }finally{


            //System.out.println("end---");
        }

    }




    private void safeCloseStream(InputStream in,OutputStream out,Socket socket){
        //一些必要的清理工作
        if(in != null){
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            in = null;
        }
        if(out != null){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out = null;
        }
        if(socket != null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            socket = null;
        }
    }


}
