public Task ProcessMessage(ValidatedMessage message) { try { logger.LogInformation("Start Processing"); logger.LogInformation("Device IMEI:" + message.IMEI); if (message.GpsStatus == GPSStatus.Valid) { if (this.State.powerStatus == MainPowerStatus.Off && message.MainPowerStatus == MainPowerStatus.Off) { return(Task.CompletedTask); } this.State.imei = message.IMEI; this.State.gpsStatus = message.GpsStatus; this.State.timestamp = message.EventTime; this.State.receivedTime = message.ReceivedTime; this.State.latitude = message.Latitude; this.State.longitude = message.Longitude; this.State.MsgId = message.MsgId; this.State.powerStatus = message.MainPowerStatus; } return(Task.CompletedTask); } catch (Exception ex) { //this.GetLogger().Info("{0},{1},{2},{3}", message.EventTime, "Error:" + ex.ToString(), message.DeviceImei, message.EventName); return(Task.CompletedTask); } }
private void bindQueue(Object myObject, EventArgs myEventArgs) { myTimer.Enabled = false; string HostName = _settings.RabbitMQSubscriberSettings.RabbitMQHost; string UserName = _settings.RabbitMQSubscriberSettings.RabbitMQUser; string Password = _settings.RabbitMQSubscriberSettings.RabbitMQPassword; int Port = int.Parse(_settings.RabbitMQSubscriberSettings.RabbitPort); string VirtualHost = _settings.RabbitMQSubscriberSettings.VirtualHost; string QueueName = _settings.RabbitMQSubscriberSettings.RabbitMQQueue; int factoryTimeoutMin = 10; //int.Parse(ConfigurationManager.AppSettings["factoryTimeoutMin"]); // Amount of time protocol operations (e.g. queue.declare) are allowed to take int channelTimeoutMin = 10; //int.Parse(ConfigurationManager.AppSettings["channelTimeoutMin"]); // Amount of time protocol operations(e.g.queue.declare) are allowed to take try { var factory = new RabbitMQ.Client.ConnectionFactory { HostName = HostName, UserName = UserName, Password = Password, Port = Port, VirtualHost = VirtualHost, }; factory.ContinuationTimeout = new TimeSpan(0, factoryTimeoutMin, 0); var connection = factory.CreateConnection(); connection.ConnectionShutdown += (a, b) => { myTimer.Enabled = true; }; var channel = connection.CreateModel(); channel.ContinuationTimeout = new TimeSpan(0, channelTimeoutMin, 0); var ok = channel.QueueDeclare(queue: QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.ConsumerCancelled += (model, ea) => { myTimer.Enabled = true; }; consumer.Registered += (model, ea) => { }; consumer.Shutdown += (model, ea) => { myTimer.Enabled = true; }; consumer.Unregistered += (model, ea) => { myTimer.Enabled = true; }; int counter = 1; consumer.Received += (model, ea) => { try { var body = ea.Body; var message = Encoding.UTF8.GetString(body); // Validate message ValidatedMessage deviceMessage = Validation.ValidateProperties(message); if (deviceMessage == null) { ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, false); return; } var deviceGrain = client.GetGrain <ISensor>(deviceMessage.IMEI); deviceGrain.ProcessMessage(deviceMessage); ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, false); counter++; } catch (Exception ex) { ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, false); myTimer.Enabled = true; } }; channel.BasicConsume(queue: QueueName, consumer: consumer); } catch (Exception ex) { myTimer.Enabled = true; } }
public static ValidatedMessage ValidateProperties(string rawMessage) { var message = JsonConvert.DeserializeObject <Message>(rawMessage); if (message.IMEI == null) { return(null); } var deviceMessage = new ValidatedMessage(); if (message.IMEI == "") { return(null); } else { deviceMessage.IMEI = message.IMEI; } try { deviceMessage.EventTime = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(message.State.Time); } catch (Exception ex) { return(null); } try { deviceMessage.ReceivedTime = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(message.ReceievedTime); } catch (Exception ex) { return(null); } if (string.IsNullOrEmpty(message.State.Latitude)) { return(null); } else { double lat; if (!double.TryParse(message.State.Latitude, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out lat)) { return(null); } else { deviceMessage.Latitude = lat; } } if (string.IsNullOrEmpty(message.State.Longitude)) { return(null); } else { double lon; if (!double.TryParse(message.State.Longitude, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out lon)) { return(null); } else { deviceMessage.Longitude = lon; } } if (!string.IsNullOrEmpty(message.State.PowerStatus)) { if (message.State.PowerStatus == "1") { deviceMessage.MainPowerStatus = MainPowerStatus.On; } else { deviceMessage.MainPowerStatus = MainPowerStatus.Off; } } else { deviceMessage.MainPowerStatus = MainPowerStatus.Unknown; } return(deviceMessage); }