Exemple #1
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;
 }
 public function receiveData(Request $request)
 {
     $path = config('path');
     $device = $request->input('device');
     $type = $request->input('type');
     $value = $request->input('value');
     $date = $request->input('date');
     $device_id = Device::select('id')->where('name', '=', $device)->get();
     $type_id = Type::select('id')->where('type', '=', $type)->get();
     $mapping = Mapping::where('device_id', '=', $device_id[0]->id)->where('type_id', '=', $type_id[0]->id)->get();
     $value = $this->convert($mapping[0]->formula, $value);
     $information = new Information();
     $information->mapping_id = $mapping[0]->id;
     $information->value = $value;
     $information->timestamp = $date;
     $information->save();
     $result = shell_exec('python ' . $path . 'publish.py /regis/data/' . config('local') . ' ' . escapeshellarg(json_encode($information)));
     return "true";
 }
Exemple #3
0
 public function deleteData(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 = Device::find((int) $data->id + (int) $local[0]->constant);
             $device->delete();
             break;
         case 'mapping':
             $mapping = Mapping::find((int) $data->id + (int) $local[0]->constant);
             $mapping->delete();
             break;
         default:
             break;
     }
     return 'true';
 }
 public function edit(Request $request)
 {
     $path = config('path');
     $device = $request->input('device');
     $types = $request->input('types');
     if (!is_array($device)) {
         return "false";
     } else {
         if (!key_exists('id', $device) || !key_exists('name', $device) || !key_exists('location', $device) || !key_exists('interval', $device)) {
             return "false";
         } else {
             if ($device['id'] == "" || $device['name'] == "" || $device['location'] == "" || $device['interval'] == "") {
                 return "false";
             } else {
                 if ($device['id'] == 'undefined' || $device['name'] == 'undefined' || $device['location'] == 'undefined' || $device['interval'] == 'undefined') {
                     return "false";
                 } else {
                     if ($device['id'] == null || $device['name'] == null || $device['location'] == null || $device['interval'] == null) {
                         return "false";
                     } else {
                         if (!is_int($device['id']) || !is_int($device['interval'])) {
                             return "false";
                         }
                     }
                 }
             }
         }
     }
     $check = $this->check_type($types);
     if (!$check) {
         return "false";
     }
     $edit_device = Device::find($device['id']);
     $edit_device->name = $device['name'];
     $edit_device->location = $device['location'];
     $edit_device->interval = $device['interval'];
     $edit_device->save();
     $device = $edit_device;
     $result = shell_exec('python ' . $path . 'publish.py /edit/device/' . config('local') . ' ' . escapeshellarg(json_encode($device)));
     for ($i = 0; $i < count($types); $i++) {
         switch ($types[$i]['item']) {
             case "old":
                 switch ($types[$i]['status']) {
                     case true:
                         $edit_type = Mapping::find($types[$i]['mapping_id']);
                         $edit_type->type_id = $types[$i]['type_id'];
                         $edit_type->unit_id = $types[$i]['unit_id'];
                         $edit_type->min_threshold = $types[$i]['min_threshold'];
                         $edit_type->max_threshold = $types[$i]['max_threshold'];
                         $edit_type->formula = $types[$i]['formula'];
                         $edit_type->save();
                         $result = shell_exec('python ' . $path . 'publish.py /edit/mapping/' . config('local') . ' ' . escapeshellarg(json_encode($edit_type)));
                         break;
                     case false:
                         $delete_type = Mapping::find($types[$i]['mapping_id']);
                         $result = shell_exec('python ' . $path . 'publish.py /delete/mapping/' . config('local') . ' ' . escapeshellarg(json_encode($delete_type)));
                         $delete_type->delete();
                         break;
                     default:
                         break;
                 }
                 break;
             case "new":
                 switch ($types[$i]['status']) {
                     case true:
                         $new_type = new Mapping();
                         $new_type->device_id = $device->id;
                         $new_type->type_id = $types[$i]['type_id'];
                         $new_type->unit_id = $types[$i]['unit_id'];
                         $new_type->min_threshold = $types[$i]['min_threshold'];
                         $new_type->max_threshold = $types[$i]['max_threshold'];
                         $new_type->formula = $types[$i]['formula'];
                         $new_type->save();
                         $result = shell_exec('python ' . $path . 'publish.py /edit/mapping/' . config('local') . ' ' . escapeshellarg(json_encode($new_type)));
                         break;
                     default:
                         break;
                 }
                 break;
             default:
                 break;
         }
     }
     $mapping = Mapping::where('device_id', '=', $device->id)->get();
     $status = "true";
     return compact('device', 'mapping', 'status');
 }