package com.example.bpa.service;

import com.example.bpa.Model.IRun;
import com.example.bpa.Model.IThread;
import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.config.DataBus;
import com.example.bpa.helper.MessageLog;
import com.example.bpa.helper.ModbusTcpServer;
import com.example.bpa.helper.ThreadManager;

/**
 * 加热服务
 */
public class ReheatServer {
    //region 私有单例
    private static volatile ReheatServer _instance;

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

    private ReheatServer() {
    }
    //endregion

    //region 变量
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    public void Init() {
        Business();
    }

    /**
     * 业务流程
     */
    public void Business() {
        try {
            ThreadManager.Get().StartLong("加热服务", true, new IThread() {
                @Override
                public void Run() throws InterruptedException {
                    if (DataBus.getInstance().PlcIsConnect)
                    {
                        if(DeviceData.Get().getFructoseTemperature1_up()>0 && DeviceData.Get().getFructoseTemperature1_down()>0 )
                        {
                            if(DeviceData.Get().getFructoseTemperature1_up()>DeviceData.Get().getFructoseTemperature1_down())
                            {
                                //温度低于最小值 加热
                                if(DeviceData.Get().getFructoseTemperature1()<DeviceData.Get().getFructoseTemperature1_down())
                                {
                                    if(DeviceData.Get().getReheatSwitch1()==false)
                                    {
                                        DeviceData.Get().setReheatSwitch1(true, new IWriteCallBack() {
                                            @Override
                                            public void onSuccess() {
                                            }
                                            @Override
                                            public void onFailure(String ErrorMsg) {
                                            }
                                        });
                                    }
                                }else if(DeviceData.Get().getFructoseTemperature1()>=DeviceData.Get().getFructoseTemperature1_up())
                                {
                                    if(DeviceData.Get().getReheatSwitch1()==true)
                                    {
                                        DeviceData.Get().setReheatSwitch1(false, new IWriteCallBack() {
                                            @Override
                                            public void onSuccess() {
                                            }
                                            @Override
                                            public void onFailure(String ErrorMsg) {
                                            }
                                        });
                                    }
                                }
                            }
                        }

                        if(DeviceData.Get().getFructoseTemperature2_up()>0 && DeviceData.Get().getFructoseTemperature2_down()>0 )
                        {
                            if(DeviceData.Get().getFructoseTemperature2_up()>DeviceData.Get().getFructoseTemperature2_down())
                            {
                                //温度低于最小值 加热
                                if(DeviceData.Get().getFructoseTemperature2()<DeviceData.Get().getFructoseTemperature2_down())
                                {
                                    if(DeviceData.Get().getReheatSwitch2()==false)
                                    {
                                        DeviceData.Get().setReheatSwitch2(true, new IWriteCallBack() {
                                            @Override
                                            public void onSuccess() {
                                            }
                                            @Override
                                            public void onFailure(String ErrorMsg) {
                                            }
                                        });
                                    }
                                }else if(DeviceData.Get().getFructoseTemperature2()>=DeviceData.Get().getFructoseTemperature2_up())
                                {
                                    if(DeviceData.Get().getReheatSwitch2()==true)
                                    {
                                        DeviceData.Get().setReheatSwitch2(false, new IWriteCallBack() {
                                            @Override
                                            public void onSuccess() {
                                            }
                                            @Override
                                            public void onFailure(String ErrorMsg) {
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    }

                    Thread.sleep(2000);
                }

                @Override
                public void RunComplete() throws InterruptedException {
                }
            });


        } catch (Exception ex) {
            MessageLog.ShowInfo("错误信息:" + ex.getMessage());
        }
    }
    //endregion
}
