/** * Run the database seeds. * * @return void */ public function run() { $allNewSamplings = array(); $faker = App::make(Faker\Generator::class); $humiditySensors = App\GenericSensor::with("sensors")->where("alias", "Humidity")->first()->sensors; $temperatureSensors = App\GenericSensor::with("sensors")->where("alias", "Temperature")->first()->sensors; $everyDate = array(); $timeThreshold = 10; // minutes $numberOfDays = 2; $numberOfLoop = 24 * 60 / $timeThreshold; $samplingTime = Carbon::now(); foreach (range(0, $numberOfDays - 1) as $day) { foreach (range(0, $numberOfLoop - 1) as $time) { array_push($everyDate, $samplingTime->copy()); $samplingTime->subMinutes($timeThreshold); } } foreach ($humiditySensors as $humiditySensor) { foreach ($everyDate as $samplingDate) { array_push($allNewSamplings, array('sensor_id' => $humiditySensor->getKey(), 'sampled' => $faker->randomFloat(2, 20, 60), 'created_at' => $samplingDate)); } } foreach ($temperatureSensors as $temperatureSensor) { foreach ($everyDate as $samplingDate) { array_push($allNewSamplings, array('sensor_id' => $temperatureSensor->getKey(), 'sampled' => $faker->randomFloat(2, 19, 24), 'created_at' => $samplingDate)); } } DB::table('samplings')->insert($allNewSamplings); }
/** * A basic test example. * * @return void */ public function testSamplingCreationWithPost() { $product = App\Product::with('sensors')->get()->random(); $generic_sensors = App\GenericSensor::all(); $generic_sensor_with_random_function = array(); foreach ($generic_sensors as $generic_sensor) { // Humidity, Pressure, Temperature switch ($generic_sensor->alias) { case "Humidity": $generic_sensor_with_random_function[$generic_sensor->getKey()] = 'getRandomHumidityValue'; break; case "Temperature": $generic_sensor_with_random_function[$generic_sensor->getKey()] = 'getRandomTemperatureValue'; break; case "Pressure": $generic_sensor_with_random_function[$generic_sensor->getKey()] = 'getRandomPressureValue'; break; } } $newSamplings = array(); foreach ($product->sensors as $sensor) { $gs_id = $sensor->generic_sensor_id; if (!array_has($generic_sensor_with_random_function, $gs_id)) { continue; } $sensor_random_value = call_user_func(array($this, $generic_sensor_with_random_function[$gs_id])); array_push($newSamplings, array('generic_sensor_id' => $gs_id, 'value' => $sensor_random_value)); } $request_body = array('product_id' => App\Product::all()->random()->getkey(), 'samplings' => $newSamplings); $this->post('api/samplings/create', $request_body)->see('Created ' . count($newSamplings) . ' sampling(s)'); }
/** * Run the database seeds. * * @return void */ public function run() { //Creates types of used sensors //For each measured value one entry App\GenericSensor::create(['name' => 'ADXL330 Analog Devices Accelerometer', 'alias' => 'Accelerometer', 'unit' => 'G']); App\GenericSensor::create(['name' => 'BME280 Bosch combined humidity and pressure sensor', 'alias' => 'Humidity', 'unit' => '']); App\GenericSensor::create(['name' => 'BME280 Bosch combined humidity and pressure sensor', 'alias' => 'Pressure', 'unit' => 'kPa']); App\GenericSensor::create(['name' => 'BME280 Bosch combined humidity and pressure sensor', 'alias' => 'Temperature', 'unit' => '°C']); }
/** * Test the product creating by sending a post to the product creation route * * @return void */ public function testProductCreationWithPost() { $newProduct = factory(App\Product::class)->make(); $genericSensorIdsCollection = App\GenericSensor::all("id"); $genericSensorIds = array(); foreach ($genericSensorIdsCollection->all() as $genericSensor) { $genericSensorIds[] = $genericSensor->id; } $this->post("api/products/create", array("id" => $newProduct->id, "version" => $newProduct->version, "sensors" => $genericSensorIds)); $this->assertNotNull(App\Product::find($newProduct->id)); }
/** * Run the database seeds. * * @return void */ public function run() { $allGenericSensors = App\GenericSensor::all("id"); factory(App\Product::class, 5)->create()->each(function (App\Product $newProduct) use($allGenericSensors) { $newSensorsForEachGeneric = array(); foreach ($allGenericSensors as $genericSensor) { $newSensorsForEachGeneric[] = new App\Sensor(['generic_sensor_id' => $genericSensor->getKey()]); } $newProduct->sensors()->saveMany($newSensorsForEachGeneric); }); }