public void GetStatus() { try { if (!_WebSocket.IsAlive) { _WebSocket.Connect(); } XfsCommandBase <GetInfoCommand> xfsCommand = new XfsCommandBase <GetInfoCommand> { CommandType = CommandType.Getinfo, ServiceType = ServiceTypes.BCR, XfsCommand = new GetInfoCommand { CommandCode = BCRDefinition.WFS_INF_BCR_STATUS, StatusType = typeof(WFSBCRSTATUS), Status = new WFSBCRSTATUS(), Result = IntPtr.Zero, } }; workingMode = WorkingMode.GettingStatus; _WebSocket.Send(Newtonsoft.Json.JsonConvert.SerializeObject(xfsCommand, Formatting.Indented)); } catch (Exception ex) { L4Logger.Error(ex); } }
public void Fill(object result) { var data = (WFSCIMSTATUS)result; this.fwDevice = data.fwDevice; this.fwIntermediateStacker = data.fwIntermediateStacker; this.fwSafeDoor = data.fwSafeDoor; this.lpszExtra = data.lpszExtra; this.fwAcceptor = data.fwAcceptor; this.fwStackerItems = data.fwStackerItems; this.fwBanknoteReader = data.fwBanknoteReader; this.bDropBox = data.bDropBox; var tmp = Activator.CreateInstance(typeof(WFSCIMOUTPOS)); XFSUtil.PtrToStructure(data.lppPositions, typeof(WFSCIMOUTPOS), ref tmp); try { this.lppPositions = new WFSCIMOUTPOS { fwPosition = ((WFSCIMOUTPOS)tmp).fwPosition, fwPositionStatus = ((WFSCIMOUTPOS)tmp).fwPositionStatus, fwShutter = ((WFSCIMOUTPOS)tmp).fwShutter, fwTransport = ((WFSCIMOUTPOS)tmp).fwTransport, fwTransportStatus = ((WFSCIMOUTPOS)tmp).fwTransportStatus }; } catch (Exception ex) { L4Logger.Error(ex); L4Logger.Info(string.Format("Device Result => {0} And PositionXfs {1} )", Newtonsoft.Json.JsonConvert.SerializeObject(tmp), data.lppPositions)); this.lppPositions = new WFSCIMOUTPOS(); } }
public void Fill(object result) { var data = (WFS_CDM_STATUS)result; this.dwGuidLights = data.dwGuidLights; this.fwDevice = data.fwDevice; this.fwDispenser = data.fwDispenser; this.fwIntermediateStacker = data.fwIntermediateStacker; this.fwSafeDoor = data.fwSafeDoor; this.lpszExtra = data.lpszExtra; this.usPowerSaveRecoveryTime = data.usPowerSaveRecoveryTime; this.wDevicePosition = data.wDevicePosition; var tmp = Activator.CreateInstance(typeof(WFS_CDM_OUTPOS)); XFSUtil.PtrToStructure(data.lppPositions, typeof(WFS_CDM_OUTPOS), ref tmp); try { this.lppPositions = new WFS_CDM_OUTPOS { fwPosition = ((WFS_CDM_OUTPOS)tmp).fwPosition, fwPositionStatus = ((WFS_CDM_OUTPOS)tmp).fwPositionStatus, fwShutter = ((WFS_CDM_OUTPOS)tmp).fwShutter, fwTransport = ((WFS_CDM_OUTPOS)tmp).fwTransport, fwTransportStatus = ((WFS_CDM_OUTPOS)tmp).fwTransportStatus }; } catch (Exception ex) { L4Logger.Error(ex); L4Logger.Info(string.Format("Device Result => {0} And PositionXfs {1} )", Newtonsoft.Json.JsonConvert.SerializeObject(tmp), data.lppPositions)); this.lppPositions = new WFS_CDM_OUTPOS(); } }
public void Open(string logicName, bool paramAutoRegister = true, string appID = "CitydiXFS", string lowVersion = "3.0", string highVersion = "3.0") { try { Task.Run(() => { L4Logger.Info("xfs to open service => " + logicName); }); serviceName = logicName; autoRegister = paramAutoRegister; int requestVersion = XFSUtil.ParseVersionString(lowVersion, highVersion); WFSVERSION srvcVersion = new WFSVERSION(); WFSVERSION spVersion = new WFSVERSION(); int hResult = 0; if (!isStartup) { hResult = XfsApi.WFSStartUp(requestVersion, ref spVersion); Task.Run(() => { L4Logger.Info("xfs start result =>" + hResult); }); if (hResult != XFSDefinition.WFS_SUCCESS && hResult != XFSDefinition.WFS_ERR_ALREADY_STARTED) { OnOpenError(hResult); return; } } appID = "Citydi"; Task.Run(() => { L4Logger.Info(string.Format( "logicName {0} appID {1} hService {2} MessageHandle {3} requestVersion {4} srvcVersion {5} spVersion {6} requestID {7}", logicName, appID, hService, MessageHandle, requestVersion, srvcVersion, spVersion, requestID)); }); //hResult= XfsApi.WFSOpen(logicName, IntPtr.Zero, appID, XFSDefinition.WFS_TRACE_API, XFSConstants.WFS_INDEFINITE_WAIT, requestVersion, ref srvcVersion, ref spVersion, ref hService); //L4Logger.Info(string.Format(" service {0} WFSOpen resule {1}", logicName, hResult)); hResult = XfsApi.WFSAsyncOpen(logicName, IntPtr.Zero, appID, XFSDefinition.WFS_TRACE_ALL_API, XFSConstants.WFS_INDEFINITE_WAIT, ref hService, MessageHandle, requestVersion, ref srvcVersion, ref spVersion, ref requestID); Task.Run(() => { L4Logger.Info(string.Format(" service {0} WFSAsyncOpen resule {1}", logicName, hResult)); }); if (hResult != XFSDefinition.WFS_SUCCESS) { OnOpenError(hResult); } } catch (Win32Exception ex) { L4Logger.Info(ex); } catch (Exception ex) { L4Logger.Error(ex); OnOpenError(-10000); } }
public void ReadData() { L4Logger.Info(MethodBase.GetCurrentMethod().Name + " Start"); if (!blockingQueue.Enqueue( new TaskModel { MethodName = "ReadData", WaitForResponse = true } )) { L4Logger.Info("another operation in progress"); return; } L4Logger.Info(MethodBase.GetCurrentMethod().Name + " Start for sending xfs command"); try { if (!_WebSocket.IsAlive) { _WebSocket.Connect(); } WFSBCRREADINPUT source = new WFSBCRREADINPUT { lpwSymbologies = (ushort)BarcodeType.WFS_BCR_SYM_UNKNOWN }; XfsCommandBase <XfsCommand> xfsCommand = new XfsCommandBase <XfsCommand> { CommandType = CommandType.Execute, ServiceType = ServiceTypes.BCR, XfsCommand = new ExecuteCommand { CommandCode = BCRDefinition.WFS_CMD_BCR_READ, Param = source, ResultModel = new BarcodeData[1], PramType = typeof(WFSBCRREADINPUT), ResultModelType = typeof(BarcodeData), ResultXfs = new WFSBCRREADOUTPUT[1], ResultXfsType = typeof(WFSBCRREADOUTPUT), CancelLastCommand = false, LightControlCommand = new LightControlCommand { }, AcceptEvents = new List <int> { } } }; _WebSocket.Send(Newtonsoft.Json.JsonConvert.SerializeObject(xfsCommand, Formatting.Indented)); } catch (Exception ex) { L4Logger.Error(ex); } }
private void SendResponse(object obj) { L4Logger.Info("Sending Response => " + JsonConvert.SerializeObject(obj, Formatting.Indented)); try { Send(JsonConvert.SerializeObject(obj, Formatting.Indented)); } catch (Exception ex) { L4Logger.Error(ex); } }
private void _WebSocket_OnError(object sender, ErrorEventArgs e) { IsOpenned = false; L4Logger.Error(string.Format(" BCR Connection Error => Message {0} , Exception {1}", e.Message, e.Exception)); return; try { _WebSocket.Close(); } catch (Exception ex) { L4Logger.Error(ex); } try { _WebSocket.Connect(); } catch (Exception ex) { L4Logger.Error(ex); } }
private void _WebSocket_OnClose(object sender, CloseEventArgs e) { IsOpenned = false; L4Logger.Error(string.Format(" BCR Connection Error => Code {0} , Reason {1}", e.Code, e.Reason)); return; try { _WebSocket.Close(); } catch (Exception ex) { L4Logger.Error(ex); } try { _WebSocket.Connect(); } catch (Exception ex) { L4Logger.Error(ex); } }
private void _WebSocket_OnMessage(object sender, MessageEventArgs e) { try { L4Logger.Info("Barcode Reader _WebSocket_OnMessage => " + e.Data); JObject Parametrs = JObject.Parse(e.Data); var Datatype = (Datatype)Enum.ToObject(typeof(Datatype), int.Parse((string)Parametrs["Datatype"])); switch (Datatype) { case Datatype.Command: { try { var obj = JsonConvert.DeserializeObject <XfsCommandBase <object> >(e.Data); ParsCommand(obj); L4Logger.Info("XfsCommandBase<IXfsCommand> data recived "); } catch (Exception ex) { L4Logger.Error(ex); } break; } case Datatype.Event: { try { var obj = Newtonsoft.Json.JsonConvert.DeserializeObject <ExecuteEventBase>(e.Data); ParsEvent(obj); L4Logger.Info("ExecuteEventBase data recived "); } catch (Exception ex) { L4Logger.Error(ex); } break; } } } catch (Exception ex) { L4Logger.Error(ex); } }
public void Cancel() { L4Logger.Info(MethodBase.GetCurrentMethod().Name + " Start for sending xfs command"); try { if (!_WebSocket.IsAlive) { _WebSocket.Connect(); } XfsCommandBase <XfsCommand> xfsCommand = new XfsCommandBase <XfsCommand> { CommandType = CommandType.Cancel, ServiceType = ServiceTypes.BCR, XfsCommand = new ExecuteCommand { } }; _WebSocket.Send(Newtonsoft.Json.JsonConvert.SerializeObject(xfsCommand, Formatting.Indented)); } catch (Exception ex) { L4Logger.Error(ex); } }
private void XFSDevice_ExecuteComplete(ServiceTypes serviceType, IntPtr obj, int EventID) { try { lock (syncObject) { if (obj == IntPtr.Zero) { XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.IsExecuteSuccessfully = true; XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.Detail = "XFSDevice_ExecuteComplete"; var XfsCommand = (XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.XfsCommand as ExecuteCommand); if (XfsCommand != null && XfsCommand.LightControlCommand != null) { ExcuteSiuCommand(XfsCommand.LightControlCommand.CompleteSIU, serviceType); } SendResponse(XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand); return; } else { XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.IsExecuteSuccessfully = true; #region old //switch (EventID) //{ // case IDCDefinition.WFS_CMD_IDC_READ_RAW_DATA: // { // //(CurrentCommand.XfsCommand as ExecuteCommand).Result = Activator.CreateInstance((CurrentCommand.XfsCommand as ExecuteCommand).ResultType); // var resobj = OnReadRawDataComplete(obj); // if(resobj.Length==0) // { // XFSDevice_ExecuteError(serviceType, "", -500, "-500"); // return; // } // (XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.XfsCommand as ExecuteCommand).ResultModel = resobj; // break; // } // default: // { // break; // } //} #endregion XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.Detail = "XFSDevice_ExecuteComplete"; var XfsCommand = (XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand.XfsCommand as ExecuteCommand); if ((XfsCommand.ResultXfs.GetType().Name.Equals("JArray"))) { int len = 0; var data = XFSUtil.XFSPtrToArray(obj, XfsCommand.ResultXfsType, ref len); var model = Array.CreateInstance(XfsCommand.ResultModelType, len); for (int i = 0; i < len; ++i) { var tmp = (IXfsResultModel)Activator.CreateInstance(XfsCommand.ResultModelType); tmp.Fill(data.GetValue(i)); model.SetValue(tmp, i); } XfsCommand.ResultModel = model; } else { object data = null; XFSUtil.PtrToStructure(obj, XfsCommand.ResultXfsType, ref data); var model = (IXfsResultModel)Activator.CreateInstance(XfsCommand.ResultModelType); model.Fill(data); XfsCommand.ResultModel = model; } if (XfsCommand != null && XfsCommand.LightControlCommand != null) { ExcuteSiuCommand(XfsCommand.LightControlCommand.CompleteSIU, serviceType); } SendResponse(XFS_DevicesCollection.Instance.GetValue(serviceType).CurrentCommand); } } } catch (Exception ex) { L4Logger.Error(ex); } }
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); } } }
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; } } }