Пример #1
0
        private void ProcessIncoming()
        {
            // Listen to tcp
            while (true)
            {
                var received = _TcpClient.Available;
                if (received > 0)
                {
                    byte[] buffer = new byte[received];
                    received = _TcpClient.GetStream().ReadAsync(buffer, 0, received).GetAwaiter().GetResult();
                    var res = Encoding.UTF8.GetString(buffer, 0, received); // text coming from TCP listener
                    res = res.Replace("\n", "");
                    var results = res.Split('\r');
                    foreach (var result in results)
                    {
                        if (!string.IsNullOrEmpty(result))
                        {
                            Console.WriteLine(result);
                            try
                            {
                                var ret = JsonConvert.DeserializeObject <Paquet>(result);
                                switch (ret.Type)
                                {
                                case PaquetType.State:
                                    State            = JsonConvert.DeserializeObject <NabState>(result);
                                    Statistics.State = State.State;
                                    StateEvent?.Invoke(this, State);
                                    break;

                                case PaquetType.EarsEvent:
                                case PaquetType.EarEvent:
                                    var earsEvent = JsonConvert.DeserializeObject <EarsEvent>(result);
                                    EarsEvent?.Invoke(this, earsEvent);
                                    break;

                                case PaquetType.ButtonEvent:
                                    var buttonEvent = JsonConvert.DeserializeObject <ButtonEvent>(result);
                                    ButtonEvent?.Invoke(this, buttonEvent);
                                    break;

                                case PaquetType.Response:
                                    var response = JsonConvert.DeserializeObject <Response>(result);
                                    if (response.Status == null)
                                    {
                                        // We had a statistics in there
                                        Statistics.Hardware    = response.Hardware;
                                        Statistics.State       = response.State;
                                        Statistics.Uptime      = response.Uptime;
                                        Statistics.Connections = response.Connections;
                                    }

                                    if (response.RequestId != null)
                                    {
                                        // If the Response.Status is null then it's the answer to the statistics
                                        _LastRequestId[response.RequestId] = response.Status == null ? new Response()
                                        {
                                            Status = Status.Ok
                                        } : response;
                                    }

                                    break;

                                case PaquetType.AsrEvent:
                                    var asrEvent = JsonConvert.DeserializeObject <AsrEvent>(result);
                                    AsrEvent?.Invoke(this, asrEvent);
                                    break;

                                case PaquetType.Statistics:
                                    Statistics = JsonConvert.DeserializeObject <Statistics>(result);
                                    break;

                                default:
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
 private static void Nabaztag_StateEvent(object sender, NabState state)
 {
     Console.WriteLine($"Nabaztag status changed, new status is {state.State}");
 }