SettingControl.cs 8.8 KB
using ConfigHelper;
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class SettingControl : UserControl
    {
        public SettingControl()
        {
            InitializeComponent();
            this.VisibleChanged += SettingControl_VisibleChanged;
        }

        private void SettingControl_VisibleChanged(object sender, EventArgs e)
        {

        }

        private void cb_EnableBuzzer_CheckedChanged(object sender, EventArgs e)
        {
            Config.Set("EnableBuzzer", cb_EnableBuzzer.Checked);
            AlarmBuzzer.Enable = cb_EnableBuzzer.Checked;
        }

        private void cb_autorun_CheckedChanged(object sender, EventArgs e)
        {
            Setting_Init.App_AutoRun = cb_autorun.Checked;
            AutoRun(Application.ExecutablePath, cb_autorun.Checked);
        }
        public static void AutoRun(string strName, bool value)
        {
            try
            {
                //创建启动对象 
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //设置运行文件
                startInfo.FileName = System.Windows.Forms.Application.StartupPath + "\\AuToRunManager.exe";
                //设置启动参数 
                startInfo.Arguments = String.Join(" ", new string[2] { strName, value.ToString() });
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                //如果不是管理员,则启动UAC 
                System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void SettingControl_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;

            Config.PropertyBind(Setting_Init.Device_EnableBuzzer.Key, cb_EnableBuzzer, "Checked", "CheckedChanged", true);
            Config.PropertyBind(Setting_Init.Printer_Labelname.Key, cb_labelselect, "SelectedItem", "SelectedValueChanged");
            Config.PropertyBind(Setting_Init.App_AutoRun.Key, cb_autorun, "Checked", "CheckedChanged", false);
            Config.PropertyBind(Setting_Init.Device_MustHavePN.Key, cb_musthavepn, "Checked", "CheckedChanged", false);
            Config.PropertyBind(Setting_Init.Device_Disable_labelling.Key, cb_disablelabelling, "Checked", "CheckedChanged", false);
            this.cb_autorun.CheckedChanged += new System.EventHandler(this.cb_autorun_CheckedChanged);
            loadlabellist();
        }

        private void btn_labeledit_Click(object sender, EventArgs e)
        {
            RobotManage.printerHelper.EditLabel();
            loadlabellist();
        }
        void loadlabellist() {
            cb_labelselect.Items.Clear();
            cb_labelselect.Items.AddRange(RobotManage.printerHelper.GetLabelList());
            cb_labelselect.SelectedItem = Setting_Init.Printer_Labelname.Val;
        }

        private void btn_Calibration_Click(object sender, EventArgs e)
        {
            if (RobotManage.isRunning)
            {
                Invoke(new Action(() => MessageBox.Show("设备正在运行,请先停止.")));
                return;
            }
            Thread td = new Thread(new ThreadStart(Calibration));
            td.Start();
        }
        bool ManualCountRunning = false;
        void Calibration()
        {
            if (ManualCountRunning)
                return;

            ManualCountRunning = true;

            //countstate?.Invoke(this, crc.GetString("xmanual_close_door", "关闭防护门"));
            RobotManage.mainMachine.CylinderMove(null, IO_Type.Counting_EnterDoor_Close, IO_Type.Counting_EnterDoor_Open, IO_VALUE.LOW);
            RobotManage.mainMachine.CylinderMove(null, IO_Type.Counting_ExitDoor_Close, IO_Type.Counting_ExitDoor_Open, IO_VALUE.LOW);
            int wait = 0;
            while (true)
            {
                if (RobotManage.mainMachine.IOValue(IO_Type.Counting_EnterDoor_Close).Equals(IO_VALUE.HIGH)
                    && RobotManage.mainMachine.IOValue(IO_Type.Counting_ExitDoor_Close).Equals(IO_VALUE.HIGH))
                {
                    break;
                }
                wait++;
                Thread.Sleep(500);
                //countstate?.Invoke(this, crc.GetString("xmanual_wait_close_door", "等待关闭防护门"));
                if (wait > 10)
                {
                    //countstate?.Invoke(this, crc.GetString("xmanual_wait_close_door_timeout", "等待关闭防护门超时"));
                    ManualCountRunning = false;
                    return;
                }
            }
            Invoke(new Action(() => MessageBox.Show("开始标定,请等待30秒")));
            if (Directory.Exists("template"))
                Directory.Delete("template", true);
            var a = RobotManage.xrayImage.GenerateTemplate(1);
            a.Wait();
            if (a.Result)
            {
                LogUtil.info("暗场校准成功");
                try
                {
                    RobotManage.mainMachine.IOMove(IO_Type.Xray_Lock, IO_VALUE.HIGH);
                    Task.Delay(1000).Wait();
                    RobotManage.XRay.Start();
                    Task.Delay(3000).Wait();
                    var b = RobotManage.xrayImage.GenerateTemplate(2);
                    b.Wait();
                    if (b.Result)
                    {
                        LogUtil.info("亮场校准成功");
                    }
                }
                finally
                {
                    RobotManage.XRay.Stop();
                    RobotManage.mainMachine.IOMove(IO_Type.Xray_Lock, IO_VALUE.LOW);
                }
            }
            RobotManage.xrayImage.Close();
            Task.Delay(1000).Wait();
            RobotManage.xrayImage.Open();
            Invoke(new Action(() => MessageBox.Show("标定完成.")));

            ManualCountRunning = false;
        }

        private void btn_openxray_Click(object sender, EventArgs e)
        {
            RobotManage.mainMachine.IOMove(IO_Type.Xray_Lock, IO_VALUE.HIGH);
            Thread.Sleep(100);
            RobotManage.XRay.Start();
        }

        private void btn_closexray_Click(object sender, EventArgs e)
        {
           
            RobotManage.XRay.Stop();
            RobotManage.mainMachine.IOMove(IO_Type.Xray_Lock, IO_VALUE.LOW);
        }

        private void btn_takephoto_Click(object sender, EventArgs e)
        {
            try
            {
                RobotManage.mainMachine.IOMove(IO_Type.Xray_Lock, IO_VALUE.HIGH);
                Thread.Sleep(100);
                RobotManage.XRay.Start();
                Thread.Sleep(2000);
                RobotManage.mainMachine.GrabImage();
                MessageBox.Show("拍照完成");
            }
            catch (Exception ex) {
                MessageBox.Show("拍照失败");
            }
            finally {
                RobotManage.XRay.Stop();
                RobotManage.mainMachine.IOMove(IO_Type.Xray_Lock, IO_VALUE.LOW);
            }
            
        }

        private void btn_printertest_Click(object sender, EventArgs e)
        {
            ReelParam reelParam = new ReelParam();
            reelParam.ReeID = "Demo ReelID";
            reelParam.PN = "Demo PN";
            reelParam.QTY = 999;
            var t =  RobotManage.printerHelper.PrintLabel(reelParam);
            t.Wait();
            if (!t.Result.Item1) {
                MessageBox.Show(t.Result.Item2);
            }else
                MessageBox.Show("打印成功");
        }

        private void buttonmunial_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PNG files (*.png)|";
            openFileDialog.InitialDirectory = "/image/Xray/back/";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                var file = openFileDialog.FileName;
                var filename = Path.GetFileNameWithoutExtension(file);
                var f = filename.Split('-');
                if (f.Length > 0)
                {
                    RobotManage.mainMachine.CountMoveInfo.MoveParam.PN = f[0];
                }
                RobotManage.mainMachine.xrayImagePath = file;
                var count = RobotManage.mainMachine.GetCountResult();
                MessageBox.Show(this, count.ToString(), "点料结果");
            }
        }
    }
}