package org.nl.acs.socket;

import cn.hutool.core.util.StrUtil;
import org.nl.acs.udw.UnifiedDataAccessor;
import org.nl.acs.udw.UnifiedDataAccessorFactory;
import org.nl.common.exception.BadRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.Socket;

public class ClientUDWReceive implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(ClientUDWReceive.class);
    private InputStream is;
    private Socket socket;
    private SocketListenerAutoRun listener;
    private String socket_key;

    public ClientUDWReceive(SocketListenerAutoRun listener, String socket_key, Socket socket, InputStream is) {
        this.listener = listener;
        this.socket_key = socket_key;
        this.socket = socket;
        this.is = is;
    }

    @Override
    public void run() {
        try {
            UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(SocketConfig.udw_unit_key);

            while (true) {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                byte[] b = new byte[512];

                int n;
                while ((n = this.is.read(b)) != -1) {
                    os.write(b, 0, n);
                    if (n != b.length) {
                        break;
                    }
                }

                os.close();
                if (n == -1) {
                    throw new BadRequestException("通信结束");
                }

                byte[] bytes = os.toByteArray();
                String code = new String(bytes, "UTF-8");
                System.out.println("扫到条码:" + code);
                if (StrUtil.isEmpty(code)) {
//                    this.businessLogger.setResource(this.socket_key, this.socket_key);
//                    this.businessLogger.setContainer("");
//                    this.businessLogger.log("扫到空条码无视", new Object[0]);
                } else if ("No Read".equals(code)) {
//                    this.businessLogger.setResource(this.socket_key, this.socket_key);
//                    this.businessLogger.setContainer(code);
//                    this.businessLogger.log("扫到条码{}无视", new Object[]{code});
                } else {
//                    this.businessLogger.setResource(this.socket_key, this.socket_key);
//                    this.businessLogger.setContainer(code);
//                    this.businessLogger.log("扫到条码值{}", new Object[]{code});
                    accessor_value.setValueWithPersistence(this.socket_key, code);
                }
            }
        } catch (Exception var7) {
            this.closeSocket(this.socket);
            this.listener.remove(this.socket_key);
            log.warn("", var7);
        }
    }

    protected void closeSocket(Socket socket) {
        try {
            if (socket != null && !socket.isClosed()) {
                socket.close();
            }
        } catch (Exception var3) {
        }

    }
}
