private static void SerialDataReceived(int port)
        {
            Thread.Sleep(10);
            try
            {
                int len = Device.GetIqueue;
                if (len <= 0)
                {
                    try
                    {
                        OverTimer.Stop();
                        switch (m_Bytes[0])
                        {
                        case 10:
                            if (m_Bytes[1] == 8)
                            {
                                JavascriptEvent.OperationOver();
                            }
                            else
                            {
                                DataManager.VerifyingRepetition(m_Bytes);

                                OverTimer.start();
                            }
                            break;

                        case 30:
                            if (m_Bytes[1] == 8)
                            {
                                OperationResult = OperationResults.Fail;
                            }
                            else
                            {
                                OperationResult = OperationResults.Success;
                            }
                            break;

                        case 43:
                            bool ret = m_Bytes[1] != 8;
                            JavascriptEvent.EndMessage(ret);
                            if (ret)
                            {
                                JavascriptEvent.IncrementingNumber();
                            }
                            break;

                        case 160:
                            JavascriptEvent.OperationOver();
                            if (m_Bytes[1] == 0)
                            {
                                JavascriptEvent.NewsMessage("发行器编号设置成功。");
                            }
                            else
                            {
                                JavascriptEvent.NewsMessage("发行器编号设置失败,请重新操作。");
                            }
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4Helper.ErrorInfo(ex.Message, ex);
                        JavascriptEvent.ErrorMessage(ex.Message);
                    }
                    finally
                    {
                        m_Bytes.Clear();
                    }
                    return;
                }
                byte[] by = Device.Read(len);
                if (m_Bytes == null)
                {
                    m_Bytes = new List <byte>();
                }
                m_Bytes.AddRange(by);
            }
            catch (Exception ex)
            {
                Log4Helper.ErrorInfo(ex.Message, ex);
                JavascriptEvent.ErrorMessage(ex.Message);
            }
        }
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);
                });
            };
        }