public function create(array $data, array $extra = [])
 {
     $user_id = $extra['user_id'];
     $template = parent::newModelInstance();
     $template->name = $data['name'];
     $template->description = isset($data['description']) ? $data['description'] : '';
     $template->user_id = $user_id;
     $template->save();
     return $template;
 }
 public function executeCommand($id, $topic, array $arguments = [])
 {
     $commandArguments = $this->argumentRepo->getByCommand($id);
     $argumentData = [];
     foreach ($arguments as $item) {
         $item['valid'] = false;
         $value = isset($item['value']) ? $item['value'] : '';
         foreach ($commandArguments as $argument) {
             if ($argument->id == $item['argument_id']) {
                 if ($argument->type == 'number') {
                     if (!$this->isFloat($value)) {
                         // print_r($argument->id .' -> not float = ' . gettype($value) . '->' . $value . '<br>');
                         break;
                     }
                     $value = floatval($value);
                     if ($value > $argument->maximum || $value < $argument->minimum) {
                         // print_r($argument->id .' -> not range');
                         break;
                     }
                     $item['value'] = $value;
                 } else {
                     if ($argument->type == 'string') {
                         if (!$value) {
                             print_r($argument->id . ' -> set default');
                             $item['value'] = $argument->default;
                         }
                     }
                 }
                 $item['valid'] = true;
                 $argumentData[$argument->name] = $item['value'];
                 break;
             }
         }
         if (!$item['valid']) {
             return 0;
         }
     }
     $command = parent::get($id);
     $data = [];
     $data['cmd'] = $command->short_cmd;
     foreach ($argumentData as $key => $value) {
         $data[$key] = $value;
     }
     $data['time'] = \Carbon\Carbon::now()->timestamp;
     $this->bridge->publish($topic, $data);
     return $data['time'];
 }
 public function addAdmin($id, array $data = [])
 {
     $device = parent::get($id);
     $userId = $data['user_id'];
     $data = ['can_read' => true, 'can_edit' => $data['can_edit'], 'can_execute' => $data['can_execute'], 'owner' => false];
     $device->administrators()->sync([$userId => $data], false);
     $data['user_id'] = $userId;
     return $data;
 }