Пример #1
0
 /**
  * Main cron job that polls Google Nest.
  */
 public function getGoogleNestInfo()
 {
     echo "<pre>\n";
     $users = User::get();
     /** @var User $user */
     foreach ($users as $user) {
         echo 'Now for user ' . $user->email . "\n";
         $data = $this->_helper->getNestData($user);
         if ($data === false) {
             echo 'Failed after 5 attempts for user #' . $user->id . '!' . "\n";
             continue;
         }
         // store structures if not exist:
         foreach ($data->structures as $identifier => $structure) {
             // update or create
             $entry = Structure::firstOrCreate(['user_id' => $user->id, 'identifier' => $identifier]);
             $entry->name = $structure->name;
             $entry->country_code = isset($structure->country_code) ? $structure->country_code : null;
             $entry->postal_code = isset($structure->postal_code) ? $structure->postal_code : null;
             $entry->time_zone = isset($structure->time_zone) ? $structure->time_zone : null;
             $entry->save();
             echo 'Updated structure #' . $entry->id . ' for user #' . $user->id . '.' . "\n";
         }
         unset($entry, $identifier, $structure);
         // store devices if not exist.
         if (isset($data->devices->thermostats)) {
             foreach ($data->devices->thermostats as $thermostatId => $thermostat) {
                 // update or create thermostat.
                 $structure = $user->structures()->where('identifier', $thermostat->structure_id)->first();
                 $entry = Device::firstOrCreate(['type' => 'thermostat', 'identifier' => $thermostatId, 'structure_id' => $structure->id]);
                 $entry->locale = $thermostat->locale;
                 $entry->temperature_scale = $thermostat->temperature_scale;
                 $entry->name = $thermostat->name;
                 $entry->save();
                 echo 'Updated device #' . $entry->id . ' for user #' . $user->id . "\n";
                 // store all other entries as log entry.
                 $logEntry = LogEntry::create(['device_id' => $entry->id, 'time' => date('Y-m-d H:i:s')]);
                 $readValues = Config::get('marauder.nestAPIValues');
                 echo json_encode($readValues) . "\n";
                 $readValues = array_unique($readValues);
                 foreach ($readValues as $name) {
                     if (isset($thermostat->{$name})) {
                         echo 'Trying to store "' . $name . '" with value "' . $thermostat->{$name} . '" for logentry #' . $logEntry->id . '.' . "\n";
                         LogValue::create(['log_entry_id' => $logEntry->id, 'name' => $name, 'value' => $thermostat->{$name}]);
                     }
                 }
                 // store value for "away"
                 foreach ($data->structures as $structureIdentifier => $structure) {
                     if ($thermostat->structure_id == $structureIdentifier) {
                         $awayValue = $structure->away;
                         LogValue::create(['log_entry_id' => $logEntry->id, 'name' => 'away', 'value' => $awayValue]);
                     }
                 }
                 echo 'Stored log entry #' . $logEntry->id . ' with ' . $logEntry->logValues()->count() . ' log values for user #' . $user->id . "\n";
             }
         }
     }
 }