Beispiel #1
0
        public List <TemperatureEvent> AnalyseNewData(RoadAndAirTempData data)
        {
            List <TemperatureEvent> events = new List <TemperatureEvent>();

            events.AddRange(CheckExtreme(data, DataInfluenced.RoadTemperature));
            events.AddRange(CheckExtreme(data, DataInfluenced.AirTemperature));

            if (LasKnownValues.ContainsKey(data.StationName))
            {
                Temperatures temps = LasKnownValues[data.StationName];

                TemperatureEvent Event = CheckSudden(data, DataInfluenced.RoadTemperature, temps.RoadTemp);
                if (Event != null)
                {
                    events.Add(Event);
                }

                Event = CheckSudden(data, DataInfluenced.AirTemperature, temps.AirTemp);
                if (Event != null)
                {
                    events.Add(Event);
                }
            }

            if (LasKnownValues.ContainsKey(data.StationName))
            {
                LasKnownValues[data.StationName] = new Temperatures(data.RoadTemperature, data.AirTemperature);
            }
            else
            {
                LasKnownValues.Add(data.StationName, new Temperatures(data.RoadTemperature, data.AirTemperature));
            }

            return(events);
        }
Beispiel #2
0
        public async Task <List <RoadAndAirTempData> > GetDataLocation(LocationRadiusDTO locationInfo)
        {
            List <RoadAndAirTempData> retList = new List <RoadAndAirTempData>();

            var data = _unitOfWork.CassandraSession.Execute(_cassandraService.SelectAllQuery(_unitOfWork.TemperatureTable));
            RoadAndAirTempData newest = null;

            foreach (var instance in data)
            {
                RoadAndAirTempData roadData = _cassandraService.ConvertCassandraTempRow(instance);
                if (_geolocationService.CalculateDistance(locationInfo.CenterLatitude, locationInfo.CenterLongitude, roadData.Latitude, roadData.Longitude) <= locationInfo.RadiusMeters)
                {
                    if (!locationInfo.Newest)
                    {
                        retList.Add(roadData);
                    }
                    else if (newest == null || newest.Timestamp < roadData.Timestamp)
                    {
                        newest = roadData;
                    }
                }
            }

            if (locationInfo.Newest)
            {
                retList.Add(newest);
            }

            return(retList);
        }
Beispiel #3
0
        public async Task <List <RoadAndAirTempData> > GetDataByStationName(String StationName, bool Newest)
        {
            List <RoadAndAirTempData> retList = new List <RoadAndAirTempData>();

            var data = _unitOfWork.CassandraSession.Execute(_cassandraService.SelectWhereQuery(_unitOfWork.TemperatureTable, "StationName", "'" + StationName + "'"));

            RoadAndAirTempData newestInfo = null;

            foreach (var instance in data)
            {
                RoadAndAirTempData roadData = _cassandraService.ConvertCassandraTempRow(instance);

                if (!Newest)
                {
                    retList.Add(roadData);
                }
                else if (newestInfo == null || roadData.Timestamp > newestInfo.Timestamp)
                {
                    newestInfo = roadData;
                }
            }

            if (Newest)
            {
                retList.Add(newestInfo);
            }

            return(retList);
        }
Beispiel #4
0
 public async Task <bool> AddData(RoadAndAirTempData newData)
 {
     newData.RoadTemperature = convertTempFtoC(newData.RoadTemperature);
     newData.AirTemperature  = convertTempFtoC(newData.AirTemperature);
     _cassandraService.InsertRoadAndAirTempDataQuery(_unitOfWork.TemperatureTable, newData);
     _messageService.Enqueue(newData);
     return(true);
 }
Beispiel #5
0
        public void InsertRoadAndAirTempDataQuery(string table, RoadAndAirTempData data)
        {
            string command = "insert into " + table + " (\"Timestamp\", \"StationName\", \"AirTemperature\", \"Latitude\", \"Longitude\", \"RecordId\", \"RoadTemperature\")";

            command += "values ('" + _dateService.ConvertDateToString(data.Timestamp) + "', '" + data.StationName + "', " + data.AirTemperature + ", " + data.Latitude + ", " +
                       +data.Longitude + ", " + data.RecordId + ", " + data.RoadTemperature + ");";
            _unitOfWork.CassandraSession.Execute(command);
        }
Beispiel #6
0
        public void Enqueue(RoadAndAirTempData data)
        {
            string body = JsonSerializer.Serialize(data);

            _unitOfWork.RabbitMQChannel.BasicPublish(exchange: "",
                                                     routingKey: _unitOfWork.RabbitMQQueue,
                                                     mandatory: true,
                                                     basicProperties: null,
                                                     body: Encoding.UTF8.GetBytes(body));
        }
Beispiel #7
0
 public RoadAndAirTempDTO(RoadAndAirTempData data)
 {
     RecordId        = data.RecordId;
     StationName     = data.StationName;
     Latitude        = data.Latitude;
     Longitude       = data.Longitude;
     AirTemperature  = data.AirTemperature;
     RoadTemperature = data.RoadTemperature;
     Timestamp       = ConvertDateToString(data.Timestamp);
 }
Beispiel #8
0
        public async Task SendCEP(RoadAndAirTempData data)
        {
            SiddhiDTO dto = new SiddhiDTO();

            dto.Event = new RoadAndAirTempDTO(data);

            HttpResponseMessage response = await _unitOfWork.HttpClient.PostAsync(_unitOfWork.SiddhiLocation + _siddhiEndpoint,
                                                                                  new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json"));

            await response.Content.ReadAsStreamAsync();
        }
Beispiel #9
0
 private TemperatureEvent BuildEvent(RoadAndAirTempData data, DataInfluenced dataInfluenced, EventType alert)
 {
     return(new TemperatureEvent()
     {
         DataInfluenced = dataInfluenced,
         EventType = alert,
         Timestamp = data.Timestamp,
         Latitude = data.Latitude,
         Longitude = data.Longitude,
         StationName = data.StationName,
         Value = dataInfluenced == DataInfluenced.RoadTemperature ? data.RoadTemperature : data.AirTemperature
     });
 }
Beispiel #10
0
        public RoadAndAirTempData ConvertCassandraTempRow(Row instance)
        {
            RoadAndAirTempData roadInfo = new RoadAndAirTempData();

            roadInfo.Timestamp       = DateTime.Parse(instance["Timestamp"].ToString());
            roadInfo.StationName     = instance["StationName"].ToString();
            roadInfo.AirTemperature  = (double)instance["AirTemperature"];
            roadInfo.Latitude        = (double)instance["Latitude"];
            roadInfo.Longitude       = (double)instance["Longitude"];
            roadInfo.RecordId        = (int)instance["RecordId"];
            roadInfo.RoadTemperature = (double)instance["RoadTemperature"];
            return(roadInfo);
        }
Beispiel #11
0
        private TemperatureEvent CheckSudden(RoadAndAirTempData data, DataInfluenced dataInfluenced, double oldValue)
        {
            double newValue = dataInfluenced == DataInfluenced.RoadTemperature ? data.RoadTemperature : data.AirTemperature;

            if (newValue > _risingPercentage * oldValue)
            {
                return(BuildEvent(data, dataInfluenced, EventType.TempRiseAlert));
            }
            if (newValue < _fallingPercentage * oldValue)
            {
                return(BuildEvent(data, dataInfluenced, EventType.TempDropAlert));
            }
            return(null);
        }
Beispiel #12
0
        public async Task <List <RoadAndAirTempData> > GetNewest()
        {
            var data = _unitOfWork.CassandraSession.Execute(_cassandraService.SelectAllQuery(_unitOfWork.TemperatureTable));
            Dictionary <string, RoadAndAirTempData> newest = new Dictionary <string, RoadAndAirTempData>();

            foreach (var instance in data)
            {
                RoadAndAirTempData roadData = _cassandraService.ConvertCassandraTempRow(instance);
                if (!newest.ContainsKey(roadData.StationName))
                {
                    newest.Add(roadData.StationName, roadData);
                }
                else if (newest[roadData.StationName].Timestamp < roadData.Timestamp)
                {
                    newest[roadData.StationName] = roadData;
                }
            }
            return(newest.Values.ToList());
        }
Beispiel #13
0
        public void Consume()
        {
            var Consumer = new EventingBasicConsumer(_unitOfWork.RabbitMQInputChannel);

            Consumer.Received += (model, ea) =>
            {
                var body                = ea.Body;
                var message             = Encoding.UTF8.GetString(body.ToArray());
                RoadAndAirTempData data = (RoadAndAirTempData)JsonSerializer.Deserialize(message, typeof(RoadAndAirTempData));
                _messageService.SendCEP(data);
                List <TemperatureEvent> events = _analyticsService.AnalyseNewData(data);
                foreach (TemperatureEvent Event in events)
                {
                    _cassandraService.InsertData(Event);
                    _messageService.EnqueueOutput(Event);
                }
            };
            _unitOfWork.RabbitMQInputChannel.BasicConsume(queue: _unitOfWork.RabbitMQInputQueue,
                                                          autoAck: true,
                                                          consumer: Consumer);
        }
Beispiel #14
0
        private List <TemperatureEvent> CheckExtreme(RoadAndAirTempData data, DataInfluenced dataInfluenced)
        {
            List <TemperatureEvent> events = new List <TemperatureEvent>();
            double temp = dataInfluenced == DataInfluenced.RoadTemperature ? data.RoadTemperature : data.AirTemperature;

            if (FiringEvents.ContainsKey(data.StationName + "_" + dataInfluenced.ToString()))
            {
                EventType eventType = FiringEvents[data.StationName + "_" + dataInfluenced.ToString()];
                if (temp >= _lowerBound && temp <= _upperBound)
                {
                    events.Add(BuildEvent(data, dataInfluenced, EventType.TempNormalized));
                    FiringEvents.Remove(data.StationName + "_" + dataInfluenced.ToString());
                }
                else if (eventType == EventType.HotAlert && temp < _lowerBound)
                {
                    FiringEvents[data.StationName + "_" + dataInfluenced.ToString()] = EventType.ColdAlert;
                    events.Add(BuildEvent(data, dataInfluenced, EventType.ColdAlert));
                }
                else if (eventType == EventType.ColdAlert && temp > _upperBound)
                {
                    FiringEvents[data.StationName + "_" + dataInfluenced.ToString()] = EventType.HotAlert;
                    events.Add(BuildEvent(data, dataInfluenced, EventType.HotAlert));
                }
            }
            else
            {
                if (temp > _upperBound)
                {
                    FiringEvents.Add(data.StationName + "_" + dataInfluenced.ToString(), EventType.HotAlert);
                    events.Add(BuildEvent(data, dataInfluenced, EventType.HotAlert));
                }
                else if (temp < _lowerBound)
                {
                    FiringEvents.Add(data.StationName + "_" + dataInfluenced.ToString(), EventType.ColdAlert);
                    events.Add(BuildEvent(data, dataInfluenced, EventType.ColdAlert));
                }
            }
            return(events);
        }
        public async Task <ActionResult> GetData(int RecordId)
        {
            RoadAndAirTempData retVal = await _tempService.GetDataByRecordId(RecordId);

            return(Ok(retVal));
        }
        public async Task <ActionResult> AddData([FromBody] RoadAndAirTempData roadData)
        {
            await _tempService.AddData(roadData);

            return(Ok());
        }