1
0
mirror of https://github.com/beestat/app.git synced 2025-05-24 02:14:03 -04:00
beestat/api/thermostat.php
Jon Ziebell a6564305b0 Fixed #229 and #90.
Loading now doesn't break if the sync fails, and as a bonus that made it trivial to display whether or not ecobee is down.
2020-01-29 22:39:05 -05:00

100 lines
2.1 KiB
PHP

<?php
/**
* Any type of thermostat.
*
* @author Jon Ziebell
*/
class thermostat extends cora\crud {
public static $exposed = [
'private' => [
'read_id',
'sync',
'dismiss_alert',
'restore_alert'
],
'public' => []
];
public static $cache = [
'sync' => 300 // 5 Minutes
];
/**
* Sync all thermostats for the current user. If we fail to get a lock, fail
* silently (catch the exception) and just return false.
*
* @return boolean true if the sync ran, false if not.
*/
public function sync() {
// Skip this for the demo
if($this->setting->is_demo() === true) {
return true;
}
try {
$lock_name = 'thermostat->sync(' . $this->session->get_user_id() . ')';
$this->database->get_lock($lock_name);
$this->api('ecobee_thermostat', 'sync');
$this->api(
'user',
'update_sync_status',
['key' => 'thermostat']
);
$this->database->release_lock($lock_name);
return true;
} catch(cora\exception $e) {
return false;
}
}
/**
* Dismiss an alert.
*
* @param int $thermostat_id
* @param string $guid
*/
public function dismiss_alert($thermostat_id, $guid) {
$thermostat = $this->get($thermostat_id);
foreach($thermostat['alerts'] as &$alert) {
if($alert['guid'] === $guid) {
$alert['dismissed'] = true;
break;
}
}
$this->update(
[
'thermostat_id' => $thermostat_id,
'alerts' => $thermostat['alerts']
]
);
}
/**
* Restore a dismissed alert.
*
* @param int $thermostat_id
* @param string $guid
*/
public function restore_alert($thermostat_id, $guid) {
$thermostat = $this->get($thermostat_id);
foreach($thermostat['alerts'] as &$alert) {
if($alert['guid'] === $guid) {
$alert['dismissed'] = false;
break;
}
}
$this->update(
[
'thermostat_id' => $thermostat_id,
'alerts' => $thermostat['alerts']
]
);
}
}