コード例 #1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            lock (syncObject)
            {
                try
                {
                    L4Logger.Info("XfsCommandBehavior WebSocket_OnMessage => " + e.Data);
                    JObject Parametrs   = JObject.Parse(e.Data);
                    var     CommandType = (CommandType)Enum.ToObject(typeof(CommandType), int.Parse((string)Parametrs["CommandType"]));
                    switch (CommandType)
                    {
                    case CommandType.Init:
                    {
                        //var Command = JsonConvert.DeserializeObject<XfsCommandBase<i>>(e.Data);
                        //CurrentCommand = new XfsCommandBase<IXfsCommand>
                        //{
                        //    CommandType = Command.CommandType,
                        //    XfsCommand = Command.XfsCommand,
                        //    IsExecuteSuccessfully = Command.IsExecuteSuccessfully,
                        //    ServiceType = Command.ServiceType
                        //};

                        //var XFSDevice = AddOrGetServiceBase(Command);

                        //XFSDevice.OpenComplete += XFSDevice_OpenComplete;
                        //XFSDevice.OpenError += XFSDevice_OpenError;
                        //XFSDevice.RegisterComplete += XFSDevice_RegisterComplete;
                        //XFSDevice.RegisterError += XFSDevice_RegisterError;

                        break;
                    }

                    case CommandType.Open:
                    {
                        var Command = JsonConvert.DeserializeObject <XfsCommandBase <OpenCommand> >(e.Data);

                        var XFSDevice = XFS_DevicesCollection.Instance.GetValue(Command.ServiceType);

                        XFSDevice.ResetEvents();

                        XFSDevice.CurrentCommand = new XfsCommandBase <XfsCommand>
                        {
                            CommandType           = CommandType.Open,
                            XfsCommand            = Command.XfsCommand,
                            IsExecuteSuccessfully = Command.IsExecuteSuccessfully,
                            ServiceType           = Command.ServiceType
                        };


                        XFSDevice.OpenComplete         -= XFSDevice_OpenComplete;
                        XFSDevice.OpenError            -= XFSDevice_OpenError; XFSDevice.RegisterComplete -= XFSDevice_RegisterComplete;
                        XFSDevice.RegisterError        -= XFSDevice_RegisterError;
                        XFSDevice.CancelComplete       -= XFSDevice_CancelComplete;
                        XFSDevice.CancelError          -= XFSDevice_CancelError;
                        XFSDevice.ExecuteComplete      -= XFSDevice_ExecuteComplete;
                        XFSDevice.ExecuteCompleteError -= XFSDevice_ExecuteCompleteError;
                        XFSDevice.ExecuteEvent         -= XFSDevice_ExecuteEvent;


                        XFSDevice.OpenComplete         += XFSDevice_OpenComplete;
                        XFSDevice.OpenError            += XFSDevice_OpenError;
                        XFSDevice.RegisterComplete     += XFSDevice_RegisterComplete;
                        XFSDevice.RegisterError        += XFSDevice_RegisterError;
                        XFSDevice.CancelComplete       += XFSDevice_CancelComplete;
                        XFSDevice.CancelError          += XFSDevice_CancelError;
                        XFSDevice.ExecuteComplete      += XFSDevice_ExecuteComplete;
                        XFSDevice.ExecuteCompleteError += XFSDevice_ExecuteCompleteError;
                        XFSDevice.ExecuteEvent         += XFSDevice_ExecuteEvent;
                        if (Command.XfsCommand.AcceptEvents != null)
                        {
                            XFSDevice.eventHandlers.Clear();
                            XFSDevice.eventHandlers.AddRange(Command.XfsCommand.AcceptEvents);
                        }
                        XFSDevice.Open(Command.XfsCommand.ServiceName);

                        break;
                    }

                    case CommandType.Cancel:
                    {
                        XfsCommandBase <CancellCommand> Command = JsonConvert.DeserializeObject <XfsCommandBase <CancellCommand> >(e.Data);
                        var XFSDevice = XFS_DevicesCollection.Instance.GetValue(Command.ServiceType);
                        XFSDevice.CurrentCommand = new XfsCommandBase <XfsCommand>
                        {
                            CommandType           = CommandType.Cancel,
                            XfsCommand            = Command.XfsCommand,
                            IsExecuteSuccessfully = Command.IsExecuteSuccessfully,
                            ServiceType           = Command.ServiceType
                        };
                        XFSDevice.Cancel();
                        break;
                    }

                    case CommandType.Getinfo:
                    {
                        try
                        {
                            XfsCommandBase <GetInfoCommand> Command = JsonConvert.DeserializeObject <XfsCommandBase <GetInfoCommand> >(e.Data);
                            var XFSDevice = XFS_DevicesCollection.Instance.GetValue(Command.ServiceType);
                            L4Logger.Info("Start get info for " + Command.ServiceType);
                            XFSDevice.CurrentCommand = new XfsCommandBase <XfsCommand>
                            {
                                CommandType           = CommandType.Getinfo,
                                XfsCommand            = Command.XfsCommand,
                                IsExecuteSuccessfully = Command.IsExecuteSuccessfully,
                                ServiceType           = Command.ServiceType
                            };
                            L4Logger.Info("call GetStatus");
                            var obj = (ISTATUS)Activator.CreateInstance(Command.XfsCommand.StatusType);

                            XFSDevice.GetStatus(out obj, Command.XfsCommand.CommandCode, Command.XfsCommand.StatusType);

                            L4Logger.Info(" Fill info data");
                            Command.XfsCommand.Status = obj;
                            if (Command.XfsCommand.StatusTypeModel != null)
                            {
                                var model = (IXfsResultModel)Activator.CreateInstance(Command.XfsCommand.StatusTypeModel);
                                model.Fill(obj);
                                Command.XfsCommand.StatusModel = model;
                            }

                            L4Logger.Info("Call to send");
                            Command.IsExecuteSuccessfully = true;
                            SendResponse(Command);
                        }
                        catch (Exception ex)
                        {
                            L4Logger.Error(ex);
                        }
                        break;
                    }

                    case CommandType.Execute:

                    {
                        XfsCommandBase <ExecuteCommand> Command = JsonConvert.DeserializeObject <XfsCommandBase <ExecuteCommand> >(e.Data);

                        var XFSDevice = XFS_DevicesCollection.Instance.GetValue(Command.ServiceType);

                        XFSDevice.CurrentCommand = new XfsCommandBase <XfsCommand>
                        {
                            CommandType           = CommandType.Execute,
                            XfsCommand            = Command.XfsCommand,
                            IsExecuteSuccessfully = Command.IsExecuteSuccessfully,
                            ServiceType           = Command.ServiceType
                        };

                        if (Command.XfsCommand.CancelLastCommand)
                        {
                            XFSDevice.Cancel(false);
                        }

                        //unsafe
                        {
                            List <int> AcceptEvents = new List <int>();
                            if (Command.XfsCommand.AcceptEvents != null)
                            {
                                AcceptEvents.AddRange(Command.XfsCommand.AcceptEvents);
                            }


                            if (Command.XfsCommand.Events != null)
                            {
                                XFSDevice.eventHandlers.Clear();
                                XFSDevice.GlobalEvents.Clear();
                                XFSDevice.eventHandlers.AddRange(AcceptEvents);
                                Command.XfsCommand.Events.ForEach(Event =>
                                    {
                                        XFSDevice.eventHandlers.Add(Event.EventId);
                                        if (Event.IsGlobalEvent)
                                        {
                                            XFSDevice.GlobalEvents.Add(new Wrapper.Common.XFSDeviceBase.GlobalEvent
                                            {
                                                EventId        = Event.EventId,
                                                EventParam     = Event.EventParam,
                                                EventParamType = Event.EventParamType
                                            });
                                        }
                                    });
                            }
                            else
                            {
                                XFSDevice.eventHandlers.Clear();
                                XFSDevice.GlobalEvents.Clear();
                                XFSDevice.eventHandlers.AddRange(AcceptEvents);
                            }
                            XFSDevice.commandHandlers.Add(Command.XfsCommand.CommandCode);
                            IntPtr cmdPtr = IntPtr.Zero;

                            if (Command.XfsCommand.ParamModel != null)
                            {
                                //var objModel = Activator.CreateInstance(Command.XfsCommand.ParamModelType);
                                var param = XFSUtil.Cast(Command.XfsCommand.ParamModel, Command.XfsCommand.ParamModelType);
                                cmdPtr = (param as IXfsModel).ToPopinter();
                            }
                            else
                            {
                                cmdPtr = XFSUtil.StructureToPtr(Command.XfsCommand.Param, Command.XfsCommand.PramType);
                            }
                            int hResult = XFSDevice.ExecuteCommand(
                                Command.XfsCommand.CommandCode,
                                cmdPtr,
                                XFSDevice_ExecuteError);
                            Marshal.FreeHGlobal(cmdPtr);
                            if (hResult == XFS4NET.Model.Common.XFSDefinition.WFS_SUCCESS)
                            {
                                if (Command.XfsCommand.LightControlCommand != null)
                                {
                                    ExcuteSiuCommand(Command.XfsCommand.LightControlCommand.ExecuteSIU, Command.ServiceType);
                                }
                                else
                                {
                                }
                            }
                        }

                        break;
                    }
                    }
                }
                catch (Exception ex)
                {
                    L4Logger.Error(ex);
                }
            }
        }
コード例 #2
0
ファイル: BCR.cs プロジェクト: tsengee/XFS4NET
        private void ParsCommand(XfsCommandBase <object> commandBase)
        {
            switch (commandBase.CommandType)
            {
            case CommandType.Execute:
            {
                blockingQueue.Clear();
                var cmnd = JsonConvert.DeserializeObject <ExecuteCommand>(JsonConvert.SerializeObject(commandBase.XfsCommand));
                if (commandBase.IsExecuteSuccessfully)
                {
                    if (cmnd.CommandCode == BCRDefinition.WFS_CMD_BCR_READ)
                    {
                        blockingQueue.Clear();
                        try
                        {
                            var data = XFSUtil.Cast <BarcodeData[]>(cmnd.ResultModel)[0].Value;
                            ReadDataCompleted?.Invoke(data);
                        }
                        catch (Exception ex)
                        {
                            L4Logger.Error(ex);
                            //MediaError.Invoke();
                        }
                    }
                }
                else
                {
                    if (commandBase.ErrorCode == XFSDefinition.WFS_ERR_CANCELED)
                    {
                        return;
                    }
                    if (cmnd.CommandCode == BCRDefinition.WFS_CMD_BCR_READ)
                    {
                        ReadDataError?.Invoke("BCR", commandBase.ErrorCode, commandBase.ErrorCode.ToString());
                        //if (workingMode == WorkingMode.None || workingMode == WorkingMode.EjectCard)
                        //    return;
                        //else
                        //    ReadRawDataError?.Invoke("IDC", commandBase.ErrorCode, commandBase.ErrorCode.ToString());

                        //workingMode = WorkingMode.None;
                    }
                }
                break;
            }

            case CommandType.Open:
            {
                var cmnd = JsonConvert.DeserializeObject <OpenCommand>(JsonConvert.SerializeObject(commandBase.XfsCommand));
                if (commandBase.IsExecuteSuccessfully)
                {
                    if (commandBase.Detail.Equals("XFSDevice_OpenComplete"))
                    {
                        IsOpenned = true;
                        OpenCompleted?.Invoke();
                    }
                    if (commandBase.Detail.Equals("XFSDevice_RegisterComplete"))
                    {
                        RegisterCompleted?.Invoke();
                    }
                }
                else
                {
                    if (commandBase.Detail.Equals("XFSDevice_OpenError"))
                    {
                        IsOpenned = false;
                        OpenError?.Invoke(commandBase.ErrorCode);
                    }
                    if (commandBase.Detail.Equals("XFSDevice_RegisterError"))
                    {
                        RegisterError?.Invoke(commandBase.ErrorCode);
                    }
                }
                break;
            }

            case CommandType.Getinfo:
            {
                var cmnd = JsonConvert.DeserializeObject <GetInfoCommand>(JsonConvert.SerializeObject(commandBase.XfsCommand));
                GetInfoResponse?.Invoke(XFSUtil.Cast <WFSBCRSTATUS>(cmnd.Status), null);
                break;
            }
            }
        }