Esempio n. 1
0
        public AnvizDevice(TcpClient socket)
        {
            DeviceStream = new AnvizStream(socket);
            DeviceStream.ReceivedPacket += (s, e) =>
            {
                switch (e.ResponseCode)
                {
                case 0x7F:
                    DevicePing?.Invoke(this, null);
                    break;

                case 0xDF:
                    ReceivedRecord?.Invoke(this, new Record(e.DATA, 0));
                    break;

                default:
                    ReceivedPacket?.Invoke(this, e);
                    break;
                }
            };
            DeviceStream.DeviceError += (s, e) =>
            {
                DeviceError?.Invoke(this, e);
            };
        }
Esempio n. 2
0
 private void OpenClient()
 {
     CheckDisposed();
     if (client == null)
     {
         client = new TcpClient();
     }
     if (!client.Connected)
     {
         client.SendTimeout       = Settings.SendTimeout;
         client.ReceiveTimeout    = Settings.ReciveTimeout;
         client.SendBufferSize    = Settings.SendBufferSize;
         client.ReceiveBufferSize = Settings.ReciveBufferSize;
         try
         {
             client.Connect(IPAddress.Parse(Settings.IPAddress), Settings.Port);
         }
         catch (Exception ex)
         {
             DeviceError?.Invoke(this, new DeviceErrorEventArgs {
                 Ex = ex
             });
         }
     }
 }
Esempio n. 3
0
        public bool Open()
        {
            CheckDisposed();
            bool result = false;

            try
            {
                if (!Busy)
                {
                    DeviceConnectingEventArgs args = new DeviceConnectingEventArgs {
                        IPAddress = Settings.IPAddress, AcceptConnection = true
                    };
                    DeviceConnecting?.Invoke(args);
                    if (args.AcceptConnection)
                    {
                        OpenClient();
                        result = StartListen();
                        Connect();
                        DeviceConnected?.Invoke(this);
                    }
                }
            }
            catch (Exception ex)
            {
                DeviceError?.Invoke(this, new DeviceErrorEventArgs {
                    Ex = ex
                });
            }
            return(result);
        }
Esempio n. 4
0
 private async void ReceiverThreadFunc()
 {
     while (DeviceSocket.Connected && !CancellationTokenSource.IsCancellationRequested)
     {
         Response response = null;
         try
         {
             response = await Response.FromStream(DeviceStream, CancellationTokenSource.Token);
         }
         catch (Exception ex)
         {
             DeviceError?.Invoke(this, ex);
         }
         if (response == null)
         {
             DeviceSocket.Close();
             taskEmitter.TrySetResult(null);
             break;
         }
         if (response.ResponseCode == 0x7F)
         {
             await new PongCommand(response.DeviceID).Send(DeviceStream);
             ReceivedPacket?.Invoke(this, response);
         }
         else if (response.ResponseCode == ResponseCode)
         {
             ResponseCode = 0;
             taskEmitter.TrySetResult(response);
         }
         else
         {
             ReceivedPacket?.Invoke(this, response);
         }
     }
 }
Esempio n. 5
0
        /* Public */

        public Controller(SerialPortStream port)
        {
            Buffer                    = new StringBuilder(16);
            TerminalQueue             = new BlockingCollectionQueue();
            DataQueue                 = new BlockingCollectionQueue();
            Port                      = port;
            Port.DataReceived        += Port_DataReceived;
            DataErrorEventThreadStart = (object x) => { DataError?.Invoke(this, (DataErrorEventArgs)x); };
            DeviceErrorThreadStart    = (object x) => { DeviceError?.Invoke(this, (TerminalEventArgs)x); };
        }
Esempio n. 6
0
 public AnvizDevice(TcpClient socket)
 {
     DeviceStream = new AnvizStream(socket);
     DeviceStream.ReceivedPacket += (s, e) =>
     {
         if (e.ResponseCode == 0x7F)
         {
             DevicePing?.Invoke(this, null);
         }
         else
         {
             ReceivedPacket?.Invoke(this, e);
         }
     };
     DeviceStream.DeviceError += (s, e) =>
     {
         DeviceError?.Invoke(this, e);
     };
 }
Esempio n. 7
0
 protected void FireDeviceError(string msg)
 {
     DeviceError?.Invoke(this, msg);
 }
 public override void OnDeviceError(CloverDeviceErrorEvent deviceErrorEvent) => DeviceError?.Invoke(deviceErrorEvent);
Esempio n. 9
0
        private void Listen(CancellationToken token)
        {
            CheckDisposed();
            DeviceStateEventArgs state = new DeviceStateEventArgs {
                IsEnabled = true
            };

            resetWait.Reset();
            NetworkStream stream = client.Connected ? client.GetStream() : null;

            while (!token.IsCancellationRequested)
            {
                if (state.IsEnabled)
                {
                    try
                    {
                        if (!client.Connected)
                        {
                            lock (lockerClient)
                            {
                                client.Dispose();
                                client = null;
                                OpenClient();
                                stream = client.Connected ? client.GetStream() : null;
                            }
                        }

                        PollingRequests();

                        if (stream?.DataAvailable ?? false)
                        {
                            byte[] buffer = new byte[client.ReceiveBufferSize];

                            int length = 0;
                            lock (lockerClient)
                            {
                                length = stream.Read(buffer, 0, buffer.Length);
                            }
                            try
                            {
                                IDeviceEventArgs message = RecivedData(buffer, length);
                                if (message != null)
                                {
                                    DeviceRecivedMessage?.Invoke(this, message);
                                }
                            }
                            catch (Exception ex)
                            {
                                DeviceError?.Invoke(this, new DeviceErrorEventArgs {
                                    Ex = ex
                                });
                            }
                        }

                        DeviceCheckState?.Invoke(this, state);
                        if (!state.IsEnabled)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        DeviceError?.Invoke(this, new DeviceErrorEventArgs {
                            Ex = ex
                        });
                    }
                }
                else
                {
                    Disconnection(false);
                }
                Thread.Sleep(pollingTimeout);
            }
            resetWait.Set();
        }
Esempio n. 10
0
 protected void InvokeDeviceError(IDeviceEventArgs e)
 {
     CheckDisposed();
     DeviceError?.Invoke(this, e);
 }
Esempio n. 11
0
 public void OnDeviceError(CloverDeviceErrorEvent deviceErrorEvent) => DeviceError?.Invoke(this, deviceErrorEvent);