public function editData(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->name = $data->name; $device->location = $data->location; $device->interval = $data->interval; $device->save(); break; case 'mapping': $mapping = Mapping::find((int) $data->id + (int) $local[0]->constant); if ($mapping == null) { $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(); } else { $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; 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'); }