Beispiel #1
0
 /**
  * Setup device using the provided registration info,
  * This inserts/updates the device/location records before inserting a new uuid to bind to the device.
  * @param $result array current API result array
  * @return array API result array
  */
 public function setupDevice($result)
 {
     // create new location record
     if ($this->locationId == null) {
         // check if a name has been provided
         if ($this->locationName == null) {
             if ($this->deviceId == null) {
                 // if we are adding a new device, location id or name must be provided.
                 $result['error'] = "No location id or name was provided";
                 return $result;
             }
         } else {
             // create a new location using the provided name
             if (($newid = $this->addNewLocation($this->locationName)) !== false) {
                 $this->locationId = $newid;
             } else {
                 $result['error'] = "Insertion of new location record failed";
                 return $result;
             }
         }
     }
     // insert new device
     if ($this->deviceId == null) {
         if ($this->deviceName == null) {
             $result['error'] = "The no device id or name was provided";
             return $result;
         } else {
             // create a new location using the provided name
             if (($newid = $this->addNewDevice($this->deviceName, $this->locationId)) !== false) {
                 $this->deviceId = $newid;
             } else {
                 $result['error'] = "Insertion of new device record failed";
                 return $result;
             }
         }
     } else {
         // check if device exists and is enabled
         $devMdl = new DevicesModel();
         $dev = $devMdl->get($this->deviceId);
         if ($dev === false || sizeof($dev) == 0) {
             $result['error'] = "The deviceid specified does not exist";
             return $result;
         }
         $dev = $dev[0];
         if ($dev['disabled'] == 1) {
             $result['error'] = "The deviceid specified is disabled";
             return $result;
         }
         if ($this->locationId != null) {
             // if location id is left out, we can leave device at it's current location
             // check if location exists (and enabled) before updating device location
             if (!$this->doesLocationExist($this->locationId)) {
                 $result['error'] = "The locationid specified does not exist or is disabled";
                 return $result;
             }
             // update location
             if ($devMdl->updateLocation($this->deviceId, $this->locationId) === false) {
                 $result['error'] = "Failed to update the devices location";
                 return $result;
             }
         }
     }
     // insert the md5 signature in the device_map table
     if ($this->addNewUuid($this->uuid, $this->deviceId)) {
         // create json record to return to the client
         $result['data'] = new stdClass();
         $result['data']->deviceid = $this->deviceId;
         $result['data']->devicename = $this->deviceName;
         $result['data']->locationid = $this->locationId;
         $result['data']->locationname = $this->locationName;
         // log data
         Logger::write("New device registered with uuid:" . $this->uuid, "CONFIG", json_encode($this->data));
         return $result;
     } else {
         $result['error'] = "Error adding the device fingerprint into the database";
         return $result;
     }
 }