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.ConfigName;
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 && ConfigName.getInstance().AutomaticHeating)
                    {
                        //1.温度值低于设置值，加热
                        //2.温度高于设置值，停止加热
                        int sx_temp=DeviceData.Get().getBWX_FructoseTemperature();//水箱温度
                        int sx_temp_sz=DeviceData.Get().getBWX_FructoseTemperatureSZ();//水箱温度设置值

                        int jrx_temp=DeviceData.Get().getJRX_FructoseTemperature();//加热箱温度
                        int jrx_temp_sz=DeviceData.Get().getJRX_FructoseTemperatureSZ();//加热箱温度设置值

                        if(sx_temp_sz>0)
                        {
                            if(sx_temp>=sx_temp_sz)
                            {
                                DeviceData.Get().setBWXSwitch(false,null);
                            }else
                            {
                                DeviceData.Get().setBWXSwitch(true,null);
                            }
                        }

                        if(jrx_temp_sz>0)
                        {
                            if(jrx_temp>=jrx_temp_sz)
                            {
                                DeviceData.Get().setJRXSwitch(false,null);
                            }else
                            {
                                DeviceData.Get().setJRXSwitch(true,null);
                            }
                        }
                    }
                    Thread.sleep(2000);
                }

                @Override
                public void RunComplete() throws InterruptedException {
                }
            });
        } catch (Exception ex) {
            MessageLog.ShowInfo("错误信息:" + ex.getMessage());
        }
    }
    //endregion
}
