using System; using Wayne.Lib; using Wayne.ForecourtControl.Fusion; #if _SINP using Wayne.ForecourtControl.Nfs; #endif namespace Wayne.ForecourtControl.OptBridge.Fusion { internal class FUSIONOpt : IOpt { // Fields private DeviceConnectionState connectionState; private int id; private FUSIONOptBridge _optBridge; public FUSIONOptBridge optBridge { get { return _optBridge; } set { _optBridge = value; } } private int reservedByClientId; public int IPPort=0, baudRate = 0, dataBit = 0, stopBit = 0, parity = 0; public string COMPort = "", address = ""; public OPTManagedBy managedBy; public OptTCP optTCP; // Events public event EventHandler OnConnectionStateChange; public event EventHandler OnDataRead; // Methods public FUSIONOpt(int id, FUSIONOptBridge _optBridge) { this.id = id; this.optBridge = _optBridge; } internal void FireOnDataRead(byte[] data) { if (this.OnDataRead != null) { this.OnDataRead.BeginInvoke(this, new OptDataEventArgs(data, 0), null, null); } } public void ReserveAsync(EventHandler reserveCompleted, object userToken) { //AsyncOperation operation = this.manager.AsyncManager.RegisterOperation(this, reserveCompleted, userToken); //Function function = new Function(0x7d1, new Parameter[] { new PrimitiveParameter(0x66, PrimitiveType.UInt16, operation.Id), new PrimitiveParameter(5, PrimitiveType.Byte, this.id) }); //this.manager.Send(function); if (reserveCompleted != null) reserveCompleted.BeginInvoke(this, new AsyncCompletedEventArgs(true, userToken), null, null); } public void UnreserveAsync(EventHandler unreserveCompleted, object userToken) { //AsyncOperation operation = this.manager.AsyncManager.RegisterOperation(this, unreserveCompleted, userToken); //Function function = new Function(0x7d2, new Parameter[] { new PrimitiveParameter(0x66, PrimitiveType.UInt16, operation.Id), new PrimitiveParameter(5, PrimitiveType.Byte, this.id) }); //this.manager.Send(function); if (unreserveCompleted != null) unreserveCompleted.BeginInvoke(this, new AsyncCompletedEventArgs(true, userToken), null, null); } public void WriteAsync(byte[] terminalData, bool waitForSendOk, EventHandler writeCompleted, object userToken) { //AsyncOperation operation = this.manager.AsyncManager.RegisterOperation(this, writeCompleted, userToken); //AsyncOperation operation2 = this.manager.AsyncManager.RegisterOperation(this, new EventHandler(this.WriteSendCompleted), operation.Id); //Function function = new Function(0x7d5, new Parameter[] { new PrimitiveParameter(0x66, PrimitiveType.UInt16, operation2.Id), new PrimitiveParameter(0x259, PrimitiveType.ByteArray, terminalData), new PrimitiveParameter(5, PrimitiveType.Byte, this.id), new PrimitiveParameter(0x77, PrimitiveType.UInt16, this.manager.ClientId), new PrimitiveParameter(0x261, PrimitiveType.UInt32, operation.Id) }); //this.manager.Send(function); if (this.managedBy == OPTManagedBy.Fusion) this.optBridge.manager.ifsfManager.OptWrite(this.Id, terminalData, writeCompleted, userToken, this); //OPTTCP else this.optTCP.OptWrite(terminalData, writeCompleted, userToken, this); } //private void WriteSendCompleted(object sender, AsyncCompletedEventArgs e) //{ // if (!e.Success && (e.UserToken is ushort)) // { // AsyncOperation operation; // ushort userToken = (ushort)e.UserToken; // if (this.manager.AsyncManager.TryGetOperation(userToken, out operation)) // { // operation.Complete(new OptWriteCompletedEventArgs(false, operation.UserToken, 0)); // } // } //} // Properties public DeviceConnectionState ConnectionState { get { return this.connectionState; } } public int Id { get { return this.id; } } public int ReservedByClientId { get { return this.reservedByClientId; } } internal DeviceConnectionState WritableConnectionState { get { return this.connectionState; } set { if (this.connectionState != value) { this.connectionState = value; Trace.WriteLine(string.Format("FUSIONOpt WritableConnectionState={0}", value)); if (this.OnConnectionStateChange != null) { this.OnConnectionStateChange.BeginInvoke(this, new ConnectionChangedEventArgs(this.connectionState), null, null); } } } } internal int WritableReservedByClientId { get { return this.reservedByClientId; } set { this.reservedByClientId = value; } } } }