Пример #1
0
        public async Task PublishAsync(string resourceUriString, string contentType, byte[] payload, bool confirmable,
                                       Action <CodeType, string, byte[]> action)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            byte[] token         = CoapToken.Create().TokenBytes;
            ushort id            = session.CoapSender.NewId(token, null, action);
            string scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string coapUriString = GetCoapUriString(scheme, resourceUriString);

            RequestMessageType mtype = confirmable ? RequestMessageType.Confirmable : RequestMessageType.NonConfirmable;
            CoapRequest        cr    = new CoapRequest(id, mtype, MethodType.POST, token, new Uri(coapUriString),
                                                       MediaTypeConverter.ConvertToMediaType(contentType), payload);

            queue.Enqueue(cr.Encode());

            while (queue.Count > 0)
            {
                byte[] message = queue.Dequeue();
                Task   t       = channel.SendAsync(message);
                await Task.WhenAll(t);
            }
        }
Пример #2
0
        public void SendMessage(NAE.Data.Telemetry message)
        {
            string jsonString = JsonConvert.SerializeObject(message);

            byte[] payload = Encoding.UTF8.GetBytes(jsonString);

            if (messageId == ushort.MaxValue || messageId == 0)
            {
                messageId = 1;
            }

            string      resource   = "http://pegasusnae.org/telemetry";
            Uri         requestUri = new Uri(String.Format("coaps://{0}/publish?topic={1}", CoapAuthority, resource));
            CoapRequest request    = new CoapRequest(messageId++, RequestMessageType.NonConfirmable, MethodType.POST, requestUri, MediaType.Json, payload);

            byte[] coap = request.Encode();

            try
            {
                Task task = client.SendAsync(coap);
                Task.WhenAll(task);
                Trace.TraceInformation("Message sent to cloud.");
                Trace.TraceInformation(jsonString);
            }
            catch (Exception ex)
            {
                Trace.TraceWarning("Web Socket expection.");
                Trace.TraceError(ex.Message);
            }
        }
Пример #3
0
        public async Task UnobserveAsync(string resourceUriString)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            if (observers.ContainsKey(resourceUriString))
            {
                string tokenString   = observers[resourceUriString];
                byte[] token         = Convert.FromBase64String(tokenString);
                ushort id            = session.CoapSender.NewId(token, false);
                string scheme        = channel.IsEncrypted ? "coaps" : "coap";
                string coapUriString = GetCoapUriString(scheme, resourceUriString);

                CoapRequest request = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.GET,
                                                      new Uri(coapUriString), null)
                {
                    Observe = false
                };
                await channel.SendAsync(request.Encode());

                session.CoapSender.Unobserve(Convert.FromBase64String(tokenString));
            }
        }
Пример #4
0
        public async Task SubscribeAsync(Uri uri)
        {
            CoapRequest request = new CoapRequest(messageId++, RequestMessageType.NonConfirmable, MethodType.POST, uri, MediaType.Json);

            byte[] message = request.Encode();
            await client.SendAsync(message);
        }
Пример #5
0
        public void Subscribe(Uri uri)
        {
            CoapRequest request = new CoapRequest(messageId++, RequestMessageType.NonConfirmable, MethodType.POST, uri, MediaType.Json);

            byte[] message = request.Encode();
            Task   task    = client.SendAsync(message);

            Task.WaitAny(task);

            Trace.TraceInformation("Subscribed - {0}", uri.ToString());
        }
Пример #6
0
        private async Task RunViaWebSocket()
        {
            if (!this.ws.IsConnected)
            {
                Thread.Sleep(5000);
            }

            int    index       = 0;
            ushort id          = 0;
            double gpsLatStart = 0;
            double gpsLonStart = 0;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Resources.RT_Telemetry_Test2)))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        id++;
                        string line = await reader.ReadLineAsync();

                        NAE.Data.Telemetry t = NAE.Data.Telemetry.Load(line);

                        if (gpsLatStart == 0)
                        {
                            gpsLatStart = t.GpsLatitude;
                            gpsLonStart = t.GpsLongitude;
                        }

                        t.GpsLatitudeStart  = gpsLatStart;
                        t.GpsLongitudeStart = gpsLonStart;
                        t.RunId             = this.runId;

                        string      jsonString    = JsonConvert.SerializeObject(t);
                        string      coapUriString = String.Format("coaps://pegasusmission.io/publish?topic={0}", telemetryUriString);
                        CoapRequest request       = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.POST,
                                                                    new Uri(coapUriString), MediaType.Json, Encoding.UTF8.GetBytes(jsonString));

                        byte[] message = request.Encode();
                        await ws.SendAsync(message);

                        index++;
                        Console.WriteLine("Sending message {0}  sleeping 500ms AirSpeed {1}", index, t.GpsSpeedMph);
                        Thread.Sleep(500);
                    }
                }
            }
        }
Пример #7
0
        public async Task UnsubscribeAsync(string resourceUriString, bool confirmable, Action <CodeType, string, byte[]> action)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            byte[]             token         = CoapToken.Create().TokenBytes;
            ushort             id            = session.CoapSender.NewId(token, null, action);
            string             scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string             coapUriString = GetCoapUriString(scheme, config.Authority, resourceUriString);
            RequestMessageType mtype         = confirmable ? RequestMessageType.Confirmable : RequestMessageType.NonConfirmable;
            CoapRequest        cr            = new CoapRequest(id, mtype, MethodType.DELETE, token, new Uri(coapUriString), null);
            await channel.SendAsync(cr.Encode());
        }
Пример #8
0
        public async Task SubscribeAsync(string resourceUriString, NoResponseType nrt)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            byte[]      token         = CoapToken.Create().TokenBytes;
            ushort      id            = session.CoapSender.NewId(token);
            string      scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string      coapUriString = GetCoapUriString(scheme, config.Authority, resourceUriString);
            CoapRequest cr            = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.PUT, token, new Uri(coapUriString), null);

            cr.NoResponse = nrt;
            await channel.SendAsync(cr.Encode());
        }
Пример #9
0
        public async Task ObserveAsync(string resourceUriString, Action <CodeType, string, byte[]> action)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            byte[]      token         = CoapToken.Create().TokenBytes;
            ushort      id            = session.CoapSender.NewId(token, true, action);
            string      scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string      coapUriString = GetCoapUriString(scheme, config.Authority, resourceUriString);
            CoapRequest cr            = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.GET, token, new Uri(coapUriString), null);

            cr.Observe = true;
            observers.Add(resourceUriString, Convert.ToBase64String(token));
            byte[] observeRequest = cr.Encode();
            await channel.SendAsync(observeRequest);
        }
Пример #10
0
        public Task PublishAsync(string resourceUriString, string contentType, byte[] payload, NoResponseType nrt)
        {
            if (!channel.IsConnected)
            {
                Open();
                Receive();
            }

            session.UpdateKeepAliveTimestamp();

            byte[] token         = CoapToken.Create().TokenBytes;
            ushort id            = session.CoapSender.NewId(token);
            string scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string coapUriString = GetCoapUriString(scheme, config.Authority, resourceUriString);


            CoapRequest cr = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.POST, new Uri(coapUriString), MediaTypeConverter.ConvertToMediaType(contentType), payload);

            cr.NoResponse = nrt;
            return(channel.SendAsync(cr.Encode()));
        }
Пример #11
0
        public void Connect()
        {
            if (client != null && client.IsConnected)
            {
                return;
            }

            if (client == null)
            {
                client = new WebSocketClient();
            }

            if (client.IsConnected)
            {
                Task closeTask = client.CloseAsync();
                Task.WaitAny(closeTask);

                if (OnStatusChange != null)
                {
                    OnStatusChange(this, "Closed");
                }

                client = null;
            }

            if (OnStatusChange != null)
            {
                OnStatusChange(this, "Connecting");
            }

            client = new WebSocketClient();

            client.OnClose   += client_OnClose;
            client.OnError   += client_OnError;
            client.OnOpen    += client_OnOpen;
            client.OnMessage += client_OnMessage;

            Task task = Task.Factory.StartNew(async() =>
            {
                await client.ConnectAsync(host, subprotocol, token);
            });

            Task.WhenAll(task);

            while (!client.IsConnected)
            {
                Thread.Sleep(100);
            }


            if (OnStatusChange != null)
            {
                OnStatusChange(this, "Connected");
            }

            foreach (Uri uri in subscriptions)
            {
                CoapRequest request = new CoapRequest(messageId++, RequestMessageType.NonConfirmable, MethodType.POST, uri, MediaType.Json);
                client.Send(request.Encode());
                //Task subTask = client.SendAsync(request.Encode());
                //Task.WaitAll(subTask);
            }
        }