private OCStackApplicationResult OnObserveCallback(IntPtr context, IntPtr handle, OCClientResponse clientResponse)
        {
            var payload = clientResponse.payload == IntPtr.Zero ? null : new RepPayload(clientResponse.payload);

            OnObserve?.Invoke(this, new ResourceObservationEventArgs(new DeviceAddress(clientResponse.devAddr), clientResponse.resourceUri, payload));
            return(OCStackApplicationResult.OC_STACK_KEEP_TRANSACTION);
        }
Esempio n. 2
0
        private void Adapter_OnObserve(object sender, ObserveMessageEventArgs e)
        {
            MessageAuditRecord record = null;
            int      length           = 0;
            DateTime sendTime         = DateTime.UtcNow;

            try
            {
                byte[] message = ProtocolTransition.ConvertToHttp(e.Message);
                Send(message).LogExceptions();
                OnObserve?.Invoke(this,
                                  new ChannelObserverEventArgs(Channel.Id, e.Message.ResourceUri, e.Message.ContentType,
                                                               e.Message.Message));

                length = message.Length;
                record = new MessageAuditRecord(e.Message.MessageId, identity, Channel.TypeId, "WSN", length,
                                                MessageDirectionType.Out, true, sendTime);
            }
            catch (Exception ex)
            {
                string msg = string.Format("{0} - WSN adapter observe error on channel '{1}' with '{2}'",
                                           DateTime.UtcNow.ToString("yyyy-MM-ddTHH-MM-ss.fffff"), Channel.Id, ex.Message);
                logger?.LogError(ex, $"WSN adapter observe error on channel '{Channel.Id}'.");
                record = new MessageAuditRecord(e.Message.MessageId, identity, Channel.TypeId, "WSN", length,
                                                MessageDirectionType.Out, true, sendTime, msg);
            }
            finally
            {
                if (e.Message.Audit)
                {
                    messageAuditor?.WriteAuditRecordAsync(record).Ignore();
                }
            }
        }
Esempio n. 3
0
 private void Observer_OnNotify(object sender, MessageNotificationArgs e)
 {
     //observeCount++;
     //Trace.TraceInformation("Obsever {0}", observeCount);
     //signal the protocol adapter
     OnObserve?.Invoke(this, new ObserveMessageEventArgs(e.Message));
 }
        private void Adapter_OnObserve(object sender, ObserveMessageEventArgs e)
        {
            logger?.LogDebugAsync("REST adapter received observed message");
            OnObserve?.Invoke(this, new ChannelObserverEventArgs(channel.Id, e.Message.ResourceUri, e.Message.ContentType, e.Message.Message));
            AuditRecord record = new UserAuditRecord(channel.Id, identity, DateTime.UtcNow);

            userAuditor?.UpdateAuditRecordAsync(record).Ignore();
            AuditRecord messageRecord = new MessageAuditRecord(e.Message.MessageId, identity, channel.TypeId, protocolType.ToString(), e.Message.Message.Length, MessageDirectionType.Out, true, DateTime.UtcNow);

            messageAuditor?.WriteAuditRecordAsync(messageRecord);
        }
Esempio n. 5
0
        private void Channel_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            try
            {
                MqttMessage msg = MqttMessage.DecodeMessage(e.Message);
                OnObserve?.Invoke(this, new ChannelObserverEventArgs(Channel.Id, null, null, e.Message));

                if (!session.IsAuthenticated)
                {
                    if (!(msg is ConnectMessage message))
                    {
                        throw new SecurityException("Connect message not first message");
                    }

                    if (session.Authenticate(message.Username, message.Password))
                    {
                        IdentityDecoder decoder = new IdentityDecoder(session.Config.IdentityClaimType, context,
                                                                      session.Config.Indexes);
                        session.Identity = decoder.Id;
                        session.Indexes  = decoder.Indexes;
                        adapter.Identity = decoder.Id;

                        UserAuditRecord record = new UserAuditRecord(Channel.Id, session.Identity,
                                                                     session.Config.IdentityClaimType, Channel.TypeId, "MQTT", "Granted", DateTime.UtcNow);
                        userAuditor?.WriteAuditRecordAsync(record).Ignore();
                    }
                    else
                    {
                        throw new SecurityException("Session could not be authenticated.");
                    }
                }
                else if (forcePerReceiveAuthn)
                {
                    if (!session.Authenticate())
                    {
                        throw new SecurityException("Per receive authentication failed.");
                    }
                }

                ProcessMessageAsync(msg).GetAwaiter();
            }
            catch (Exception ex)
            {
                logger?.LogErrorAsync(ex, $"MQTT adapter Channel_OnReceive error on channel '{Channel.Id}'.")
                .GetAwaiter();
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex));
            }
        }
Esempio n. 6
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();
            }
        }
 /// <summary>
 /// On observing an element, pass the element
 /// reference to the action(s).
 /// </summary>
 /// <param name="elementRef">The element to observe</param>
 public void Observe(ElementRef elementRef)
 {
     OnObserve?.Invoke(this.Id, elementRef);
 }
Esempio n. 8
0
 private void Adapter_OnObserve(object sender, ObserveMessageEventArgs e)
 {
     byte[] payload = ProtocolTransition.ConvertToHttp(e.Message);
     OnObserve?.Invoke(this, new ChannelObserverEventArgs(e.Message.ResourceUri, e.Message.ContentType, payload));
 }
Esempio n. 9
0
 /// <summary>
 /// On observing an element, pass the element
 /// reference to the action(s).
 /// </summary>
 /// <param name="elementRef">The element to observe</param>
 public async ValueTask Observe(ElementReference elementRef)
 {
     await(OnObserve.Invoke(this.Id, elementRef));
 }