/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $ftp = Storage::disk('ftp');
     $local = Storage::disk('xml');
     $list = $ftp->files('data');
     $count = 0;
     //$file = $list[0];
     foreach ($list as $file) {
         $filename = basename($file);
         $this->info(Carbon::now()->toDateTimeString() . " - Processando arquivo {$file}");
         if ($content = $ftp->read($file)) {
             $xml = simplexml_load_string($content);
             $local->put($filename, $content);
             $ftp->delete($file);
         }
         foreach ($xml->xpath('POSITION') as $pos) {
             $device = Device::where('serial', xmlGetVal($pos, 'FIRMWARE/SERIAL'))->where('model', xmlGetVal($pos, 'FIRMWARE/PROTOCOL'))->first();
             $now = Carbon::now();
             $date = new Carbon(xmlGetVal($pos, 'GPS/DATE', 'str'));
             if ($now->timestamp >= $date->timestamp) {
                 //										$this->info(xmlGetVal($pos,'FIRMWARE/SERIAL').": usando date");
                 $prefix = $date->timestamp * 100000;
             } else {
                 //										$this->info(xmlGetVal($pos,'FIRMWARE/SERIAL').":usando now");
                 $prefix = $now->timestamp * 100000;
             }
             $memory_index = xmlGetVal($pos, 'FIRMWARE/MEMORY_INDEX', 'int') + $prefix;
             $position = array('serial' => xmlGetVal($pos, 'FIRMWARE/SERIAL'), 'model' => xmlGetVal($pos, 'FIRMWARE/PROTOCOL'), 'memory_index' => $memory_index, 'transmission_reason' => xmlGetVal($pos, 'FIRMWARE/TRANSMISSION_REASON', 'int'), 'date' => xmlGetVal($pos, 'GPS/DATE', 'str'), 'power_supply' => xmlGetVal($pos, 'HARDWARE_MONITOR/POWER_SUPPLY', 'float'), 'temperature' => xmlGetVal($pos, 'HARDWARE_MONITOR/TEMPERATURE', 'int'), 'ignition' => xmlGetVal($pos, 'HARDWARE_MONITOR/INPUTS/IGNITION', 'int'), 'panic' => xmlGetVal($pos, 'HARDWARE_MONITOR/INPUTS/PANIC', 'int'), 'battery_charging' => xmlGetVal($pos, 'HARDWARE_MONITOR/FLAG_STATE/BATTERY_CHARGING', 'int'), 'battery_failure' => xmlGetVal($pos, 'HARDWARE_MONITOR/FLAG_STATE/BATTERY_FAILURE', 'int'), 'latitude' => xmlGetVal($pos, 'GPS/LATITUDE', 'float'), 'longitude' => xmlGetVal($pos, 'GPS/LONGITUDE', 'float'), 'speed' => xmlGetVal($pos, 'GPS/SPEED', 'float'), 'hodometer' => xmlGetVal($pos, 'GPS/HODOMETER', 'int'), 'lifetime' => xmlGetVal($pos, 'FIRMWARE/LIFE_TIME', 'int'), 'gps_signal' => xmlGetVal($pos, 'GPS/FLAG_STATE/GPS_SIGNAL', 'int'), 'gps_antenna_failure' => xmlGetVal($pos, 'GPS/FLAG_STATE/GPS_ANTENNA_FAILURE', 'int'));
             $new = new Position($position);
             $new->save();
             if ($device) {
                 $new->Device()->associate($device);
                 $new->save();
                 if ($device->has('Vehicle')) {
                     $vehicle = $device->Vehicle;
                     $new->Vehicle()->associate($vehicle);
                     $new->save();
                 }
             }
             //$this->info("Nova posição: ". $new->id);
             $count++;
         }
     }
     if ($count > 0) {
         $this->info("{$count} Posicoes encontradas!");
     } else {
         $this->error("Nenhuma Posicao encontrada!");
     }
 }
 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!');
 }
 public function audit($id)
 {
     $device = Device::findOrFail($id);
     $logs = $device->logs;
     //dd($logs);
     $audit = array();
     $labels = array('name' => 'Identificação', 'serial' => 'Número de Série', 'model' => 'Modelo', 'company_id' => 'Empresa', 'vehicle_id' => 'Veículo', 'description' => 'Observações');
     if ($logs) {
         foreach ($logs as $log) {
             foreach ($log->new_value as $key => $value) {
                 switch ($key) {
                     case 'model':
                         $audit[] = array('label' => $labels[$key], 'old' => testVal($log->old_value, $key) ? fieldValue("devices", $log->old_value[$key]) : '[novo]', 'new' => fieldValue("devices", $value), 'user' => $log->user->username, 'date' => date('d/m/Y H:i:s', strtotime($log->updated_at)));
                         break;
                     case 'vehicle_id':
                         $audit[] = array('label' => $labels[$key], 'old' => testVal($log->old_value, $key) ? Vehicle::find($log->old_value[$key])->plate : '[não associado]', 'new' => testVal($log->new_value, $key) ? Vehicle::find($log->new_value[$key])->plate : '[não associado]', 'user' => $log->user->username, 'date' => date('d/m/Y H:i:s', strtotime($log->updated_at)));
                         break;
                     case 'company_id':
                         $audit[] = array('label' => $labels[$key], 'old' => testVal($log->old_value, $key) ? Company::find($log->old_value[$key])->name : '[não associado]', 'new' => testVal($log->new_value, $key) ? Company::find($log->new_value[$key])->name : '[não associado]', 'user' => $log->user->username, 'date' => date('d/m/Y H:i:s', strtotime($log->updated_at)));
                         break;
                     default:
                         $audit[] = array('label' => $labels[$key], 'old' => testVal($log->old_value, $key) ? $log->old_value[$key] : '', 'new' => testVal($log->new_value, $key) ? $log->new_value[$key] : '', 'user' => $log->user->username, 'date' => date('d/m/Y H:i:s', strtotime($log->updated_at)));
                         break;
                 }
             }
         }
     }
     $grid = \DataGrid::source($audit);
     $grid->attributes(array("class" => "table table-striped .table-condensed"));
     $grid->add('label', 'Campo');
     $grid->add('old', 'Valor Anterior');
     $grid->add('new', 'Novo Valor');
     $grid->add('user', 'Usuário');
     $grid->add('date', 'Data/Hora');
     $grid->orderBy('date', 'DESC');
     $grid->paginate(10);
     return view('layouts.audit', compact('grid'));
 }