bool Dispatch(string routingKey, IDictionary <string, object> headers, byte[] body, ulong deliveryTag) { if (routingKey != null && routingKey.Length > 0) { string MID = MessageRoutingKey.ParseMID(routingKey); if (MID == _MID && body != null && body.Length > 0) { if (MessageRoutingKey.IsDeviceCommand(routingKey)) { string json = Encoding.UTF8.GetString(body); Command command = JsonConvert.DeserializeObject <Command>(json); CommandSubscription <Command> cs = _commandSubscriptions.Find(s => s.CommandId == command.Id); if (cs != null) { cs.CommandHandler(MID, command); lock (_modelLock) { _model.BasicAck(deliveryTag, false); } return(true); } } else if (MessageRoutingKey.IsApplicationPulse(routingKey)) { string json = Encoding.UTF8.GetString(body); Pulse pulse = JsonConvert.DeserializeObject <Pulse>(json); PulseSubscription ps = _pulseSubscriptions.Find(s => s.PulseId == pulse.PulseId); if (ps != null) { ps.PulseHandler(MID, pulse.PulseId, pulse); lock (_modelLock) { _model.BasicAck(deliveryTag, false); } return(true); } } } lock (_modelLock) { _model.BasicNack(deliveryTag, false, false); } } return(false); }
/// <summary> /// Registers a new callback method that is called when a specified command is received. /// </summary> /// <param name="commandId">Command identifier.</param> /// <param name="commandHandler">Callback method with argument Command (e.g. "void MyCallback(string MID, Command cmd) { ... }"</param> public void RegisterCommandHandler(int commandId, Action <string, Command> commandHandler) { CommandSubscription <Command> commandSubscription = new CommandSubscription <Command>(_MID, commandId, commandHandler); _commandSubscriptions.Add(commandSubscription); }