. */ /* * ***************************Includes********************************* */ require_once __DIR__ . '/../../../../core/php/core.inc.php'; require_once dirname(__FILE__) . '/../../core/php/pronotlink.inc.php'; class pronotlink extends eqLogic { /* * *************************Attributs****************************** */ public static function templateWidget() { $return['action']['message']['message'] = array( 'template' => 'message', 'replace' => array("#_desktop_width_#" => "100", "#_mobile_width_#" => "50", "#title_disable#" => "1", "#message_disable#" => "0") ); $return['action']['message']['embed'] = array( 'template' => 'embed', 'replace' => array("#_desktop_width_#" => "100", "#_mobile_width_#" => "50", "#title_disable#" => "1", "#message_disable#" => "0") ); return $return; } public static function pronoteCron() { foreach (eqLogic::byType('pronotlink') as $eqLogic) { if ($eqLogic->getConfiguration("forceRefresh", false)) { self::refreshinfo($eqLogic); $eqLogic->setConfiguration("forceRefresh", false); $eqLogic->setConfiguration("noRefresh", true); $eqLogic->save(); } } } /* * ***********************Methode static*************************** */ public function refreshinfo($eqLogic) { if ($eqLogic->getIsEnable() != 1) return; $username = $eqLogic->getConfiguration('username'); $password = $eqLogic->getConfiguration('password'); $urlpronote = $eqLogic->getConfiguration('url'); $cas = $eqLogic->getConfiguration('cas'); $typecon = $eqLogic->getConfiguration('typecon'); $name = $eqLogic->getConfiguration('enfantName', "null"); if (!isset($username) && !isset($password) && !isset($urlpronote) && !isset($cas)) return; if ($typecon == "parent" && ($name == "null" || !isset($name))) return; log::add('pronotlink', 'info', 'Refresh des info du compte : ' . $eqLogic->getConfiguration('username')); prolink_work::setupDay($eqLogic); $date = new DateTime(); $timestamp = $date->getTimestamp(); $EDT = prolink_data::getEDT($eqLogic, date("Y-m-d", $timestamp - 432000), date("Y-m-d", $timestamp + 1814400)); $Notes = prolink_work::getAllNotes($eqLogic); $Competence = prolink_work::getAllCompetence($eqLogic); $Homeworks = prolink_data::getHomeworks($eqLogic, date("Y-m-d", $timestamp - 64800), date("Y-m-d", $timestamp + 1814400)); $Infos = prolink_data::getInfos($eqLogic); $Absence = prolink_data::getAbsences($eqLogic); if ($EDT != false) { $EDTcmd = $eqLogic->getCmd('info', 'JsonEDT'); $EDTcmd->event(json_encode($EDT)); $EDTcmd->save(); } if ($Notes != false) { $Notescmd = $eqLogic->getCmd('info', 'JsonNotes'); $Notescmd->event(json_encode($Notes)); $Notescmd->save(); $JsonNoteByDate = prolink_work::setNotesDate($Notes); $JsonNoteByDatecmd = $eqLogic->getCmd('info', 'JsonNoteByDate'); prolink_event::getLastMarks($eqLogic, $Notes); $JsonNoteByDatecmd->event(json_encode($JsonNoteByDate)); $JsonNoteByDatecmd->save(); } if ($Competence != false) { $Competencecmd = $eqLogic->getCmd('info', 'JsonComp'); $Competencecmd->event(json_encode($Competence)); $Competencecmd->save(); prolink_event::getLastCompetence($eqLogic, $Competence); } if ($Homeworks != false) { $Homeworkscmd = $eqLogic->getCmd('info', 'JsonDevoirs'); $Homeworkscmd->event(json_encode($Homeworks)); $Homeworkscmd->save(); $HomeWorksArray = prolink_work::setHomeWork($Homeworks); if ($HomeWorksArray != false) { $JsonHomeworksMSGcmd = $eqLogic->getCmd('info', 'JsonHomeworksMSG'); $JsonHomeworksMSGcmd->event(json_encode($HomeWorksArray)); $JsonHomeworksMSGcmd->save(); $HomeWorksMSG = prolink_work::setupMessageDevoirs($HomeWorksArray); if ($HomeWorksArray != false) { $HomeWorksMSGcmd = $eqLogic->getCmd('info', 'HomeworksMSG'); $HomeWorksMSGcmd->event($HomeWorksMSG); $HomeWorksMSGcmd->save(); } } } if ($Infos != false) { $Infoscmd = $eqLogic->getCmd('info', 'JsonInformation'); $Infoscmd->event(json_encode($Infos)); $Infoscmd->save(); } if ($Absence != false) { $Absencecmd = $eqLogic->getCmd('info', 'JsonAbsences'); $Absencecmd->event(json_encode($Absence)); $Absencecmd->save(); prolink_event::getLastAbsence($eqLogic, $Absence['data']); } log::add('pronotlink', 'debug', 'Fin de l\'update'); } public static function pronoteCron15() { foreach (eqLogic::byType('pronotlink') as $eqLogic) { self::refreshinfo($eqLogic); } } /* * *********************Méthodes d'instance************************* */ public static function dependancy_info() { $return = array(); $return['log'] = 'pronotlink_dep'; $resources = realpath(dirname(__FILE__) . '/../../resources/'); $packageJson = json_decode(file_get_contents($resources . '/package.json'), true); $state = 'ok'; foreach ($packageJson["dependencies"] as $dep => $ver) { if (!file_exists($resources . '/node_modules/' . $dep . '/package.json')) { $state = 'nok'; } } $return['progress_file'] = jeedom::getTmpFolder('pronotlink') . '/dependance'; $return['state'] = $state; return $return; } public static function dependancy_install($verbose = "false") { if (file_exists(jeedom::getTmpFolder('pronotlink') . '/dependance')) { return; } log::remove('pronotlink_dep'); $_debug = 0; if (log::getLogLevel('pronotlink') == 100 || $verbose === "true" || $verbose === true) $_debug = 1; log::add('pronotlink', 'info', 'Installation des dépendances : '); $resource_path = realpath(dirname(__FILE__) . '/../../resources'); return array('script' => $resource_path . '/install.sh ' . $resource_path . ' pronotlink ' . $_debug, 'log' => log::getPathToLog('pronotlink_dep')); } public static function deamon_start($_debug = false) { self::deamon_stop(); $deamon_info = self::deamon_info(); if ($deamon_info['launchable'] != 'ok') throw new Exception(__('Veuillez vérifier la configuration', __FILE__)); log::add('pronotlink', 'info', 'Lancement du bot'); $log = $_debug ? '1' : '0'; $sensor_path = realpath(dirname(__FILE__) . '/../../resources'); $cmd = 'nice -n 19 sh ' . $sensor_path . '/start.sh'; log::add('pronotlink', 'debug', 'Lancement démon pronotlink : ' . $cmd); $result = exec('NODE_ENV=production nohup ' . $cmd . ' >> ' . log::getPathToLog('pronotlink_node') . ' 2>&1 &'); if (strpos(strtolower($result), 'error') !== false || strpos(strtolower($result), 'traceback') !== false) { log::add('pronotlink', 'error', $result); return false; } $i = 0; while ($i < 30) { $deamon_info = self::deamon_info(); if ($deamon_info['state'] == 'ok') break; sleep(1); $i++; } if ($i >= 30) { log::add('pronotlink', 'error', 'Impossible de lancer le démon pronotlink, vérifiez le port', 'unableStartDeamon'); return false; } message::removeAll('pronotlink', 'unableStartDeamon'); log::add('pronotlink', 'info', 'Démon pronotlink lancé'); self::setupCron(); return true; } public static function deamon_stop() { log::add('pronotlink', 'info', 'Arrêt du service pronotlink 2'); $procces = shell_exec('ps aux | grep "/bin/server.js" | grep -v "grep" | wc -l'); $pos = strpos($procces, '1'); if ($pos !== false) { $deamon_info = self::deamon_info(); if ($deamon_info['state'] == 'ok') { sleep(1); shell_exec('sudo kill -9 $(ps aux | grep "/bin/server.js" | grep -v "grep" | awk \'{print $2}\') &>/dev/null'); } $deamon_info = self::deamon_info(); if ($deamon_info['state'] == 'ok') { sleep(1); shell_exec('sudo kill -9 $(ps aux | grep "/bin/server.js" | grep -v "grep" | awk \'{print $2}\') &>/dev/null'); } } } public static function deamon_info() { $return = array(); $return['log'] = 'pronotlink_node'; $return['state'] = 'nok'; // Regarder si pronotlink.js est lancé $pid = trim(shell_exec('ps ax | grep "resources/bin/server.js" | grep -v "grep" | wc -l')); if ($pid != '' && $pid != '0') $return['state'] = 'ok'; // Regarder si le token est ok $return['launchable'] = 'ok'; return $return; } public static function setupCron() { $pronoteCron15 = cron::byClassAndFunction('pronotlink', 'pronoteCron15'); if (!is_object($pronoteCron15)) { $pronoteCron15 = new cron(); $pronoteCron15->setClass('pronotlink'); $pronoteCron15->setFunction('pronoteCron15'); $pronoteCron15->setEnable(1); $pronoteCron15->setSchedule('*/15 * * * *'); $pronoteCron15->setTimeout('10'); $pronoteCron15->save(); } $pronoteCron15->start(); $pronoteCron15->run(); $pronoteCron = cron::byClassAndFunction('pronotlink', 'pronoteCron'); if (!is_object($pronoteCron)) { $pronoteCron = new cron(); $pronoteCron->setClass('pronotlink'); $pronoteCron->setFunction('pronoteCron'); $pronoteCron->setEnable(1); $pronoteCron->setSchedule('* * * * *'); $pronoteCron->setTimeout('2'); $pronoteCron->save(); } $pronoteCron->start(); $pronoteCron->run(); } public function preInsert() { } public function postInsert() { pronotlink::CreateRefreshCmd(); } public static function CreateRefreshCmd() { $eqLogics = eqLogic::byType('pronotlink'); foreach ($eqLogics as $eqLogic) { pronotlink::deleteoldcmd($eqLogic); $TabCmd = array( 'FirstCourToday' => array('cmdUserView' => 1, 'Libelle' => 'Premier cours du jour', 'Type' => 'info', 'SubType' => 'string', 'visible' => 1, 'Template' => ''), 'LastCourToday' => array('cmdUserView' => 1, 'Libelle' => 'Dernier cours du jour', 'Type' => 'info', 'SubType' => 'string', 'visible' => 1, 'Template' => ''), 'FirstCourTodayDebut' => array('cmdUserView' => 1, 'Libelle' => 'Premier cours du jour Début', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'LastCourTodayDebut' => array('cmdUserView' => 1, 'Libelle' => 'Dernier cours du jour Début', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'FirstCourTodayFin' => array('cmdUserView' => 1, 'Libelle' => 'Premier cours du jour Fin', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'LastCourTodayFin' => array('cmdUserView' => 1, 'Libelle' => 'Dernier cours du jour Fin', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'FirstCourDemain' => array('cmdUserView' => 1, 'Libelle' => 'Premier cours demain', 'Type' => 'info', 'SubType' => 'string', 'visible' => 1, 'Template' => ''), 'LastCourDemain' => array('cmdUserView' => 1, 'Libelle' => 'Dernier cours demain', 'Type' => 'info', 'SubType' => 'string', 'visible' => 1, 'Template' => ''), 'FirstCourDemainDebut' => array('cmdUserView' => 1, 'Libelle' => 'Premier cours demain Début', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'LastCourDemainDebut' => array('cmdUserView' => 1, 'Libelle' => 'Dernier cours demain Début', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'FirstCourDemainFin' => array('cmdUserView' => 1, 'Libelle' => 'Premier cours demain Fin', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'LastCourDemainFin' => array('cmdUserView' => 1, 'Libelle' => 'Dernier cours demain Fin', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'todayinhome' => array('cmdUserView' => 1, 'Libelle' => 'Journée à la maison', 'Type' => 'info', 'SubType' => 'binary', 'visible' => 0, 'Template' => ''), 'tomorrowinhome' => array('cmdUserView' => 1, 'Libelle' => 'Demain à la maison', 'Type' => 'info', 'SubType' => 'binary', 'visible' => 0, 'Template' => ''), 'HomeworksMSG' => array('cmdUserView' => 1, 'Libelle' => 'Prochains devoirs', 'Type' => 'info', 'SubType' => 'string', 'visible' => 1, 'Template' => ''), 'LastToken' => array('cmdUserView' => 0, 'Libelle' => 'Last Token', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonEDT' => array('cmdUserView' => 0, 'Libelle' => 'Json Emplois du Temps', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonNotes' => array('cmdUserView' => 0, 'Libelle' => 'Json Notes', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonComp' => array('cmdUserView' => 0, 'Libelle' => 'Json Competence', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonDevoirs' => array('cmdUserView' => 0, 'Libelle' => 'Json Devoirs', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonInformation' => array('cmdUserView' => 0, 'Libelle' => 'Json Information', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonAbsences' => array('cmdUserView' => 0, 'Libelle' => 'Json Absences', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonMenuCantine' => array('cmdUserView' => 0, 'Libelle' => 'Json Menu Cantine', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonNoteByDate' => array('cmdUserView' => 0, 'Libelle' => 'Json Note par date', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonPeriods' => array('cmdUserView' => 0, 'Libelle' => 'Json Periods', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonEventNotes' => array('cmdUserView' => 0, 'Libelle' => 'Json Events Notes', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonEventCompetences' => array('cmdUserView' => 0, 'Libelle' => 'Json Events Competences', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => ''), 'JsonHomeworksMSG' => array('cmdUserView' => 0, 'Libelle' => 'Json Devoirs Message', 'Type' => 'info', 'SubType' => 'string', 'visible' => 0, 'Template' => '') ); //Chaque commande $Order = 0; foreach ($TabCmd as $CmdKey => $Cmd) { $PronotlinkCmd = $eqLogic->getCmd(null, $CmdKey); if (!is_object($PronotlinkCmd)) { $PronotlinkCmd = new pronotlinkCmd(); } $PronotlinkCmd->setName($Cmd['Libelle']); $PronotlinkCmd->setEqLogic_id($eqLogic->getId()); $PronotlinkCmd->setType($Cmd['Type']); $PronotlinkCmd->setSubType($Cmd['SubType']); $PronotlinkCmd->setLogicalId($CmdKey); $PronotlinkCmd->setIsVisible($Cmd['visible']); $PronotlinkCmd->setDisplay('generic_type', 'GENERIC_INFO'); $PronotlinkCmd->setConfiguration('cmdUserView',$Cmd['cmdUserView']); if (!empty($Cmd['Template'])) { $PronotlinkCmd->setTemplate("dashboard", $Cmd['Template']); $PronotlinkCmd->setTemplate("mobile", $Cmd['Template']); } $PronotlinkCmd->setOrder($Order); $PronotlinkCmd->save(); $Order++; } } } public static function deleteoldcmd($eqLogic) { $TabCmd = array('LastSucceError', 'LastSucceUpdate', 'LastErrorUpdate', 'LastNote'); foreach ($TabCmd as $CmdKey) { $PronotlinkCmd = $eqLogic->getCmd(null, $CmdKey); if (is_object($PronotlinkCmd)) { $PronotlinkCmd->remove(); } } } public function preSave() { self::CreateRefreshCmd(); self::setData($this); if ($this->getConfiguration("noRefresh", false)) { $this->setConfiguration("noRefresh", false); } else { $this->setConfiguration("forceRefresh", true); } $id = $this->getid(); $this->setConfiguration("id", $id); } public function setData($eqLogic) { log::add('pronotlink', 'info', 'Set des data du compte : ' . $eqLogic->getConfiguration('username')); $username = $eqLogic->getConfiguration('username'); $password = $eqLogic->getConfiguration('password'); $urlpronote = $eqLogic->getConfiguration('url'); $cas = $eqLogic->getConfiguration('cas'); $typecon = $eqLogic->getConfiguration('typecon'); if ($eqLogic->getIsEnable() != 1) return; if (!isset($username) && !isset($password) && !isset($urlpronote) && !isset($cas)) return; $Data = prolink_data::getData($eqLogic); $data = $Data['data']['params']; $periods = self::setPeridodSyntaxe($eqLogic); if ($Data != false) { $eqLogic->setConfiguration("firstDay", $data['firstDay']); $eqLogic->setConfiguration("lastDay", $data['lastDay']); } if ($periods != false) { $Periodscmd = $eqLogic->getCmd('info', 'JsonPeriods'); $Periodscmd->event(json_encode($periods, true)); $Periodscmd->save(); } if ($typecon != "student") { switch ($typecon) { case "parent": $children = prolink_data::getchildren($eqLogic); if ($children != false) { $childrens = array(); foreach ($children['data']['user']['students'] as $child) { array_push($childrens, array("id" => $child['name'], "name" => $child['name'] . " (" . $child['studentClass']['name'] . ")")); } $eqLogic->setConfiguration("getConfiguration", json_encode($childrens)); } break; } } log::add('pronotlink', 'info', 'Set des data du compte : ' . $eqLogic->getConfiguration('username') . ' Finish'); } public function setPeridodSyntaxe($eqLogic) { $Periods = prolink_data::getPeriods($eqLogic); if ($Periods == false) return false; $periods = $Periods['data']['params']['periods']; $result = array(); $id = 0; foreach ($periods as $period) { array_push($result, array("id" => $id, "name" => $period['name'], "from" => $period['from'], "to" => $period['to'])); $id++; } return $result; } public function postSave() { pronotlink::CreateRefreshCmd(); } 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') { } */ /* * 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_() { } */ /* * **********************Getteur Setteur*************************** */ } class pronotlinkCmd 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 = null) { if ($this->getLogicalId() == 'refresh') { $this->getEqLogic()->refresh(); return; } pronotlink::refreshinfo($this->getEqLogic()); } /* * **********************Getteur Setteur*************************** */ } ?>