Esempio n. 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);
            }
        }
Esempio n. 2
0
        public async Task <List <MediaTypeViewModel> > GetAllMediaTypeAsync(
            CancellationToken ct = default(CancellationToken))
        {
            var mediaTypes = MediaTypeConverter.ConvertList(await _mediaTypeRepository.GetAllAsync(ct));

            return(mediaTypes);
        }
Esempio n. 3
0
        public async Task <MediaTypeViewModel> GetMediaTypeByIdAsync(int id,
                                                                     CancellationToken ct = default(CancellationToken))
        {
            var mediaTypeViewModel = MediaTypeConverter.Convert(await _mediaTypeRepository.GetByIdAsync(id, ct));

            mediaTypeViewModel.Tracks = await GetTrackByMediaTypeIdAsync(mediaTypeViewModel.MediaTypeId, ct);

            return(mediaTypeViewModel);
        }
        public Task <CoapMessage> PostAsync(CoapMessage message)
        {
            TaskCompletionSource <CoapMessage> tcs = new TaskCompletionSource <CoapMessage>();
            CoapUri             uri = new CoapUri(message.ResourceUri.ToString());
            ResponseMessageType rmt = message.MessageType == CoapMessageType.Confirmable ? ResponseMessageType.Acknowledgement : ResponseMessageType.NonConfirmable;


            registry.GetAction("POST", uri.Resource)?.Invoke(MediaTypeConverter.ConvertFromMediaType(message.ContentType), message.Payload);
            CoapMessage response = new CoapResponse(message.MessageId, rmt, ResponseCodeType.Created, message.Token);

            tcs.SetResult(response);
            return(tcs.Task);
        }
Esempio n. 5
0
        private void Channel_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            try
            {
                CoapMessage message = CoapMessage.DecodeMessage(e.Message);

                if (!session.IsAuthenticated || forcePerReceiveAuthn)
                {
                    session.EnsureAuthentication(message, forcePerReceiveAuthn);

                    UserAuditRecord record = new UserAuditRecord(Channel.Id, session.Identity,
                                                                 session.Config.IdentityClaimType, Channel.TypeId, "COAP", "Granted", DateTime.UtcNow);
                    userAuditor?.WriteAuditRecordAsync(record).Ignore();
                }

                OnObserve?.Invoke(this,
                                  new ChannelObserverEventArgs(Channel.Id, message.ResourceUri.ToString(),
                                                               MediaTypeConverter.ConvertFromMediaType(message.ContentType), message.Payload));

                Task task = Task.Factory.StartNew(async() =>
                {
                    CoapMessageHandler handler = CoapMessageHandler.Create(session, message, dispatcher);
                    CoapMessage msg            = await handler.ProcessAsync();

                    if (msg != null)
                    {
                        byte[] payload = msg.Encode();
                        await Channel.SendAsync(payload);
                    }
                });

                task.LogExceptions();
            }
            catch (Exception ex)
            {
                logger?.LogErrorAsync(ex, $"CoAP adapter receiveing on channel '{Channel.Id}'.").GetAwaiter();
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex));
                Channel.CloseAsync().Ignore();
            }
        }
Esempio n. 6
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, resourceUriString);

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

            return(channel.SendAsync(cr.Encode()));
        }
Esempio n. 7
0
        public static byte[] ConvertToCoap(CoapSession session, EventMessage message, byte[] observableToken = null)
        {
            CoapMessage coapMessage = null;
            CoapToken   token       = CoapToken.Create();

            ushort id = observableToken == null?session.CoapSender.NewId(token.TokenBytes) : session.CoapSender.NewId(observableToken);

            string uriString = CoapUri.Create(session.Config.Authority, message.ResourceUri, IsEncryptedChannel);

            if (message.Protocol == ProtocolType.MQTT)
            {
                MqttMessage    msg = MqttMessage.DecodeMessage(message.Message);
                PublishMessage pub = msg as PublishMessage;
                MqttUri        uri = new MqttUri(pub.Topic);
                if (observableToken == null)
                {
                    RequestMessageType messageType = msg.QualityOfService == QualityOfServiceLevelType.AtMostOnce ? RequestMessageType.NonConfirmable : RequestMessageType.Confirmable;
                    //request
                    coapMessage = new CoapRequest(id, messageType, MethodType.POST, new Uri(uriString), MediaTypeConverter.ConvertToMediaType(message.ContentType));
                }
                else
                {
                    //response
                    coapMessage = new CoapResponse(id, ResponseMessageType.NonConfirmable, ResponseCodeType.Content, observableToken, MediaTypeConverter.ConvertToMediaType(uri.ContentType), msg.Payload);
                }
            }
            else if (message.Protocol == ProtocolType.COAP)
            {
                CoapMessage msg = CoapMessage.DecodeMessage(message.Message);
                if (observableToken == null)
                {
                    //request
                    coapMessage = new CoapRequest(id, msg.MessageType == CoapMessageType.Confirmable ? RequestMessageType.Confirmable : RequestMessageType.NonConfirmable, MethodType.POST, new Uri(uriString), MediaTypeConverter.ConvertToMediaType(message.ContentType), msg.Payload);
                }
                else
                {
                    //response
                    coapMessage = new CoapResponse(id, ResponseMessageType.NonConfirmable, ResponseCodeType.Content, observableToken, MediaTypeConverter.ConvertToMediaType(message.ContentType), msg.Payload);
                }
            }
            else
            {
                if (observableToken == null)
                {
                    //request
                    coapMessage = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.POST, new Uri(uriString), MediaTypeConverter.ConvertToMediaType(message.ContentType), message.Message);
                }
                else
                {
                    //response
                    coapMessage = new CoapResponse(id, ResponseMessageType.NonConfirmable, ResponseCodeType.Content, observableToken, MediaTypeConverter.ConvertToMediaType(message.ContentType), message.Message);
                }
            }

            return(coapMessage.Encode());
        }