public function getVehicle($id)
 {
     if (Auth::user()->isAdmin()) {
         $device = Device::find($id);
         $vehicles = Vehicle::whereHas('Account', function ($query) {
             $query->where('company_id', Auth::user()->company_id);
         })->doesntHave('Device')->get()->lists("fullname", "id")->all();
         $form = \DataForm::create();
         $form->add('device_id', '', 'hidden')->insertValue($id);
         $form->add('device_name', 'Aparelho', 'text')->insertValue($device->name)->mode('readonly');
         $form->link('/devices', 'Voltar', 'TR');
         if ($device->vehicle_id != '') {
             $form->add('vehicle_name', 'Veículo', 'text')->insertValue($device->Vehicle->fullname)->mode('readonly');
             $form->add('vehicle_id', '', 'hidden')->insertValue($device->vehicle_id);
             $form->add('install_date', 'Data de Instalação', 'date')->format('d/m/Y')->insertValue($device->install_date)->mode('readonly');
             $form->textarea('description', 'Observações')->insertValue($device->description)->mode('readonly');
             $form->add('action', '', 'hidden')->insertValue('remove');
             $form->label('Remover Aparelho');
             $form->submit('Confirma Retirada');
         } else {
             $form->add('vehicle_id', 'Veículo', 'select')->option("", "Selecione")->options($vehicles)->rule('required');
             $form->add('install_date', 'Data de Instalação', 'date')->format('d/m/Y')->rule('required');
             $form->textarea('description', 'Observações')->rule('required|min:15');
             $form->add('action', '', 'hidden')->insertValue('assign');
             $form->label('Instalar Aparelho');
             $form->submit('Salvar');
         }
         return $form->view('devices::vehicle', compact('form'));
     } else {
         return $form->view('errors.503');
     }
 }
 public function createMass(Request $request)
 {
     //dd($request->session()->all());
     //dd($request->all());
     $params = $request->session()->get('params');
     $values = $request->session()->get('values');
     $command_id = $request->session()->get('id_command');
     $type = config('commands_syntax.' . $command_id . '.TYPE');
     $count = count($params);
     foreach ($request->ids as $device_id) {
         $device = Device::find($device_id);
         $now = Carbon::now()->format('U');
         $timeout = Carbon::now()->addMonth()->toDateTimeString();
         $id_command = $command_id . '_' . $device->model . '_' . $device->serial . '_' . $now;
         //Salvando o Comando na base de dados
         $new_command = new Command();
         $new_command->id_command = $id_command;
         $new_command->type = $type;
         $device->Commands()->save($new_command);
         $xml = new \DOMDocument("1.0", "iso-8859-1");
         $xml->formatOutput = true;
         $commands = $xml->createElement("COMMANDS");
         $command = $xml->createElement('COMMAND');
         $element = $xml->createElement('PROTOCOL', $device->model);
         $command->appendChild($element);
         $element = $xml->createElement('SERIAL', $device->serial);
         $command->appendChild($element);
         $element = $xml->createElement('ID_COMMAND', $id_command);
         $command->appendChild($element);
         $element = $xml->createElement('TYPE', $type);
         $command->appendChild($element);
         $element = $xml->createElement('ATTEMPTS', 10);
         $command->appendChild($element);
         $element = $xml->createElement('COMMAND_TIMEOUT', $timeout);
         $command->appendChild($element);
         $element = $xml->createElement('PARAMETERS');
         for ($p = 0; $p < $count; $p++) {
             $par = 'PAR_' . $p;
             $val = 'VAL_' . $p;
             $new_parameter = new CommandParameter();
             $new_parameter->parameter_id = $params[$par];
             $new_parameter->value = $values[$val];
             $new_command->Parameters()->save($new_parameter);
             $parameter = $xml->createElement('PARAMETER');
             $parameter_id = $xml->createElement('ID', $params[$par]);
             $parameter_value = $xml->createElement('VALUE', $values[$val]);
             $parameter->appendChild($parameter_id);
             $parameter->appendChild($parameter_value);
             $element->appendChild($parameter);
         }
         $command->appendChild($element);
         $commands->appendChild($command);
         $xml->appendChild($commands);
         Storage::disk('ftp')->put("commands/{$id_command}.cmd", $xml->saveXML());
         //printf ("<pre>%s</pre>", htmlentities ($xml->saveXML()));
     }
     return redirect('/commands')->with('message', 'Comandos Enviados!');
 }