예제 #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';
 }
예제 #2
0
 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";
 }
예제 #3
0
 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');
 }