. */ try { require_once dirname(__FILE__) . '/../../../../core/php/core.inc.php'; require_once dirname(__FILE__) . '/../class/smartthings.class.php'; include_file('core', 'authentification', 'php'); if (!isConnect('admin')) { throw new Exception(__('401 - Accès non autorisé', __FILE__)); } ajax::init(); if (init('action') == 'loginSmartthings') { if (init('force') == 'bt_loginSmartthings') { if (network::getUserLocation() == 'internal') { ajax::success(array('error' => 'internal')); } if (network::getNetworkAccess('external') == 'http:') { ajax::success(array('error' => 'external')); } } $url = smartthings::checkAndCreateApp(); ajax::success(array('redirect' => $url)); } if (init('action') == 'updateConf') { smartthings::updateConf(false); ajax::success(); } if (init('action') == 'synchronize') { smartthings::synchronize(); ajax::success(); } if (init('action') == 'getDevicesInfo') { $result = array(); $eqLogic = smartthings::byId(init('id')); $result['state'] = 'NOK'; if (is_object($eqLogic)) { if ($eqLogic->getConfiguration('type') == 'Location') { $link = smartthings::API_LOCATIONS_URL . "/" . $eqLogic->getConfiguration('locationId'); $result['state'] = 'OK'; $result['result'] = smartthings::sendRequest($link); } else { $link = smartthings::API_DEVICES_URL . "/" . $eqLogic->getConfiguration('deviceId') . "/status"; $result['state'] = 'OK'; $result['result'] = smartthings::sendRequest($link); } $result['url'] = $link; } ajax::success($result); } if (init('action') == 'deleteEquipments') { smartthings::deleteEquipments(init('what')); ajax::success(); } if (init('action') == 'setOrder') { if (!isConnect('admin')) { throw new Exception(__('401 - Accès non autorisé', __FILE__)); } $position = 0; $order = ''; foreach (json_decode(init('objects'), true) as $id) { $order .= $id.'|'.$position.';'; $position++; } config::save('catOrder', $order, 'smartthings'); ajax::success(); } if (init('action') == 'fixCategories') { config::remove('catOrder', 'smartthings'); ajax::success(); } if (init('action') == 'autoDetectModule') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } if (init('createcommand') == 1) { foreach ($eqLogic->getCmd() as $cmd) { $cmd->remove(); } } $eqLogic->loadCmdFromConf('refreshInfo'); if ($eqLogic->getConfiguration('type') == 'Location') { smartthings::getLocations(); } else { $eqLogic->loadCmdFromConf('refreshInfo'); $eqLogic->loadCmdFromConf('health'); smartthings::getDevicesInfo($eqLogic, $eqLogic->getConfiguration('deviceId')); smartthings::fixInfoValueActionCmd($eqLogic); //màj setValue dans les cmd } ajax::success(); } if (init('action') == 'removeRoom') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = smartthings::manageRoom("DELETE", $eqLogic, init('roomId')); ajax::success($result); } if (init('action') == 'createRoom') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = smartthings::manageRoom("CREATE", $eqLogic, null, init('name')); ajax::success($result); } if (init('action') == 'modifyRoom') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } if (init('name') == init('oldName')) { throw new Exception(__('Nom de pièce SmartThings inchangé : ', __FILE__) . init('name')); } $result = smartthings::manageRoom("MODIFY", $eqLogic, init('roomId'), init('name')); ajax::success($result); } if (init('action') == 'syncRooms') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } smartthings::getLocations(); ajax::success(); } if (init('action') == 'syncDevice') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = smartthings::getDevices(false); ajax::success($result); } if (init('action') == 'syncHub') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = $eqLogic->getHub(); ajax::success($result); } if (init('action') == 'getPathLogo') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = $eqLogic->getPathLogo(); ajax::success($result); } if (init('action') == 'getCacheAndConfig') { $result = array(); $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = array("cache" => $eqLogic->getCache(),"configuration" => $eqLogic->getConfiguration()); ajax::success($result); } if (init('action') == 'getCmdFromLogicalId') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { ajax::success(); } $cmd = $eqLogic->getCmd('info', init('component').'::'. init('capability').'::'.init('command')); if (is_object($cmd)) { ajax::success(['cmd_id' => $cmd->getId(), 'cmd_name' => $cmd->getName()]); } ajax::success(); } if (init('action') == 'getEqLogicByDeviceId') { ajax::success(smartthings::byDeviceId(init('deviceId'))); } if (init('action') == 'removeOnSmartthings') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } $result = smartthings::manageDevice("DELETE", $eqLogic, init('id')); ajax::success($result); } if (init('action') == 'modifyLocationOnSmartthings') { $body = array(); $listActions = array('name','countryCode','latitude','longitude','regionRadius','temperatureScale','timeZoneId','locale'); $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } foreach ($listActions as $action) { if ($action == 'name' && init($action) !== "") { $body[$action] = init($action); } elseif (strval($eqLogic->getConfiguration($action)) !== init($action)) { $body[$action] = init($action); } } log::add('smartthings', 'debug', 'Information modifiée envoyée à Smartthings : '. json_encode($body)); $result = smartthings::manageLocation("MODIFY", $eqLogic, init('id'), $body); ajax::success($result); } if (init('action') == 'modifyDeviceOnSmartthings') { $listActions = array('label','locationId','roomId'); $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { throw new Exception(__('SmartThings eqLogic non trouvé : ', __FILE__) . init('id')); } foreach ($listActions as $action) { if (strval($eqLogic->getConfiguration($action)) !== init($action)) { $body[$action] = init($action); } } log::add('smartthings', 'debug', 'Information modifiée envoyée à Smartthings : '. json_encode($body)); $result = smartthings::manageDevice("MODIFY", $eqLogic, init('id'), $body); ajax::success($result); } if (init('action') == 'getConfFile') { $eqLogic = smartthings::byId(init('id')); if (!is_object($eqLogic)) { ajax::success(); } ajax::success($eqLogic->getConfFilePath(init('type'))); } if (init('action') == 'searchCapabilityOnST') { $cmd = cmd::byId(init('id')); if (!is_object($cmd)) { $result['state'] = 'NOK'; ajax::success($result); } $item = smartthings::API_CAPABILITIES_URL . "/" . init('capability'); $result['state'] = 'NOK'; $result['result'] = smartthings::sendRequest($item); if (!is_array($result['result'])) { $result['state'] = 'NOK'; ajax::success($result); } $version = "1"; if ($result['result']['items'][0]['version'] !== "" && is_numeric($result['result']['items'][0]['version'])) { $version = $result['result']['items'][0]['version']; } $versionItem = $item . "/" . $version; $result['versionResult'] = smartthings::sendRequest($versionItem); if (!is_array($result['versionResult'])) { $result['versionState'] = 'NOK'; ajax::success($result); } $result['versionState'] = 'OK'; if (!is_file(dirname(__FILE__) . '/../../3rdparty/capabilities/' . init('capability') . '.json')) { file_put_contents(dirname(__FILE__) . '/../../3rdparty/capabilities/' . init('capability') . '.json', json_encode($result['versionResult'])); } log::add('smartthings', 'debug', __('Fichier téléchargé et mis dans le dossier ', __FILE__) . json_encode($result['versionResult'])); ajax::success($result); } if (init('action') == 'listTemplate') { $return = smartthings::listTemplate(init('id')); ajax::success($return); } if (init('action') == 'getListValue') { $cmd = cmd::byId(init('id')); $list = (init('list')) ? init('list') : 'listValue'; if (is_object($cmd)) { ajax::success($cmd->getConfiguration($list, false)); } } if (init('action') == 'getWidgetHelp') { $filePath = dirname(__FILE__) . '/../template/dashboard/' . init('widgetName'); $translatePath = 'plugins/smartthings/core/template/dashboard/' . init('widgetName'); if (file_exists($filePath)) { $template = file_get_contents($filePath); if (strpos($template, '') !== false) { $templateHelp = explode('', $template)[0]; $templateHelp = explode('