Ejemplo n.º 1
0
 public function storeData(Request $request)
 {
     $parameter = $request->input('parameter');
     $local = $request->input('local');
     $data = json_decode($request->input('data'));
     $local = Local::where('name', '=', $local)->get();
     switch ($parameter) {
         case 'device':
             $device = new Device();
             $device->id = (int) $data->id + (int) $local[0]->constant;
             $device->name = $data->name;
             $device->location = $data->location;
             $device->interval = $data->interval;
             $device->local_id = $local[0]->id;
             $device->save();
             break;
         case 'mapping':
             $mapping = new Mapping();
             $mapping->id = (int) $data->id + (int) $local[0]->constant;
             $mapping->device_id = (int) $data->device_id + (int) $local[0]->constant;
             $mapping->type_id = $data->type_id;
             $mapping->unit_id = $data->unit_id;
             $mapping->min_threshold = $data->min_threshold;
             $mapping->max_threshold = $data->max_threshold;
             $mapping->save();
             break;
         case 'data':
             $mapping_id = (int) $data->mapping_id + (int) $local[0]->constant;
             $mapping = Mapping::where('id', '=', $mapping_id)->get();
             $device_id = $mapping[0]->device_id;
             $type_id = $mapping[0]->type_id;
             $unit = Unit::find($mapping[0]->unit_id)->unit;
             $convert = new Convert();
             $convert->device_id = $device_id;
             $convert->type_id = $type_id;
             $convert->value = $this->convert($data->value, $type_id, $unit);
             $convert->timestamp = $data->timestamp;
             $convert->save();
             break;
         default:
             break;
     }
     return 'true';
 }
Ejemplo n.º 2
0
 public function chart($device_id, $type_id)
 {
     $header = app('request')->header();
     if (!$this->check($header)) {
         return response('Unauthorized', 401);
     }
     $chart = Device::find($device_id);
     $chart->standard = Standard::with('type')->where('type_id', '=', $type_id)->get();
     $chart->chart = Convert::select(DB::raw('*, HOUR(timestamp) as hour'))->where('device_id', '=', $device_id)->where('type_id', '=', $type_id)->whereRaw('DATE(timestamp) = CURDATE()')->groupBy('hour')->get();
     $chart->threshold = Mapping::select('min_threshold', 'max_threshold')->where('device_id', '=', $device_id)->where('type_id', '=', $type_id)->get();
     return $chart;
 }