Exemplo n.º 1
0
        private static void ConnectionDeviceThread(object sender, ElapsedEventArgs e)
        {
            m_TimerEnabled = true;

            try
            {
                if (SerialPortManager.Device.IsOpen)
                {
                    return;
                }
                string deal = PortAgreement.EncryptionDevice();
                for (int i = 0; i < 3; i++)
                {
                    if (m_StopTimer)
                    {
                        return;
                    }
                    foreach (string item in PortMonitor.GetPortNames)
                    {
                        if (m_StopTimer)
                        {
                            return;
                        }
                        if (m_Serial == null)
                        {
                            m_Serial = InitSerial();
                        }
                        m_Serial.PortName = item;
                        try
                        {
                            m_Serial.Open();
                            m_Serial.Write(deal);
                            bool ret = WaitResult();
                            if (ret)
                            {
                                m_Serial.DataReceived -= SerialDataReceived;
                                Thread.Sleep(10);
                                SerialPortManager.SetSerial(m_Serial);
                                return;
                            }
                            m_Serial.Close();
                        }
                        catch (Exception ex)
                        {
                            Log4Helper.ErrorInfo(ex.Message, ex);
                        }
                    }
                }

                if (!SerialPortManager.Device.IsOpen)
                {
                    JavascriptEvent.ConnectionFail();
                }
            }
            catch (Exception ex)
            {
                Log4Helper.ErrorInfo(ex.Message, ex);
                JavascriptEvent.ErrorMessage(ex.Message);
            }
            finally
            {
                m_Serial           = null;
                m_ConnectionDevice = false;
                m_TimerEnabled     = false;
                m_StopTimer        = false;
            }
        }
Exemplo n.º 2
0
        public Main()
            : base("http://res.app.local/index.html")
        {
            InitializeComponent();

            MainForm = this;

            LoadHandler.OnLoadEnd += HtmlLoadEnd;

            GlobalObject.AddFunction("ShowDevTools").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    Chromium.ShowDevTools();
                });
            };

            GlobalObject.AddFunction("ChangeSerialConnection").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    string portName = args.Arguments[0].StringValue;
                    if (SerialPortManager.Device.IsOpen)
                    {
                        SerialPortManager.Close();
                    }
                    else
                    {
                        SerialPortManager.Open(portName);
                    }
                    args.SetReturnValue(SerialPortManager.Device.IsOpen);
                });
            };

            GlobalObject.AddFunction("ChangeConnectionState").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    bool state = args.Arguments[0].BoolValue;
                    if (state)
                    {
                        ConnectionManager.Start();
                    }
                    else
                    {
                        ConnectionManager.Stop();
                    }
                });
            };

            GlobalObject.AddFunction("ReconnectDevice").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    ConnectionManager.Start();
                });
            };

            GlobalObject.AddFunction("RefreshClick").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    bool ret = false;
                    try
                    {
                        DataManager.Params.Clear();
                        string deal = PortAgreement.ReadAllCard();
                        SerialPortManager.Write(deal);
                        OverTimer.start();

                        ret = true;
                    }
                    catch (Exception ex)
                    {
                        Log4Helper.ErrorInfo(ex.Message, ex);
                        JavascriptEvent.ErrorMessage(ex.Message);
                    }
                    args.SetReturnValue(ret);
                });
            };


            GlobalObject.AddFunction("DownloadClick").Execute += (func, args) =>
            {
                int count = DataManager.Params.Where(e => e.State != "设置成功" && e.DataType == "正常").Count();
                if (count == 0)
                {
                    args.SetReturnValue(count);
                    return;
                }
                this.RequireUIThread(() =>
                {
                    string strClientNumber = args.Arguments[0].StringValue;

                    Task.Factory.StartNew(() =>
                    {
                        foreach (Param item in DataManager.Params)
                        {
                            if (item.State != "设置成功" && item.DataType == "正常")
                            {
                                string deal = PortAgreement.WriteClientNumber(item.CardNumber, strClientNumber);
                                bool ret    = SerialPortManager.Write(deal);
                                if (ret)
                                {
                                    SerialPortManager.OperationResult = OperationResults.None;
                                    for (int i = 0; i < 500; i++)
                                    {
                                        Thread.Sleep(10);
                                        if (SerialPortManager.OperationResult != OperationResults.None)
                                        {
                                            if (SerialPortManager.OperationResult == OperationResults.Success)
                                            {
                                                item.State = "设置成功";

                                                DataManager.ViewListDisplay();
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        JavascriptEvent.OperationOver();
                    });
                });
                args.SetReturnValue(-1);
            };

            GlobalObject.AddFunction("SetDeviceClient").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    string strClientNumber = args.Arguments[0].StringValue;
                    string deal            = PortAgreement.EncryptionDevice(strClientNumber);
                    bool ret = SerialPortManager.Write(deal);
                    args.SetReturnValue(ret);
                });
            };

            GlobalObject.AddFunction("SetCardNumber").Execute += (func, args) =>
            {
                this.RequireUIThread(() =>
                {
                    string strOldNumber  = args.Arguments[0].StringValue;
                    string strCardNumber = args.Arguments[1].StringValue;
                    string strType       = args.Arguments[2].StringValue;
                    string deal          = PortAgreement.WriteCardNumber(strOldNumber, strCardNumber, strType);
                    bool ret             = SerialPortManager.Write(deal);
                    if (ret)
                    {
                        if (strCardNumber != "797979" || strCardNumber != "123456")
                        {
                            ConfigManager.SetConfig("Number", strCardNumber);
                        }
                    }
                    args.SetReturnValue(ret);
                });
            };
        }