public virtual void UpdateSensorTypes(IDictionary <string, APISensorType> availableSensorsFromAPI) { // guard IEnumerable <string> notRegistered = availableSensorsFromAPI.Keys.Except(this.dbContext.SensorTypes.Select(x => x.SensorIdICB)); if (notRegistered == null || notRegistered.Count() == 0) { return; } else { foreach (string sensorId in notRegistered) { APISensorType newSensorType = availableSensorsFromAPI[sensorId]; this.dbContext.SensorTypes.Add(new SensorType() { SensorIdICB = newSensorType.SensorId, MeasureType = newSensorType.MeasureType, MinPollingIntervalInSeconds = newSensorType.MinPollingIntervalInSeconds, Tag = newSensorType.Tag, Description = newSensorType.Description, IsNumericValue = newSensorType.Description.Split().Contains("between") }); } } this.dbContext.SaveChanges(); }
private async Task SeedSensors(int sensorsNumber = 5) { IDictionary <string, APISensorType> availableSensors = await this.APIService.ListSensorsFromAPI(); //i v rega v sensor controlera this.DBService.UpdateSensorTypes(availableSensors); IList <string> supportedSensorTypes = availableSensors.Keys.ToList(); IEnumerable <string> registeredUsers = this.DBService.ListAllUsers(); bool inverse = true; foreach (string user in registeredUsers) { CustomSensorController customSensorController = new CustomSensorController(user, this.DBService, this.APIService); for (int i = 0; i < sensorsNumber; i++) { inverse = !inverse; APISensorType mainSensorInfo = availableSensors[supportedSensorTypes[this.random.Next(0, supportedSensorTypes.Count - 1)]]; string descriptionByICB = mainSensorInfo.Description; string[] subDescription = descriptionByICB.Split(); //"This sensor will return values between 6 and 18" or "This sensor will return true or false value" double minValue = 0; double maxValue = 0; if (subDescription.Contains("between")) { try //try with int { int mainMinValueI = int.Parse(subDescription[subDescription.Length - 3]); int mainMaxValueI = int.Parse(subDescription[subDescription.Length - 1]); int randomMinValue = this.random.Next(mainMinValueI, mainMaxValueI); minValue = randomMinValue; int randomMaxValue = this.random.Next(randomMinValue, mainMaxValueI); maxValue = randomMaxValue; } catch { try //try with double. Trunkate because of Random() does not work with noninteger numbers { double mainMinValueD = Math.Truncate(double.Parse(subDescription[subDescription.Length - 3])); double mainMaxValueD = Math.Truncate(double.Parse(subDescription[subDescription.Length - 1])); int randomMinValue = this.random.Next((int)mainMinValueD, (int)mainMaxValueD); minValue = randomMinValue; int randomMaxValue = this.random.Next(randomMinValue, (int)mainMaxValueD); maxValue = randomMaxValue; } catch //ebago { } } } int pollingInterval = this.random.Next(mainSensorInfo.MinPollingIntervalInSeconds, mainSensorInfo.MinPollingIntervalInSeconds * 3); RegisterNewSensor sensorModel = new RegisterNewSensor { AvailableSensors = null, IsPublic = inverse, PollingInterval = pollingInterval, SensorIdICB = mainSensorInfo.SensorId, Tag = mainSensorInfo.Tag, UserDefinedSensorName = string.Format("{0}`s {1} sensor.", user, mainSensorInfo.Tag), UserDefinedDescription = mainSensorInfo.Description, UserDefinedMeasureType = mainSensorInfo.MeasureType, //to improove later UserDefinedMinValue = minValue, UserDefinedMaxValue = maxValue }; customSensorController.RegisterNewSensor(sensorModel); } } }