package com.example.bpa.app;

import com.example.bpa.config.ConfigName;
import com.example.bpa.helper.MY;
import com.example.bpa.helper.T;
import com.example.bpa.modbus4j.ModbusParam;
import com.example.bpa.modbus4j.ModbusReq;
import com.example.bpa.modbus4j.OnRequestBack;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * PLC 控制
 * add fengyoufu 20230414
 */
public class PLC {

    //region 变量
    /**
     * Ip地址
     */
    public String Host = "192.168.0.104";
    /**
     * 端口
     */
    public int Post = 502;
    /**
     * 是否已连接
     */
    public boolean IsConnect = false;
    //endregion

    //region 私有
    private static PLC mInstance; //实例变量设置私有，防止直接通过类名访问

    public static synchronized PLC getInstance() { //静态同步方法作为唯一的实例对象获取方式
        if (mInstance == null) {
            mInstance = new PLC();
        }
        return mInstance;
    }

    private PLC() { //默认构造函数私有，防止类外直接new创建对象

    }
    //endregion

    //region 外部调用事件
    /**
     * 初始化Modbus
     */
    public void InitMobus() {
        /**
        ModbusReq.getInstance().setParam(new ModbusParam()
                        .setHost(Host)//TCP PLC_IP地址
                        .setPort(Post)//端口默认502
                        .setEncapsulated(false)
                        .setKeepAlive(true)
                        .setTimeout(2000)
                        .setRetries(0))
                .init(new OnRequestBack<String>() {
                    @Override
                    public void onSuccess(String s) {
                        T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接成功!");
                    }

                    @Override
                    public void onFailed(String msg) {
                        T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接失败!" + msg);
                    }
                });
         **/
    }

    /**
     * 写PLC
     *
     * @param address
     * @param value
     */
    public void WritePlcData(String address, Object value) {
        byte slaveAddress = 1;
        int startAddress = 200;//起始地址
        //startAddress=address;

        short[] shorts = new short[2];
        shorts[0] = 100;
        shorts[1] = 200;
        //32位的float 拆成两个16位的short
//        float value = 1000000.52f;
//        int intBits = Float.floatToRawIntBits(value);
//        int[] send = new int[]{(intBits >>> 16) & 0xFFFF, intBits & 0xFFFF};
//
//        //注意高位低位处理
//        short[] shorts = new short[2];
//        shorts[0]=(short) send[1];
//        shorts[1]=(short) send[0];
        try {


            ModbusReq.getInstance().writeRegisters(new OnRequestBack<String>() {
                @Override
                public void onSuccess(String s) {
                }

                @Override
                public void onFailed(String msg) {
                }
            }, slaveAddress, startAddress, shorts);
        } catch (Exception e) {
            T.show(ConfigName.getInstance().dishesCon, "与PLC通信失败");
        }
    }

    /**
     * 读PLC数据
     * @param address
     */
    public void ReadPlcData(String address) {
        byte slaveAddress = 1;
        int startAddress = 200;//起始地址
        try {
            ModbusReq.getInstance().readInputRegisters(new OnRequestBack<short[]>() {
                @Override
                public void onSuccess(short[] var1) {
                }
                @Override
                public void onFailed(String var1) {
                }
            }, slaveAddress, startAddress, 2);

        } catch (Exception e) {
            T.show(ConfigName.getInstance().dishesCon, "与PLC通信失败");
        }
    }
    //endregion
}
