package com.bonait.bnframework.modules.home.fragment.from.fragment;

import static com.bonait.bnframework.MainApplication.getContext;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.business.ExecuteTest;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.db.mode.BPA_SILOS;
import com.bonait.bnframework.common.db.res.StatusMode;
import com.bonait.bnframework.common.helper.I.IWriteCallBack;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.model.mode.SpeedEnum;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.gnleft_adapter;
import com.bonait.bnframework.modules.home.adapter.gongxubuzhou_adapter;
import com.bonait.bnframework.modules.home.fragment.mode.imagebuttom;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AutoTestOneFragment extends BaseFragment {

    //region 变量
    @BindView(R.id.datatab_left)
    ListView datatab_left;

    @BindView(R.id.runtime)
    Chronometer runtime;//计时器 计时器

    @BindView(R.id.start_goodmake)
    ImageView start_goodmake;//开始

    @BindView(R.id.showbuttomtop)
    RelativeLayout showbuttomtop;//蒙版


    //锅口朝向
    @BindView(R.id.gkcx1)
    EditText gkcx1;//
    @BindView(R.id.gkcx2)
    EditText gkcx2;//
    @BindView(R.id.gkcx3)
    EditText gkcx3;//
    @BindView(R.id.gkcx11)
    TextView gkcx11;//
    @BindView(R.id.gkcx22)
    TextView gkcx22;//


    //锅口朝向
    @BindView(R.id.touliao1)
    EditText touliao1;//
    @BindView(R.id.touliao11)
    TextView touliao11;//


    //投料
    @BindView(R.id.qingxi1)
    EditText qingxi1;//
    @BindView(R.id.qingxi11)
    TextView qingxi11;//
    @BindView(R.id.touliao_ck4)
    CheckBox touliao_ck4;//
    @BindView(R.id.touliao_ck3)
    CheckBox touliao_ck3;//
    @BindView(R.id.touliao_ck2)
    CheckBox touliao_ck2;//
    @BindView(R.id.touliao_ck1)
    CheckBox touliao_ck1;//

    //液体料
    @BindView(R.id.yetiliao1)
    EditText yetiliao1;//
    @BindView(R.id.yetiliao11)
    TextView yetiliao11;//

    @BindView(R.id.chuliao_ck4)
    CheckBox chuliao_ck4;//
    @BindView(R.id.chuliao_ck3)
    CheckBox chuliao_ck3;//
    @BindView(R.id.chuliao_ck2)
    CheckBox chuliao_ck2;//
    @BindView(R.id.chuliao_ck1)
    CheckBox chuliao_ck1;//


    //延迟
    @BindView(R.id.yanchi1)
    EditText yanchi1;//
    @BindView(R.id.yanchi2)
    EditText yanchi2;//
    @BindView(R.id.yanchi3)
    EditText yanchi3;//
    @BindView(R.id.yanchi11)
    TextView yanchi11;//
    @BindView(R.id.HuoLi)
    Spinner HuoLi;

    //加热
    @BindView(R.id.jiare1)
    EditText jiare1;//
    @BindView(R.id.jiare2)
    EditText jiare2;//
    @BindView(R.id.jiare3)
    EditText jiare3;//
    @BindView(R.id.jiare11)
    TextView jiare11;//
    @BindView(R.id.jiaredangwei)
    TextView jiaredangwei;

    //加热稳定性
    @BindView(R.id.jiarewending1)
    EditText jiarewending1;//
    @BindView(R.id.jiarewending2)
    EditText jiarewending2;//
    @BindView(R.id.jiarewending3)
    EditText jiarewending3;//
    @BindView(R.id.jiarewending11)
    TextView jiarewending11;//
    @BindView(R.id.HuoLi1)
    Spinner HuoLi1;


    @BindView(R.id.miaoshu)
    TextView miaoshu;//


    @BindView(R.id.re_gkcx)
    RelativeLayout re_gkcx;//锅口朝向

    @BindView(R.id.re_qingxi)
    RelativeLayout re_qingxi;//清洗

    @BindView(R.id.re_touliao)
    RelativeLayout re_touliao;//投料

    @BindView(R.id.re_yetiliao)
    RelativeLayout re_yetiliao;//液体料

    @BindView(R.id.re_yanchi)
    RelativeLayout re_yanchi;//延迟

    @BindView(R.id.re_jiare)
    RelativeLayout re_jiare;//加热

    @BindView(R.id.re_jiarewending)
    RelativeLayout re_jiarewending;//加热
    //endregion

    //region 界面
    private Context context;

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_auto_test_one, null);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Logger.d("我的fragment创建");
        context = getContext();
        Init();
        RefreshThread();
    }
    //endregion

    //region 公共线程
    public gnleft_adapter adapter = null;

    /**
     * 初始化
     */
    public void Init() {
        List<String> data = new ArrayList<>();
        data.add("锅口");
        data.add("清洗");
        data.add("投料");
        data.add("出料");
        data.add("延迟");
        data.add("加热");
        data.add("热稳定");

        adapter = new gnleft_adapter(getContext(), R.layout.item_left_cd, data, null);
        datatab_left.setAdapter(adapter);
        datatab_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                // TODO Auto-generated method stub
                adapter.setSelectedPosition(position);
                adapter.notifyDataSetInvalidated();
                SetSelect(position);//单击工序行，显示变量
            }
        });
        adapter.setSelectedPosition(0);//默认选中第一行
        SetSelect(0);//单击工序行，显示变量

        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(ConfigName.getInstance().HeatingGearL.keySet()));
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
        HuoLi.setAdapter(adapter2);
        HuoLi.setSelection(0);


        ArrayAdapter<String> adapter3 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(ConfigName.getInstance().HeatingGearL.keySet()));
        adapter3.setDropDownViewResource(R.layout.spinner_dropdown_item);
        HuoLi1.setAdapter(adapter3);
        HuoLi1.setSelection(0);

        runtime.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                long elapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();
                int hours = (int) (elapsedMillis / 3600000);
                int minutes = (int) (elapsedMillis - hours * 3600000) / 60000;
                int seconds = (int) (elapsedMillis - hours * 3600000 - minutes * 60000) / 1000;
                String time = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
                chronometer.setText(time);
            }
        });


        gkcxCount=ConfigName.getInstance().gkcxCount;
        jiaobanCount=ConfigName.getInstance().jiaobanCount;
        qingxiCount=ConfigName.getInstance().qingxiCount;
        touliaoCount=ConfigName.getInstance().touliaoCount;
        yetiliaoCount=ConfigName.getInstance().yetiliaoCount;
        yanchiCount=ConfigName.getInstance().yanchiCount;
        jiareCount=ConfigName.getInstance().jiareCount;
        jiarewendingCount=ConfigName.getInstance().jiarewendingCount;

        gkcx11.setText(gkcxCount + "");
        gkcx22.setText(jiaobanCount + "");
        qingxi11.setText(qingxiCount + "");
        touliao11.setText(touliaoCount + "");
        yetiliao11.setText(yetiliaoCount + "");
        yanchi11.setText(yanchiCount + "");
        jiare11.setText(jiareCount + "");
        jiarewending11.setText(jiarewendingCount + "");

    }

    /**
     * 刷新UI线程
     */
    public void RefreshThread() {
        //这是主线控制流程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        if (ExecuteTest.IsStart) {
                            switch (RunIndex) {
                                case 0:
                                    MakeGKCX_ZFZ();
                                    break;
                                case 1:
                                    MakeQX();
                                    break;
                                case 2:
                                    MakeTL();
                                    break;
                                case 3:
                                    MakeYTL();
                                    break;
                                case 4:
                                    MakeYCJR();
                                    break;
                                case 5:
                                    MakeJR();
                                    break;
                                case 6:
                                    MakeJRWDX();
                                    break;
                            }
                        }
                        Thread.sleep(1000);
                    } catch (Exception ex) {

                    }
                }
            }
        }).start();

        //这是搅拌控制逻辑
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        if (ExecuteTest.IsStart) {
                            switch (RunIndex) {
                                case 0:
                                    MakeGKCX_JB();
                                    break;
                            }
                        }

                        //故障信息
                        for (StatusMode item:ConfigName.getInstance().statusModes)
                        {
                            if(ExecuteTheRecipe.getConcurrentHash(item.sort))
                            {
                                SetMiaosShu1(item.Name);
                            }
                        }

                        String x1= ConfigName.getInstance().statusModes_电机.get(ExecuteTheRecipe.getConcurrentHash_dj("搅拌电机"));
                        String x2= ConfigName.getInstance().statusModes_电机.get(ExecuteTheRecipe.getConcurrentHash_dj("翻转电机"));
                        if(!x1.equals("无故障"))
                        {
                            SetMiaosShu1("搅拌电机故障代码："+x1);
                        }

                        if(!x2.equals("无故障"))
                        {
                            SetMiaosShu1("翻转电机故障代码："+x2);
                        }

                        Thread.sleep(2000);
                    } catch (Exception ex) {

                    }
                }
            }
        }).start();
    }
    //endregion

    //region 界面操作

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.start_goodmake, R.id.qingkong,
            R.id.qingkongjishu1,R.id.qingkongjishu2,R.id.qingkongjishu3,R.id.qingkongjishu4,R.id.qingkongjishu5,R.id.qingkongjishu6,R.id.qingkongjishu7,

    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.start_goodmake:
                if (!ExecuteTest.IsStart) {
                    if (DataIsHF()) {
                        onRecordStart();
                    }
                } else {
                    String title = "停止操作提示！";
                    String message = "请问客官确定要运行测试吗？";
                    AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            //强制结束
                            onRecordStop();
                            dialog.dismiss();
                        }
                    });

                }
                break;
            case R.id.qingkong:
                ClearMiaosShu();
                break;

            case R.id.qingkongjishu1:
                gkcxCount=0;
                ConfigName.getInstance().gkcxCount          =gkcxCount         ;
                jiaobanCount=0;
                ConfigName.getInstance().jiaobanCount          =jiaobanCount         ;
                ConfigData.getInstance().SavePZ();
                gkcx11.setText(gkcxCount + "");
                gkcx22.setText(jiaobanCount + "");
                break;
            case R.id.qingkongjishu2:
                qingxiCount=0;
                ConfigName.getInstance().qingxiCount          =qingxiCount         ;
                ConfigData.getInstance().SavePZ();
                qingxi11.setText(qingxiCount + "");
                break;
            case R.id.qingkongjishu3:
                touliaoCount=0;
                ConfigName.getInstance().touliaoCount          =touliaoCount         ;
                ConfigData.getInstance().SavePZ();
                touliao11.setText(touliaoCount + "");
                break;
            case R.id.qingkongjishu4:
                yetiliaoCount=0;
                ConfigName.getInstance().yetiliaoCount          =yetiliaoCount         ;
                ConfigData.getInstance().SavePZ();
                yetiliao11.setText(yetiliaoCount + "");
                break;
            case R.id.qingkongjishu5:
                yanchiCount=0;
                ConfigName.getInstance().yanchiCount          =yanchiCount         ;
                ConfigData.getInstance().SavePZ();
                yanchi11.setText(yanchiCount + "");
                break;
            case R.id.qingkongjishu6:
                jiareCount=0;
                ConfigName.getInstance().jiareCount          =jiareCount         ;
                ConfigData.getInstance().SavePZ();
                jiare11.setText(jiareCount + "");
                break;
            case R.id.qingkongjishu7:
                jiarewendingCount=0;
                ConfigName.getInstance().jiarewendingCount          =jiarewendingCount         ;
                ConfigData.getInstance().SavePZ();
                jiarewending11.setText(jiarewendingCount + "");
                break;
        }
    }

    /**
     * 设置选中
     *
     * @param index
     */
    public void SetSelect(int index) {
        RunIndex=index;
        if (index == 0) {
            re_gkcx.setVisibility(View.VISIBLE);
            re_qingxi.setVisibility(View.GONE);
            re_touliao.setVisibility(View.GONE);
            re_yetiliao.setVisibility(View.GONE);
            re_yanchi.setVisibility(View.GONE);
            re_jiare.setVisibility(View.GONE);
            re_jiarewending.setVisibility(View.GONE);
        } else if (index == 1) {
            re_gkcx.setVisibility(View.GONE);
            re_qingxi.setVisibility(View.VISIBLE);
            re_touliao.setVisibility(View.GONE);
            re_yetiliao.setVisibility(View.GONE);
            re_yanchi.setVisibility(View.GONE);
            re_jiare.setVisibility(View.GONE);
            re_jiarewending.setVisibility(View.GONE);

        } else if (index == 2) {
            re_gkcx.setVisibility(View.GONE);
            re_qingxi.setVisibility(View.GONE);
            re_touliao.setVisibility(View.VISIBLE);
            re_yetiliao.setVisibility(View.GONE);
            re_yanchi.setVisibility(View.GONE);
            re_jiare.setVisibility(View.GONE);
            re_jiarewending.setVisibility(View.GONE);


        } else if (index == 3) {
            re_gkcx.setVisibility(View.GONE);
            re_qingxi.setVisibility(View.GONE);
            re_touliao.setVisibility(View.GONE);
            re_yetiliao.setVisibility(View.VISIBLE);
            re_yanchi.setVisibility(View.GONE);
            re_jiare.setVisibility(View.GONE);
            re_jiarewending.setVisibility(View.GONE);

        } else if (index == 4) {
            re_gkcx.setVisibility(View.GONE);
            re_qingxi.setVisibility(View.GONE);
            re_touliao.setVisibility(View.GONE);
            re_yetiliao.setVisibility(View.GONE);
            re_yanchi.setVisibility(View.VISIBLE);
            re_jiare.setVisibility(View.GONE);
            re_jiarewending.setVisibility(View.GONE);

        } else if (index == 5) {
            re_gkcx.setVisibility(View.GONE);
            re_qingxi.setVisibility(View.GONE);
            re_touliao.setVisibility(View.GONE);
            re_yetiliao.setVisibility(View.GONE);
            re_yanchi.setVisibility(View.GONE);
            re_jiare.setVisibility(View.VISIBLE);
            re_jiarewending.setVisibility(View.GONE);

        }else if (index == 6) {
            re_gkcx.setVisibility(View.GONE);
            re_qingxi.setVisibility(View.GONE);
            re_touliao.setVisibility(View.GONE);
            re_yetiliao.setVisibility(View.GONE);
            re_yanchi.setVisibility(View.GONE);
            re_jiare.setVisibility(View.GONE);
            re_jiarewending.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 判断数据是否合法
     *
     * @return
     */
    public boolean DataIsHF() {
        boolean sucess = false;
        switch (RunIndex) {
            case 0:
                if (GetTextInput(gkcx1) == 0) {
                    SetMiaosShu("锅体正反转循环一次的圈数不能为0或空!!!");
                } else if (GetTextInput(gkcx2) == 0) {
                    SetMiaosShu("锅体正反转共运行次数不能为0或空!!!");
                } else if (GetTextInput(gkcx3) == 0) {
                    SetMiaosShu("搅拌组件运行分钟数不能为0或空!!!");
                } else {
                    sucess = true;
                    //gkcx11.setText("0");
                   // gkcx22.setText("0");
                    //gkcxCount = 0;
                    //jiaobanCount = 0;

                    gkcxYiQuan = GetTextInput(gkcx1);
                    gkcxAllCount = GetTextInput(gkcx2);
                    jiaobanAllCount = GetTextInput(gkcx3);
                }
                break;
            case 1:
                if (GetTextInput(qingxi1) == 0) {
                    SetMiaosShu("运行次数不能为0或空!!!");
                } else {
                    sucess = true;
                   // qingxi11.setText("0");
                   // qingxiCount = 0;
                    qingxiAllCount = GetTextInput(qingxi1);
                }
                break;
            case 2:
                if (GetTextInput(touliao1) == 0) {
                    SetMiaosShu("运行次数不能为0或空!!!");
                } else {
                    sucess = true;
                    //touliao11.setText("0");
                    //touliaoCount = 0;
                    touliaoAllCount = GetTextInput(touliao1);

                    tl_ck1=touliao_ck1.isChecked();
                    tl_ck2=touliao_ck2.isChecked();
                    tl_ck3=touliao_ck3.isChecked();
                    tl_ck4=touliao_ck4.isChecked();

                }
                break;
            case 3:
                if (GetTextInput(yetiliao1) == 0) {
                    SetMiaosShu("运行次数不能为0或空!!!");
                } else {
                    sucess = true;
                    //yetiliao11.setText("0");
                    //yetiliaoCount = 0;
                    yetiliaoAllCount = GetTextInput(yetiliao1);

                   cl_ck1=chuliao_ck1.isChecked();
                   cl_ck2=chuliao_ck2.isChecked();
                   cl_ck3=chuliao_ck3.isChecked();
                   cl_ck4=chuliao_ck4.isChecked();
                }
                break;
            case 4:
                if (GetTextInput(yanchi1) == 0) {
                    SetMiaosShu("加热持续时间不能为0或空!!!");
                } else if (GetTextInput(yanchi2) == 0) {
                    SetMiaosShu("冷却时间不能为0或空!!!");
                } else if (GetTextInput(yanchi3) == 0) {
                    SetMiaosShu("运行次数不能为0或空!!!");
                } else {
                    sucess = true;
                  //  yanchi11.setText("0");
                   // yanchiCount = 0;
                    yanchiAllCount = GetTextInput(yanchi3);

                    yanchiJR = GetTextInput(yanchi1);
                    yanchiLQ = GetTextInput(yanchi2);

                    dw = HuoLi.getSelectedItem().toString();
                    ExecuteTheRecipe.WritePLC("搅拌速度", 40, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            ExecuteTheRecipe.WritePLC("搅拌", true, null);
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                        }
                    });

                }
                break;
            case 5:
                if (GetTextInput(jiare1) == 0) {
                    SetMiaosShu("加热持续时间不能为0或空!!!");
                } else if (GetTextInput(jiare2) == 0) {
                    SetMiaosShu("停止时间不能为0或空!!!");
                } else if (GetTextInput(jiare3) == 0) {
                    SetMiaosShu("运行次数不能为0或空!!!");
                } else {
                    sucess = true;
                    //jiare11.setText("0");
                   // jiareCount = 0;
                    jiareAllCount = GetTextInput(jiare3);

                    jiareJR = GetTextInput(jiare1);
                    jiareLQ = GetTextInput(jiare2);

                    ExecuteTheRecipe.WritePLC("搅拌速度", 40, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            ExecuteTheRecipe.WritePLC("搅拌", true, null);
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                        }
                    });
                }
                break;
            case 6:
                if (GetTextInput(jiarewending1) == 0) {
                    SetMiaosShu("加热时间不能为0或空!!!");
                } else if (GetTextInput(jiarewending2) == 0) {
                    SetMiaosShu("再次加热时间不能为0或空!!!");
                } else if (GetTextInput(jiarewending3) == 0) {
                    SetMiaosShu("运行次数不能为0或空!!!");
                } else {
                    sucess = true;
                    //jiarewending11.setText("0");
                    //jiarewendingCount = 0;
                    jiarewendingAllCount = GetTextInput(jiarewending3);

                    jiarewendingJR = GetTextInput(jiarewending1);
                    jiarewendingLQ = GetTextInput(jiarewending2);

                    dw1 = HuoLi1.getSelectedItem().toString();

                    ExecuteTheRecipe.WritePLC("搅拌速度", 40, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            ExecuteTheRecipe.WritePLC("搅拌", true, null);
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                        }
                    });

                }
                break;
        }
        return sucess;
    }
    //endregion

    //region 锅口朝向测试
    public int gkcxCount = 0;
    public int gkcxYiQuan = 0;
    public int gkcxAllCount = 0;

    /**
     * 炒锅正反转
     */
    public void MakeGKCX_ZFZ() {
        if (gkcxCount < gkcxAllCount)//运行次数少于设置总数
        {
            for (int i = 0; i < gkcxYiQuan; i++) {
                if (ExecuteTest.IsStart) {
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get位置("原点位").processname, ExecuteTest.Get位置("原点位").processvalue);
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get位置("倒菜位").processname, ExecuteTest.Get位置("倒菜位").processvalue);
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get位置("清洗位").processname, ExecuteTest.Get位置("清洗位").processvalue);
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get位置("原点位").processname, ExecuteTest.Get位置("原点位").processvalue);
                    gkcxCount++;
                }
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                gkcx11.setText(gkcxCount + "");
                if (gkcxCount >= gkcxAllCount && jiaobanCount >= jiaobanAllCount) {
                    onRecordStop();
                }
            }
        });
    }

    public int jiaobanCount = 0;//搅拌分钟数
    public int jiaobanAllCount = 0;

    /**
     * 搅拌功能
     */
    public void MakeGKCX_JB() {
        if (jiaobanCount < jiaobanAllCount)//搅拌分钟数
        {
            Object obj1 = ExecuteTheRecipe.ReadPLC("搅拌");
            if (obj1 != null && (boolean) obj1 == false) {
                ExecuteTheRecipe.WritePLC("搅拌速度", 40, new IWriteCallBack() {
                    @Override
                    public void onSuccess() {
                        ExecuteTheRecipe.WritePLC("搅拌", true, null);
                    }

                    @Override
                    public void onFailure(String ErrorMsg) {
                    }
                });
            }

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    long elapsedMillis = SystemClock.elapsedRealtime() - runtime.getBase();
                    int mm = (int) (elapsedMillis / 1000);
                    jiaobanCount = mm / 60;
                }
            });
        } else //达到分钟数 关闭搅拌
        {
            Object obj1 = ExecuteTheRecipe.ReadPLC("搅拌");
            if (obj1 != null && (boolean) obj1) {
                ExecuteTheRecipe.WritePLC("搅拌", false, null);
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                gkcx22.setText(jiaobanCount + "");
                if (gkcxCount >= gkcxAllCount && jiaobanCount >= jiaobanAllCount) {
                    onRecordStop();
                }
            }
        });

    }
    //endregion

    //region 清洗
    public int qingxiCount = 0;
    public int qingxiAllCount = 0;

    /**
     * 清洗
     */
    public void MakeQX() {
        if (qingxiCount < qingxiAllCount)//搅拌分钟数
        {
            if (ExecuteTest.IsStart) {
                ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get清洗(0).processname, ExecuteTest.Get清洗(0).processvalue);
                qingxiCount++;
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                qingxi11.setText(qingxiCount + "");
                if (qingxiCount >= qingxiAllCount) {
                    onRecordStop();
                }
            }
        });

    }
    //endregion

    //region 投料
    public boolean tl_ck1=false;
    public boolean tl_ck2=false;
    public boolean tl_ck3=false;
    public boolean tl_ck4=false;

    public int touliaoCount = 0;
    public int touliaoAllCount = 0;

    /**
     * 投料
     */
    public void MakeTL() {
        if (touliaoCount < touliaoAllCount) {
            if (ExecuteTest.IsStart) {
                ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get位置("炒菜位1").processname, ExecuteTest.Get位置("炒菜位1").processvalue);

                if(tl_ck1)
                {
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get主料(1).processname, ExecuteTest.Get主料(1).processvalue);
                }

                if(tl_ck2)
                {
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get主料(2).processname, ExecuteTest.Get主料(2).processvalue);
                }

                if(tl_ck3)
                {
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get主料(3).processname, ExecuteTest.Get主料(3).processvalue);
                }

                if(tl_ck4)
                {
                    ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get主料(4).processname, ExecuteTest.Get主料(4).processvalue);
                }
                touliaoCount++;
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                touliao11.setText(touliaoCount + "");
                if (touliaoCount >= touliaoAllCount) {
                    onRecordStop();
                }
            }
        });

    }
    //endregion

    //region 液体料
    public boolean cl_ck1=false;
    public boolean cl_ck2=false;
    public boolean cl_ck3=false;
    public boolean cl_ck4=false;
    public int yetiliaoCount = 0;
    public int yetiliaoAllCount = 0;

    /**
     * 投料
     */
    public void MakeYTL() {
        if (yetiliaoCount < yetiliaoAllCount) {
            if (ExecuteTest.IsStart) {
                if(cl_ck1)
                {
                    ExecuteTest.Write_PLC_Material(1, 200);
                }
                if(cl_ck2)
                {
                    ExecuteTest.Write_PLC_Material(2, 200);
                }
                if(cl_ck3)
                {
                    ExecuteTest.Write_PLC_Material(3, 200);
                }
                if(cl_ck4)
                {
                    ExecuteTest.Write_PLC_Material(4, 200);
                }


                if(cl_ck1)
                {
                    DDDDD("料仓1下料完成");
                }
                if(cl_ck2)
                {
                    DDDDD("料仓2下料完成");
                }
                if(cl_ck3)
                {
                    DDDDD("料仓3下料完成");
                }
                if(cl_ck4)
                {
                    DDDDD("料仓4下料完成");
                }
                yetiliaoCount++;
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                yetiliao11.setText(yetiliaoCount + "");
                if (yetiliaoCount >= yetiliaoAllCount) {
                    onRecordStop();
                }
            }
        });

    }

    public void DDDDD(String NA)
    {
        final boolean[] IsComplete = {false};
        long a = System.currentTimeMillis();
        while (!IsComplete[0] && !ExecuteTest.IsForcedEnd) {
            if ((System.currentTimeMillis() - a) > 1000 * 100) {
                break;
            } else {
                Object sb = ExecuteTheRecipe.ReadPLC(NA);//    ListeningValue.get(name);
                if (sb != null) {
                    IsComplete[0] = (boolean) (sb);
                }
            }
            try {
                Thread.sleep(100);//10 *6
            } catch (InterruptedException e) {

            }
        }
    }
    //endregion

    //region 延迟
    public int yanchiCount = 0;
    public int yanchiAllCount = 0;

    public int yanchiJR = 0;
    public int yanchiLQ = 0;

    public String dw = "三档";

    /**
     * 延迟加热
     */
    public void MakeYCJR() {
        if (yanchiCount < yanchiAllCount) {
            if (ExecuteTest.IsStart) {
                ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get加热(dw).processname, ExecuteTest.Get加热(dw).processvalue);
                ExecuteTest.ThreadDelay(yanchiJR);//加热持续多少秒
                ExecuteTheRecipe.WritePLC("加热", false, null);
                ExecuteTest.ThreadDelay(yanchiLQ * 60);//冷却多少分钟
                yanchiCount++;
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                yanchi11.setText(yanchiCount + "");
                if (yanchiCount >= yanchiAllCount) {
                    onRecordStop();
                }
            }
        });

    }
    //endregion

    //region 加热
    public int jiareCount = 0;
    public int jiareAllCount = 0;

    public int jiareJR = 0;
    public int jiareLQ = 0;


    /**
     * 加热
     */
    public void MakeJR() {
        List<String> Dwei = new ArrayList<String>() {{
            add("一档");
            add("二档");
            add("三档");
            add("四档");
            add("五档");
            add("六档");
            add("七档");
            add("八档");
        }};
        for (String item : Dwei) {
            if (ExecuteTest.IsStart && !ExecuteTest.IsForcedEnd) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        jiaredangwei.setText(item + "");
                        jiare11.setText( "0");
                    }
                });

                for (int i = 0; i < jiareAllCount; i++) {
                    if (ExecuteTest.IsStart && !ExecuteTest.IsForcedEnd) {
                        ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get加热(item).processname, ExecuteTest.Get加热(item).processvalue);
                        ExecuteTest.ThreadDelay(jiareJR);//加热持续多少秒
                        ExecuteTheRecipe.WritePLC("加热", false, null);
                        ExecuteTest.ThreadDelay(jiareLQ * 60);//冷却多少分钟

                        jiareCount=i+1;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                jiare11.setText(jiareCount + "");
                                jiaredangwei.setText(item + "");
                            }
                        });
                    }
                }
            }
        }
        onRecordStop();
    }
    //endregion

    //region 加热稳定性
    public int jiarewendingCount = 0;
    public int jiarewendingAllCount = 0;

    public int jiarewendingJR = 0;
    public int jiarewendingLQ = 0;

    public String dw1 = "三档";

    /**
     * 加热稳定性
     */
    public void MakeJRWDX() {
        if (jiarewendingCount < jiarewendingAllCount) {
            if (ExecuteTest.IsStart) {
                ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get加热(dw1).processname, ExecuteTest.Get加热(dw1).processvalue);
                ExecuteTest.ThreadDelay(jiarewendingJR);//加热持续多少秒
                ExecuteTheRecipe.WritePLC("加热", false, null);
                ExecuteTest.ThreadDelay(1);//冷却多少分钟
                ExecuteTest.ExecuteOperationSteps(ExecuteTest.Get加热(dw1).processname, ExecuteTest.Get加热(dw1).processvalue);
                ExecuteTest.ThreadDelay(jiarewendingLQ);//冷却多少分钟
                ExecuteTheRecipe.WritePLC("加热", false, null);
                jiarewendingCount++;
            }
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                jiarewending11.setText(jiarewendingCount + "");
                if (jiarewendingCount >= jiarewendingAllCount) {
                    onRecordStop();
                }
            }
        });

    }
    //endregion

    //region 私有函数


    /**
     * 获取输入文字
     *
     * @param editText
     * @return
     */
    public int GetTextInput(EditText editText) {
        int res = 0;
        try {
            if (!editText.getText().toString().isEmpty()) {
                res = Integer.parseInt(editText.getText().toString());
            }
        } catch (Exception ex) {
            res = 0;
        }
        return res;
    }

    /**
     * 设置显示
     *
     * @param text
     */
    public void SetMiaosShu(String text) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date();
                String tempMsg = miaoshu.getText() + formatter.format(date) + "：" + text + "\n";
                miaoshu.setText(tempMsg);
                ToastUtils.warning(formatter.format(date) + "：" + text);
            }
        });
    }

    /**
     * 不重复
     * @param text
     */
    public void SetMiaosShu1(String text) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 String YL=   miaoshu.getText().toString();
                if(!YL.isEmpty() && YL.contains(text))
                {

                }else
                {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date();
                    String tempMsg =  YL+ formatter.format(date) + "：" + text + "\n";
                    miaoshu.setText(tempMsg);
                    ToastUtils.warning(formatter.format(date) + "：" + text);
                }
            }
        });
    }

    /**
     * 清空显示
     *
     * @param
     */
    public void ClearMiaosShu() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                miaoshu.setText("");
            }
        });
    }
    //endregion

    //region 计时器

    /**
     * 记录下来的总时间
     */
    private long recordingTime = 0;
    /**
     * 当前运行总时长
     */
    public int TimeOut = 0;
    /**
     * 当前运行的index
     */
    public int RunIndex = 0;

    /**
     * 启动定时器
     */
    public void onRecordStart() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ExecuteTest.StopForcedEnd();
                runtime.setBase(SystemClock.elapsedRealtime() - recordingTime);// 跳过已经记录了的时间，起到继续计时的作用
                runtime.start();
                ExecuteTest.IsStart = true;
                TimeOut = 0;
                showbuttomtop.setVisibility(View.VISIBLE);
                start_goodmake.setImageResource(R.mipmap.zhizuo_js2);
            }
        });
    }

    /**
     * 停止计时器
     */
    public void onRecordStop() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ExecuteTest.SetForcedEnd();
                runtime.stop();
                ExecuteTest.IsStart = false;

                recordingTime = 0;
                long elapsedMillis = SystemClock.elapsedRealtime() - runtime.getBase();
                TimeOut = (int) (elapsedMillis / 1000);

                showbuttomtop.setVisibility(View.GONE);
                start_goodmake.setImageResource(R.mipmap.zhizuo_ks2);

                ExecuteTheRecipe.BottomClick1("平移-去1号位");
                ExecuteTheRecipe.WritePLC("搅拌", false, null);
                ExecuteTheRecipe.WritePLC("加热", false, null);
                ExecuteTheRecipe.BottomClick1("原点位");

                ConfigName.getInstance().gkcxCount          =gkcxCount         ;
                ConfigName.getInstance().jiaobanCount       =jiaobanCount      ;
                ConfigName.getInstance().qingxiCount        =qingxiCount       ;
                ConfigName.getInstance().touliaoCount       =touliaoCount      ;
                ConfigName.getInstance().yetiliaoCount      =yetiliaoCount     ;
                ConfigName.getInstance().yanchiCount        =yanchiCount       ;
                ConfigName.getInstance().jiareCount         =jiareCount        ;
                ConfigName.getInstance().jiarewendingCount  =jiarewendingCount ;
                ConfigData.getInstance().SavePZ();
            }
        });
    }
    //endregion
}