Source of file ConsistencyManager.php
Size: 11,292 Bytes - Last Modified: 2020-10-24T02:46:31+00:00
/home/travis/build/NextDom/nextdom-core/src/Managers/ConsistencyManager.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 | <?php /* * This file is part of the NextDom software (https://github.com/NextDom or http://nextdom.github.io). * Copyright (c) 2018 NextDom. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. /* This file is part of Jeedom. * * Jeedom is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Jeedom is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jeedom. If not, see <http://www.gnu.org/licenses/>. */ namespace NextDom\Managers; use NextDom\Enums\Common; use NextDom\Exceptions\CoreException; use NextDom\Model\Entity\Cron; /** * Class ConsistencyManager * @package NextDom\Managers */ class ConsistencyManager { /** * @var array */ private static $defaultSummary = [ 'security' => ['key' => 'security', 'name' => 'Alerte', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-alerte2"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'motion' => ['key' => 'motion', 'name' => 'Mouvement', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-mouvement"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'door' => ['key' => 'door', 'name' => 'Porte', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-porte-ouverte"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'windows' => ['key' => 'windows', 'name' => 'Fenêtre', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-fenetre-ouverte"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'shutter' => ['key' => 'shutter', 'name' => 'Volet', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-volet-ouvert"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'light' => ['key' => 'light', 'name' => 'Lumière', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-lumiere-on"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'outlet' => ['key' => 'outlet', 'name' => 'Prise', 'calcul' => 'sum', 'icon' => '<i class="icon nextdom-prise"></i>', 'unit' => '', 'count' => 'binary', 'allowDisplayZero' => false], 'temperature' => ['key' => 'temperature', 'name' => 'Température', 'calcul' => 'avg', 'icon' => '<i class="icon divers-thermometer31"></i>', 'unit' => '°C', 'allowDisplayZero' => true], 'humidity' => ['key' => 'humidity', 'name' => 'Humidité', 'calcul' => 'avg', 'icon' => '<i class="fa fa-tint"></i>', 'unit' => '%', 'allowDisplayZero' => true], 'luminosity' => ['key' => 'luminosity', 'name' => 'Luminosité', 'calcul' => 'avg', 'icon' => '<i class="icon meteo-soleil"></i>', 'unit' => 'lx', 'allowDisplayZero' => false], 'power' => ['key' => 'power', 'name' => 'Puissance', 'calcul' => 'sum', 'icon' => '<i class="fa fa-bolt"></i>', 'unit' => 'W', 'allowDisplayZero' => false], ]; /** * Start consistency check of the system * * @throws CoreException */ public static function checkConsistency() { try { self::ensureConfiguration(); CronManager::clean(); self::removeDeprecatedCrons(); self::checkAllDefaultCrons(); self::cleanWidgetCache(); self::saveObjects(); self::resetCommandsActionID(); } catch (\Exception $e) { throw new CoreException("error while checking system consistency: " . $e->getMessage()); } } /** * @TODO: ??? * @throws \Exception */ private static function ensureConfiguration() { $summary = ConfigManager::byKey("object:summary"); if (!is_array($summary)) { ConfigManager::save("object:summary", self::$defaultSummary); } ConfigManager::save('hardware_name', ''); if ("" == ConfigManager::byKey('api')) { ConfigManager::save('api', ConfigManager::genKey()); } } /** * Remove deprecated cron task * * @throws CoreException * @throws \ReflectionException */ private static function removeDeprecatedCrons() { $cronTasksToRemove = [ ['target_class' => 'nextdom', 'action' => 'persist'], ['target_class' => 'history', 'action' => 'historize'], ['target_class' => 'cmd', 'action' => 'collect'], ['target_class' => 'nextdom', 'action' => 'updateSystem'], ['target_class' => 'nextdom', 'action' => 'checkAndCollect'], ['target_class' => 'DB', 'action' => 'optimize'], ]; foreach ($cronTasksToRemove as $cronTask) { $cron = CronManager::byClassAndFunction($cronTask['target_class'], $cronTask['action']); if (true == is_object($cron)) { $cron->remove(); } } } /** * Check if all default cron task are present and add it */ private static function checkAllDefaultCrons() { foreach (self::getDefaultCrons() as $cronClass => $cronData) { foreach ($cronData as $cronName => $cronConfig) { try { $cron = CronManager::byClassAndFunction($cronClass, $cronName); if (false == is_object($cron)) { $cron = new Cron(); } $cron->setClass($cronClass) ->setFunction($cronName) ->setSchedule($cronConfig[Common::SCHEDULE]) ->setTimeout($cronConfig[Common::TIMEOUT]) ->setDeamon(0); if (true == array_key_exists("enabled", $cronConfig)) { $cron->setEnable($cronConfig["enabled"]); } $cron->save(); } catch (\Exception $e) { } } } } /** * @return array */ public static function getDefaultCrons(): array { return [ "nextdom" => [ "backup" => [ Common::SCHEDULE => mt_rand(10, 59) . ' 0' . mt_rand(0, 7) . ' * * *', Common::TIMEOUT => 60, "enabled" => 1 ], "cronDaily" => [ Common::SCHEDULE => "00 00 * * * *", Common::TIMEOUT => 240, "enabled" => 1 ], "cronHourly" => [ Common::SCHEDULE => "00 * * * * *", Common::TIMEOUT => 60, "enabled" => 1 ], "cron10" => [ Common::SCHEDULE => "*/10 * * * * *", Common::TIMEOUT => 10 ], "cron5" => [ Common::SCHEDULE => "*/5 * * * * *", Common::TIMEOUT => 5 ], "cron" => [ Common::SCHEDULE => "* * * * * *", Common::TIMEOUT => 2 ], ], "plugin" => [ "cronDaily" => [ Common::SCHEDULE => "00 00 * * * *", Common::TIMEOUT => 240, "enabled" => 1 ], "cronHourly" => [ Common::SCHEDULE => "00 * * * * *", Common::TIMEOUT => 60, "enabled" => 1 ], "cron" => [ Common::SCHEDULE => "* * * * * *", Common::TIMEOUT => 2 ], "cron5" => [ Common::SCHEDULE => "*/5 * * * * *", Common::TIMEOUT => 5, "enabled" => 1 ], "cron10" => [ Common::SCHEDULE => "*/10 * * * * *", Common::TIMEOUT => 10 ], "cron15" => [ Common::SCHEDULE => "*/15 * * * * *", Common::TIMEOUT => 15 ], "cron30" => [ Common::SCHEDULE => "*/15 * * * * *", Common::TIMEOUT => 30 ], "checkDeamon" => [ Common::SCHEDULE => "*/5 * * * * *", Common::TIMEOUT => 5 ], "heartbeat" => [ Common::SCHEDULE => "*/5 * * * * *", Common::TIMEOUT => 10, "enabled" => 1 ], ], "scenario" => [ "check" => [ Common::SCHEDULE => "* * * * * *", Common::TIMEOUT => 30, "enabled" => 1 ], "control" => [ Common::SCHEDULE => "* * * * * *", Common::TIMEOUT => 30, "enabled" => 1 ], ], "cache" => [ "persist" => [ Common::SCHEDULE => "*/30 * * * * *", Common::TIMEOUT => 30 ], ], "history" => [ "archive" => [ Common::SCHEDULE => "00 5 * * * *", Common::TIMEOUT => 240 ], ], ]; } /** * @throws \Exception */ private static function cleanWidgetCache() { foreach (EqLogicManager::all() as $c_item) { try { $c_item->emptyCacheWidget(); } catch (\Exception $e) { } } } private static function saveObjects() { try { foreach (JeeObjectManager::all() as $c_item) { $c_item->save(); } } catch (\Exception $e) { } } /** * @throws \Exception */ private static function resetCommandsActionID() { foreach (CmdManager::all() as $c_cmd) { try { $value = $c_cmd->getConfiguration("nextdomCheckCmdCmdActionId"); if ("" != $value) { $c_cmd->setConfiguration("nextdomCheckCmdCmdActionId", ""); } $c_cmd->save(); } catch (\Exception $e) { } } } } |