Beispiel #1
0
 public MQTTInbound(MqttClient connection, String sitewhereTopic, String commandTopic, IAgentCommandProcessor processor, ISiteWhereEventDispatcher dispatcher)
 {
     this.connection     = connection;
     this.sitewhereTopic = sitewhereTopic;
     this.commandTopic   = commandTopic;
     this.processor      = processor;
     this.dispatcher     = dispatcher;
 }
        /// <summary>
        /// process custom command (SiteWhere/commands/HardwareId)
        /// </summary>
        /// <param name="message"></param>
        /// <param name="dispatcher"></param>
        public void processSpecificationCommand(byte[] message, ISiteWhereEventDispatcher dispatcher)
        {
            try
            {
                dynamic obj         = Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(message));
                string  commandName = (string)obj.command.command.name;

                var method = this.GetType().GetMethod(commandName);
                if (method != null)
                {
                    var inputParas = (Newtonsoft.Json.Linq.JObject)obj.command.parameters;
                    Dictionary <string, object> paralist = new Dictionary <string, object>();

                    foreach (var p in inputParas)
                    {
                        paralist.Add(p.Key, ((Newtonsoft.Json.Linq.JValue)p.Value).Value);
                    }

                    List <object> sortedParas = new List <object>();

                    foreach (var p in method.GetParameters())
                    {
                        var find = paralist.Where(i => i.Key.ToUpper() == p.Name.ToUpper());
                        if (find.Any())
                        {
                            sortedParas.Add(find.First().Value);
                        }
                    }

                    var originid = (string)obj.command.invocation.id;
                    sortedParas.Add(new DeviceEventOriginator(originid));

                    method.Invoke(this, sortedParas.ToArray());
                }
            }
            catch (Exception e)
            {
                LOGGER.info(e.Message);
            }
        }
 public override void executeStartupLogic(string hardwareId, string specificationToken, ISiteWhereEventDispatcher dispatcher)
 {
     sendRegistration(hardwareId, specificationToken);
     base.executeStartupLogic(hardwareId, specificationToken, dispatcher);
 }
Beispiel #4
0
 public void setDispatcher(ISiteWhereEventDispatcher dispatcher)
 {
     this.dispatcher = dispatcher;
 }
        /// <summary>
        /// process SiteWhere command (SiteWhere/system/HardwareId)
        /// </summary>
        /// <param name="message"></param>
        /// <param name="dispatcher"></param>
        public void processSiteWhereCommand(byte[] message, ISiteWhereEventDispatcher dispatcher)
        {
            if (this.processProto == ProcessProtocolEnum.Json)
            {
                try
                {
                    dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(message));

                    switch ((string)obj.systemCommand.type)
                    {
                    case "RegistrationAck":

                        try
                        {
                            longitudeSettingByServer = obj.nestingContext.gateway.metadata.longitude;
                            latitudeSettingByServer  = obj.nestingContext.gateway.metadata.latitude;
                            elevationSettingByServer = obj.nestingContext.gateway.metadata.elevation;
                        }
                        catch
                        {
                        }

                        handleRegistrationAckJson((string)obj.systemCommand.reason);
                        break;
                    }
                }
                catch (Exception e)
                {
                    LOGGER.warning("Can not process message content. " + e.Message);
                }
            }
            //TODO: protobuf doesn't work
            else if (processProto == ProcessProtocolEnum.ProtoBuf)
            {
                var stream = new MemoryStream(message);
                try
                {
                    var header = Lib.SiteWhere.Device.Types.Header.Parser.ParseDelimitedFrom(stream);
                    switch (header.Command)
                    {
                    case Device.Types.Command.AckRegistration:
                    {
                        var ack = SiteWhere.Device.Types.RegistrationAck.Parser.ParseDelimitedFrom(stream);
                        handleRegistrationAck(header, ack);
                        break;
                    }

                    case Device.Types.Command.AckDeviceStream:
                    {
                        // TODO: Add device stream support.
                        break;
                    }

                    case Device.Types.Command.ReceiveDeviceStreamData:
                    {
                        // TODO: Add device stream support.
                        break;
                    }
                    }
                }
                catch (IOException e)
                {
                    throw new SiteWhereAgentException(e);
                }
            }
            else
            {
                LOGGER.info("unknown process proto");
            }
        }
 public virtual void executeStartupLogic(String hardwareId, String specificationToken, ISiteWhereEventDispatcher dispatcher)
 {
 }
 public void setEventDispatcher(ISiteWhereEventDispatcher eventDispatcher)
 {
     this.eventDispatcher = eventDispatcher;
 }