From 35edb03ce7e0a01374e6eeaca2bc4e1e493086fb Mon Sep 17 00:00:00 2001 From: DanoneKiD Date: Tue, 29 Jan 2019 16:23:42 +0100 Subject: [PATCH] Fix issue #4 --- core/ajax/meross.ajax.php | 19 +-- core/class/meross.class.php | 247 +++++++++++++++++---------------- core/class/merossCmd.class.php | 40 ++---- desktop/js/meross.js | 17 --- desktop/modal/health.php | 19 +-- desktop/php/meross.php | 17 --- plugin_info/configuration.php | 49 +++---- plugin_info/install.php | 2 +- 8 files changed, 173 insertions(+), 237 deletions(-) diff --git a/core/ajax/meross.ajax.php b/core/ajax/meross.ajax.php index 3ede99b..492aff8 100644 --- a/core/ajax/meross.ajax.php +++ b/core/ajax/meross.ajax.php @@ -2,7 +2,6 @@ /* * 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 @@ -17,22 +16,6 @@ * along with this program. If not, see . */ -/* This file is part of NextDom. -* -* NextDom 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. -* -* NextDom 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 NextDom. If not, see . -*/ - /** * Fichier appelé lorsque le plugin effectue une requête Ajax */ @@ -71,4 +54,4 @@ } catch (\Exception $e) { // Affiche l'exception levé à l'utilisateur ajax::error(displayExeption($e), $e->getCode()); -} +} \ No newline at end of file diff --git a/core/class/meross.class.php b/core/class/meross.class.php index 32ae7e7..24d2d60 100755 --- a/core/class/meross.class.php +++ b/core/class/meross.class.php @@ -1,44 +1,26 @@ . + * along with this program. If not, see . */ -/* This file is part of NextDom. -* -* NextDom 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. -* -* NextDom 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 NextDom. If not, see . -*/ - /* * ***************************Includes********************************* */ require_once __DIR__ . '/../../../../core/php/core.inc.php'; require_once "merossCmd.class.php"; class meross extends eqLogic { - private static $_FakeJson = __DIR__ . '/../../3rdparty/fakedevices.json'; private static $_Script = __DIR__ . '/../../3rdparty/meross.sh'; @@ -59,39 +41,33 @@ class meross extends eqLogic public static function cron15() { log::add('meross', 'debug', 'cron15: Update informations for all eqLogics...'); - $eqLogics = eqLogic::byType('meross', true); - - $stdout = self::launchScript('--refresh --show'); - if ($stdout != null) - { - foreach ($eqLogics as $eqLogic) { - if ($eqLogic->getIsEnable() == 1) { - $eqLogic->updateInfo($stdout); - } - } - } else { - log::add('meross', 'error', 'cron15: No output from script'); - } + self::syncMeross(false); log::add('meross', 'debug', 'cron15: Cron completed.'); - } - public function launchScript($_args) + + /** + * Launch sh script to use meross lib + * + * @param mixed $_args + * + * @return void + */ + public static function launchScript($_args) { $email = config::byKey('merossEmail', 'meross'); $password = config::byKey('merossPassword', 'meross'); - if ($email == '' || $password == '') - { + if ($email == '' || $password == '') { log::add('meross', 'error', 'shell_exec: Email or password not provided. Please go to plugin configuration.'); return null; } try { - $command = "sudo sh " . self::$_Script . ' --email ' . $email . ' --password ' . $password . ' ' . $_args; - $log = str_replace($password,'xxx',str_replace($email,'xxx',$command)); + $command = "sh " . self::$_Script . ' --email ' . $email . ' --password ' . $password . ' ' . $_args; + $log = str_replace($password, 'xxx', str_replace($email, 'xxx', $command)); log::add('meross', 'debug', 'shell_exec: ' . $log); $stdout = shell_exec($command); + log::add('meross', 'debug', 'shell_exec: result: ' . $stdout); return $stdout; - } catch (\Exception $e) { log::add('meross', 'error', 'shell_exec: Unable to launch script. ' . $e); } @@ -106,7 +82,7 @@ public function launchScript($_args) * * @return void */ - public function getJson($_stdout) + public static function getJson($_stdout) { try { $json = json_decode($_stdout, true); @@ -125,7 +101,7 @@ public function getJson($_stdout) * * @return void */ - public function getJsonFromFile($_file) + public static function getJsonFromFile($_file) { try { $data = file_get_contents($_file); @@ -145,76 +121,85 @@ public function getJsonFromFile($_file) * * @return void */ - public function syncMeross($_fakeDevices = false) + public static function syncMeross($_fakeDevices = false) { log::add('meross', 'debug', 'syncMeross: Load devices from Meross Cloud'); - if ($_fakeDevices == false){ + if ($_fakeDevices == false) { $stdout = self::launchScript('--refresh --show'); $json = self::getJson($stdout); - }else{ + } else { log::add('meross', 'debug', 'syncMeross: Load fake devices for developement'); $json = self::getJsonFromFile(self::$_FakeJson); } - - log::add('meross', 'debug', $json); + foreach ($json as $key=>$devices) { - $device = self::byLogicalId($key, 'meross'); - if (!is_object($device)) { - log::add('meross', 'debug','syncMeross: Add device=' . $devices["name"]); - $device = new self(); - $device->setName($devices["name"]); - $device->setEqType_name("meross"); - $device->setLogicalId($key); - $device->setConfiguration('type', $devices["type"]); - $device->setConfiguration('ip', $devices["ip"]); - $device->setConfiguration('mac', $devices["mac"]); - $device->setConfiguration('online', $devices["online"]); - $device->setConfiguration('appname', $devices["name"]); - $device->setConfiguration('firmversion', $devices["firmversion"]); - $device->setConfiguration('hardversion', $devices["hardversion"]); - $device->save(); + $eqLogic = self::byLogicalId($key, 'meross'); - // Charge la définition du device - $jsonCmd = self::getJsonFromFile(__DIR__ . '/../../core/config/devices/'.$devices["type"].'/def.json'); - foreach($jsonCmd['commands'] as $key=>$commandes){ - $cmd = $device->getCmd(null, $commandes['logicalId']); - if (!is_array($cmd) || !is_object($cmd) ) { - log::add('meross', 'debug','syncMeross: - Add cmd=' .$commandes['logicalId']); - $cmd = new merossCmd(); - $cmd->setLogicalId($commandes['logicalId']); - $cmd->setName(__($commandes['name'], __FILE__)); - $cmd->setType($commandes['type']); - $cmd->setSubType($commandes['subtype']); - $cmd->setEqLogic_id($device->getId()); - $cmd->setIsVisible($commandes['isVisible']); - if(isset($commandes['isHistorized']) && $commandes['type'] == 'info') - { - $cmd->setIsHistorized($commandes['isHistorized']); - } else { - $cmd->setIsHistorized(false); - } - $cmd->setDisplay('generic_type', $commandes['display']['generic_type']); - $cmd->setTemplate('dashboard', $commandes['template']['dashboard']); - $cmd->setTemplate('mobile', $commandes['template']['mobile']); + if (!is_object($eqLogic)) { + // Device doesn't exist in DB + log::add('meross', 'debug', 'syncMeross: Add device=' . $devices["name"]); + $eqLogic = new self(); + $eqLogic->setName($devices["name"]); + $eqLogic->setEqType_name("meross"); + $eqLogic->setLogicalId($key); + } else { + log::add('meross', 'debug', 'syncMeross: Update device=' . $devices["name"]); + } - $cmd->save(); - - $splitCommandes = explode("_", $commandes['logicalId']); - if ($splitCommandes[0] == 'onoff'){ - // Mémorise l'ID de la cmd onoff_x pour affecter aux cmd "on_x" & "off_x" - $etatid = $cmd->getId(); - } elseif ($splitCommandes[0] == 'on' || $splitCommandes[0] == 'off' ) - { - // Affecte l'ID de la cmd onoff_x en value - log::add('meross', 'debug','syncMeross: - Set value : ' .$commandes["value"]); - $cmd->setValue($etatid); - $cmd->save(); - } + // Update infos of eqLogic (for both, adding and updating) + self::updateEqLogicConfig($eqLogic, $devices); + + // Load device def from json + $jsonCmd = self::getJsonFromFile(__DIR__ . '/../../core/config/devices/'.$devices["type"].'/def.json'); + foreach ($jsonCmd['commands'] as $key=>$commandes) { + $cmd = $eqLogic->getCmd(null, $commandes['logicalId']); + if (!is_object($cmd)) { + // Add cmd to eqLogic + log::add('meross', 'debug', 'syncMeross: - Add cmd=' .$commandes['logicalId']); + $cmd = new merossCmd(); + $cmd->setName(__($commandes['name'], __FILE__)); + $cmd->setIsVisible($commandes['isVisible']); + if (isset($commandes['isHistorized']) && $commandes['type'] == 'info') { + $cmd->setIsHistorized($commandes['isHistorized']); + } else { + $cmd->setIsHistorized(false); } + $cmd->setLogicalId($commandes['logicalId']); + $cmd->setEqLogic_id($eqLogic->getId()); + } else { + // cmd already exist + log::add('meross', 'debug', 'syncMeross: - Update cmd=' .$commandes['logicalId']); + } + + // Update cmd def + $cmd->setType($commandes['type']); + $cmd->setSubType($commandes['subtype']); + $cmd->setDisplay('generic_type', $commandes['display']['generic_type']); + $cmd->setGeneric_type($commandes['display']['generic_type']); + if (isset($commandes['unite']) && $commandes['type'] == 'info') { + $cmd->setUnite($commandes['unite']); + } + + //$cmd->setTemplate('dashboard', $commandes['template']['dashboard']); + //$cmd->setTemplate('mobile', $commandes['template']['mobile']); + + $cmd->save(); + + $splitCommandes = explode("_", $commandes['logicalId']); + if ($splitCommandes[0] == 'onoff') { + // Mémorise l'ID de la cmd onoff_x pour affecter aux cmd "on_x" & "off_x" + $etatid = $cmd->getId(); + } elseif ($splitCommandes[0] == 'on' || $splitCommandes[0] == 'off') { + // Affecte l'ID de la cmd onoff_x en value + log::add('meross', 'debug', 'syncMeross: - Set value : ' .$commandes["value"]); + $cmd->setValue($etatid); + $cmd->save(); } - } else { - log::add('meross', 'debug','syncMeross: ' . $devices["name"] . ' already exist.'); } + + // For Update all cmds values + log::add('meross', 'debug', 'syncMeross: Update cmds values for eqLogic=' . $eqLogic->getLogicalId()); + self::updateInfo($eqLogic, $stdout); } log::add('meross', 'debug', 'syncMeross: synchronization completed.'); @@ -222,9 +207,9 @@ public function syncMeross($_fakeDevices = false) - public function updateInfo($_stdout) + public static function updateInfo($_eqLogic, $_stdout) { - log::add('meross', 'debug','updateInfo: ' . $_stdout ); + log::add('meross', 'debug', 'updateInfo: ' . $_stdout); try { $infos = self::getJson($_stdout); @@ -234,33 +219,49 @@ public function updateInfo($_stdout) } foreach ($infos as $key=>$devices) { - if ($key == $this->getLogicalId()) { + if ($key == $_eqLogic->getLogicalId()) { foreach ($devices as $key2=>$Commands) { - $cmd = $this->getCmd(null, $key2); - if (!is_array($cmd) || !is_object($cmd) ) { - if($key2 == "onoff") { + $cmd = $_eqLogic->getCmd(null, $key2); + if (!is_array($cmd) || !is_object($cmd)) { + if ($key2 == "onoff") { foreach ($Commands as $key3=>$value) { log::add('meross', 'debug', 'updateInfo: -channel_' . $key3 .'=' . $value); - $this->checkAndUpdateCmd("onoff_".$key3, $value); + $_eqLogic->checkAndUpdateCmd("onoff_".$key3, $value); } - }else{ + } else { log::add('meross', 'debug', 'updateInfo: -' . $key2 .'=' . $devices[$key2]); - $this->checkAndUpdateCmd($key2, $devices[$key2]); + $_eqLogic->checkAndUpdateCmd($key2, $devices[$key2]); } } } - log::add('meross', 'debug', 'updateInfo: -Update eqLogic informations'); - $this->setConfiguration('ip', $devices['ip']); - $this->setConfiguration('online', $devices['online']); - $this->setConfiguration('firmversion', $devices['firmversion']); - $this->setConfiguration('hardversion', $devices['hardversion']); - $this->setConfiguration('appname', $devices['name']); - $this->save(); + self::updateEqLogicConfig($_eqLogic, $devices); + break; } } log::add('meross', 'debug', 'updateInfo: Update completed'); } - + + /** + * Update informations of eqLogic and save to configuration + * + * @param mixed $_eqLogic + * @param mixed $_device + * + * @return void + */ + public static function updateEqLogicConfig($_eqLogic, $_device) + { + log::add('meross', 'debug', 'updateEqLogicConfig: Update eqLogic informations'); + $_eqLogic->setConfiguration('type', $_device["type"]); + $_eqLogic->setConfiguration('ip', $_device['ip']); + $_eqLogic->setConfiguration('mac', $_device["mac"]); + $_eqLogic->setConfiguration('online', $_device['online']); + $_eqLogic->setConfiguration('firmversion', $_device['firmversion']); + $_eqLogic->setConfiguration('hardversion', $_device['hardversion']); + $_eqLogic->setConfiguration('appname', $_device['name']); + $_eqLogic->save(); + log::add('meross', 'debug', 'updateEqLogicConfig: Completed'); + } } diff --git a/core/class/merossCmd.class.php b/core/class/merossCmd.class.php index 45e54ad..3481345 100644 --- a/core/class/merossCmd.class.php +++ b/core/class/merossCmd.class.php @@ -1,37 +1,21 @@ . + * along with this program. If not, see . */ -/* This file is part of NextDom. -* -* NextDom 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. -* -* NextDom 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 NextDom. If not, see . -*/ - require_once __DIR__ . '/../../../../core/php/core.inc.php'; class merossCmd extends cmd @@ -45,7 +29,7 @@ public function execute($_options = array()) $password = config::byKey('merossPassword', 'meross'); // Base cmd - $command = 'sudo sh ' . __DIR__ . '/../../3rdparty/meross.sh' . ' --email ' . $email . ' --password ' . $password . ' --uuid ' . $eqLogic->getLogicalId() . ' --show '; + $command = 'sh ' . __DIR__ . '/../../3rdparty/meross.sh' . ' --email ' . $email . ' --password ' . $password . ' --uuid ' . $eqLogic->getLogicalId() . ' --show '; // If action need to be executed $execute = false; @@ -73,10 +57,10 @@ public function execute($_options = array()) log::add('meross','debug','shell_exec: ' . $log); $result = trim(shell_exec($command)); log::add('meross','debug','shell_exec: result: ' . $result); - $eqLogic->updateInfo($result); + meross::updateInfo($eqLogic, $result); } else { log::add('meross','debug','action: Action=' . $action . ' not implemented. '); } } -} +} \ No newline at end of file diff --git a/desktop/js/meross.js b/desktop/js/meross.js index c31e1a2..acc55cd 100755 --- a/desktop/js/meross.js +++ b/desktop/js/meross.js @@ -1,6 +1,5 @@ /* * 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 @@ -15,22 +14,6 @@ * along with this program. If not, see . */ -/* This file is part of NextDom. -* -* NextDom 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. -* -* NextDom 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 NextDom. If not, see . -*/ - $("#table_cmd").sortable({ axis: "y", diff --git a/desktop/modal/health.php b/desktop/modal/health.php index bc30764..b879d23 100755 --- a/desktop/modal/health.php +++ b/desktop/modal/health.php @@ -1,18 +1,19 @@ . + * along with this program. If not, see . */ if (!isConnect('admin')) { diff --git a/desktop/php/meross.php b/desktop/php/meross.php index e439020..554910c 100755 --- a/desktop/php/meross.php +++ b/desktop/php/meross.php @@ -1,7 +1,6 @@ . */ -/* This file is part of NextDom. -* -* NextDom 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. -* -* NextDom 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 NextDom. If not, see . -*/ - if (!isConnect('admin')) { throw new \Exception('{{401 - Accès non autorisé}}'); } diff --git a/plugin_info/configuration.php b/plugin_info/configuration.php index fb2d778..9c48a52 100644 --- a/plugin_info/configuration.php +++ b/plugin_info/configuration.php @@ -22,35 +22,36 @@ include_file('core', 'authentification', 'php'); if (!isConnect()) { - include_file('desktop', '404', 'php'); - die(); + include_file('desktop', '404', 'php'); + die(); } ?>
-
-
-
- -
- - +
+
+
+ +
+ + +
+
-
-
-
- -
-
- -
- - +
+ +
+
+ +
+ + +
+
-
-
-
-
- + + \ No newline at end of file diff --git a/plugin_info/install.php b/plugin_info/install.php index f6cc3d0..f396ee9 100755 --- a/plugin_info/install.php +++ b/plugin_info/install.php @@ -64,7 +64,7 @@ function Meross_remove() /** * Remove __pycache__ from 3rdparty folders - * + * * @return void */ function cleanPyCache()