package com.example.bpa.app;

import com.example.bpa.Model.IReadCallBack;
import com.example.bpa.Model.IRun;
import com.example.bpa.Model.IThread;
import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.helper.RTrig;
import com.example.bpa.helper.ThreadManager;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class DeviceData {
    private static volatile DeviceData _instance;

    public static DeviceData Get() {
        if (_instance == null)
            _instance = new DeviceData();
        return _instance;
    }

    public void Init() {
        ThreadManager.Get().StartLong("Plc设备数据监听", true, new IThread() {
            @Override
            public void Run() throws InterruptedException {
                //获取校准值
                ModbusTcpServer.get().ReadShort("VW100", 10, new IReadCallBack<short[]>() {
                    @Override
                    public void onSuccess(short[] shorts) {
                        for (int i = 0; i < shorts.length; i++) {
                            CalibrationValue.put(i + 1, shorts[i]);
                        }
                    }
                });

                //获取称当前重量
                ModbusTcpServer.get().ReadFloat("VD260", 9, new IReadCallBack<float[]>() {
                    @Override
                    public void onSuccess(float[] floats) {
                        for (int i = 0; i < floats.length; i++) {
                            CallCurrentWeight.put(i + 1, floats[i]);
                        }
                    }
                });

                //获取校准基准时间
                ModbusTcpServer.get().ReadFloat("VD124", 1, new IReadCallBack<float[]>() {
                    @Override
                    public void onSuccess(float[] floats) {
                        CalibrationReferenceTime = floats[0];
                    }
                });

                //获取清洗参数
                ModbusTcpServer.get().ReadShort("VW140", 4, new IReadCallBack<short[]>() {
                    @Override
                    public void onSuccess(short[] shorts) {
                        DrainageTime = shorts[0];
                        AddCleaningAgentTime = shorts[1];
                        InletTime = shorts[2];
                        CyclicCleaningTime = shorts[3];
                    }
                });

                //配料完成 M0.3
                CompleteListen("M0.3", "配料完成", ChargeMixtureCompleteNotify);

                //清洗完成 M0.6
                CompleteListen("M0.6", "清洗完成", CleaningCompleteNotify);

                //去皮完成 M1.3
                CompleteListen("M1.3", "去皮完成", PeelingCompleteNotify);

                Thread.sleep(10);
            }

            @Override
            public void RunComplete() throws InterruptedException {

            }
        });
    }

    private void CompleteListen(String add, String name, IRun callback) {
        ModbusTcpServer.get().ReadBool(add, 1, new IReadCallBack<boolean[]>() {
            @Override
            public void onSuccess(boolean[] booleans) {
                RTrig.get(name).Start(booleans[0], new IRun() {
                    @Override
                    public void Run() {
                        if (callback != null)
                            callback.Run();
                    }
                });
            }
        });
    }

    /**
     * 配料完成通知
     */
    public IRun ChargeMixtureCompleteNotify;

    /**
     * 清洗完成通知
     */
    public IRun CleaningCompleteNotify;

    /**
     * 去皮完成
     */
    public IRun PeelingCompleteNotify;

    /**
     * 校准值写入
     * ,PLC 地址VW100--VW118
     *
     * @param value 需要写入的值
     * @param ch    需要写入的通道编号，1--10
     */
    public void setCalibrationValue(short value, int ch, IWriteCallBack callback) {
        if (ch >= 1 && ch <= 10) {
            String add = "VW" + (98 + ch * 2);
            ModbusTcpServer.get().WriteShort(add, value, callback);
        }
    }

    /**
     * 校准值
     */
    ConcurrentHashMap<Integer, Short> CalibrationValue = new ConcurrentHashMap<>();

    /**
     * 获取校准值
     *
     * @param ch 要获取的通道号
     * @return
     */
    public short getCalibrationValue(int ch) {
        if (!CalibrationValue.containsKey(ch))
            return 0;
        return CalibrationValue.get(ch);
    }

    /**
     * 需求值写入
     * ,PLC 地址 VW200--VW218
     *
     * @param value 需要写入的值
     * @param ch    需要写入的通道编号，1--10
     */
    public void setDemandValue(short value, int ch, IWriteCallBack callback) {
        if (ch >= 1 && ch <= 10) {
            String add = "VW" + (198 + ch * 2);
            ModbusTcpServer.get().WriteShort(add, value, callback);
        }
    }

    /**
     * 设置通道校准开关
     * ,PLC 地址 M2.0--M3.1
     *
     * @param value 需要写入的值
     * @param ch    需要写入的通道编号，1--10
     */
    public void setChCalibrationSwitch(boolean value, int ch, IWriteCallBack callback) {
        if (ch >= 1 && ch <= 10) {
            String add = ModbusTcpServer.get().getBitSingleAdd("M", 2, ch);
            if (add.length() > 0)
                ModbusTcpServer.get().WriteBool(add, value, callback);
        }
    }

    /**
     * 称去皮校准
     *
     * @param value
     * @param ch    设置称的通道号，ch1-ch8
     */
    public void setRemovePeelCalibration(boolean value, int ch, IWriteCallBack callback) {
        if (ch >= 1 && ch <= 8) {
            String add = ModbusTcpServer.get().getBitSingleAdd("M", 5, ch);
            if (add.length() > 0)
                ModbusTcpServer.get().WriteBool(add, value, callback);
        }
    }

    /**
     * 砝码值写入
     *
     * @param value
     * @param ch    设置称的砝码通道号，ch1-ch8
     */
    public void setWeightValue(boolean value, int ch, IWriteCallBack callback) {
        if (ch >= 1 && ch <= 8) {
            String add = ModbusTcpServer.get().getBitSingleAdd("M", 15, ch);
            if (add.length() > 0)
                ModbusTcpServer.get().WriteBool(add, value, callback);
        }
    }

    /**
     * 重量清零
     */
    public void setWeightClear(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M6.0", true, callback);
    }

    /**
     * 关闭写保护
     */
    public void setCloseWriteProtect(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M6.1", true, callback);
    }

    /**
     * 打开写保护
     */
    public void setOpenWriteProtect(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M6.4", true, callback);
    }

    /**
     * 零点校准
     */
    public void setZeroPointCalibration(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M6.2", true, callback);
    }

    /**
     * 砝码值写入
     */
    public void setWeightValueWrite(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M6.3", true, callback);
    }

    /**
     * 砝码校准模式
     */
    public void setWeightCalibrationMode(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M6.5", true, callback);
    }

    /**
     * 启动通道校准
     */
    public void setChStartCalibration(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M0.0", true, callback);
    }

    /**
     * 启动配料
     */
    public void setChargeMixtureStart(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M0.1", true, callback);
    }

    /**
     * 手自动切换
     *
     * @param value true:自动模式，flase:手动模式
     */
    public void setHandOrAutoSwitch(boolean value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M0.2", value, callback);
    }

    /**
     * 手自动模式
     */
    boolean HandOrAutoMode;

    /**
     * 获取手自动模式的状态
     *
     * @return true:自动模式，flase:手动模式
     */
    public boolean getHandOrAutoSwitch() {
        return HandOrAutoMode;
    }

    /**
     * 设置清洗模式
     *
     * @param value true:清洗模式，false:配料模式
     */
    public void setCleaningMode(boolean value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M0.4", value, callback);
    }

    /**
     * 清洗模式
     */
    boolean CleaningMode;

    /**
     * 获取清洗模式
     *
     * @return true:清洗模式，false:配料模式
     */
    public boolean getCleaningMode() {
        return CleaningMode;
    }

    /**
     * 设置开始清洗
     */
    public void setStartCleaning(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M0.5", true, callback);
    }

    /**
     * 设置一键去皮
     */
    public void setOneClickPeeling(IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M1.2", true, callback);
    }

    /**
     * 一键去皮完成状态
     */
    boolean OneClickPeelingComplete;

    /**
     * 设置一键去皮完成状态
     *
     * @param value
     */
    public void setOneClickPeelingComplete(boolean value) {
        OneClickPeelingComplete = value;
    }

    /**
     * 清洗完成
     */
    boolean CleaningComplete;

    /**
     * 获取清洗完成的状态
     *
     * @return
     */
    public boolean getCleaningComplete() {
        return CleaningComplete;
    }

    /**
     * 设置校准模式
     *
     * @param value true:零点校准模式，false:清零去皮模式
     */
    public void setCalibrationMode(boolean value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteBool("M1.4", value, callback);
    }

    /**
     * 校准模式
     */
    boolean CalibrationMode;

    /**
     * 获取校准模式
     *
     * @return true:零点校准模式，false:清零去皮模式
     */
    public boolean getCalibrationMode() {
        return CalibrationMode;
    }

    /**
     * 称当前重量
     */
    ConcurrentHashMap<Integer, Float> CallCurrentWeight = new ConcurrentHashMap<Integer, Float>();

    /**
     * 获取指定称的当前重量
     *
     * @param ch 需要获取的通道号，称1--称9
     * @return
     */
    public float getCallCurrentWeight(int ch) {
        if (!CallCurrentWeight.containsKey(ch)) {
            return 0.0f;
        }
        return CallCurrentWeight.get(ch);
    }

    /**
     * 设置校准基准时间
     *
     * @param value 需要写入的值
     */
    public void setCalibrationReferenceTime(float value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteFloat("VD124", value, callback);
    }

    /**
     * 校准基准时间
     */
    float CalibrationReferenceTime;

    /**
     * 获取校准基准时间
     *
     * @return
     */
    public float getCalibrationReferenceTime() {
        return CalibrationReferenceTime;
    }

    /**
     * 设置排水时间
     *
     * @param value
     */
    public void setDrainageTime(short value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteShort("VW140", value, callback);
    }

    /**
     * 排水时间
     */
    short DrainageTime;

    /**
     * 获取排水时间
     *
     * @return
     */
    public short getDrainageTime() {
        return DrainageTime;
    }

    /**
     * 设置清洗剂添加时间
     *
     * @param value
     */
    public void setAddCleaningAgentTime(short value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteShort("VW142", value, callback);
    }

    /**
     * 清洗剂添加时间
     */
    short AddCleaningAgentTime;

    /**
     * 获取清洗剂添加时间
     *
     * @return
     */
    public short getAddCleaningAgentTime() {
        return AddCleaningAgentTime;
    }

    /**
     * 设置进水时间
     *
     * @param value
     */
    public void setInletTime(short value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteShort("VW144", value, callback);
    }

    /**
     * 进水时间
     */
    short InletTime;

    /**
     * 获取进水时间
     *
     * @return
     */
    public short getInletTime() {
        return InletTime;
    }

    /**
     * 设置循环清洗时间
     *
     * @param value
     */
    public void setCyclicCleaningTime(short value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteShort("VW146", value, callback);
    }

    /**
     * 循环清洗时间
     */
    short CyclicCleaningTime;

    /**
     * 获取循环清洗时间
     *
     * @return
     */
    public short getCyclicCleaningTime() {
        return CyclicCleaningTime;
    }

    /**
     * 设置8路称校准砝码重量
     *
     * @param value
     */
    public void setCalibrationWeight(short value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteShort("250", value, callback);
    }

    /**
     * 设置称重9校准砝码重量
     *
     * @param value
     */
    public void setCalibrationWeight9(short value, IWriteCallBack callback) {
        ModbusTcpServer.get().WriteShort("254", value, callback);
    }


}
