public void ThrowsInvalidOperationException_WhenPassedInvalidString(string stringText)
 {
     Assert.ThrowsException <InvalidOperationException>(() => ApiDataHelper.GetLastValue(stringText), "Invalid last value response");
 }
        public void Return_CorrectFloatValue(string stringText)
        {
            var result = ApiDataHelper.GetLastValue(stringText);

            Assert.AreEqual(16.5, result);
        }
        public void Return_CorrectValue_WhenPassedFalseAsString(string stringText)
        {
            var result = ApiDataHelper.GetLastValue(stringText);

            Assert.AreEqual(0, result);
        }
Beispiel #4
0
        public async Task UpdateSensorsData()
        {
            using (var scope = this.serviceProvider.CreateScope())
            {
                var icbApi              = scope.ServiceProvider.GetService <IIcbApiService>();
                var sensorsService      = scope.ServiceProvider.GetService <ISensorsService>();
                var notificationService = scope.ServiceProvider.GetService <INotificationService>();
                var dbContext           = scope.ServiceProvider.GetService <SmartDormitoryContext>();

                var liveDataCache          = new Dictionary <string, ApiSensorValueDTO>();
                var sensorsToUpdate        = new List <Sensor>();
                var alarmsActivatedSensors = new List <Sensor>();

                var userSensorsForUpdate = await sensorsService.GetAllForUpdate();

                foreach (var userSensor in userSensorsForUpdate)
                {
                    try
                    {
                        // caching all 13 api sensors data for current BackgroundJob iteration
                        if (!liveDataCache.ContainsKey(userSensor.IcbSensorId))
                        {
                            var newApiData = await icbApi.GetIcbSensorDataById(userSensor.IcbSensorId);

                            liveDataCache[userSensor.IcbSensorId] = newApiData;
                        }

                        var   liveSensorData = liveDataCache[userSensor.IcbSensorId];
                        float newValue       = ApiDataHelper.GetLastValue(liveSensorData.LastValue);

                        //if live data value is same like last time, skip
                        if (newValue != userSensor.CurrentValue)
                        {
                            userSensor.CurrentValue = newValue;
                        }
                        userSensor.LastUpdateOn = liveSensorData.TimeStamp;

                        // populate list of sensors which data should be updated
                        sensorsToUpdate.Add(userSensor);

                        if (userSensor.AlarmOn &&
                            (newValue <= userSensor.MinRangeValue || newValue >= userSensor.MaxRangeValue))
                        {
                            // populate list of sensors with activated alarms
                            alarmsActivatedSensors.Add(userSensor);
                        }
                    }
                    catch (HttpRequestException e)
                    {
                        await this.notificationManager.SendUIErrorAlerts(e.Message);
                    }
                }

                //stage notifications
                var notifications = await notificationService.CreateAlarmNotifications(alarmsActivatedSensors);

                foreach (var notify in notifications)
                {
                    await this.notificationManager.SendNotification(notify.ReceiverId, notify.Title);
                }

                //--- Save everything(sensors + notifies) with one transaction
                dbContext.UpdateRange(sensorsToUpdate);
                await dbContext.SaveChangesAsync();
            }
        }