public void PostAnalystResult(AnalystResult analystResult) { var db = client.GetDatabase("iot"); var collection = db.GetCollection <AnalystResult>("sensor-analyst"); collection.InsertOne(analystResult); }
public void Subscribe(string qName) { //channel.ExchangeDeclare(exchange: qName, type: ExchangeType.Fanout); channel.QueueDeclare(queue: qName, durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); Console.WriteLine("Command service: Recieved [JSON message] from AnalystService: message=" + message); AnalystResult analystResult = JsonConvert.DeserializeObject <AnalystResult>(message.ToString()); Console.WriteLine("Command service: Deserialized object from message: object=" + analystResult.RadiationHigh); //send object for further command sending string msg = _cs.checkCommands(analystResult); //Console.WriteLine("Command Received"); this.hubContext.Clients.All.SendAsync("ReceivedMsg", msg); }; channel.BasicConsume(queue: qName, autoAck: true, consumer: consumer); }
public void Publish(AnalystResult content, string queueName) { using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null); string datajson = JsonConvert.SerializeObject(content); var body = Encoding.UTF8.GetBytes(datajson); channel.BasicPublish(exchange: "", routingKey: queueName, mandatory: true, basicProperties: null, body: body); } }
public string checkCommands(AnalystResult ar) { SensorMetaData smd = _GetMetaDataFromSensorAsync().Result; string msg = string.Empty; //testfun(69f, 69f); DateTime unixTime = DateTime.Now; string interval = ""; string threshold = ""; string weatherStatus = ""; string radiationStatus = ""; string periodOfDay = ""; if (ar.DayTimeDay) { Console.WriteLine("CommandProcessing: daytime day"); periodOfDay = "Day time"; if (ar.RadiationHigh) { Console.WriteLine("CommandProcessing: radiation high"); radiationStatus = "High"; if (ar.WeatherGood) { _SetInterval(5000f, smd.Interval); //interval=5s i tsh=2.5 Console.WriteLine("CommandProcessing: weather good"); weatherStatus = "Good"; interval = "5000ms"; } else { _SetInterval(10000f, smd.Interval);//interval=15s i tsh=2.5 Console.WriteLine("CommandProcessing: weather bad"); weatherStatus = "Bad"; interval = "7500ms"; } _SetThreshold(25f, smd.Threshold); threshold = "25"; } else { Console.WriteLine("CommandProcessing: radiation low"); radiationStatus = "Low"; // int 10s tsh 75 _SetInterval(7500f, smd.Interval); _SetThreshold(75f, smd.Threshold); interval = "7500ms"; threshold = "75"; } } else { _SetInterval(10000f, smd.Interval); _SetThreshold(0.025f, smd.Threshold);//0.025 Console.WriteLine("CommandProcessing: daytime night"); periodOfDay = "Night time"; interval = "10000ms"; threshold = "0.025"; if (ar.RadiationHigh) { radiationStatus = "High"; } else { radiationStatus = "Low"; } } if (ar.WeatherGood) { weatherStatus = "Good"; } else { weatherStatus = "Bad"; } return(unixTime + "," + interval + "," + threshold + "," + weatherStatus + "," + radiationStatus + "," + periodOfDay); }
public async Task Analyze(SensorData sd) { Console.WriteLine("AnalystService: Analyze started"); if (!_running) { return; } AnalystResult _ar = new AnalystResult(); //----- DateTime tmpTime = _UnixTimeStampToDateTime(sd.UnixTime); if (tmpTime.TimeOfDay > _date1.TimeOfDay || tmpTime.TimeOfDay < _date2.TimeOfDay) { _ar.DayTimeDay = true;//korekcija } else { _ar.DayTimeDay = false; } Console.WriteLine($"time: {tmpTime.TimeOfDay.ToString()} flag(false == day): {_ar.DayTimeDay}"); _ar.HighRisk = false; if (sd.Speed < 5.82f && sd.Temperature > 56f && sd.Humidity < 69.93f && sd.Pressure < 30.43) { //vreme je pogodno _ar.WeatherGood = true; if (sd.Radiation > 229.6f) { //visoka radijacija _ar.RadiationHigh = true; _ar.HighRisk = true; } else { //niska radijacija _ar.RadiationHigh = false; _ar.HighRisk = false; } } else { //vreme nije pogodno _ar.WeatherGood = false; _ar.HighRisk = false; if (sd.Radiation > 229.6f) { //visoka radijacija _ar.RadiationHigh = true; } else { //niska radijacija _ar.RadiationHigh = false; } } //-----*/ _ar.TimeStamp = DateTime.Now; var publisher = new Publisher(); publisher.Publish(_ar, "AnalystServiceQueue"); _SaveLog(_ar); Console.WriteLine("AnalystService is published data"); }
private void _SaveLog(AnalystResult result) { repository.PostAnalystResult(result); }