package com.ruoyi.wms.util;

import com.ruoyi.wms.service.AGVService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Arrays;

/**
 * @Verasion:1.0
 * @Author:DZY
 * @Date:2023/7/5
 **/
@Slf4j
public class TcpUtil extends Thread {

    private AGVService agvService;
    public static TcpUtil tcpUtil;
    private Socket clientSocket;
    private PrintWriter out;
    private InputStream in;
    OutputStream outputStream = null;
    DataOutputStream dataOutputStream = null;

    @PostConstruct
    public void init() {
        tcpUtil = this;
    }

    public TcpUtil(Socket socket, AGVService agvService) {
        this.clientSocket = socket;
        this.agvService = agvService;
    }

    //public TcpUtil() {
    //    configureTasks();
    //}

    //private void configureTasks() {
    //    try {
    //        if (clientSocket == null)
    //            return;
    //        outputStream = clientSocket.getOutputStream();
    //        dataOutputStream = new DataOutputStream(outputStream);
    //        byte[] bytes = new byte[8];
    //        bytes[0] = 1;
    //        bytes[1] = 2;
    //        bytes[2] = 0;
    //        bytes[3] = 0;
    //        bytes[4] = 0;
    //        bytes[5] = 0;
    //        bytes[6] = 121;
    //        bytes[7] = -55;
    //        //log.info("发送数据：" + bytes.toString());
    //        dataOutputStream.write(bytes);
    //    } catch (IOException e) {
    //        clientSocket = null;
    //        e.printStackTrace();
    //    }
    //}

    @Override
    public void run() {
        try {
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = clientSocket.getInputStream();
            while (true) {
                log.info("接收");
                log.info(clientSocket.toString());
                String inputLine = "";
                byte[] temp = new byte[10];
                int length = 0;
                //clientSocket.shutdownOutput();
                try {
                    length = in.read(temp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (length == -1) {
                    return;
                }
                inputLine += new String(temp, 0, length, "UTF-8");
                //log.info("inputLine:" + inputLine);
                if (".".equals(inputLine)) {
                    out.println("bye");
                    break;
                }
                final String s = BinaryToHexString(temp);
                final String[] s1 = s.split(" ");
                //log.info("tcps:" + s);
                //设备信号 02：空车入库 01：出库1 04：出库2
                String status = s1[3];
                if (status.equals("06") || status.equals("04")) {
                    //成品出库
                    agvService.outProdStore("04");
                } else if (status.equals("03") || status.equals("01")) {
                    //成品出库
                    agvService.outProdStore("01");
                } else if (status.equals("00") || status.equals("02")) {
                    //成品出库
                    agvService.outProdStore("04");
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    agvService.outProdStore("01");
                }
            }
            in.close();
            out.close();
            clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 将字节数组转换成十六进制的字符串
     *
     * @return
     */
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }
}
