. */ /* * ***************************Includes********************************* */ require_once __DIR__ . '/../../../../core/php/core.inc.php'; use GuzzleHttp\Client; define('__ROOT__', dirname(dirname(dirname(__FILE__)))); require_once (__ROOT__.'/3rparty/Netatmo-API-PHP/src/Netatmo/autoload.php'); define('__CSSICON__', str_replace($_SERVER['DOCUMENT_ROOT'], '', __ROOT__.'/core/css/icon/')); /* error_reporting(-1); */ class netatmopro extends eqLogic { /* * *************************Attributs****************************** */ /* * ***********************Methode static*************************** */ public static function synchronize($update = true) { log::add('netatmopro', 'debug', 'synchronize:: update:' . var_export($update, true)); $dayweek = array('1' => 'Lundi', '2' => 'Mardi', '3' => 'Mercredi', '4' => 'Jeudi', '5' => 'Vendredi', '6' => 'Samedi', '7' => 'Dimanche'); $config = array("client_id" => config::byKey('client_id', 'netatmopro'), "client_secret" => config::byKey('client_secret', 'netatmopro'), "username" => config::byKey('username', 'netatmopro'), "password" => config::byKey('password', 'netatmopro'), "scope" => 'read_station read_homecoach read_presence access_presence read_camera access_camera read_smokedetector read_thermostat write_thermostat'); $scope = ( $update === true ? config::byKey('scope', 'netatmopro') : [] ); log::add('netatmopro', 'debug', 'synchronize:: scope:' . var_export($scope, true)); if ( $update === false || ( $update === true && preg_match("/read_station/", $scope) ) ) { log::add('netatmopro', 'debug', 'synchronize:: NAWSApiClient'); $client = new Netatmo\Clients\NAWSApiClient($config); $tokens = $client->getAccessToken(); $data = $client->getData(); log::add('netatmopro', 'debug', 'synchronize:: WS:' . var_export($data, true)); foreach ($data['devices'] as $device) { foreach ($device['modules'] as $module) { $eqLogic = eqLogic::byLogicalId($module['_id'], 'netatmopro'); if ( !is_object($eqLogic) && $update === false ) { log::add('netatmopro', 'debug', 'synchronize:: M_CREATE_IN_PROGRESS:' . var_export($module, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($module); $eqLogic->createCmd(); } if ( is_object($eqLogic) && ( $update === false || ( $update === true && $eqLogic->getConfiguration('last_message') < $module['last_message'] ) ) ) { log::add('netatmopro', 'debug', 'synchronize:: M_UPDATE_IN_PROGRESS:' . var_export($module, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $module['dashboard_data'][$key]); } $eqLogic->checkAndUpdateCmd('battery_percent', $module['battery_percent']); $eqLogic->setConfiguration('station_name', $device['station_name']); $eqLogic->setConfiguration('firmware', $module['firmware']); $eqLogic->setConfiguration('rf_status', $module['rf_status']); $eqLogic->setConfiguration('battery_percent', $module['battery_percent']); $eqLogic->setConfiguration('last_message', $module['last_message']); $eqLogic->batteryStatus($module['battery_percent']); $eqLogic->save(); $eqLogic->refreshWidget(); } } $eqLogic = eqLogic::byLogicalId($device['_id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($device, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($device); $eqLogic->createCmd(); } if ( is_object($eqLogic) && ( $update === false || ( $update === true && $eqLogic->getConfiguration('last_status_store') < $device['last_status_store'] ) ) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($device, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $device['dashboard_data'][$key]); } $eqLogic->setConfiguration('station_name', $device['station_name']); $eqLogic->setConfiguration('firmware', $device['firmware']); $eqLogic->setConfiguration('wifi_status', $device['wifi_status']); $eqLogic->setConfiguration('last_status_store', $device['last_status_store']); $eqLogic->save(); $eqLogic->refreshWidget(); } if ($update === false) { $scope[] = 'read_station'; } } } if ( $update === false || ( $update === true && preg_match("/read_homecoach/", $scope) ) ) { log::add('netatmopro', 'debug', 'synchronize:: NAHomeApiClient'); $client = new Netatmo\Clients\NAHomeApiClient($config); $tokens = $client->getAccessToken(); $data = $client->getData(); log::add('netatmopro', 'debug', 'synchronize:: HOME:' . var_export($data, true)); foreach ($data['devices'] as $device) { $eqLogic = eqLogic::byLogicalId($device['_id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($device, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($device); $eqLogic->createCmd(); } if ( is_object($eqLogic) && ( $update === false || ( $update === true && $eqLogic->getConfiguration('last_status_store') < $device['last_status_store'] ) ) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($device, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $device['dashboard_data'][$key]); } $eqLogic->setConfiguration('firmware', $device['firmware']); $eqLogic->setConfiguration('wifi_status', $device['wifi_status']); $eqLogic->setConfiguration('last_status_store', $device['last_status_store']); $eqLogic->save(); $eqLogic->refreshWidget(); } if ($update === false) { $scope[] = 'read_homecoach'; } } } if ( $update === false || ( $update === true && preg_match("/read_presence access_presence read_camera access_camera read_smokedetector/", $scope) ) ) { log::add('netatmopro', 'debug', 'synchronize:: NAWelcomeApiClient'); $client = new Netatmo\Clients\NAWelcomeApiClient($config); $tokens = $client->getAccessToken(); $data = $client->getData(null, 0)->getDecodedBody(); log::add('netatmopro', 'debug', 'synchronize:: SECURITY:' . var_export($data, true)); foreach ($data['homes'] as $home) { foreach ($home['persons'] as $person) { if (!(array_key_exists('pseudo', $person))) continue; $person['type'] = 'NAPerson'; $eqLogic = eqLogic::byLogicalId($person['id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($person, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($person, $person['pseudo']); $eqLogic->createCmd(); } if ( is_object($eqLogic) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($person, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $person[$key]); } $eqLogic->setConfiguration('home_id', $home['id']); $eqLogic->setConfiguration('home_name', $home['name']); $eqLogic->save(); } } $smokes = $modules = []; if (!empty($home['cameras']) || !empty($home['smokedetectors'])) { netatmopro::GetGuzzleHd($guzzle, $jar); log::add('netatmopro', 'debug', 'synchronize:: guzzle:' . var_export($guzzle, true)); log::add('netatmopro', 'debug', 'synchronize:: jar:' . var_export($jar, true)); $uri = 'https://app.netatmo.net/api/gethomedata'; log::add('netatmopro', 'debug', 'synchronize:: uri:' . var_export($uri, true)); $response = $guzzle->request('POST', $uri, [ 'headers' => [ 'Authorization' => 'Bearer ' . urldecode($jar->getCookieByName('netatmocomaccess_token')->getValue()) ], 'body' => '{"home_id":"' . $home['id'] . '"}' ]); log::add('netatmopro', 'debug', 'synchronize:: response:' . var_export($response, true)); $wdata = json_decode($response->getBody()->getContents(), true); foreach ($wdata['body']['homes'] as $whome) { foreach ($whome['cameras'] as $wcamera) { if ( array_key_exists('modules', $wcamera) ) { foreach ($wcamera['modules'] as $wmodule) { $modules[$wmodule['id']] = $wmodule; } } } foreach ($whome['smokedetectors'] as $wsmoke) { $smokes[$wsmoke['id']] = $wsmoke; } } log::add('netatmopro', 'debug', 'synchronize:: modules:' . var_export($modules, true)); log::add('netatmopro', 'debug', 'synchronize:: smokes:' . var_export($smokes, true)); } foreach ($home['cameras'] as $camera) { $eqLogic = eqLogic::byLogicalId($camera['id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($camera, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($camera); $eqLogic->createCmd(); } if ( is_object($eqLogic) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($camera, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $camera[$key]); } $eqLogic->setConfiguration('home_id', $home['id']); $eqLogic->setConfiguration('home_name', $home['name']); $eqLogic->setConfiguration('vpn_url', $camera['vpn_url']); $eqLogic->setConfiguration('is_local', $camera['is_local']); $eqLogic->save(); } if ( array_key_exists('modules', $camera) ) { foreach ($camera['modules'] as $module) { $eqLogic = eqLogic::byLogicalId($module['id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($module, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($module); $eqLogic->createCmd(); } if ( is_object($eqLogic) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($module, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $module[$key]); } $eqLogic->setConfiguration('home_id', $home['id']); $eqLogic->setConfiguration('home_name', $home['name']); $eqLogic->setConfiguration('firmware', $modules[$module['id']]['firmware']); $eqLogic->setConfiguration('rf', $module['rf']); $eqLogic->setConfiguration('battery_percent', $module['battery_percent']); $eqLogic->batteryStatus($module['battery_percent']); if ( $module['type'] == 'NACamDoorTag' ) { $eqLogic->setConfiguration('category', $module['category']); } $eqLogic->save(); } } } } foreach ($home['smokedetectors'] as $smoke) { $eqLogic = eqLogic::byLogicalId($smoke['id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($smoke, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($smoke); $eqLogic->createCmd(); } if ( is_object($eqLogic) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($smoke, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $smokes[$smoke['id']][$key]); } $eqLogic->setConfiguration('home_id', $home['id']); $eqLogic->setConfiguration('home_name', $home['name']); $eqLogic->setConfiguration('firmware', $smokes[$smoke['id']]['firmware']); $eqLogic->setConfiguration('wifi_strength', $smokes[$smoke['id']]['wifi_strength']); $eqLogic->setConfiguration('battery_percent', $smokes[$smoke['id']]['battery_percent']); $eqLogic->batteryStatus($smokes[$smoke['id']]['battery_percent']); $eqLogic->save(); } } if ($update === false) { $scope[] = 'read_presence access_presence read_camera access_camera read_smokedetector'; } } log::add('netatmopro', 'debug', 'synchronize:: webhook:' . var_export(network::getNetworkAccess('external'), true)); $client->subscribeToWebhook(network::getNetworkAccess('external') . '/plugins/netatmopro/core/php/NAWebhookClient.php?apikey=' . jeedom::getApiKey('netatmopro')); } if ( $update === false || ( $update === true && preg_match("/read_thermostat write_thermostat/", $scope) ) ) { log::add('netatmopro', 'debug', 'synchronize:: NATherm2ApiClient'); $client = new Netatmo\Clients\NATherm2ApiClient($config); $tokens = $client->getAccessToken(); $data = $client->getData(null, 'NAPlug'); log::add('netatmopro', 'debug', 'synchronize:: ENERGY:' . var_export($data, true)); netatmopro::GetGuzzleHd($guzzle, $jar); log::add('netatmopro', 'debug', 'synchronize:: guzzle:' . var_export($guzzle, true)); log::add('netatmopro', 'debug', 'synchronize:: jar:' . var_export($jar, true)); $uri = 'https://app.netatmo.net/api/homesdata'; log::add('netatmopro', 'debug', 'synchronize:: uri:' . var_export($uri, true)); $response = $guzzle->request('POST', $uri, [ 'headers' => [ 'Authorization' => 'Bearer ' . urldecode($jar->getCookieByName('netatmocomaccess_token')->getValue()) ], 'body' => '{"gateway_types":["NAPlug"],"detailed_config":true,"home_id":null,"gateway_parameters":{"NAPlug":{"sync_measures":true}}}' ]); log::add('netatmopro', 'debug', 'synchronize:: response:' . var_export($response, true)); $wdata = json_decode($response->getBody()->getContents(), true); foreach ($wdata['body']['homes'] as $whome) { $homes[$whome['id']] = $whome; } log::add('netatmopro', 'debug', 'synchronize:: homes:' . var_export($homes, true)); foreach ($data['homes'] as $home) { if (!(array_key_exists('rooms', $home) && array_key_exists('modules', $home) && array_key_exists('schedules', $home))) continue; $rooms = $modules = $schedules = []; foreach ($home['rooms'] as $room) { $rooms[$room['id']] = $room; } foreach ($home['modules'] as $module) { $modules[$module['id']] = $module; } foreach ($home['schedules'] as $schedule) { $schedules[$schedule['id']] = $schedule; } $status = $client->getStatus($home['id'], 'NAPlug'); log::add('netatmopro', 'debug', 'synchronize:: ENERGY:' . var_export($status, true)); foreach ($status['home']['rooms'] as $room) { $room['type'] = 'NARoom'; $rooms[$room['id']]['status'] = $room; $eqLogic = eqLogic::byLogicalId($room['id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($room, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($room, $rooms[$room['id']]['name']); $eqLogic->createCmd(); } if ( is_object($eqLogic) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($room, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $room[$key]); } $eqLogic->setConfiguration('home_id', $home['id']); $eqLogic->setConfiguration('home_name', $home['name']); $eqLogic->setConfiguration('room_type', $rooms[$room['id']]['type']); $eqLogic->save(); } } foreach ($status['home']['modules'] as $module) { if ( $module['type'] == 'NOC' || $module['type'] == 'NACamera' ) continue; $eqLogic = eqLogic::byLogicalId($module['id'], 'netatmopro'); if (!is_object($eqLogic) && $update === false) { log::add('netatmopro', 'debug', 'synchronize:: D_CREATE_IN_PROGRESS:' . var_export($module, true)); $eqLogic = new netatmopro(); $eqLogic->createEqLogic($module, $modules[$module['id']]['name']); $eqLogic->createCmd(); } if ( is_object($eqLogic) ) { log::add('netatmopro', 'debug', 'synchronize:: D_UPDATE_IN_PROGRESS:' . var_export($module, true)); foreach ($eqLogic->getConfiguration('dashboard') as $key) { $eqLogic->checkAndUpdateCmd($key, $module[$key]); } $eqLogic->setConfiguration('home_id', $home['id']); $eqLogic->setConfiguration('home_name', $home['name']); $eqLogic->setConfiguration('firmware_revision', $module['firmware_revision']); if ( $module['type'] == 'NAPlug' ) { $eqLogic->setConfiguration('wifi_strength', $module['wifi_strength']); $cmd = $eqLogic->getCmd('action', 'switchhomeschedule'); if ( is_object($cmd) ) { $listValue = ''; foreach ($schedules as $kschedule => $vschedule) { $listValue .= $kschedule . '|' . $vschedule['name'] . ';'; if ( array_key_exists('selected', $vschedule) && $vschedule['selected'] == true ) { $eqLogic->checkAndUpdateCmd('homes_schedule', $vschedule['name']); $monday = date('U',strtotime('Monday this week')); $now = time(); log::add('netatmopro', 'debug', 'synchronize:: monday:' . var_export($monday, true)); foreach (array_reverse($vschedule['timetable']) as $ktimetable => $vtimetable) { if ($now-$monday >= $vtimetable['m_offset']*60) { log::add('netatmopro', 'debug', 'synchronize:: vtimetable:' . var_export($vtimetable, true)); break; } } foreach ($vschedule['zones'] as $kzones => $vzones) { if ($vtimetable['zone_id'] == $vzones['id']) { log::add('netatmopro', 'debug', 'synchronize:: vzones:' . var_export($vzones, true)); break; } } $eqLogic->checkAndUpdateCmd('therm_setpoint_zone', $vzones['name']); log::add('netatmopro', 'debug', 'synchronize:: therm_setpoint_zone:' . var_export($vzones['name'], true)); if ( count($vschedule['timetable'])-$ktimetable < count($vschedule['timetable']) ) { $nschedule = $monday + ($vschedule['timetable'][count($vschedule['timetable'])-$ktimetable]['m_offset']*60); if (date("z-Y",$now) == date("z-Y",$nschedule)) { $eqLogic->checkAndUpdateCmd('therm_setpoint_timetable', 'Jusqu\'à ' . date("H:i",$nschedule)); } else { $eqLogic->checkAndUpdateCmd('therm_setpoint_timetable', 'Jusqu\'à ' . $dayweek[date("N",$nschedule)] . ' ' . date("H:i",$nschedule)); } } else { $nschedule = date('U',strtotime('Monday next week')); $eqLogic->checkAndUpdateCmd('therm_setpoint_timetable', 'Jusqu\'à Lundi 00:00'); } } } log::add('netatmopro', 'debug', 'synchronize:: switchhomeschedule:' . var_export($listValue, true)); $cmd->setConfiguration('listValue', substr($listValue, 0, -1)); $cmd->save(); } $eqLogic->checkAndUpdateCmd('therm_mode', $home['therm_mode']); $eqLogic->checkAndUpdateCmd('therm_heating_priority', $homes[$home['id']]['therm_heating_priority']); } else { $eqLogic->setConfiguration('room_id', $rooms[$modules[$module['id']]['room_id']]['id']); $eqLogic->setConfiguration('room_name', $rooms[$modules[$module['id']]['room_id']]['name']); $eqLogic->setConfiguration('room_type', $rooms[$modules[$module['id']]['room_id']]['type']); $eqLogic->setConfiguration('bridge', $module['bridge']); $eqLogic->setConfiguration('rf_strength', $module['rf_strength']); $eqLogic->setConfiguration('battery_level', $module['battery_level']); $eqLogic->setConfiguration('battery_state', $module['battery_state']); $eqLogic->batteryStatus(($module['battery_state'] == 'full' ? 100 : (($module['battery_state'] == 'high' ? 80 : ($module['battery_state'] == 'medium' ? 50 : 20 ))))); } $eqLogic->save(); } } if ($update === false) { $scope[] = 'read_thermostat write_thermostat'; } } } if ( $update === false && !empty($scope) ) { log::add('netatmopro', 'debug', 'synchronize:: scope:' . var_export(implode(';', array_unique($scope)), true)); config::save('scope', implode(';', array_unique($scope)), 'netatmopro'); } } /* * Fonction exécutée automatiquement toutes les minutes par Jeedom */ public static function cron() { $refresh = config::byKey('refresh', 'netatmopro'); log::add('netatmopro', 'debug', 'cron:: refresh:' . var_export($refresh, true)); if (!empty($refresh) && $refresh == true) { config::save('refresh', false, 'netatmopro'); netatmopro::cron5(); } } /* * Fonction exécutée automatiquement toutes les heures par Jeedom public static function cronHourly() { } */ /* * Fonction exécutée automatiquement tous les jours par Jeedom public static function cronDaily() { } */ public static function cron5() { /* * Netatmo\Exceptions\NAApiErrorType - 13 - Application does not have the good scope rights * Netatmo\Exceptions\NAApiErrorType - 500 - Internal Server Error * Netatmo\Exceptions\NACurlErrorType - 7 - Failed to connect to api.netatmo.com port 443: Connection refused * GuzzleHttp\Exception\ConnectException - 0 - cURL error 7: Failed to connect to app.netatmo.net port 443: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) * GuzzleHttp\Exception\ClientException - 403 - Client error: `POST https://xxx` resulted in a `403 Forbidden` response: {"error":{"code":2,"message":"Invalid access token"}} */ try { netatmopro::synchronize(true); } catch (Netatmo\Exceptions\NAApiErrorType $ex) { if ($ex->getCode() === 13 && $ex->getMessage() == 'Application does not have the good scope rights') { } elseif ($ex->getCode() === 500 && $ex->getMessage() == 'Internal Server Error') { } else { log::add('netatmopro', 'error', 'cron5:: ex:' . var_export('Netatmo\Exceptions\NAApiErrorType' . ' - ' . get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } catch (Netatmo\Exceptions\NACurlErrorType $ex) { if ($ex->getCode() === 7 && $ex->getMessage() == 'Failed to connect to api.netatmo.com port 443: Connection refused') { } else { log::add('netatmopro', 'error', 'cron5:: ex:' . var_export('Netatmo\Exceptions\NACurlErrorType' . ' - ' . get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } catch (GuzzleHttp\Exception\ClientException $ex) { if ($ex->getCode() === 403 && strpos($ex->getMessage(), '{"code":2,"message":"Invalid access token"}')) { $jar = new \GuzzleHttp\Cookie\FileCookieJar(jeedom::getTmpFolder('netatmopro') . '/b27536f76a9d4d240a34dbd84bc85085', true); $jar->clear(); $jar->save(jeedom::getTmpFolder('netatmopro') . '/b27536f76a9d4d240a34dbd84bc85085'); } else { log::add('netatmopro', 'error', 'cron5:: ex:' . var_export('GuzzleHttp\Exception\ClientException' . ' - ' . get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } catch (GuzzleHttp\Exception\ConnectException $ex) { if ($ex->getCode() === 0 && strpos($ex->getMessage(), 'cURL error 7: Failed to connect to app.netatmo.net port 443: Connection refused')) { } else { log::add('netatmopro', 'error', 'cron5:: ex:' . var_export('GuzzleHttp\Exception\ConnectException' . ' - ' . get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } catch (Exception $ex) { log::add('netatmopro', 'error', 'cron5:: ex:' . var_export(get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } /* * *********************Méthodes d'instance************************* */ public function preInsert() { } public function postInsert() { } public function preSave() { } public function postSave() { } public function preUpdate() { } public function postUpdate() { } public function preRemove() { } public function postRemove() { } /* * Non obligatoire mais permet de modifier l'affichage du widget si vous en avez besoin */ public function toHtml($_version = 'dashboard') { if ( $this->getConfiguration('type') == 'NAPerson' || $this->getConfiguration('type') == 'NAPlug' || $this->getConfiguration('type') == 'NATherm1' || $this->getConfiguration('type') == 'NARoom' || $this->getConfiguration('type') == 'NRV' || $this->getConfiguration('type') == 'NSD' || $this->getConfiguration('type') == 'NIS' || $this->getConfiguration('type') == 'NACamDoorTag' ) { return parent::toHtml(); } $replace = $this->preToHtml($_version); if (!is_array($replace)) { return $replace; } $version = jeedom::versionAlias($_version); $array = array( 'down' => '', 'up' => '', 'stable' => '', ); foreach ($this->getCmd() as $cmd) { if ($cmd->getType() == 'info') { $replace['#' . $cmd->getLogicalId() . '#'] = $cmd->execCmd(); } } if ( $this->getConfiguration('type') == 'NAMain' ) { $replace['#last_status_store#'] = date('H:i:s', $this->getConfiguration('last_status_store')); $replace['#TemperatureCSS#'] = $this->temperatureToCSS($replace['#Temperature#']); $replace['#temp_symbol#'] = (array_key_exists($replace['#temp_trend#'], $array) ? $array[$replace['#temp_trend#']] : ''); $replace['#pressure_symbol#'] = (array_key_exists($replace['#pressure_trend#'], $array) ? $array[$replace['#pressure_trend#']] : ''); } elseif ( $this->getConfiguration('type') == 'NAModule1' ) { $replace['#last_message#'] = date('H:i:s', $this->getConfiguration('last_message')); $replace['#TemperatureCSS#'] = $this->temperatureToCSS($replace['#Temperature#']); $replace['#temp_symbol#'] = (array_key_exists($replace['#temp_trend#'], $array) ? $array[$replace['#temp_trend#']] : ''); } elseif ( $this->getConfiguration('type') == 'NAModule2' ) { $replace['#last_message#'] = date('H:i:s', $this->getConfiguration('last_message')); $replace['#WindStrengthText#'] = $this->WindStrengthToLib($replace['#WindStrength#']); $replace['#WindAngleText#'] = $this->WindAngleToLib($replace['#WindAngle#']); $replace['#WingAngleCSS#'] = ".thearrow2,.thearrow2:after{left:50%;top:0;transition-duration:3s}.thearrow2:after,.thearrow2:before{content:'';position:absolute;box-sizing: border-box;}.thearrow2{transform:rotate(" . $replace['#WindAngle#'] . "deg);position:absolute;z-index:200;margin-left:-5px;width:10px;height:50%;transform-origin:50% 100%}.thearrow2:after{background-color:NONE;width:0;height:0;border-style:solid;border-width:14px 9px 0;border-color:RGBA(255,121,58,1) transparent transparent;transform:translate(-50%,-50%)}.thearrow2:before{width:6px;height:6px;z-index:9;left:2px;top:-5px;border:2px solid;border-radius:100%}"; } elseif ( $this->getConfiguration('type') == 'NAModule3' ) { $replace['#last_message#'] = date('H:i:s', $this->getConfiguration('last_message')); $SumRain24CSS1 = '
'; $SumRain24CSS2 = '
h-24
'; $replace['#NA3SvgUrl1#'] = __CSSICON__ . 'marker.svg'; $replace['#NA3SvgUrl2#'] = __CSSICON__ . 'water.svg'; $replace['#NA3SvgUrl3#'] = __CSSICON__ . 'rainrulerw34.svg'; $replace['#Rain#'] = (is_numeric($replace['#Rain#']) ? number_format($replace['#Rain#'], 1) : 0); $replace['#sum_rain_24#'] = (is_numeric($replace['#sum_rain_24#']) ? number_format($replace['#sum_rain_24#'], 1) : 0); $replace['#sum_rain_1#'] = (is_numeric($replace['#sum_rain_1#']) ? number_format($replace['#sum_rain_1#'], 1) : 0); $replace['#SumRain24CSS#'] = ($replace['#sum_rain_24#'] == 0 ? $SumRain24CSS1 : $SumRain24CSS2); $replace['#SumRain24#'] = ($replace['#sum_rain_24#'] == 0 ? 0 : number_format(($replace['#sum_rain_24#'] * 2) + 7, 0)); $replace['#SumRain1#'] = ($replace['#sum_rain_1#'] == 0 ? 0 : number_format(($replace['#sum_rain_1#'] * 6) + 3, 0)); } elseif ( $this->getConfiguration('type') == 'NAModule4' ) { $replace['#last_message#'] = date('H:i:s', $this->getConfiguration('last_message')); $replace['#TemperatureCSS#'] = $this->temperatureToCSS($replace['#Temperature#']); $replace['#temp_symbol#'] = (array_key_exists($replace['#temp_trend#'], $array) ? $array[$replace['#temp_trend#']] : ''); } elseif ( $this->getConfiguration('type') == 'NHC' ) { $replace['#last_status_store#'] = date('H:i:s', $this->getConfiguration('last_status_store')); $replace['#TemperatureCSS#'] = $this->temperatureToCSS($replace['#Temperature#']); $replace['#temp_symbol#'] = (array_key_exists($replace['#temp_trend#'], $array) ? $array[$replace['#temp_trend#']] : ''); $replace['#pressure_symbol#'] = (array_key_exists($replace['#pressure_trend#'], $array) ? $array[$replace['#pressure_trend#']] : ''); $replace['#HealthIndexCSS#'] = $this->HealthIndexToCSS($replace['#health_idx#']); } elseif ( $this->getConfiguration('type') == 'NACamera' ) { $replace['#vpn_url#'] = $this->getConfiguration('vpn_url'); } elseif ( $this->getConfiguration('type') == 'NOC' ) { $replace['#vpn_url#'] = $this->getConfiguration('vpn_url'); } return $this->postToHtml($_version, template_replace($replace, getTemplate('core', $version, $this->getConfiguration('type'), 'netatmopro'))); } /* * Non obligatoire mais ca permet de déclencher une action après modification de variable de configuration public static function postConfig_() { } */ /* * Non obligatoire mais ca permet de déclencher une action avant modification de variable de configuration public static function preConfig_() { } */ public function getIconFile() { log::add('netatmopro', 'debug', 'getIconFile::'); $type = $this->getConfiguration('type'); $filename = __ROOT__.'/core/config/devices/'.$type.'/'.$type.'.png'; return (file_exists($filename) === true ? ('plugins/netatmopro/core/config/devices/'.$type.'/'.$type.'.png') : ('plugins/netatmopro/core/config/devices/default.png')); } private function loadConfigFile() { log::add('netatmopro', 'debug', 'loadConfigFile::'); $type = $this->getConfiguration('type'); $filename = __ROOT__.'/core/config/devices/'.$type.'/'.$type.'.json'; if ( file_exists($filename) === false ) { throw new Exception('Impossible de trouver le fichier de configuration pour l\'équipement de type ' . $this->getConfiguration('type')); } $content = file_get_contents($filename); if (!is_json($content)) { throw new Exception('Le fichier de configuration \'' . $filename . '\' est corrompu'); } $data = json_decode($content, true); if (!is_array($data) || !isset($data['configuration']) || !isset($data['commands'])) { throw new Exception('Le fichier de configuration \'' . $filename . '\' est invalide'); } return $data; } private function createEqLogic($data = null, $name = null) { log::add('netatmopro', 'debug', 'createEqLogic:: data:' . var_export($data, true)); log::add('netatmopro', 'debug', 'createEqLogic:: name:' . var_export($name, true)); $this->setName( $data['type'] == 'NHC' ? $data['station_name'] : ( ($data['type'] == 'NOC' || $data['type'] == 'NACamera' || $data['type'] == 'NAPlug' || $data['type'] == 'NATherm1' || $data['type'] == 'NRV' || $data['type'] == 'NARoom' || $data['type'] == 'NAPerson' || $data['type'] == 'NSD' || $data['type'] == 'NIS' || $data['type'] == 'NACamDoorTag' ) ? ( ($data['type'] == 'NAPlug' || $data['type'] == 'NATherm1' || $data['type'] == 'NRV' || $data['type'] == 'NARoom' || $data['type'] == 'NAPerson' ) ? $name : $data['name'] ) : $data['module_name'] ) ); $this->setLogicalId(($data['type'] == 'NOC' || $data['type'] == 'NACamera' || $data['type'] == 'NAPlug' || $data['type'] == 'NATherm1' || $data['type'] == 'NRV' || $data['type'] == 'NARoom' || $data['type'] == 'NAPerson' || $data['type'] == 'NSD' || $data['type'] == 'NIS' || $data['type'] == 'NACamDoorTag' ) ? $data['id'] : $data['_id']); $this->setEqType_name('netatmopro'); $this->setIsEnable(1); $this->setConfiguration('type', $data['type']); $config = $this->loadConfigFile(); foreach ($config['configuration'] as $key => $value) { $this->setConfiguration($key, $value); } $this->save(); } private function createCmd() { log::add('netatmopro', 'debug', 'createCmd::'); $config = $this->loadConfigFile(); $dashboard = array(); $i = 0; foreach ($config['commands'] as $command) { $cmd = new netatmoproCmd(); $cmd->setOrder($i++); $cmd->setEqLogic_id($this->getId()); utils::a2o($cmd, $command); $cmd->save(); if ( $command['isDashboard'] == true ) { $dashboard[] = $command['logicalId']; } } $this->setConfiguration('dashboard', $dashboard); $this->save(); } private function temperatureToCSS($temperature) { log::add('netatmopro', 'debug', 'temperatureToCSS:: temperature:' . var_export($temperature, true)); $array = array( 'freezing' => '270deg, #00a4b4, #32004b', 'cold' => '270deg, #00a4b4, #087883', 'gettingcolder' => '90deg, #01a4b4, #03bf87', 'colder' => '90deg, #00a4b4, #88b04b', 'cooler' => '270deg, #9aba2f, #1b9aa7', 'mild' => '90deg, #9aba2f, #ffae00', 'milder' => '90deg, #88b04b, #e5c21f', 'gettingcooler' => '90deg, #88b04b, #ff8841', 'warm' => '90deg, #efa80f, #ff8841', 'warmer' => '90deg, #ff8841, #ec6615', 'hot' => '90deg, #ff8841, #ec6615', 'hotter' => '90deg, #ff8841, #e60e4b', 'veryhot' => '90deg, #fd7641, #637ff6', 'extremehot' => '90deg, #de2c52, #637ff6', ); $key = ( $temperature < 0 ? 'freezing' : ( $temperature < 5 ? 'cold' : ( $temperature < 7 ? 'gettingcolder' : ( $temperature < 10 ? 'colder' : ( $temperature < 12 ? 'cooler' : ( $temperature < 15 ? 'mild' : ( $temperature < 18 ? 'milder' : ( $temperature < 20 ? 'gettingcooler' : ( $temperature < 23 ? 'warm' : ( $temperature < 25 ? 'warmer' : ( $temperature < 27 ? 'hot' : ( $temperature < 30 ? 'hotter' : ( $temperature < 35 ? 'hotter' : ( $temperature < 40 ? 'veryhot' : 'extremehot' ) ) ) ) ) ) ) ) ) ) ) ) ) ); return $array[$key]; } private function WindStrengthToLib($wstrength) { log::add('netatmopro', 'debug', 'WindStrengthToLib:: wstrength:' . var_export($wstrength, true)); return ( $wstrength < 1 ? 'Calme' : ( $wstrength < 6 ? 'Très légère brise' : ( $wstrength < 12 ? 'Légère brise' : ( $wstrength < 20 ? 'Petite brise' : ( $wstrength < 29 ? 'Jolie brise' : ( $wstrength < 39 ? 'Bonne brise' : ( $wstrength < 50 ? 'Vent frais' : ( $wstrength < 62 ? 'Grand frais' : ( $wstrength < 75 ? 'Coup de vent' : ( $wstrength < 89 ? 'Fort coup de vent' : ( $wstrength < 103 ? 'Tempête' : ( $wstrength < 118 ? 'Violente tempête' : 'Ouragan' ) ) ) ) ) ) ) ) ) ) ) ); } private function WindAngleToLib($wangle) { log::add('netatmopro', 'debug', 'WindAngleToLib:: wangle:' . var_export($wangle, true)); return ( $wangle < 16.88 ? 'Nord' : ( $wangle < 39.38 ? 'Nord
Nord-Est' : ( $wangle < 61.88 ? 'Nord-Est' : ( $wangle < 84.38 ? 'Est
Nord-Est' : ( $wangle < 106.88 ? 'Est' : ( $wangle < 129.38 ? 'Est
Sud-Est' : ( $wangle < 151.88 ? 'Sud Est' : ( $wangle < 174.38 ? 'Sud
Sud-Est' : ( $wangle < 196.88 ? 'Sud' : ( $wangle < 219.38 ? 'Sud
Sud-Ouest' : ( $wangle < 241.88 ? 'Sud-Ouest' : ( $wangle < 264.38 ? 'Ouest
Sud-Ouest' : ( $wangle < 268.88 ? 'Ouest
' : ( $wangle < 309.38 ? 'Ouest
Nord-Ouest' : ( $wangle < 331.88 ? 'Nord-Ouest' : ( $wangle < 354.38 ? 'Nord
Nord-Ouest' : 'Nord' ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ); } private function HealthIndexToCSS($index) { log::add('netatmopro', 'debug', 'HealthIndexToCSS:: index:' . var_export($index, true)); $array = array( 0 => '#2ecc71', 1 => '#ffc300', 2 => '#ef5733', 3 => '#c70039', 4 => '#900c3f', ); return $array[$index]; } private static function GetGuzzleHd(&$client, &$jar) { $jar = new \GuzzleHttp\Cookie\FileCookieJar(jeedom::getTmpFolder('netatmopro') . '/b27536f76a9d4d240a34dbd84bc85085', true); $client = new Client([ 'cookies' => $jar ]); log::add('netatmopro', 'debug', 'GetGuzzleHd:: FileCookieJar:' . var_export($jar, true)); log::add('netatmopro', 'debug', 'GetGuzzleHd:: netatmocomci_csrf_cookie_na:' . var_export($jar->getCookieByName('netatmocomci_csrf_cookie_na'), true)); if ( $jar->getCookieByName('netatmocomci_csrf_cookie_na') ) { log::add('netatmopro', 'debug', 'GetGuzzleHd:: netatmocomci_csrf_cookie_na:' . var_export(date('l jS \of F Y h:i:s A',$jar->getCookieByName('netatmocomci_csrf_cookie_na')->getExpires()) . ' / ' . date('l jS \of F Y h:i:s A',$jar->getCookieByName('netatmocomci_csrf_cookie_na')->getExpires()-36000), true)); } if ( $jar->getCookieByName('netatmocomci_csrf_cookie_na') == null || ($jar->getCookieByName('netatmocomci_csrf_cookie_na')->getExpires() - 36000) < time() ) { $jar->clear(); $response = $client->request('GET', 'https://my.netatmo.com'); $body = $response->getBody()->getContents(); preg_match('/.*.*/m', $body, $matches); log::add('netatmopro', 'debug', 'GetGuzzleHd:: body:' . var_export($body, true)); log::add('netatmopro', 'debug', 'GetGuzzleHd:: csrf-token:' . var_export($matches[1], true)); $response = $client->request('POST', 'https://auth.netatmo.com/access/postlogin', [ 'form_params' => [ 'email' => config::byKey('username', 'netatmopro'), 'password' => config::byKey('password', 'netatmopro'), 'stay_logged' => 'on', '_token' => $matches[1] ] ]); $jar->save(jeedom::getTmpFolder('netatmopro') . '/b27536f76a9d4d240a34dbd84bc85085'); } } /* * **********************Getteur Setteur*************************** */ } class netatmoproCmd extends cmd { /* * *************************Attributs****************************** */ /* * ***********************Methode static*************************** */ /* * *********************Methode d'instance************************* */ /* * Non obligatoire permet de demander de ne pas supprimer les commandes même si elles ne sont pas dans la nouvelle configuration de l'équipement envoyé en JS public function dontRemoveCmd() { return true; } */ public function execute($_options = array()) { log::add('netatmopro', 'debug', 'execute:: options:' . var_export($_options, true)); log::add('netatmopro', 'debug', 'execute:: this:' . var_export($this, true)); log::add('netatmopro', 'debug', 'execute:: this:' . var_export($this->getConfiguration(), true)); log::add('netatmopro', 'alert', 'Starting task (' . $this->getEqLogic()->getName() . '/' . $this->getName() . ' #' . $this->getId() . ')'); $options = array( 'changestatus' => [ 'method' => 'GET', 'select' => [ 0 => 'status=off', 1 => 'status=on' ] ], 'floodlight_set_config' => [ 'method' => 'GET', 'select' => [ 0 => 'config=%7B%22mode%22:%22off%22%7D', 1 => 'config=%7B%22mode%22:%22on%22%7D', 2 => 'config=%7B%22mode%22:%22auto%22%7D' ] ], 'presence_record_humans' => [ 'method' => 'POST', 'select' => [ 0 => 'presence_settings%5Bpresence_record_humans%5D=ignore', 1 => 'presence_settings%5Bpresence_record_humans%5D=record', 2 => 'presence_settings%5Bpresence_record_humans%5D=record_and_notify' ] ], 'presence_record_vehicles' => [ 'method' => 'POST', 'select' => [ 0 => 'presence_settings%5Bpresence_record_vehicles%5D=ignore', 1 => 'presence_settings%5Bpresence_record_vehicles%5D=record', 2 => 'presence_settings%5Bpresence_record_vehicles%5D=record_and_notify' ] ], 'presence_record_animals' => [ 'method' => 'POST', 'select' => [ 0 => 'presence_settings%5Bpresence_record_animals%5D=ignore', 1 => 'presence_settings%5Bpresence_record_animals%5D=record', 2 => 'presence_settings%5Bpresence_record_animals%5D=record_and_notify' ] ], 'presence_record_movements' => [ 'method' => 'POST', 'select' => [ 0 => 'presence_settings%5Bpresence_record_movements%5D=ignore', 1 => 'presence_settings%5Bpresence_record_movements%5D=record', 2 => 'presence_settings%5Bpresence_record_movements%5D=record_and_notify' ] ], 'notify_unknowns' => [ 'method' => 'POST', 'select' => [ 0 => 'notify_unknowns=empty', 1 => 'notify_unknowns=always' ] ], 'record_movements' => [ 'method' => 'POST', 'select' => [ 0 => 'record_movements=never', 1 => 'record_movements=empty', 2 => 'record_movements=always', ] ], 'notify_movements' => [ 'method' => 'POST', 'select' => [ 0 => 'notify_movements=never', 1 => 'notify_movements=empty', 2 => 'notify_movements=always', ] ], 'record_alarms' => [ 'method' => 'POST', 'select' => [ 0 => 'record_alarms=never', 1 => 'record_alarms=empty', 2 => 'record_alarms=always', ] ], 'smart_notifs' => [ 'method' => 'POST', 'select' => [ 0 => 'smart_notifs=false', 1 => 'smart_notifs=true', ] ], 'startcalibration' => [ 'method' => 'POST', ], 'setthermmode' => [ 'class' => 'Netatmo\Clients\NATherm2ApiClient', 'select' => [ 0 => 'hg', 1 => 'away', 2 => 'schedule', ], ], 'switchhomeschedule' => [ 'class' => 'Netatmo\Clients\NATherm2ApiClient', ], 'setroomthermpoint' => [ 'class' => 'Netatmo\Clients\NATherm2ApiClient', ], 'cancelroomthermpoint' => [ 'class' => 'Netatmo\Clients\NATherm2ApiClient', ], 'setpersonshome' => [ 'class' => 'Netatmo\Clients\NAWelcomeApiClient', ], 'setpersonsaway' => [ 'class' => 'Netatmo\Clients\NAWelcomeApiClient', ], 'truetemperature' => [ 'method' => 'POST', ], 'setheatingpriority' => [ 'method' => 'POST', 'select' => [ 0 => 'priority=eco', 1 => 'priority=comfort', ], ], ); if ( array_key_exists('method', $options[$this->getLogicalId()]) ) { $jar = new \GuzzleHttp\Cookie\FileCookieJar(jeedom::getTmpFolder('netatmopro') . '/b27536f76a9d4d240a34dbd84bc85085', true); $client = new Client([ 'cookies' => $jar ]); log::add('netatmopro', 'debug', 'execute:: FileCookieJar:' . var_export($jar, true)); log::add('netatmopro', 'debug', 'execute:: netatmocomci_csrf_cookie_na:' . var_export($jar->getCookieByName('netatmocomci_csrf_cookie_na'), true)); if ( $jar->getCookieByName('netatmocomci_csrf_cookie_na') ) { log::add('netatmopro', 'debug', 'execute:: netatmocomci_csrf_cookie_na:' . var_export(date('l jS \of F Y h:i:s A',$jar->getCookieByName('netatmocomci_csrf_cookie_na')->getExpires()) . ' / ' . date('l jS \of F Y h:i:s A',$jar->getCookieByName('netatmocomci_csrf_cookie_na')->getExpires()-36000), true)); } if ( $jar->getCookieByName('netatmocomci_csrf_cookie_na') == null || ($jar->getCookieByName('netatmocomci_csrf_cookie_na')->getExpires() - 36000) < time() ) { $jar->clear(); $response = $client->request('GET', 'https://my.netatmo.com'); $body = $response->getBody()->getContents(); preg_match('/.*.*/m', $body, $matches); log::add('netatmopro', 'debug', 'execute:: body:' . var_export($body, true)); log::add('netatmopro', 'debug', 'execute:: csrf-token:' . var_export($matches[1], true)); $response = $client->request('POST', 'https://auth.netatmo.com/access/postlogin', [ 'form_params' => [ 'email' => config::byKey('username', 'netatmopro'), 'password' => config::byKey('password', 'netatmopro'), 'stay_logged' => 'on', '_token' => $matches[1] ] ]); $jar->save(jeedom::getTmpFolder('netatmopro') . '/b27536f76a9d4d240a34dbd84bc85085'); } try { if ( $options[$this->getLogicalId()]['method'] === 'GET' ) { $uri = $this->getEqLogic()->getConfiguration('vpn_url') . '/command/' . $this->getLogicalId() . '?' . $options[$this->getLogicalId()]['select'][$_options['select']]; log::add('netatmopro', 'debug', 'execute:: uri:' . var_export($uri, true)); $response = $client->request($options[$this->getLogicalId()]['method'], $uri); } elseif ( $options[$this->getLogicalId()]['method'] === 'POST') { if ( $this->getEqLogic()->getConfiguration('type') == 'NOC' || $this->getEqLogic()->getConfiguration('type') == 'NACamera' ) { $uri = 'https://app.netatmo.net/api/updatehome?home_id=' . $this->getEqLogic()->getConfiguration('home_id') . '&' . $options[$this->getLogicalId()]['select'][$_options['select']] . '&ci_csrf_netatmo=' . $jar->getCookieByName('netatmocomci_csrf_cookie_na')->getValue(); } elseif ( $this->getEqLogic()->getConfiguration('type') == 'NARoom' ) { $uri = 'https://app.netatmo.net/api/truetemperature?home_id=' . $this->getEqLogic()->getConfiguration('home_id') . '&room_id=' . $this->getEqLogic()->getLogicalId() . '&corrected_temperature=' . $_options['title'] . '¤t_temperature=' . $this->getEqLogic()->getCmd('info', 'therm_measured_temperature')->execCmd() . '&ci_csrf_netatmo=' . $jar->getCookieByName('netatmocomci_csrf_cookie_na')->getValue(); } elseif ( $this->getEqLogic()->getConfiguration('type') == 'NAPlug' ) { $uri = 'https://app.netatmo.net/api/setheatingpriority?home_id=' . $this->getEqLogic()->getConfiguration('home_id') . '&' . $options[$this->getLogicalId()]['select'][$_options['select']] . '&ci_csrf_netatmo=' . $jar->getCookieByName('netatmocomci_csrf_cookie_na')->getValue(); } else { $uri = 'https://app.netatmo.net/syncapi/v1/' . $this->getLogicalId() . '?home_id=' . $this->getEqLogic()->getConfiguration('home_id') . '&device_id=' . $this->getEqLogic()->getConfiguration('bridge') . '&module_id=' . $this->getEqLogic()->getLogicalId(); } log::add('netatmopro', 'debug', 'execute:: uri:' . var_export($uri, true)); $response = $client->request($options[$this->getLogicalId()]['method'], $uri, [ 'headers' => [ 'Authorization' => 'Bearer ' . urldecode($jar->getCookieByName('netatmocomaccess_token')->getValue()) ] ]); } } catch (Exception $ex) { log::add('netatmopro', 'error', 'execute:: ex:' . var_export(get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } else { $config = array("client_id" => config::byKey('client_id', 'netatmopro'), "client_secret" => config::byKey('client_secret', 'netatmopro'), "username" => config::byKey('username', 'netatmopro'), "password" => config::byKey('password', 'netatmopro'), "scope" => 'read_station read_homecoach read_presence access_presence read_camera access_camera write_camera read_thermostat write_thermostat'); $client = new $options[$this->getLogicalId()]['class']($config); $tokens = $client->getAccessToken(); try { if ( $this->getLogicalId() == 'setthermmode' ) { $client->setThermMode($this->getEqLogic()->getConfiguration('home_id'), $options[$this->getLogicalId()]['select'][$_options['select']]); } elseif ($this->getLogicalId() == 'switchhomeschedule' ) { $client->switchHomeSchedule($_options['select'], $this->getEqLogic()->getConfiguration('home_id')); } elseif ($this->getLogicalId() == 'setroomthermpoint' ) { $client->setRoomThermpoint($this->getEqLogic()->getConfiguration('home_id'), $this->getEqLogic()->getLogicalId(), $_options['title'] ? 'manual' : 'home', $_options['title'] ? $_options['title'] : NULL, ($_options['title'] && $_options['message']) ? (time() + ($_options['message']*60)) : NULL); } elseif ($this->getLogicalId() == 'cancelroomthermpoint' ) { $client->setRoomThermpoint($this->getEqLogic()->getConfiguration('home_id'), $this->getEqLogic()->getLogicalId(), 'home'); } elseif ($this->getLogicalId() == 'setpersonshome' ) { $client->setPersonsHome($this->getEqLogic()->getConfiguration('home_id'), [$this->getEqLogic()->getLogicalId()]); } elseif ($this->getLogicalId() == 'setpersonsaway' ) { $client->setPersonsAway($this->getEqLogic()->getConfiguration('home_id'), $this->getEqLogic()->getConfiguration('type') == 'NACamera' ? NULL : $this->getEqLogic()->getLogicalId()); } } catch (Exception $ex) { log::add('netatmopro', 'error', 'execute:: ex:' . var_export(get_class($ex) . ' - ' . $ex->getCode() . ' - ' . $ex->getMessage(), true)); throw $ex; } } log::add('netatmopro', 'alert', 'Task finished (' . $this->getEqLogic()->getName() . '/' . $this->getName() . ' #' . $this->getId() . ')'); } /* * **********************Getteur Setteur*************************** */ }