. * https://coolkit-technologies.github.io/apiDocs/#/en/PlatformOverview */ /* * ***************************Includes********************************* */ require_once __DIR__ . '/../../../../core/php/core.inc.php'; if (!class_exists('mDNS')) { require_once dirname(__FILE__) . '/../../data/lan/lib/mdns.php'; } class ewejee extends eqLogic { // $path = realpath(dirname(__FILE__) . '/../../'); == var/www/html/plugins/ewejee /* * *************************Attributs****************************** */ public $nb_array; public $name; public $device_id; public $switch_action; public $switch_info; public $pulse_width; public $pulse; public $startup; public $humidity; public $temperature; public $pulse_array; public $pulse_width_array; public $name_channel; public $switches_array; public $configure_array; public $rftrig_array; public $rflist_array; public $rfname_tags; public $current; public $voltage; public $power; public $bright; public $brightness; public $model; public $brand; public $apikey; public $colorR; public $colorG; public $colorB; public retry = 0; public function store($json) { $ewejee = new Ewejee; // Attirbue les valeurs à l'objet suivant l'analyse json $ewejee->name = $json['name']; $ewejee->device_id = $json['deviceid']; $ewejee->switch_action = $json['params']['switch']; // 1 $ewejee->switch_info = $json['params']['switch']; // 100 $ewejee->pulse_width = $json['params']['pulseWidth']; // 101 $ewejee->pulse = $json['params']['pulse']; // 102 $ewejee->startup = $json['params']['startup']; // 103 $ewejee->humidity = $json['params']['currentHumidity']; // 104 $ewejee->temperature = $json['params']['currentTemperature']; // 105 $ewejee->pulse_array = $json['params']['pulses']; // 110 array $ewejee->pulse_width_array = $json['params']['pulses']; $ewejee->name_channel = $json['tags']; $ewejee->switches_array = $json['params']['switches']; // 120-21-22-23 etc... $ewejee->configure_array = $json['params']['configure']; $ewejee->rftrig_array = $json['params']; // 141-42-43... Début a 141 !! $ewejee->current = $json['params']['current']; //106 $ewejee->voltage = $json['params']['voltage']; //107 $ewejee->power = $json['params']['power']; //108 $ewejee->bright = $json['params']['bright']; //bright $ewejee->brightness = $json['params']['brightness']; //bright $ewejee->model = $json['productModel']; $ewejee->brand = $json['brandName']; $ewejee->motorTurn = $json['params']['motorTurn']; $ewejee->currLocation = $json['params']['currLocation']; $ewejee->location = $json['params']['location']; $ewejee->colorR = $json['params']['colorR']; $ewejee->colorG = $json['params']['colorG']; $ewejee->colorB = $json['params']['colorB']; return $ewejee; } public function test_mode() // Passer à 0 pour sortir du mode { // a finir $test_mode['ignore_cloud_update'] = '0'; // Pour ne prendre en compte que les retour mdns // a faire pas en place... $test_mode['sync'] = '0'; // Pour utiliser le json ci dessous $test_mode['sync_data'] = ''; return $test_mode; } public function send_debug($msg) { $urlmsg = urlencode($msg); file_get_contents('http://192.168.1.100/core/api/jeeApi.php?apikey=&type=cmd&id=2935&title=montitre&message=' . $urlmsg); } /** eWeJee V2 */ public function add_basicr3($data, $eqLogic, $workMode, $info) // V3 Info inutile { /** workMode 1 = switch function with power stat */ if ($workMode == "1" OR $workMode == "3") { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('current_00'); $cmd->setLogicalId('current_00'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('voltage_00'); $cmd->setLogicalId('voltage_00'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('V'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('actPow_00'); $cmd->setLogicalId('ctPow_00'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('reactPow_00'); // A modifier suivant si pourcentage ouverture ou fermeture $cmd->setLogicalId('reactPow_00'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); $locationid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('apparentPow_00'); // state = on $cmd->setLogicalId('apparentPow_00'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setValue($locationid); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('current_01'); $cmd->setLogicalId('current_01'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('voltage_01'); $cmd->setLogicalId('voltage_01'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('V'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('actPow_01'); $cmd->setLogicalId('ctPow_01'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('reactPow_01'); // A modifier suivant si pourcentage ouverture ou fermeture $cmd->setLogicalId('reactPow_01'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); $locationid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('apparentPow_01'); // state = on $cmd->setLogicalId('apparentPow_01'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('W'); $cmd->save(); } /** workMode 2 = motor function */ if ($workMode == "2") { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Ouverture'); $cmd->setLogicalId('open_dualr3'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Fermeture'); $cmd->setLogicalId('close_dualr3'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Stop'); $cmd->setLogicalId('stop_dualr3'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Pourcentage de fermeture'); // A modifier suivant si pourcentage ouverture ou fermeture $cmd->setLogicalId('location_dualr3'); $cmd->setUnite('%'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); $locationid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Fermeture slider'); // state = on $cmd->setLogicalId('setLocation_dualr3_slider'); $cmd->setType('action'); $cmd->setSubType('slider'); $cmd->setValue($locationid); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Etat moteur'); // A modifier suivant si pourcentage ouverture ou fermeture $cmd->setLogicalId('motorTurn_dualr3'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); } } public function setLocation($device_id, $apikey, $location) { $go = file_get_contents('http://localhost:6005/R3/setclose/' . $device_id . '/' . $apikey . '/' . $location); } public function motorTurn($device_id, $apikey, $way) { $go = file_get_contents('http://localhost:6005/R3/motorTurn/' . $device_id . '/' . $apikey . '/' . $way); } public function ptz_cam($eqLogic, $ch) { $ip = $eqLogic->getConfiguration('ip'); $dir = str_replace('cam_', '', $ch); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'http://'.$ip.':8080/cgi-bin/ptz.sh?dir='.$dir, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => array( 'Authorization: Basic ' . base64_encode("foulek:123456") ), )); $response = curl_exec($curl); curl_close($curl); } /** For the pow * @param String $msg = strpos 'dropped' from cloud */ public function hundredDaysKwhData($data, $eqLogic) { $path = realpath(dirname(__FILE__) . '/../../'); $json = json_decode(file_get_contents($path . '/desktop/js/ewejee_pow_data_' . $eqLogic->getLogicalId() . '.json'), true); $array = str_split($data, 6); $i = 0; foreach ($array as $conso) { $Wh = $conso[0] . $conso[1] . $conso[3] . $conso[5]; $kWh = $Wh / 100; $json[date('y', strtotime('- ' . $i . ' days'))][date('m', strtotime('- ' . $i . ' days'))][date('d', strtotime('- ' . $i . ' day'))] = $kWh; //[2020][01][22] $i++; } $jsonencode = json_encode($json); $save = file_put_contents($path . '/desktop/js/ewejee_pow_data_' . $eqLogic->getLogicalId() . '.json', $jsonencode); log::add('ewejee', 'debug', 'Saved'); } /** To check if the cloud are open * @param String $msg = strpos 'dropped' from cloud */ public function check_cloud($msg) // V3 ?? { ewejee::deamon_cloud_stop(); ewejee::deamon_LAN_stop(); log::add('ewejee', 'error', 'Le cloud à été fermé par eWeLink, tentative de relance du démon.'); $time = time(); if (!config::byKey("last_check_cloud", "ewejee")) { log::add('ewejee', 'debug', 'No config last_check_cloud'); config::save("last_check_cloud", time(), "ewejee"); ewejee::deamon_start(); } $last = config::byKey("last_check_cloud", "ewejee"); if (!$last) { $last = time() - 120; } $next = $last + 20; log::add('ewejee', 'debug', '[check_cloud] last = ' . $last . ' and next = ' . $next . ' || ' . time() . ' | ' . $next); if (time() > $next) { config::save("last_check_cloud", time(), "ewejee"); log::add('ewejee', 'debug', 'Relance en cours ...'); ewejee::deamon_start(); ewejee::alert_cloud(); } } /** To send the warning msg to user */ public function alert_cloud() { $alert_cloud = config::byKey("alert_cloud", "ewejee"); $alert_cloud_cmd = str_replace('#', '', config::byKey("alert_cloud_cmd", "ewejee")); if ($alert_cloud == 'oui') { $cmd = cmd::byId($alert_cloud_cmd); if ($cmd) { $options = array('title' => 'Ewejee', 'message' => 'Le cloud eWeLink s\'est coupé, tentative de relance...'); $cmd->execCmd($options); } } } /** Event for a cmd * @param Object $eqLogic * @param Int $cmdId * @param Mixed $info = Bool, int or string, it depend of the data */ public function event($eqLogic, $cmdId, $info) { $cmd = $eqLogic->getCmd(null, $cmdId); if (is_object($cmd)) { $old_value = $cmd->getConfiguration('last_update'); if ($old_value != date("Y-m-d H:i:s")) { //log::add('ewejee', 'debug', '[event][old_value][' . $eqLogic->getLogicalId() . '] ' . $old_value); $cmd->event($info); $cmd->setConfiguration('last_update', date("Y-m-d H:i:s")); $cmd->save(); } } } /** Not user at the moment */ public function parse($data) { switch ($data['productModel']) { case 'RF_Bridge': $this->add_rfbridge($data); break; default: # code... break; } } /** To add a key to the plugin, will not add it to eWeLink app !!! * @param Int $id = ID of the eqLogic * @param String $result = ID of the channel */ public function rf_add_key($id, $result) { $eqLogic = eqLogic::byLogicalId($id, 'ewejee'); $device_id = $eqLogic->getLogicalId(); $apikey = $eqLogic->getConfiguration('apikey'); file_get_contents('http://localhost:6005/rfbridge/add/' . $device_id . '/' . $apikey . '/' . $result); } /** Add the RFBridge * @param Array $data = Json/Array of the data from eWeLink socket */ public function add_rfbridge($data) { // On crée l'équipement $eqLogic = new eqLogic(); $eqLogic->setEqType_name('ewejee'); $eqLogic->setIsEnable(1); $eqLogic->setIsVisible(1); $eqLogic->setName($data['name']); $eqLogic->setLogicalId($data['deviceid']); $eqLogic->setConfiguration("brand", $data['brandName']); $eqLogic->setConfiguration("model", $data['productModel']); $eqLogic->setConfiguration("apikey", $data['apikey']); $eqLogic->setConfiguration("devicekey", $data['devicekey']); $eqLogic->setConfiguration("mac", $data['extra']['extra']['mac']); $array_names = ewejee::rf_names($data); $eqLogic->setConfiguration("names", $array_names); $eqLogic->save(); log::add('ewejee', 'debug', '[add_rfbridge TEST][eqLogic] ' . $eqLogic->getId()); foreach ($data['params']['rfList'] as $rflist) { ewejee::add_rfbridge_cmd($rflist, $eqLogic, $array_names); } } /** Add a cmd action and info for a new channel * @param Array $rflists = list of all the channel * @param Object $eqLogic * @param Array $array_names = If name are know else = null */ public function add_rfbridge_cmd($rflist, $eqLogic, $array_names = null) { log::add('ewejee', 'debug', '[add_rfbridge_cmd][rflist] ' . $rflist['rfChl'] . ' [eqLogic] ' . $eqLogic->getId() . ' [array_names] ' . $array_names); // On récupère le nom de la commande if ($array_names != null) { $name = $array_names[$rflist['rfChl']]; } else { $name = $rflist['rfChl']; } if ($name == '') { $name = $rflist['rfChl']; } $cmd = $eqLogic->getCmd(null, 'RF_INFO_' . $rflist['rfChl']); if (!is_object($cmd)) { // On ajoute la commande info $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName($name . ' Info ch' . $rflist['rfChl']); $cmd->setLogicalId('RF_INFO_' . $rflist['rfChl']); // RF_INFO_0 $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); } $cmd = $eqLogic->getCmd(null, 'RF_ACTION_' . $rflist['rfChl']); if (!is_object($cmd)) { // On ajoute la commande action $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName($name . ' Action ch' . $rflist['rfChl']); $cmd->setLogicalId('RF_ACTION_' . $rflist['rfChl']); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); } } /** Update the info cmd of a device of the RFBridge * @param Array $update = Json/Array of the data from eWeLink socket * * TODO : Change value to timestamp or other format ?? */ public function update_rfbridge_cmd($update) { // On check si le commande existe $eqLogic = eqLogic::byLogicalId($update['deviceid'], 'ewejee'); if (array_key_last($update['params']) == 'rfList') { // rfList si c'est un ajout ! ewejee::add_rfbridge_cmd($update['params']['rfList'], $eqLogic); } $full_name = array_key_last($update['params']); // On récupère la dernière clé du array params $channel = preg_replace('/[^0-9]/', '', $full_name); // On supprime toutes les lettre et on extrait le et les chiffres $cmd = $eqLogic->getCmd(null, 'RF_INFO_' . $channel); $update['rfChl'] = $channel; //Si elle n'éxiste pas, on crée une nouvelle commande if (is_object($cmd)) { // Sinon si la commande éxiste, on met à jour la valeur ewejee::event($eqLogic, 'RF_INFO_' . $channel, $update['params'][$full_name]); } } /** Take the names of the device (RFBridge) and put it in an array * @param Array $data = Json/Array of the data from eWeLink socket */ public function rf_names($data) { log::add('ewejee', 'debug', '[rf_names][Début]'); $array_key = []; $array_value = []; log::add('ewejee', 'debug', '[tags][zyx_info]'); foreach ($data['tags']['zyx_info'] as $zyx) { foreach ($zyx['buttonName'] as $buttonName) { foreach ($buttonName as $key => $value) { array_push($array_key, $key); array_push($array_value, $value); } } } $array_names = array_combine($array_key, $array_value); return $array_names; } /** To add the Zigbee Switch * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * * TODO : Wait of the delivery to check bug reported on the community */ public function add_capteur_zig($data, $eqLogic) { // On ajoute la commande info $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('lock'); $cmd->setLogicalId('lock'); $cmd->setType('info'); $cmd->setSubType('binary'); $cmd->save(); } /** To add the Zigbee Temp Sensor (Temperature) * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * */ public function add_zig_temperature($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Température'); $cmd->setLogicalId('temperature'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('°C'); $cmd->save(); } /** To add the Zigbee Temp Sensor (Humidity) * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * */ public function add_zig_humidity($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Humiditée'); $cmd->setLogicalId('humidity'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('%'); $cmd->save(); } public function add_zig_smoke($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Smoke'); $cmd->setLogicalId('smoke'); $cmd->setType('info'); $cmd->setSubType('binary'); $cmd->save(); } /** To update the Zigbee Temp Sensor (Humidity) * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * */ public function update_zig_temperature($data, $eqLogic) { $temperature = $data['params']['temperature'] / 100; ewejee::event($eqLogic, 'temperature', $temperature); } /** To update the Zigbee Temp Sensor (Humidity) * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * */ public function update_zig_humidity($data, $eqLogic) { $humidity = $data['params']['humidity'] / 100; ewejee::event($eqLogic, 'humidity', $humidity); } /** To add the cmd battery of all the Zigbee Devices * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * */ public function add_battery($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('battery'); $cmd->setLogicalId('battery'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setUnite('%'); $cmd->save(); } /** To add the cmd of the timestamp * @param Array $data = Json/Array of the data from eWeLink socket * @param Object $eqLogic * */ public function add_trigTime($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('trigTime'); $cmd->setLogicalId('trigTime'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); } /** Same as previous */ public function add_key($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('key'); $cmd->setLogicalId('key'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); } /** Same as previous */ public function add_motion($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('motion'); $cmd->setLogicalId('motion'); $cmd->setType('info'); $cmd->setSubType('binary'); $cmd->save(); } /** Same as previous */ public function add_key_action($data, $eqLogic) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Simple'); $cmd->setLogicalId('simple_key'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Double'); $cmd->setLogicalId('double_key'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); } /** * Add a device manuallyif they are not detected on the sync * @param String $logicalId = DeviceID of the device * * TODO : Add the device to the LAN cache */ public function add_manually($logicalId) { $string_alone = file_get_contents('http://localhost:6005/add_manually/' . $logicalId); // Pour récupérer le json $string = '[' . $string_alone . ']'; $json = json_decode($string, true); // On décode le json ewejee::synchronisation($json); // On lance la synchro en donnant le Json récupéré avec la fonction sync() } /** eWeJee V1 */ /** Function of V1 */ /** To change ... */ public function attribute($json, $i = 0) { // Attirbue les valeurs à l'objet suivant l'analyse json $this->nb_array = count($json); $this->name = $json[$i]['name']; $this->device_id = $json[$i]['deviceid']; $this->switch_action = $json[$i]['params']['switch']; // 1 $this->switch_info = $json[$i]['params']['switch']; // 100 $this->pulse_width = $json[$i]['params']['pulseWidth']; // 101 $this->pulse = $json[$i]['params']['pulse']; // 102 $this->startup = $json[$i]['params']['startup']; // 103 $this->humidity = $json[$i]['params']['currentHumidity']; // 104 $this->temperature = $json[$i]['params']['currentTemperature']; // 105 $this->pulse_array = $json[$i]['params']['pulses']; // 110 array $this->pulse_width_array = $json[$i]['params']['pulses']; $this->name_channel = $json[$i]['tags']; $this->switches_array = $json[$i]['params']['switches']; // 120-21-22-23 etc... $this->configure_array = $json[$i]['params']['configure']; $this->rftrig_array = $json[$i]['params']; // 141-42-43... Début a 141 !! $this->rflist_array = $json[$i]['params']['rfList']; // 151-152... $this->rfname_tags = $json[$i]['tags']; // 151-152... $this->current = $json[$i]['params']['current']; //106 $this->voltage = $json[$i]['params']['voltage']; //107 $this->power = $json[$i]['params']['power']; //108 $this->bright = $json[$i]['params']['bright']; //bright $this->brightness = $json[$i]['params']['brightness']; //brightness (D1) $this->colorR = $json[$i]['params']['colorR']; $this->colorG = $json[$i]['params']['colorG']; $this->colorB = $json[$i]['params']['colorB']; if ($json[$i]['productModel'] == " ") { $this->model = $json[$i]['extra']['extra']['model']; } else { $this->model = $json[$i]['productModel']; } $this->brand = $json[$i]['brandName']; $this->apikey = $json[$i]['apikey']; $this->motorTurn = $json[$i]['params']['motorTurn']; $this->currLocation = $json[$i]['params']['currLocation']; $this->location = $json[$i]['params']['location']; //$this->tags = $json[$i]['tags']['zyx_info']; } public function attribute_update($json) { $ewejee = ewejee::store($json); $online = $json['online']; $eqLogic = eqLogic::byLogicalId($ewejee->device_id, 'ewejee'); log::add('ewejee', 'debug', '[attribute_update] Device ID = ' . $json['deviceid'] . ' Nom = ' . $eqLogic->getName()); if ($eqLogic->getConfiguration('model') === 'KING-Q4') { if ($json['params']['setclose'] !== NULL) { ewejee::event($eqLogic, 'Q4-setclose', $json['params']['setclose']); if ($json['params']['setclose'] !== "0" and $json['params']['setclose'] !== "100") { ewejee::event($eqLogic, '100', 'Pause'); } elseif ($json['params']['setclose'] === "0") { ewejee::event($eqLogic, '100', 'Ouvert'); } elseif ($json['params']['setclose'] === "100") { ewejee::event($eqLogic, '100', 'Fermé'); } } if ($json['params']['switch'] !== NULL) { // Ne fonctionne pas car pas seul le switch est donné pas de setclose donc if précédente ne va pas... if ($json['params']['switch'] === 'on') { $json['params']['switch'] = 'Ouvert'; } elseif ($json['params']['switch'] === 'off') { $json['params']['switch'] = 'Fermé'; } elseif ($json['params']['switch'] === 'pause') { $json['params']['switch'] = 'Pause'; } ewejee::event($eqLogic, '100', $json['params']['switch']); } } else { if ($ewejee->switch_info !== NULL) { ewejee::event($eqLogic, '100', $ewejee->switch_info); log::add('ewejee', 'debug', 'INFO : '.$ewejee->switch_info); } if ($ewejee->pulse_width !== NULL) { ewejee::event($eqLogic, '101', $ewejee->pulse_width); } if ($ewejee->pulse !== NULL) { ewejee::event($eqLogic, '102', $ewejee->pulse); } if ($ewejee->startup !== NULL) { ewejee::event($eqLogic, '103', $ewejee->startup); } if ($ewejee->humidity !== NULL) { ewejee::event($eqLogic, '104', $ewejee->humidity); } if ($ewejee->temperature !== NULL) { ewejee::event($eqLogic, '105', $ewejee->temperature); } if ($ewejee->current !== NULL) { ewejee::event($eqLogic, '106', $ewejee->current); } if ($ewejee->voltage !== NULL) { ewejee::event($eqLogic, '107', $ewejee->voltage); } if ($ewejee->power !== NULL) { ewejee::event($eqLogic, '108', $ewejee->power); } if ($online !== NULL) { $ewejee->setConfiguration("online", $online); } // array if ($ewejee->switches_array !== NULL) { $switches = $json['params']['switches']; if ($switches[0]['outlet'] !== NULL) { $eqLogic->setConfiguration('channel', 'outlet'); } if ($switches[0]['outconst'] !== NULL) { $eqLogic->setConfiguration('channel', 'outlet'); } $eqLogic->save(); $outlets = $json['params']['switches'][0][$eqLogic->getConfiguration('channel')]; $outlet = $outlets + 1; if (count($switches) == 1) { $a = $outlet; foreach ($switches as $switch) { ewejee::event($eqLogic, '11' . $a, $switch['switch']); $a = $a + 1; } } elseif (count($switches) == 3) { $a = $outlet; foreach ($switches as $switch) { ewejee::event($eqLogic, '11' . $a, $switch['switch']); $a = $a + 1; } } else { $a = 1; foreach ($switches as $switch) { ewejee::event($eqLogic, '11' . $a, $switch['switch']); $a = $a + 1; } } } if ($ewejee->configure_array != NULL) { $configure = $ewejee->configure_array; $a = 1; foreach ($configure as $startup) { ewejee::event($eqLogic, '12' . $a, $startup['startup']); $a = $a + 1; } } if ($ewejee->pulse_array != NULL) { $pulses = $ewejee->pulse_array; $a = 1; foreach ($pulses as $pulse) { ewejee::event($eqLogic, '13' . $a, $pulse['pulse']); $a = $a + 1; } } if (is_object($eqLogic)) { if ($eqLogic->getConfiguration('model') == 'RFBridge' or $eqLogic->getConfiguration('model') == 'RF_Bridge') { // V2 ewejee::update_rfbridge_cmd($json); // V2 END } } if ($json['params']['lock']) { ewejee::event($eqLogic, 'lock', $json['params']['lock']); } if ($json['water'] !== NULL) { ewejee::event($eqLogic, '310', $json['water']); } if ($json['switch'] !== NULL) { ewejee::event($eqLogic, '312', $json['switch']); } if ($json['state'] !== NULL) { ewejee::event($eqLogic, '311', $json['state']); } if ($json['params']['bright'] !== NULL) { ewejee::event($eqLogic, 'bright', $json['params']['bright']); } if ($json['params']['colorR'] !== NULL) { $r = $json['params']['colorR']; $g = $json['params']['colorG']; $b = $json['params']['colorB']; $hex = ewejee::rgb_to_hex($r, $g, $b); ewejee::event($eqLogic, 'color', $hex); } if ($json['params']['colorG'] !== NULL) { ewejee::event($eqLogic, 'colorG', $json['params']['colorG']); } if ($json['params']['colorB'] !== NULL) { ewejee::event($eqLogic, 'colorB', $json['params']['colorB']); } if ($json['params']['brightness'] !== NULL) { ewejee::event($eqLogic, 'brightness', $json['params']['brightness']); } if ($json['params']['brightness'] !== NULL) { ewejee::event($eqLogic, 'dbrightness', $json['params']['brightness']); } if ($json['params']['lock'] !== NULL) { ewejee::event($eqLogic, 'lock', $json['params']['lock']); } if ($json['params']['battery'] !== NULL) { ewejee::event($eqLogic, 'battery', $json['params']['battery']); $battery = $json['params']['battery']; /** If < 4 it is volts ! So need to convert */ if ($battery <= 4) { $battery = $battery/0.03; } /** Maybe, if the voltage are 3.1V, the % are heiger than 100%, so need to correct it ... */ if ($battery >= 100) { $battery = 100; } /** Will add to the "Equipement" page of Jeedom */ $eqLogic->batteryStatus($battery); $eqLogic->save(); } if ($json['params']['trigTime'] !== NULL && $json['params']['key'] !== NULL) { $ts = substr($json['params']['trigTime'], 0, 10); $date = date('Y-m-d H:i:s', $ts); ewejee::event($eqLogic, 'trigTime', $date); } if ($json['params']['temperature'] !== NULL) { self::update_zig_temperature($json, $eqLogic); } if ($json['params']['humidity'] !== NULL) { self::update_zig_humidity($json, $eqLogic); } if ($json['params']['smoke'] !== NULL) { ewejee::event($eqLogic, 'smoke', $json['params']['smoke']); } if ($json['params']['key'] !== NULL) { ewejee::event($eqLogic, 'key', $json['params']['key']); sleep(1); ewejee::event($eqLogic, 'key', '-1'); } if ($json['params']['motion'] !== NULL) { ewejee::event($eqLogic, 'motion', $json['params']['motion']); } if ($json['config']['hundredDaysKwhData'] !== NULL) { ewejee::hundredDaysKwhData($json['config']['hundredDaysKwhData'], $eqLogic); } if ($json['params']['motorTurn'] !== NULL) { ewejee::event($eqLogic, 'motorTurn', $json['params']['motorTurn']); ewejee::event($eqLogic, 'motorTurn', $json['params']['motorTurn']); } if ($json['params']['currLocation'] !== NULL) { ewejee::event($eqLogic, 'currLocation', $json['params']['currLocation']); ewejee::event($eqLogic, 'location_dualr3', $json['params']['currLocation']); } if ($json['params']['location'] !== NULL) { ewejee::event($eqLogic, 'location', $json['params']['location']); ewejee::event($eqLogic, 'location', $json['params']['location']); } if ($json['params']['current_00'] !== NULL) { ewejee::event($eqLogic, 'current_00', $json['params']['current_00']); } if ($json['params']['voltage_00'] !== NULL) { ewejee::event($eqLogic, 'voltage_00', $json['params']['voltage_00']); } if ($json['params']['ctPow_00'] !== NULL) { ewejee::event($eqLogic, 'ctPow_00', $json['params']['ctPow_00']); } if ($json['params']['reactPow_00'] !== NULL) { ewejee::event($eqLogic, 'reactPow_00', $json['params']['reactPow_00']); } if ($json['params']['apparentPow_00'] !== NULL) { ewejee::event($eqLogic, 'apparentPow_00', $json['params']['apparentPow_00']); } if ($json['params']['current_01'] !== NULL) { ewejee::event($eqLogic, 'current_01', $json['params']['current_01']); } if ($json['params']['voltage_01'] !== NULL) { ewejee::event($eqLogic, 'voltage_01', $json['params']['voltage_01']); } if ($json['params']['actPow_01'] !== NULL) { ewejee::event($eqLogic, 'ctPow_01', $json['params']['actPow_01']); } if ($json['params']['reactPow_01'] !== NULL) { ewejee::event($eqLogic, 'reactPow_01', $json['params']['reactPow_01']); } if ($json['params']['apparentPow_01'] !== NULL) { ewejee::event($eqLogic, 'apparentPow_01', $json['params']['apparentPow_01']); } if ($json['params']['mode'] !== NULL) { // A TEST 18/02/23 $json_names = ewejee::get_json_names(); $info = $json_names[$json['params']['mode']]; ewejee::event($eqLogic, 'mode_info', $info); } // On récupère le modèle $model = $eqLogic->getConfiguration('model'); // On voit si c'est une ifan if ($model == "iFan02" or $model == "iFan") { // on récupère la commande du channel correspondant à la lumière $cmd_light = $eqLogic->getCmd(null, '111'); $info_light = $cmd_light->execCmd(); ewejee::event($eqLogic, 'ifanlight', $info_light); // Au transmet a l'info lumière // On récupère les info des channels correspondant au ventilo $cmd1 = $eqLogic->getCmd(null, '112'); $info1 = $cmd1->execCmd(); $cmd2 = $eqLogic->getCmd(null, '113'); $info2 = $cmd2->execCmd(); $cmd3 = $eqLogic->getCmd(null, '114'); $info3 = $cmd3->execCmd(); // On les met à jour if ($info1 == 'off') { ewejee::event($eqLogic, 'ifanspeed', 'off'); } elseif ($info1 == 'on' and $info2 == 'off' and $info3 == 'off') { ewejee::event($eqLogic, 'ifanspeed', '1'); } elseif ($info1 == 'on' and $info2 == 'on' and $info3 == 'off') { ewejee::event($eqLogic, 'ifanspeed', '2'); } elseif ($info1 == 'on' and $info2 == 'off' and $info3 == 'on') { ewejee::event($eqLogic, 'ifanspeed', '3'); } } $test = $eqLogic->getConfiguration('model'); } } /** * Called by the button from configuration pannel of eWeJee to sync */ public function auto_sync() { $test_mode = ewejee::test_mode(); if ($test_mode['sync'] != '1') { //ewejee::deamon_cloud_start(); $i = 0; while ($i < 30) { $deamon_info = self::deamon_cloud_info(); if (trim($deamon_info['state']) == 'ok') { break; } log::add('ewejee', 'debug', '[Auto Sync] Attente du démon Cloud ...'); sleep(3); $i++; } log::add('ewejee', 'debug', '[Auto Sync] Attente 15 secondes ...'); sleep(60); log::add('ewejee', 'debug', '[Auto Sync] Fin ...'); } $json = ewejee::sync(); // On lance la fonction sync() et on récupère le json ewejee::synchronisation($json); // On lance la synchro en donnant le Json récupéré avec la fonction sync() } /** Called by auto_sync() * Decode the Json from eWelink Cloud server * and retrun it as Array * Handle if an error are in the Json as false authentification, no device in eWeLink... */ public function sync() { $test_mode = ewejee::test_mode(); if ($test_mode['sync'] != '1') { log::add('ewejee', 'debug', '[Sync] Récupération des apareils ...'); $string = file_get_contents('http://localhost:6005/sync'); // Pour récupéré le json // On sauvgarde les données au cas ou $file = 'sync.txt'; file_put_contents($file, $string); } else { $string = $test_mode['sync_data']; log::add('ewejee', 'debug', '[TEST MODE][sync]' . $string); } $json = json_decode($string, true); // On décode le json if ($json == '') { $deamon = self::deamon_info(); if ($deamon['state'] == 'nok') { log::add('ewejee', 'error', 'Le démon n\'est pas lancé ! Veuillez lancer le démon dans la configuration du plugin'); } else { log::add('ewejee', 'error', 'Aucune données transmises, consultez le log ewejee_node et la documentation !'); } } config::save("powNr", 0, "ewejee"); log::add('ewejee', 'debug', 'PowNr set à 0'); return $json; } /** Handle all the device by sync * Create the equipement and cmd for each type of devices * * TODO : Group all in one function and clear the unless code... */ public function synchronisation($json) { if ($json["error"] != "") { log::add('ewejee', 'error', 'Non connecté !, synchronisation $json[error] != "", Erreur = ' . $json['error']); } $i = 0; log::add('ewejee', 'debug', '[Synchronisation] Equipements récupéré : ' . count($json)); foreach ($json as $data) { log::add('ewejee', 'debug', '[synchronisation]Lancement de la fonction Synchronisation(' . $i . ')'); $ewejee = new Ewejee; $ewejee->attribute($json, $i); $already = eqLogic::byLogicalId($ewejee->device_id, 'ewejee'); $binaire = config::byKey("binaire", "ewejee"); if ($binaire == 'oui') { $info = 'binary'; } else { $info = 'string'; } if ($already) { if ($ewejee->switches_array != NULL) { $switches = $data['params']['switches']; if ($switches[0]['outlet'] != NULL) { $already->setConfiguration('channel', 'outlet'); } if ($switches[0]['outconst'] != NULL) { $already->setConfiguration('channel', 'outlet'); } } log::add('ewejee', 'debug', '[synchronisation][already] channel or outconst ? | ' .$switches[0]['outlet'] ); $already->setConfiguration("devicekey", $data['devicekey']); $already->save(); log::add('ewejee', 'debug', '[synchronisation][already] Equipement ' . $ewejee->name . ' déjà connu, ajout ignoré, si vous voulez synchroniser cet equippement, veuillez le supprimer et relancer une synchronisation.'); } else { if ($ewejee->model == 'RFBridge' or $ewejee->model == 'RF_Bridge') { ewejee::add_rfbridge($data); } else { $eqLogic = new eqLogic(); $eqLogic->setEqType_name('ewejee'); $eqLogic->setIsEnable(1); $eqLogic->setIsVisible(0); $eqLogic->setName($ewejee->name); $eqLogic->setLogicalId($ewejee->device_id); $eqLogic->setConfiguration("brand", $ewejee->brand); $eqLogic->setConfiguration("model", $ewejee->model); $eqLogic->setConfiguration("apikey", $ewejee->apikey); $eqLogic->setConfiguration("devicekey", $data['devicekey']); if ($data['extra']['extra']['mac'] !== NULL) { $eqLogic->setConfiguration("mac", $data['extra']['extra']['mac']); } else { $eqLogic->setConfiguration("mac", $data['extra']['extra']['staMac']); } $eqLogic->save(); } if ($ewejee->model == "iFan02" or $ewejee->model == "iFan") { if ($ewejee->switches_array != NULL) { $switches = $ewejee->switches_array; $a = 0; foreach ($switches as $switch) { $name = 'Ne pas suppr'; $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName($name . ' ' . $a); $a = $a + 1; $cmd->setLogicalId('11' . $a); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); } } $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('ON Lumière'); $cmd->setLogicalId('101'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('OFF Lumière'); $cmd->setLogicalId('501'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('ON Ventilateur'); $cmd->setLogicalId('ifanon'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('OFF Ventilateur'); $cmd->setLogicalId('ifanoff'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Vitesse 1'); $cmd->setLogicalId('ifanvit1'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Vitesse 2'); $cmd->setLogicalId('ifanvit2'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Vitesse 3'); $cmd->setLogicalId('ifanvit3'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Lumière'); $cmd->setLogicalId('ifanlight'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Vitesse'); $cmd->setLogicalId('ifanspeed'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); } elseif ($ewejee->model == "AGW-WI16") { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Allumer'); $cmd->setLogicalId('1'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Éteindre'); $cmd->setLogicalId('01'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('low (Non fonctionnel)'); $cmd->setLogicalId('302'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('high (Non fonctionnel)'); $cmd->setLogicalId('303'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Niveau eau'); $cmd->setLogicalId('310'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Niveau de puissance'); $cmd->setLogicalId('311'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Etat'); $cmd->setLogicalId('312'); $cmd->setType('info'); $cmd->setSubType($info); $cmd->save(); } elseif ($ewejee->model == "B1") // B1_R2 { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Allumer'); // state = on $cmd->setLogicalId('B11'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Éteindre'); // state = off $cmd->setLogicalId('B101'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Blanc Froid'); $cmd->setLogicalId('B11cold'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Blanc Chaud'); $cmd->setLogicalId('B11warm'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Statut'); // state $cmd->setLogicalId('B1100'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Rouge'); $cmd->setLogicalId('B1r'); // 0-255 Channel 2 $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Vert'); $cmd->setLogicalId('B1g'); // 0-255 Channel 3 $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Bleu'); $cmd->setLogicalId('B1b'); // 0-255 Channel 4 $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Type'); // warm - cold - middle = Couleur ou warm + cold $cmd->setLogicalId('B1type'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Type'); // 25-255 = 100% Luminositée $cmd->setLogicalId('B1cold'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Type'); // 25-255 = 100% Luminositée $cmd->setLogicalId('B1warm'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); } elseif ($ewejee->model == "KING-Q4") { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Ouverture'); // "switch": "on", $cmd->setLogicalId('Q4-open'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Fermeture'); // "switch": "off", $cmd->setLogicalId('Q4-close'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Pause'); // "switch": "off", $cmd->setLogicalId('Q4-pause'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Statut'); // warm - cold - middle = Couleur ou warm + cold $cmd->setLogicalId('100'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Pourcentage de fermeture'); // A modifier suivant si pourcentage ouverture ou fermeture $cmd->setLogicalId('Q4-setclose'); $cmd->setUnite('%'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); $setcloseid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Fermeture slider'); // state = on $cmd->setLogicalId('Q4-setClose-slider'); $cmd->setType('action'); $cmd->setSubType('slider'); $cmd->setValue($setcloseid); $cmd->save(); } else { if ($ewejee->model == "SN-OPL1200-L3RGBIC") { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Mode_Info'); $cmd->setLogicalId('mode_info'); $cmd->setType('info'); $cmd->setSubType("string"); $cmd->save(); $setlistid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Mode'); $cmd->setLogicalId('mode'); $cmd->setType('action'); $cmd->setSubType('select'); $cmd->setValue($setlistid); $list = ewejee::get_json_names(); $listValue = ""; foreach ($list as $key => $value) { $listValue .= $key . "|" . $value . ";"; } $cmd->setConfiguration('listValue', $listValue); $cmd->save(); } if ($ewejee->switch_action != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Allumer'); $cmd->setLogicalId('1'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Éteindre'); $cmd->setLogicalId('01'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); } if ($ewejee->switch_info != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Statut'); $cmd->setLogicalId('100'); $cmd->setType('info'); $cmd->setSubType($info); $cmd->save(); } if ($ewejee->pulse_width != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Pulse Width'); $cmd->setLogicalId('101'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); } if ($ewejee->pulse != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Pulse'); $cmd->setLogicalId('102'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); } if ($ewejee->startup != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Startup'); $cmd->setLogicalId('103'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); } if ($ewejee->humidity != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Humiditée'); $cmd->setLogicalId('104'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); } if ($ewejee->temperature != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Température'); $cmd->setLogicalId('105'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); } if ($ewejee->bright != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Luminosité'); $cmd->setLogicalId('bright'); $cmd->setUnite('%'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); $setbrightid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Luminosité slider'); // state = on $cmd->setLogicalId('bright-slider'); $cmd->setType('action'); $cmd->setSubType('slider'); $cmd->setValue($setbrightid); $cmd->save(); } if ($ewejee->colorR != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Couleur'); $cmd->setLogicalId('color'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); $setcolorid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Choix Couleur'); $cmd->setLogicalId('colorChoise'); $cmd->setType('action'); $cmd->setSubType('color'); $cmd->setValue($setcolorid); $cmd->save(); } if ($ewejee->brightness != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Luminosité'); $cmd->setLogicalId('dbrightness'); $cmd->setUnite('%'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->save(); $setbrightnessid = $cmd->getId(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Luminosité slider'); // state = on $cmd->setLogicalId('dbrightness-slider'); $cmd->setType('action'); $cmd->setSubType('slider'); $cmd->setValue($setbrightnessid); $cmd->save(); } if ($ewejee->switches_array != NULL) { $switches = $ewejee->switches_array; $a = 1; foreach ($switches as $switch) { $name = $json[$i]['params']['tags']['ck_channel_name'][$a - 1]; if ($switch['outlet'] != NULL) { $ewejee->setConfiguration('channel', 'outlet'); } if ($switch['outconst'] != NULL) { $ewejee->setConfiguration('channel', 'outlet'); } $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('ON ' . $a); $cmd->setLogicalId('10' . $a); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('OFF ' . $a); $cmd->setLogicalId('50' . $a); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Statut ' . $a); $cmd->setLogicalId('11' . $a); $cmd->setType('info'); $cmd->setSubType($info); $cmd->save(); $a = $a + 1; } } if ($ewejee->configure_array != NULL) { $configure = $ewejee->configure_array; $a = 1; foreach ($configure as $startup) { // Changer nom + logical id, suivant $a $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Startup Ch ' . $a); $cmd->setLogicalId('12' . $a); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); $a = $a + 1; } } if ($ewejee->pulse_array != NULL) { $pulses = $ewejee->pulse_array; $a = 1; foreach ($pulses as $pulse) { // Changer nom + logical id, suivant $a $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Pulse Ch ' . $a); $cmd->setLogicalId('13' . $a); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); $a = $a + 1; } } if ($ewejee->current != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Intensité'); $cmd->setLogicalId('106'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); } if ($ewejee->voltage != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Voltage'); $cmd->setLogicalId('107'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->save(); } if ($ewejee->power != NULL) { $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('power'); $cmd->setLogicalId('108'); $cmd->setType('info'); $cmd->setSubType('numeric'); $cmd->setConfiguration('isHistorized', '0'); $cmd->setUnite('W'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Actualisation'); $cmd->setLogicalId('get'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Mensuel'); $cmd->setLogicalId('pow_mensuel'); $cmd->setType('info'); $cmd->setSubType('string'); $cmd->setIsVisible(0); $cmd->save(); // A suppr après test $cmd = new EwejeeCmd; $cmd->setEqLogic_id($eqLogic->getId()); $cmd->setName('Rafraichir Mensuel (test)'); $cmd->setLogicalId('get_pow_mensuel'); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); // Pour éviter la déco à la requette d'actualisation auto (Ajout délai dans cron) $powNr = config::byKey("powNr", "ewejee"); $powNr10 = $powNr + 10; config::save('powNr', $powNr10, "ewejee"); $eqLogic->setConfiguration('powNr', $powNr10); $eqLogic->save(); } if ($data['params']['lock'] !== NULL) { ewejee::add_capteur_zig($data, $eqLogic); } if ($data['params']['battery'] !== NULL) { ewejee::add_battery($data, $eqLogic); } if ($data['params']['trigTime'] !== NULL) { ewejee::add_trigTime($data, $eqLogic); } if ($data['params']['key'] !== NULL) { ewejee::add_key($data, $eqLogic); self::add_key_action($data, $eqLogic); } if ($data['params']['motion'] !== NULL) { ewejee::add_motion($data, $eqLogic); } if ($data['params']['temperature'] !== NULL) { ewejee::add_zig_temperature($data, $eqLogic); } if ($data['params']['humidity'] !== NULL) { ewejee::add_zig_humidity($data, $eqLogic); } if ($data['params']['workMode'] !== NULL) { ewejee::add_basicr3($data, $eqLogic, $data['params']['workMode'], $info); } } } $i = $i + 1; log::add('ewejee', 'debug', '--------------------------------------------------------------------------------------------------'); log::add('ewejee', 'debug', 'Fin de la fonction Synchronisation, passage à Synchronisation (' . $i . ')'); log::add('ewejee', 'debug', '--------------------------------------------------------------------------------------------------'); $ewejee->attribute_update($data); if (config::byKey('deamon_type', 'ewejee') == 'Lan') { ewejee::deamon_cloud_stop(); } } } /** Swich device state (ON and OFF) * WARNING - To support the OLD devices added before 1.1.4, we need to convert the command for the new system * * */ public function switch_device($device_id, $apikey, $ch, $action = 'on') { log::add('ewejee', 'debug', 'Démarrage switch ...'); // Si il n'y a pas plusieurs channels if ($ch == '1' or $ch === '01') { $ch = 'single'; // On modifie le channel par single pour le détecter sur le serveur log::add('ewejee', 'debug', '-- Un seul channel.'); } // On détècte si le channel à 3 chiffres, et on modifie $ch par le dernier chiffre if (strlen($ch) == 3) { log::add('ewejee', 'debug', '-- Channel ' . $ch . ' Modifié par : ' . $ch[2]); $ch = $ch[2]; } // On laisse le fonctionnement d'avant la V1.1.4 if ($action == 'old') { $url = 'http://localhost:6005/switch/' . $device_id . '/' . $ch; log::add('ewejee', 'debug', '-- OLD'); } else { $url = 'http://localhost:6005/switch/' . $action . '/' . $device_id . '/' . $apikey . '/' . $ch; } log::add('ewejee', 'debug', '** Envoi de la commande au serveur : Device : ' . $device_id . ' Apikey : ' . $apikey . ' Channel : ' . $ch . ' Action : ' . $action); // On envoi la commande au serveur $data = file_get_contents($url); log::add('ewejee', 'debug', 'Commande pour ' . $device_id . ' correctement Envoyé'); $a = 0; // ??? } /** Same but for LAN... * */ public function switch_device_LAN($device_id, $ch, $action = 'on') // Passer a OFF après tests { log::add('ewejee', 'debug', 'Démarrage switch ...'); // Si il n'y a pas plusieurs channels if ($ch == '1' or $ch === '01') { $ch = 'single'; // On modifie le channel par single pour le détecter sur le serveur log::add('ewejee', 'debug', '-- Un seul channel.'); } // On détècte si le channel à 3 chiffres, et on modifie $ch par le dernier chiffre if (strlen($ch) == 3) { log::add('ewejee', 'debug', '-- Channel ' . $ch . ' Modifié par : ' . $ch[2]); $ch = $ch[2]; } // On laisse le fonctionnement d'avant la V1.1.4 if ($action == 'old') { $url = 'http://localhost:6006/switch/' . $device_id . '/' . $ch; log::add('ewejee', 'debug', '-- OLD'); } else { $url = 'http://localhost:6006/switch/' . $action . '/' . $device_id . '/' . $ch; } log::add('ewejee', 'debug', '** Envoi de la commande au serveur : Device : ' . $device_id . ' Channel : ' . $ch . ' Action : ' . $action); // On envoi la commande au serveur $data = file_get_contents($url); log::add('ewejee', 'debug', 'Commande pour ' . $device_id . ' correctement Envoyé'); $a = 0; // ??? } /** Following is desactivate because are not good ... * * TODO : Add the possibility to add a command manally... */ /* public function duplicate_cmd($value) { // En cours de développement log::add('ewejee', 'debug','Value = ' . $value); $explode = explode(',', $value); $eqlogic_id = $explode[1]; $a = $explode[0]; log::add('ewejee', 'debug','EQ = ' . $eqlogic_id); $eqLogic = eqLogic::byId($eqlogic_id, 'ewejee'); log::add('ewejee', 'debug','$a = ' . $a); $cmd = $eqLogic->getCmd(null, '1'); $cmd2 = new EwejeeCmd; $cmd2->setEqLogic_id($eqLogic->getId()); $cmd2->setName('Double ON OFF ' . $a); $cmd2->setLogicalId('50' . $a); $cmd2->setType('action'); $cmd2->setSubType('other'); $cmd2->save(); log::add('ewejee', 'debug','EQ get id = ' . $test); } */ /** To handle the IFAN * Very hard device, need to change multiple state at the same time * See the newserver.js file for more details ... */ public function ifan($device_id, $apikey, $ch) // For ifan02 and fan03 { switch ($ch) { case 'ifanon': $test = file_get_contents('http://localhost:6005/ifan/on/' . $device_id . '/' . $apikey); break; case 'ifanoff': $test = file_get_contents('http://localhost:6005/ifan/off/' . $device_id . '/' . $apikey); break; case 'ifanvit1': $test = file_get_contents('http://localhost:6005/ifan/vit1/' . $device_id . '/' . $apikey); break; case 'ifanvit2': $test = file_get_contents('http://localhost:6005/ifan/vit2/' . $device_id . '/' . $apikey); break; case 'ifanvit3': $test = file_get_contents('http://localhost:6005/ifan/vit3/' . $device_id . '/' . $apikey); break; default: log::add('ewejee', 'debug', 'Aucune commande correspondante pour ' . $device_id . ' ch = ' . $ch); } } /** Really ? No comment ... * * TODO : Translate the value to a date format * or timestamp ?? hum... */ public function date_et_heure($value) // Pour le RF Bridge { //$value = "2019-10-06T08:44:05.000Z"; // $d_h = $value[8] . $value[9] . $value[7] . $value[5] . $value[6] . $value[4] . $value[0] . $value[1] . $value[2] . $value[3] . ' H ' . $value[11] . $value[12] . $value[13] . $value[14] . $value[15] . $value[16] . $value[17] . $value[18]; return $value; } /** * Change the state of the rfbridge */ public function rfbridge($device_id, $apikey, $channel) { $ch = intval($channel); log::add('ewejee', 'debug', '-- channel numérique = ' . $ch); $string = file_get_contents('http://localhost:6005/rfbridge/' . $device_id . '/' . $apikey . '/' . $ch); } /* **************************************************** 4CH Pro ****************************************** */ /** * No comment... */ public function on_4ch($device_id, $ch, $apikey) { $test = file_get_contents('http://localhost:6005/4ch/on/' . $device_id . '/' . $apikey . '/' . $ch); log::add('ewejee', 'debug', '-- on_4ch - Device id = ' . $device_id . ' Apikey = ' . $apikey . ' Channel = ' . $ch); } /** * No comment... */ public function off_4ch($device_id, $ch, $apikey) { $test = file_get_contents('http://localhost:6005/4ch/off/' . $device_id . '/' . $apikey . '/' . $ch); log::add('ewejee', 'debug', '-- off_4ch - Device id = ' . $device_id . ' Apikey = ' . $apikey . ' Channel = ' . $ch); } /** For W16 devices (Humidifactor) */ public function change_color($device_id, $apikey, $hex_color) { $rgb_color = ewejee::hex_to_rgb($hex_color); $r = $rgb_color[0]; $g = $rgb_color[1]; $b = $rgb_color[2]; $change = file_get_contents('http://localhost:6005/wi16/' . $device_id . '/' . $apikey . '/' . $r . '/' . $g . '/' . $b); } public function hex_to_rgb($hex_color) // Retourne un array !! { $hexr = $hex_color[1] . '' . $hex_color[2]; $hexg = $hex_color[3] . '' . $hex_color[4]; $hexb = $hex_color[5] . '' . $hex_color[6]; log::add('ewejee', 'debug', '-- Color Hex R' . $hexr . 'G' . $hexg . 'B' . $hexb); $r = hexdec($hexr); $g = hexdec($hexg); $b = hexdec($hexb); log::add('ewejee', 'debug', '-- Color RGB R' . $r . 'G' . $g . 'B' . $b); $rgb = [$r, $g, $b]; return $rgb; } public function rgb_to_hex($R, $G, $B) { $R = dechex($R); if (strlen($R)<2) $R = '0'.$R; $G = dechex($G); if (strlen($G)<2) $G = '0'.$G; $B = dechex($B); if (strlen($B)<2) $B = '0'.$B; return '#' . $R . $G . $B; } public function wi16_create_cmd($color, $logicalId, $id) // color doit commencer aveec un hastag pour la detection { $cmds = cmd::byEqLogicId($id); foreach ($cmds as $commande) { $identifiant = $commande->getlogicalId(); if ($identifiant == $color) { echo "ATTENTION ! Impossible de crée la couleur " . $color . " car elle est déjà crée ! Voici l'identifiant = " . $commande->getId(); exit(); } log::add('ewejee', 'debug', '-- color logical id = ' . $identifiant); } $eqLogic = eqLogic::byLogicalId($logicalId, 'ewejee'); $cmd = new EwejeeCmd; $cmd->setEqLogic_id($id); $cmd->setName($color); $cmd->setLogicalId($color); $cmd->setType('action'); $cmd->setSubType('other'); $cmd->save(); //log::add('ewejee', 'debug', '-- color logical id = ' . var_dump($cmds_array)); //log::add('ewejee', 'debug', '-- color logical id = ' . $cmds[0]); } /** * B1 handle color etc... * * TODO : Why RGB are commented ? Bug ? Please contact me if you have a B1 Bulbe */ public function b1($ch, $device_id, $apikey) { log::add('ewejee', 'debug', '* Fonction B1 : id ' . $ch . ', device id ' . $device_id . ', apikey ' . $apikey); if ($ch == 'B11') { // on $test = file_get_contents('http://localhost:6005/b1/on/' . $device_id . '/' . $apikey); } elseif ($ch == 'B101') { // off $test = file_get_contents('http://localhost:6005/b1/off/' . $device_id . '/' . $apikey); } elseif ($ch == 'B11cold') { // cold 100% $test = file_get_contents('http://localhost:6005/b1/cold/' . $device_id . '/' . $apikey); } elseif ($ch == 'B11warm') { // warm 100% $test = file_get_contents('http://localhost:6005/b1/warm/' . $device_id . '/' . $apikey); } elseif (strpos($ch, "#") !== FALSE) { /*$rgb_color = ewejee::hex_to_rgb($hex_color); $r = $rgb_color[0]; $g = $rgb_color[1]; $b = $rgb_color[2]; $test = file_get_contents('http://localhost:6005/b1/rgb/' . $device_id . '/' . $apikey . '/' . $r . '/' . $g . '/' . $b);*/ } else { log::add('ewejee', 'debug', '/!\/!\/!\/!\/!\/!\ - Commande inconnu : ' . $ch); } } /** * * Sonoff POW * */ /** No more used, new function in V2 hundredDaysKwhData() * * TODO : To clean... * */ public function getpowui($device_id, $apikey) { $string = file_get_contents('http://localhost:6005/get/pow/' . $device_id . '/' . $apikey); } /** To get the data of the POW * * $action store no more used, need to clear this function */ public function pow_stat($device_id = 'nada', $action = 'nada', $json = 'nada') { $path = realpath(dirname(__FILE__) . '/../../'); if ($action == 'get') { $eqLogic = eqLogic::byLogicalId($device_id, 'ewejee'); $url = 'http://localhost:6005/hundredDaysKwh/' . $device_id . '/' . $eqLogic->getConfiguration('apikey'); $data = file_get_contents($url); } elseif ($action == 'store') { log::add('ewejee', 'debug', '[pow_stat] Device = ' . $device_id); log::add('ewejee', 'debug', '[pow_stat] Action = store'); log::add('ewejee', 'debug', '[pow_stat] Json = ' . $json); $annee = exec('date +%Y'); $mois = exec('date +%B'); if ($mois == 'January') { $mois = 'Janvier'; } if ($mois == 'February') { $mois = 'Fevrier'; } if ($mois == 'March') { $mois = 'Mars'; } if ($mois == 'April') { $mois = 'Avril'; } if ($mois == 'May') { $mois = 'Mai'; } if ($mois == 'June') { $mois = 'Juin'; } if ($mois == 'July') { $mois = 'Juillet'; } if ($mois == 'August') { $mois = 'Aout'; } if ($mois == 'September') { $mois = 'Septembre'; } if ($mois == 'October') { $mois = 'Octobre'; } if ($mois == 'November') { $mois = 'Novembre'; } if ($mois == 'December') { $mois = 'Decembre'; } $load = $json; $ar = json_decode($load, true); $load_old = file_get_contents($path . '/../../data/ewejee_pow_data_' . $device_id . '.json'); $array = json_decode($load_old, true); foreach ($ar['daily'] as $day) { $array[$annee][$mois][$day['day']] = $day['usage']; log::add('ewejee', 'debug', '[pow_stat] Jour = ' . $day['day'] . ' et donnée ' . $day['usage']); } $data = json_encode($array); $save = file_put_contents($path . '/../../data/ewejee_pow_data_' . $device_id . '.json', $data); $powstat = $data; $eqLogic = eqLogic::byLogicalId($device_id, 'ewejee'); ewejee::event($eqLogic, 'pow_mensuel', $data); $eqLogic->setConfiguration("powstat", $powstat); $eqLogic->save(); /* Créer la commande au sync Créer la detection de la commande (en bas) Faire un update html Plus tard Ajouter un onglet html pour les POW */ /* log::add('ewejee', 'error',$json['device_id'] . ' Status'); log::add('ewejee', 'error',$json['monthly'] . ' Mois'); log::add('ewejee', 'error',$json['daily'] . ' Jours array ?'); foreach ($json['daily'] as $day) { log::add('ewejee', 'error',$day['day'] . ' Jour'); log::add('ewejee', 'error',$day['usage'] . ' Conso'); } */ } } /** * Switch to open or close store etc... * @param Int $device_id * @param String $apikey * @param Int $close = 0-100 % */ public function setClose($device_id, $apikey, $close) { $go = file_get_contents('http://localhost:6005/shutter/setclose/' . $device_id . '/' . $apikey . '/' . $close); } public function pause($device_id, $apikey) { $go = file_get_contents('http://localhost:6005/shutter/pause/' . $device_id . '/' . $apikey); } /** * Switch or bulb with brightness * @param Int $device_id * @param String $apikey * @param Int $bright (or $brightness) = 0-100 % */ public function bright($device_id, $apikey, $bright, $eqLogic) { log::add('ewejee', 'debug', 'Device id ' . $device_id); if ($eqLogic->getCmd(null, 'color')) { $cmdInfo = $eqLogic->getCmd(null, 'color'); $rgb = ewejee::hex_to_rgb($cmdInfo->execCmd()); $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2]; $go = file_get_contents('http://localhost:6005/brightAndColor/' . $device_id . '/' . $apikey . '/' . $bright . '/' . $r . '/' . $g . '/' . $b); } else { $go = file_get_contents('http://localhost:6005/bright/' . $device_id . '/' . $apikey . '/' . $bright); } } public function brightAndColor($device_id, $apikey, $color, $eqLogic) { log::add('ewejee', 'debug', 'Device id ' . $device_id); $rgb = ewejee::hex_to_rgb($color); $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2]; $cmdInfo = $eqLogic->getCmd(null, 'bright'); $bright = $cmdInfo->execCmd(); $go = file_get_contents('http://localhost:6005/brightAndColor/' . $device_id . '/' . $apikey . '/' . $bright . '/' . $r . '/' . $g . '/' . $b); } public function brightness($device_id, $apikey, $brightness) { log::add('ewejee', 'debug', 'Device id ' . $device_id); $go = file_get_contents('http://localhost:6005/brightness/' . $device_id . '/' . $apikey . '/' . $brightness); } /** If a device are detected as Offline, change the state on the dashboard * @param Int $device_id */ public function offline($device_id) { if ($this->retry = 0) { $this->retry = 1; } else { $eqLogic = eqLogic::byLogicalId($device_id, 'ewejee'); $eqLogic->setConfiguration("online", "offline"); $eqLogic->save(); sleep(1); $json['deviceid'] = $device_id; $json['params']['switch'] = "offline"; $json['params']['switches'][0]['outlet'] = 0; $json['params']['switches'][1]['outlet'] = 1; $json['params']['switches'][2]['outlet'] = 2; $json['params']['switches'][3]['outlet'] = 3; $json['params']['switches'][0]['switch'] = "offline"; $json['params']['switches'][1]['switch'] = "offline"; $json['params']['switches'][2]['switch'] = "offline"; $json['params']['switches'][3]['switch'] = "offline"; $json['params']['power'] = "0"; $json['params']['voltage'] = "0"; $json['params']['current'] = "0"; $json['switch'] = "offline"; $json['state'] = "offline"; ewejee::attribute_update($json); } } /** If a device was detected as Offline and are now Online * @param Int $device_id */ public function online($device_id) { $this->retry = 0; $eqLogic = eqLogic::byLogicalId($device_id, 'ewejee'); $eqLogic->setConfiguration("online", "online"); $eqLogic->save(); log::add('ewejee', 'debug', 'Device id ' . $device_id); } /** * Deamon (Based on a cron of Jeedom) * To force the device to send they data over LAN * To by pass the delay of the device ... */ public function deamon_pow() { $sleep_check = []; $off_check = []; while (true) { foreach (eqLogic::byType('ewejee') as $eqLogic) { if ($off_check[$eqLogic->getLogicalId()] == '') { $off_check[$eqLogic->getLogicalId()] = 1; } if ($eqLogic->getConfiguration('model') == 'POW' OR $eqLogic->getConfiguration('model') == 'PSF-X67') { $sleep = $eqLogic->getConfiguration('cronMode'); if ($sleep != '' and is_numeric($sleep)) { if ($sleep_check[$eqLogic->getLogicalId()] == '') { $sleep_check[$eqLogic->getLogicalId()] = 0; } else if ($sleep_check[$eqLogic->getLogicalId()] >= $sleep) { $cmd1 = $eqLogic->getCmd(null, '100'); $info1 = $cmd1->execCmd(); if ($info1 === 1 or $info1 === 'on') { $info = $eqLogic->send_lan($eqLogic, '/zeroconf/switch', '{"switch":"on"}'); log::add('ewejee', 'debug', '[deamon_pow][' . $eqLogic->getLogicalId() . '][on] Commande envoyé pour ' . $eqLogic->getLogicalId()); $off_check[$eqLogic->getLogicalId()] = 1; } else if ($info1 === 0 or $info1 === 'off') { if ($off_check[$eqLogic->getLogicalId()] <= 5) { $info = $eqLogic->send_lan($eqLogic, '/zeroconf/switch', '{"switch":"of"}'); log::add('ewejee', 'debug', '[deamon_pow][' . $eqLogic->getLogicalId() . '][off] Commande envoyé pour ' . $eqLogic->getLogicalId()); } else { log::add('ewejee', 'debug', '[deamon_pow][' . $eqLogic->getLogicalId() . '][off] Aucune commande envoyé pour ' . $eqLogic->getLogicalId() . ' car off_check > 5 : ' . $off_check[$eqLogic->getLogicalId()]); } $off_check[$eqLogic->getLogicalId()]++; } else { log::add('ewejee', 'debug', '[deamon_pow][' . $eqLogic->getLogicalId() . '][info] Ni on Ni off, bug info = ' . $info1); } $sleep_check[$eqLogic->getLogicalId()] = 0; } $sleep_check[$eqLogic->getLogicalId()]++; log::add('ewejee', 'debug', '[deamon_pow][' . $eqLogic->getLogicalId() . '][while][sleep_check] ' . $sleep_check[$eqLogic->getLogicalId()]); } } } sleep(1); } } public static function cron() { // Check the LAN deamon $deamon_LAN_info = ewejee::deamon_LAN_info(); $deamon_MDNS_info = ewejee::deamon_MDNS_info(); $deamon_POW_info = ewejee::deamon_POW_info(); if ($deamon_LAN_info['state'] == 'ok') { if ($deamon_MDNS_info['state'] != 'ok') { ewejee::deamon_MDNS_start(); log::add('ewejee', 'info', '[cron()][deamon_MDNS_start] Arret détecté, relance'); } if ($deamon_POW_info['state'] != 'ok') { ewejee::deamon_POW_start(); log::add('ewejee', 'debug', '[cron()][deamon_POW_start] Arret détecté, relance'); } } // Check the websocket, if detect that the websocket are closed, then we restart the deamon, see bash script $ip = config::byKey("internalAddr", "ewejee"); $port = config::byKey("internalPort", "ewejee"); //$exec = exec("/bin/bash html/plugins/ewejee/resources/check.sh " . $ip . " " . $port); //log::add('ewejee', 'debug', '[cron()][check cloud sh] ' . $exec); // Actu pow foreach (self::byType('ewejee') as $ewejee) { $model = $ewejee->getConfiguration('model'); if ($model == "POW" OR $model == "PSF-X67" OR $model == "SN-ESP32D0-THR3-01") { $powcron = $ewejee->getConfiguration('powcron'); $powNr = $ewejee->getConfiguration('powNr'); ewejee::send_lan($ewejee, '/zeroconf/info'); if ($powcron == "1") { if ($ewejee->getConfiguration('lan') != 'oui') { sleep($powNr); $device_id = $ewejee->getLogicalId(); $apikey = $ewejee->getConfiguration('apikey'); $ewejee->getpowui($device_id, $apikey); } } } } } public static function cron5() { foreach (self::byType('ewejee') as $ewejee) { $model = $ewejee->getConfiguration('model'); if ($model == "POW" OR $model == "PSF-X67" OR $model == "SN-ESP32D0-THR3-01") { $powcron = $ewejee->getConfiguration('powcron'); $powNr = $ewejee->getConfiguration('powNr'); if ($powcron == "5") { if ($ewejee->getConfiguration('lan') != 'oui') { sleep($powNr); $device_id = $ewejee->getLogicalId(); $apikey = $ewejee->getConfiguration('apikey'); $ewejee->getpowui($device_id, $apikey); } } } } } public static function cron10() { foreach (self::byType('ewejee') as $ewejee) { $model = $ewejee->getConfiguration('model'); if ($model == "POW" OR $model == "PSF-X67" OR $model == "SN-ESP32D0-THR3-01") { $powcron = $ewejee->getConfiguration('powcron'); $powNr = $ewejee->getConfiguration('powNr'); if ($powcron == "10") { if ($ewejee->getConfiguration('lan') != 'oui') { sleep($powNr); $device_id = $ewejee->getLogicalId(); $apikey = $ewejee->getConfiguration('apikey'); $ewejee->getpowui($device_id, $apikey); } } } } } public static function cron15() { foreach (self::byType('ewejee') as $ewejee) { $model = $ewejee->getConfiguration('model'); if ($model == "POW" OR $model == "PSF-X67" OR $model == "SN-ESP32D0-THR3-01") { $powcron = $ewejee->getConfiguration('powcron'); $powNr = $ewejee->getConfiguration('powNr'); if ($powcron == "15") { if ($ewejee->getConfiguration('lan') != 'oui') { sleep($powNr); $device_id = $ewejee->getLogicalId(); $apikey = $ewejee->getConfiguration('apikey'); $ewejee->getpowui($device_id, $apikey); } } } } } // Fonction exécutée automatiquement toutes les 15 par Jeedom public static function cron30() { foreach (self::byType('ewejee') as $ewejee) { $model = $ewejee->getConfiguration('model'); if ($model == "POW" OR $model == "PSF-X67" OR $model == "SN-ESP32D0-THR3-01") { $powcron = $ewejee->getConfiguration('powcron'); $powNr = $ewejee->getConfiguration('powNr'); if ($powcron == "30") { if ($ewejee->getConfiguration('lan') != 'oui') { sleep($powNr); $device_id = $ewejee->getLogicalId(); $apikey = $ewejee->getConfiguration('apikey'); $ewejee->getpowui($device_id, $apikey); } } } } } public static function cronHourly() { foreach (self::byType('ewejee') as $ewejee) { $model = $ewejee->getConfiguration('model'); if ($model == "POW" OR $model == "PSF-X67" OR $model == "SN-ESP32D0-THR3-01") { $powcron = $ewejee->getConfiguration('powcron'); $powNr = $ewejee->getConfiguration('powNr'); if ($powcron == "H") { if ($ewejee->getConfiguration('lan') != 'oui') { sleep($powNr); $device_id = $ewejee->getLogicalId(); $apikey = $ewejee->getConfiguration('apikey'); $ewejee->getpowui($device_id, $apikey); } } } } } /* public static function cronDaily() { } */ /* * *********************Méthodes d'instance************************* */ // config::addKey("80", "port", "ewejee"); public function preInsert() { } public function postInsert() { } public function preSave() { } public function postSave() { } public function preUpdate() { } public function postUpdate() { } public function preRemove() { } public function postRemove() { } /* ******************************************************************************************************************************************* */ /* *************************** EWEJEE V2 Configuration LAN *********************************************************************************** */ /* ******************************************************************************************************************************************* */ /** * To add the data to the file used by for the LAN cloud * Need to add to 3 files * - arp-table.json * - deviceip-table.json * - devices-cache.json * * TODO * Passer la config LAN a oui * Ajouter un bouton pour désactiver LAN * */ public function sync_LAN($device_id) { $path = realpath(dirname(__FILE__) . '/../../'); $eqLogic = eqLogic::byLogicalId($device_id, 'ewejee'); $eqLogic->setConfiguration('lan', 'oui'); // On Active le mode LAN $eqLogic->save(); //var_dump(stripslashes($encode)); ewejee::deamon_LAN_start(); } public function unSync_LAN($logicalId) { $eqLogic = eqLogic::byLogicalId($logicalId, 'ewejee'); $eqLogic->setConfiguration('lan', ''); // On Désactive le mode LAN $eqLogic->save(); } public function get_ip_arp($mac) { $ip = exec("sudo arp-scan --localnet | grep " . $mac . " | awk '{print $1}'"); return $ip; } /** * Init LAN * */ public function lan_setup() { $path = realpath(dirname(__FILE__) . '/../../'); /* $ip = file_get_contents($path . '/data/lan/deviceip-table.json'); $ipjson = json_decode($ip, true); $arp = array(); foreach ($ipjson as $device_id => $ip) // Pour chaques lignes où on a pu récupérer l'IP { if ($eqLogic = eqLogic::byLogicalId($device_id, 'ewejee')) // Si le deviceid est connu dans Jeedom { if (!$eqLogic->getConfiguration('mac')) // Car on rajoute le MAC à la configuration de l'équipement V2 { log::add('ewejee', 'error', '--- LAN MODE --- Veuillez faire une synchronisation avant d\'activer le mode LAN !!'); } //$eqLogic->setConfiguration('ip', $ip); // On ajoute l'IP $eqLogic->setConfiguration('lan', 'oui'); // On Active le mode LAN if ($eqLogic->getConfiguration('cloud_forced') === 'oui') { //$eqLogic->setConfiguration('lan', 'non'); // On désactive le mode LAN log::add('ewejee', 'debug', '[lan_setup][cloud_forced] ' . $eqLogic->getConfiguration('cloud_forced')); } log::add('ewejee', 'debug', '--- LAN MODE --- IP ' . $ip . ' Ajouté à ' . $device_id . ' avec l\'adresse MAC ' . $eqLogic->getConfiguration('mac')); $eqLogic->save(); // On préparre une array avec IP vs MAC $key["ip"] = $eqLogic->getConfiguration('ip'); $key["mac"] = strtolower($eqLogic->getConfiguration('mac')); if ($key["mac"] == '') { log::add('ewejee', 'error', '--- LAN MODE --- L\'adresse MAC de l\'équipment avec l\IP ' . $key['ip'] . ' est vide, cet équipement ne peut etre utilise en LAN.'); } else { array_push($arp, $key); } } } // On l'enregistre sous format JSON $arp_table = json_encode($arp); //$exec = file_put_contents($path . '/data/lan/arp-table.json', $arp_table); */ $config = config::save('lan', '1', 'ewejee'); self::deamon_LAN_start(); self::deamon_POW_start(); self::deamon_start(); } public function lan_stop() { $path = realpath(dirname(__FILE__) . '/../../'); /*$cmd = 'sudo /bin/bash ' . $path . '/data/lan/getIP.sh'; // Récupère IP et Deviceid $result = exec($cmd . ' >> ' . log::getPathToLog('ewejee_lan') . ' 2>&1 &'); if (strpos(strtolower($result), 'error') !== false || strpos(strtolower($result), 'traceback') !== false) { log::add('ewejee', 'error', $result); return false; }*/ $plugin = plugin::byId('ewejee'); $eqLogics = eqLogic::byType($plugin->getId()); /*foreach ($eqLogics as $eqLogic) // Pour chaques lignes où on a pu récupérer l'IP { $eqLogic->setConfiguration('lan', 'non'); // On Active le mode LAN $eqLogic->save(); }*/ $config = config::save('lan', '0', 'ewejee'); self::deamon_LAN_stop(); self::deamon_POW_stop(); self::deamon_start(); } /** * To send the data to the eWeLink devices over LAN * @param Object $eqLogic * @param String $path = '/zeroconf/switch' for example * @param String $data = Data to send example '{"switch":"off"}' * Data are the param to send to the devices. */ public function send_lan($eqLogic, $path, $data_to_send = '{}') { $curl = curl_init(); $data = $data_to_send; $sequence = time() . '123'; $device_id = $eqLogic->getLogicalId(); $devicekey = $eqLogic->getConfiguration('devicekey'); $ip = $eqLogic->getConfiguration('ip'); $iv_encoded = 'MTIzNDU2Nzg5MTIzNDU2Nw=='; $iv_decoded = base64_decode($iv_encoded); $md5 = md5($devicekey, true); $num = 16; $long = strlen($data); $modulus = $num - ($long % $num); $data .= str_repeat(chr($modulus), $modulus); $string = ''; for($i=0;$i 31 && ord($ascii) != 39 && ord($ascii) != 92 && ord($ascii) != 96 && ord($ascii) < 127) { $string .= $ascii; } } $data = openssl_encrypt($data, 'aes-128-cbc', $md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv_decoded); $data_encoded = base64_encode($data); curl_setopt_array($curl, array( CURLOPT_URL => "http://".$ip.":8081".$path, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS =>"{\r\n \"sequence\": \"" . $sequence . "\",\r\n \"deviceid\": \"" . $device_id . "\",\r\n \"selfApikey\": \"" . $devicekey . "\",\r\n \"iv\": \"" . $iv_encoded . "\",\r\n \"encrypt\": true,\r\n \"data\": \"" . $data_encoded . "\"\r\n}", CURLOPT_HTTPHEADER => array( "Content-Type: text/plain" ), )); log::add('ewejee', 'debug', '[send_lan]['.$device_id.'] Sending ... ' . $data_to_send ); $response = curl_exec($curl); log::add('ewejee', 'debug', "[send_lan]['.$device_id.'] {\r\n \"sequence\": \"" . $sequence . "\",\r\n \"deviceid\": \"" . $device_id . "\",\r\n \"selfApikey\": \"" . $devicekey . "\",\r\n \"iv\": \"" . $iv_encoded . "\",\r\n \"encrypt\": true,\r\n \"data\": \"" . $data_encoded . "\"\r\n}"); log::add('ewejee', 'debug', '[send_lan]['.$device_id.'] ' . $response); if ($response == '') { log::add('ewejee', 'debug', '[send_lan]['.$device_id.'] Seem that device are offline :( '); ewejee::offline($eqLogic->getLogicalId()); } curl_close($curl); } /** Sonoff L3 */ public function get_json($model) // SN-OPL1200-L3RGBIC { $data = file_get_contents(dirname(__FILE__) . "/../../data/specific/" . $model . ".json"); $json = json_decode($data, true); $options = $json[0]; return $options; } public function get_json_names($json = null) { if ($json === null) { $json = ewejee::get_json("L3"); } $names = array_keys($json); return $names; } /** * * EWEJEE V2 MDNS DEAMON * */ /** * Because of this function, I have no more hair ... * Look comment to understand */ public function deamon() { $mdns = new mDNS(); // Boucle infine... while (true) { $packet = $mdns->readIncoming(); // On lit if ($packet->packetheader != NULL) // On a un résultat { $reponses = $packet->packetheader->getAnswerRRs(); // Nombre de réponses if ($reponses > 0) { if ($packet->answerrrs[0]->name == '_ewelink._tcp.local') // On filtre ewelink { for ($a = 0; $a < sizeof($packet->answerrrs); $a++) { if ($packet->answerrrs[$a]->qtype == 12) // https://en.wikipedia.org/wiki/List_of_DNS_record_types { $string = ''; for ($b = 0; $b < sizeof($packet->answerrrs[$a]->data); $b++) { $ascii = $packet->answerrrs[$a]->data[$b]; if ($ascii > 31 && $ascii < 127 && $ascii != 34 && $ascii != 39 && $ascii != 92 && $ascii != 96) { $string .= chr($ascii); } if ($packet->answerrrs[$a]->name == '_ewelink._tcp.local') { $name = ''; for ($c = 0; $c < sizeof($packet->answerrrs[$a]->data); $c++) { $name .= chr($packet->answerrrs[$a]->data[$c]); } } } } if ($packet->answerrrs[$a]->qtype == 16) // https://en.wikipedia.org/wiki/List_of_DNS_record_types { $string = ''; for ($b = 0; $b < sizeof($packet->answerrrs[$a]->data); $b++) { $ascii = $packet->answerrrs[$a]->data[$b]; if ($ascii > 31 && $ascii < 127 && $ascii != 34 && $ascii != 39 && $ascii != 92 && $ascii != 96) { $string .= chr($ascii); } } $data = $ascii = $packet->answerrrs[$a]->data; $pos = 0; $buffer = '{'; $long = $data[$pos]; $string = ''; for ($d = 1; $d <= $long; $d++) { $ascii = $data[$long + $d]; if ($ascii > 31 && $ascii < 127 && $ascii != 34 && $ascii != 39 && $ascii != 92 && $ascii != 96) { $string .= chr($ascii); } } $separateur = strpos($string, '='); $key = substr($string, 0, $separateur); $value = substr($string, $separateur + 1); $buffer .= '"' . $key . '":"' . $value . '"'; $pos = $pos + $long + 1; while ($data[$pos] <> 0 && sizeof($data)) { $long = $data[$pos]; $string = ''; for ($e = 1; $e <= $long; $e++) { $ascii = $data[$pos + $e]; if ($ascii > 31 && $ascii < 127 && $ascii != 34 && $ascii != 39 && $ascii != 92 && $ascii != 96) { $string .= chr($ascii); } } $separateur = strpos($string, '='); $key = substr($string, 0, $separateur); $value = substr($string, $separateur + 1); $buffer .= ',"' . $key . '":"' . $value . '"'; $pos = $pos + $long + 1; } $buffer .= '}'; if ($packet->answerrrs[$a]->name == '_ewelink._tcp.local') { $name = ''; for ($f = 0; $f < sizeof($packet->answerrrs[$a]->data); $f++) { $name .= chr($packet->answerrrs[$a]->data[$f]); } } } if ($packet->answerrrs[$a]->qtype == 33) // https://en.wikipedia.org/wiki/List_of_DNS_record_types { $g = $packet->answerrrs[$a]->data; $pos = 6; $long = $g[$pos]; $pos++; $device = ''; for ($h = 0; $h < $long; $h++) { $device .= chr($g[$pos + $h]); } $device_id = str_replace('eWeLink_', '', $device); } if ($packet->answerrrs[$a]->qtype == 1) { $d = $packet->answerrrs[$a]->data; $ip = $g[0] . '.' . $g[1] . '.' . $g[2] . '.' . $g[3]; // Si déjà, on récupère l'IP, mais pour le moment, rien est fait avec } $buffer_array = json_decode($buffer, true); $data = ''; if (isset($buffer_array['data'])) { $data = $buffer_array['data']; } else if (isset($buffer_array['data1'])) { $data = $buffer_array['data1']; if (isset($buffer_array['data2'])) { //$data .= $buffer_array['data2']; $data = $data . $buffer_array['data2']; if (isset($buffer_array['data3'])) { $data = $data . $buffer_array['data3']; if (isset($buffer_array['data4'])) { $data = $data . $buffer_array['data4']; // Au cas ou... } } } } } // On récupère l'equipement pour récupèrer le localkey pour décrypter data foreach (eqLogic::byType('ewejee') as $eqLogic) { $eq_device_id = $eqLogic->getLogicalId(); if ($eq_device_id == $device_id) { $devicekey = $eqLogic->getConfiguration('devicekey'); $key = md5($devicekey, true); $iv = base64_decode($data['iv']); $cmd = base64_decode($data); $string = openssl_decrypt($cmd, 'aes-128-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv); if ($packet->answerrrs[$a]->name == "_ewelink._tcp.local") { // Ne se lance pas ??!! $name = ""; for ($b = 0; $b < sizeof($packet->answerrrs[$a]->data); $b++) { $name .= chr($packet->answerrrs[$a]->data[$b]); } } $begin = strpos($string, ':'); // Mauvais caractères au début... $delete = substr($string, 0, $begin); // On save les mauvais caractères à supprimer $space = str_replace($delete, '{"params":{"configure"', $string); // On reconstruit proprement $pos_end = strrpos($space, '}'); // On note la position de la dernière accolade $json = substr($space, 0, $pos_end + 1) . '}'; // On dégage le reste (espaces en caractères chelou... Surement chinois...) et on ajoute l'accolade de fin... $update = json_decode($json, true); // On transforme le json en array log::add('ewejee', 'debug', '[MDNS][Update] ******************** Données brutes ********************'); log::add('ewejee', 'debug', '[MDNS][Update] ' . $json); log::add('ewejee', 'debug', '[MDNS][Update] ******************** Données brutes ********************'); // ON ajoute les infos manquantes $update['deviceid'] = $device_id; $update['productModel'] = $eqLogic->getConfiguration('model'); $update['online'] = 'online'; ewejee::attribute_update($update); // Envoi pour mise a jour du status ;) } } } } } } } /** * Cron for MDNS checking to update the LAN status * */ /** */ public function deamon_MDNS_stop() { $cron = cron::byClassAndFunction('ewejee', 'deamon'); if (!is_object($cron)) { throw new Exception(__('Tache cron introuvable', __FILE__)); } $cron->halt(); } /** */ public function deamon_MDNS_start() { ewejee::deamon_MDNS_stop(); $deamon_info = ewejee::deamon_MDNS_info(); if ($deamon_info['launchable'] != 'ok') { throw new Exception(__('Veuillez vérifier la configuration', __FILE__)); } $cron = cron::byClassAndFunction('ewejee', 'deamon'); if (!is_object($cron)) { throw new Exception(__('Tache cron introuvable', __FILE__)); } $cron->run(); } /** */ public function deamon_MDNS_info() { $return = array(); $return['log'] = ''; $return['state'] = 'nok'; $cron = cron::byClassAndFunction('ewejee', 'deamon'); if (is_object($cron) && $cron->running()) { $return['state'] = 'ok'; } $return['launchable'] = 'ok'; return $return; } public function deamon_POW_start() { ewejee::deamon_POW_stop(); $deamon_info = ewejee::deamon_POW_info(); if ($deamon_info['launchable'] != 'ok') { throw new Exception(__('Veuillez vérifier la configuration', __FILE__)); } $cron = cron::byClassAndFunction('ewejee', 'deamon_pow'); if (!is_object($cron)) { throw new Exception(__('Tache cron introuvable', __FILE__)); } $cron->run(); } public function deamon_POW_stop() { $cron = cron::byClassAndFunction('ewejee', 'deamon_pow'); if (!is_object($cron)) { throw new Exception(__('Tache cron introuvable', __FILE__)); } $cron->halt(); } public function deamon_POW_info() { $return = array(); $return['log'] = ''; $return['state'] = 'nok'; $cron = cron::byClassAndFunction('ewejee', 'deamon_pow'); if (is_object($cron) && $cron->running()) { $return['state'] = 'ok'; } $return['launchable'] = 'ok'; return $return; } /** * * EWEJEE V2 MDNS DEAMON * */ /** * * EWEJEE V2 CLOUD DEAMON * */ // log du demon dans /var/www/html/core/ajax public static function deamon_info() { $return = array(); $return['log'] = 'ewejee_node'; $return['state'] = 'nok'; $cloud = ewejee::deamon_cloud_info(); $lan = ewejee::deamon_LAN_info(); $deamon_type = config::byKey('deamon_type', 'ewejee'); switch ($deamon_type) { case 'Cloud_and_lan': if ($cloud['state'] == 'ok' && $lan['state'] == 'ok') { $return['state'] = 'ok'; } break; case 'Cloud': if ($cloud['state'] == 'ok') { $return['state'] = 'ok'; ewejee::deamon_LAN_stop(); } break; case 'Lan': if ($lan['state'] == 'ok') { $return['state'] = 'ok'; ewejee::deamon_cloud_stop(); } break; default: // break; } $return['launchable'] = 'ok'; return $return; } public static function deamon_start() { if (!config::byKey('deamon_type', 'ewejee')) { config::save('deamon_type','cloud','ewejee'); } ewejee::deamon_cloud_stop(); ewejee::deamon_LAN_stop(); $cloud = ewejee::deamon_cloud_info(); $lan = ewejee::deamon_LAN_info(); $deamon_type = config::byKey('deamon_type', 'ewejee'); switch ($deamon_type) { case 'Cloud_and_lan': if ($cloud['state'] != 'ok') { ewejee::deamon_cloud_start(); } if ($lan['state'] != 'ok') { ewejee::deamon_LAN_start(); } break; case 'Cloud': if ($cloud['state'] != 'ok') { ewejee::deamon_cloud_start(); // If we switch to Cloud only, we will desactivate LAN for each deveices $eqLogics = eqLogic::byType('ewejee'); foreach($eqLogics as $eqLogic) { if ($eqLogic->getConfiguration('lan') == "oui") { ewejee::unSync_LAN($eqLogic->getLogicalId()); } } } break; case 'Lan': if ($lan['state'] != 'ok') { ewejee::deamon_LAN_start(); } break; default: // break; } } public static function deamon_stop() { ewejee::deamon_cloud_stop(); ewejee::deamon_LAN_stop(); /* $cloud = ewejee::deamon_cloud_info(); $lan = ewejee::deamon_LAN_info(); $deamon_type = config::byKey('deamon_type', 'ewejee'); switch ($deamon_type) { case 'Cloud_and_lan': if ($cloud['state'] != 'ok') { ewejee::deamon_cloud_stop(); } if ($lan['state'] != 'ok') { ewejee::deamon_LAN_stop(); } break; case 'Cloud': if ($cloud['state'] != 'ok') { ewejee::deamon_cloud_stop(); } break; case 'Lan': if ($lan['state'] != 'ok') { ewejee::deamon_LAN_stop(); } break; default: // break; }*/ } public static function deamon_cloud_start() { $path = realpath(dirname(__FILE__) . '/../../'); if (config::byKey("port", "ewejee") == "") { config::save("port", "80", "ewejee"); log::add('ewejee', 'debug', '[deamon_cloud_start()] Initialisation sur le port ' . config::byKey("port", "ewejee")); } else { log::add('ewejee', 'debug', '[deamon_cloud_start()] Port du localhost (Par défault = 80) = ' . config::byKey("port", "ewejee")); } self::deamon_cloud_stop(); $deamon_cloud_info = self::deamon_cloud_info(); if ($deamon_cloud_info['launchable'] != 'ok') { throw new Exception(__('Veuillez vérifier la configuration', __FILE__)); } log::add('ewejee', 'info', 'Lancement du démon.'); $login = config::byKey('login_ewe', 'ewejee'); $pass = config::byKey('pass_ewe', 'ewejee'); $ssl = config::byKey('ssl', 'ewejee'); if ($ssl == 'non') { $ip = config::byKey("internalAddr", "core"); $port = config::byKey("internalPort", "core"); } elseif ($ssl == 'oui') { $ip = config::byKey("externalAddr", "core"); $port = config::byKey("externalPort", "core"); } elseif ($ssl == 'custom') { $ip = config::byKey("externalAddr", "core"); $port = config::byKey("custom_port", "core"); } else { config::save("ssl", "non", "ewejee"); log::add('ewejee', 'info', '[deamon_cloud_start()]Configuration manquante = Acces interne. Mis par défaut su l\'acces interne'); $ip = config::byKey("internalAddr", "core"); $port = config::byKey("internalPort", "core"); } $cmd = 'sudo nice node ' . $path . '/core/js/newserver.js ' . $login . ' ' . $pass . ' ' . $port . ' ' . $ip . ' ' . $ssl; $result = exec($cmd . ' >> ' . log::getPathToLog('ewejee_node') . ' 2>&1 &'); if (strpos(strtolower($result), 'error') !== false || strpos(strtolower($result), 'traceback') !== false) { log::add('ewejee', 'error', $result); return false; } $i = 0; while ($i < 30) { $deamon_cloud_info = self::deamon_cloud_info(); if (trim($deamon_cloud_info['state']) == 'ok') { break; } sleep(3); $i++; } if ($i >= 30) { log::add('ewejee', 'error', '[deamon_cloud_start()][$i>30] Impossible de lancer le démon. Avez vous remis la configuration du plugin ?', 'unableStartDeamon'); return false; } message::removeAll('ewejee', 'unableStartDeamon'); log::add('ewejee', 'info', '[deamon_cloud_start()] Démon ewejee lancé'); return true; } public static function deamon_cloud_info() { $return = array(); $return['log'] = 'ewejee_node'; $return['state'] = 'nok'; $listen = exec("ps -eo pid,command | grep newserver | grep -v grep | awk '{print $1}'"); if ($listen != '') { $return['state'] = 'ok'; } $return['launchable'] = 'ok'; return $return; } public static function deamon_cloud_stop() { $deamon_cloud_info = self::deamon_cloud_info(); if ($deamon_cloud_info['state'] == 'ok') { $pid = exec("ps -eo pid,command | grep newserver | grep -v grep | awk '{print $1}'"); if ($pid) { $cmd = 'sudo kill ' . $pid; log::add('ewejee', 'debug', '[deamon_stop]PID = ' . $pid); $result = exec($cmd . ' > /dev/null 2>&1'); } } $deamon_cloud_info = self::deamon_cloud_info(); if ($deamon_cloud_info['state'] == 'ok') { $a = 0; while ($pid != '' or $a == 30) { if ($pid) { log::add('ewejee', 'info', 'Plusieurs PID en cours, kill PID Nr : ' . $pid); $cmd = 'sudo kill ' . $pid; $result = exec($cmd . ' > /dev/null 2>&1'); $a = $a + 1; $pid = exec("ps -eo pid,command | grep newserver | grep -v grep | awk '{print $1}'"); } } } $deamon_cloud_info = self::deamon_cloud_info(); if ($deamon_cloud_info['state'] == 'ok') { sleep(10); $pid = exec("ps -eo pid,command | grep newserver | grep -v grep | awk '{print $1}'"); $cmd = 'sudo kill ' . $pid; $result = exec($cmd . ' > /dev/null 2>&1'); log::add('ewejee', 'info', 'Impossible d\'arrêter le deamon ... Veuillez redémarrer Jeedom'); } } /** * Just same as cloud run a nodejs server to send the command to eWeLink devices over LAN */ public function deamon_LAN_start() { $path = realpath(dirname(__FILE__) . '/../../'); log::add('ewejee', 'info', 'Lancement du demon LAN'); if (config::byKey("port", "ewejee") == "") { config::save("port", "80", "ewejee"); log::add('ewejee', 'debug', '[deamon_cloud_start()] Initialisation sur le port ' . config::byKey("port", "ewejee")); } else { log::add('ewejee', 'debug', '[deamon_cloud_start()] Port du localhost (Par défault = 80) = ' . config::byKey("port", "ewejee")); } self::deamon_LAN_stop(); $deamon_LAN_info = self::deamon_LAN_info(); if ($deamon_LAN_info['launchable'] != 'ok') { throw new Exception(__('Veuillez vérifier la configuration', __FILE__)); } $login = config::byKey('login_ewe', 'ewejee'); $pass = config::byKey('pass_ewe', 'ewejee'); $ssl = config::byKey('ssl', 'ewejee'); if ($ssl == 'non') { $ip = config::byKey("internalAddr", "core"); $port = config::byKey("internalPort", "core"); } elseif ($ssl == 'oui') { $ip = config::byKey("externalAddr", "core"); $port = config::byKey("externalPort", "core"); } elseif ($ssl == 'custom') { $ip = config::byKey("externalAddr", "core"); $port = config::byKey("custom_port", "core"); } else { config::save("ssl", "non", "ewejee"); log::add('ewejee', 'info', '[deamon_cloud_start()]Configuration manquante = Acces interne. Mis par défaut su l\'acces interne'); $ip = config::byKey("internalAddr", "core"); $port = config::byKey("internalPort", "core"); } $cmd_lan = 'sudo nice node ' . $path . '/core/js/lanserver.js ' . $login . ' ' . $pass . ' ' . $port . ' ' . $ip . ' ' . $ssl . ' ' . $path; $result_lan = exec($cmd_lan . ' >> ' . log::getPathToLog('ewejee_lan') . ' 2>&1 &'); if (strpos(strtolower($result_lan), 'error') !== false || strpos(strtolower($result_lan), 'traceback') !== false) { log::add('ewejee', 'error', $result_lan); return false; } log::add('ewejee', 'info', '[deamon_LAN_start]Demon LAN lancé'); config::save("demon_LAN", "ok", "ewejee"); ewejee::deamon_MDNS_start(); return true; } public function deamon_LAN_stop() { ewejee::deamon_MDNS_stop(); $pid = preg_replace('/[^0-9]/', '', exec('sudo lsof -i TCP:6006 | cut -c1-18')); // awk '$8 == "TCP" { print $2 }' $cmd = 'sudo kill -9 ' . $pid; $result = exec($cmd . ' > /dev/null 2>&1'); $deamon_info = self::deamon_LAN_info(); if ($deamon_info['state'] == 'ok') { sleep(10); log::add('ewejee', 'info', 'Impossible d\'arreter le démon'); $cmd = 'sudo kill -9 ' . $pid; $result = exec($cmd . ' > /dev/null 2>&1'); } $deamon_info = self::deamon_LAN_info(); if ($deamon_info['state'] == 'ok') { sleep(10); $cmd = 'sudo kill -9 ' . $pid; $result = exec($cmd . ' > /dev/null 2>&1'); log::add('ewejee', 'info', 'Démon toujours pas arreté, problème majeur ... Veuillez redémarrer Jeedom'); } config::save("demon_LAN", "nok", "ewejee"); } public function deamon_LAN_info() { $return = []; $return['log'] = 'ewejee_lan'; $server = exec('sudo lsof -i TCP:6006'); if ($server != '') { $return['state'] = "ok"; } else { $return['state'] = "nok"; } $return['launchable'] = 'ok'; return $return; } public static function dependancy_info() { $return = array(); $return['log'] = 'ewejee_dep'; $resources = realpath(dirname(__FILE__) . '/../../core/js/node_modules'); $dep = '/ewelink-api'; $state = 'ok'; if (!is_dir($resources . $dep)) { $state = 'nok'; } $return['progress_file'] = jeedom::getTmpFolder('ewejee') . '/dependance'; //$return['state'] = is_dir($resources.'/node_modules') ? 'ok' : 'nok'; $return['state'] = $state; return $return; } public static function dependancy_install() { if (file_exists(jeedom::getTmpFolder('ewejee') . '/dependance')) { return; } log::remove('ewejee_dep'); $_debug = 0; log::add('ewejee', 'info', 'Installation des dépendances'); $resource_path = realpath(dirname(__FILE__) . '/../../resources'); return array('script' => $resource_path . '/install.sh ' . $resource_path . ' ewejee ' . $_debug, 'log' => log::getPathToLog('ewejee_dep')); } /* * 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 ewejeeCmd 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()) { /* ************************************************************************************** ***************************************************************************************** ******************************* V2 ********************************************** ***************************************************************************************** ************************************************************************************** */ $eqlogic = $this->getEqLogic(); //récupère l'éqlogic de la commande $this $logicalId = $this->getLogicalId(); // Récupère l'id logic de la commande ! $device_id = $eqlogic->getLogicalId(); // Récupère le deviceID de l'équipement $apikey = $eqlogic->getConfiguration('apikey'); // Apikey de l'équipement $model = $eqlogic->getConfiguration('model'); // Modèle du sonoff switch ($model) { case 'RFBridge': $ch = $logicalId[10] . $logicalId[11]; // Voir si ok ou s'il faut ajouter 1 $eqlogic->rfbridge($device_id, $apikey, $ch); break; default: // Modèle non connu # code... break; } /* ************************************************************************************** ***************************************************************************************** ******************************* V1 ********************************************** ***************************************************************************************** ************************************************************************************** */ $eqlogic = $this->getEqLogic(); //récupère l'éqlogic de la commande $this /* ************************************************************************************** ***************************************************************************************** ******************************* Mode CLOUD ********************************************** ***************************************************************************************** ************************************************************************************** */ if ($eqlogic->getConfiguration('lan') != 'oui') { if ($this->getLogicalId() == "colorChoise") { $eqlogic->brightAndColor($device_id, $apikey, $_options['color'], $eqlogic); } switch ($eqlogic->getName()) { // Récupère le nom case 'auth': $eqlogic->auth(); break; case 'test': $eqlogic->batteryStatus(100); $eqlogic->save(); echo "ok"; break; case 'cut': $url = 'http://localhost:6005/cut'; $data = file_get_contents($url); echo $data; break; case 'Sync': log::add('ewejee', 'debug', 'DEV début execute Sync'); $ewejee = new Ewejee; $json = $ewejee->sync(); //$ewejee->synchronisation($json); log::add('ewejee', 'debug', 'DEV fin execute Sync'); break; case $eqlogic->getName(): // On reconnait le nom de l'équippement $ch = $this->getLogicalId(); $device_id = $eqlogic->getLogicalId(); $apikey = $eqlogic->getConfiguration('apikey'); $model = $eqlogic->getConfiguration('model'); if ($model == "iFan02" or $model == "iFan") { $eqlogic->ifan($device_id, $apikey, $ch); } elseif ($model == 'POW' && $ch == 'get') { $eqlogic->getpowui($device_id, $apikey); } elseif ($model == 'POW' && $ch == 'get_pow_mensuel') { $url = 'http://localhost:6005/hundredDaysKwh/' . $device_id . '/' . $apikey; $data = file_get_contents($url); } elseif ($model == 'PSF-X67' && $ch == 'get') { $eqlogic->getpowui($device_id, $apikey); } elseif ($model == 'PSF-X67' && $ch == 'get_pow_mensuel') { $url = 'http://localhost:6005/hundredDaysKwh/' . $device_id . '/' . $apikey; $data = file_get_contents($url); } elseif ($model == 'KING-Q4') { switch ($ch) { case 'Q4-setClose-slider': $eqlogic->setClose($device_id, $apikey, $_options['slider']); break; case 'Q4-open': $eqlogic->switch_device($device_id, $apikey, 1, 'on'); break; case 'Q4-close': $eqlogic->switch_device($device_id, $apikey, 1, 'off'); break; case 'Q4-pause': $eqlogic->pause($device_id, $apikey); break; } //} elseif ($model == 'RFBridge') { //continue; } elseif ($model === 'zigbee_ON_OFF_SWITCH_1000') { switch ($ch) { case 'simple_key': $url = 'http://localhost:6005/switch/zig/' . $model . '/' . $device_id . '/' . $apikey . '/0'; $data = file_get_contents($url); break; case 'double_key': $url = 'http://localhost:6005/switch/zig/' . $model . '/' . $device_id . '/' . $apikey . '/1'; $data = file_get_contents($url); break; } } if (strpos($ch, '_dualr3')) { if (!$_options['slider']) { $_options['slider'] = 0; } $url = 'http://localhost:6005/dualr3/' . $ch . '/' . $device_id . '/' . $apikey . '/' . $_options['slider']; $data = file_get_contents($url); log::add('ewejee', 'info', 'data = ' . $data); } /*else if ($model === "DUALR3") { switch ($ch) { case 'setLocation_dualr3_slider': $eqlogic->setLocation($device_id, $apikey, $_options['slider']); break; case 'open_dualr3': $eqlogic->motorTurn($device_id, $apikey, 1, 'on'); break; case 'close_dualr3': $eqlogic->motorTurn($device_id, $apikey, 1, 'off'); break; case 'stop_dualr3': $eqlogic->motorTurn($device_id, $apikey); break; } }*/ switch ($ch[0]) { case '2': // Pour RF Bridge $eqlogic->rfbridge($device_id, $apikey, $ch[2]); break; case '3': // Pour Humidificateur if ($ch == '300' or $ch == '301') { $eqlogic->switch_device($device_id, $ch); } break; case 'B': //B1 Ampoule $eqlogic->b1($ch, $device_id, $apikey); break; case 'i': //Pour bloquer le double switch ventilo ifan log::add('ewejee', 'debug', 'Case i ok'); break; case 'b': if ($ch === 'bright-slider') { $eqlogic->bright($device_id, $apikey, $_options['slider'], $eqlogic); } break; case 'd': if ($ch === 'dbrightness-slider') { $eqlogic->brightness($device_id, $apikey, $_options['slider']); } break; case 'c': $ip = $eqlogic->getConfiguration('ip'); ewejee::ptz_cam($eqlogic, $ch); if ($ch === 'cam_mov_1') { $cmd = $eqlogic->getCmd(null, 'cam_motion'); $cmd->event('1'); } else if ($ch === 'cam_mov_0') { $cmd = $eqlogic->getCmd(null, 'cam_motion'); $cmd->event('0'); } break; default: if (strpos($ch, "#") !== FALSE) { if ($model == "B1") // B1 color { $eqlogic->b1($ch, $device_id, $apikey); } else { // WI16 $eqlogic->change_color($device_id, $apikey, $ch); } break; } $name = $this->getName(); // if name onoff alors check valeur si on ou off puis change name on ou off pour compatibilitée actuelle if ($name == 'ON-OFF') { $name = 'old'; } if (strpos($name, 'ON') !== FALSE or strpos($name, 'Allumer') !== FALSE) { $name = 'on'; } if (strpos($name, 'OFF') !== FALSE or strpos($name, 'Éteindre') !== FALSE) { $name = 'off'; log::add('ewejee', 'debug', '- OFF en off'); } // faire aussi le changement de nom et ajouter une commande lors de la sync pour TOUS les switches log::add('ewejee', 'debug', '---------- Switch device ' . $device_id . ' channel : ' . $ch . ' Action : ' . $name); $info = $eqlogic->switch_device($device_id, $apikey, $ch, $name); break; } break; } } else { /* ************************************************************************************** ***************************************************************************************** ******************************* Mode LAN ************************************************ ***************************************************************************************** ************************************************************************************** */ $eqlogic->setConfiguration('lan', 'oui'); // Pour la case 'Forcer Cloud' $eqlogic->save(); switch ($eqlogic->getName()) { // Récupère le nom case 'auth': $eqlogic->auth(); break; case 'test': $eqlogic->send_lan($eqlogic, '/zeroconf/switch', '{"switch":"on"}'); break; case 'Sync': log::add('ewejee', 'debug', 'DEV début execute Sync'); $ewejee = new Ewejee; $json = $ewejee->sync(); //$ewejee->synchronisation($json); log::add('ewejee', 'debug', 'DEV fin execute Sync'); break; case $eqlogic->getName(): // On reconnait le nom de l'équippement $ch = $this->getLogicalId(); $device_id = $eqlogic->getLogicalId(); $apikey = $eqlogic->getConfiguration('apikey'); $model = $eqlogic->getConfiguration('model'); if ($model == "iFan02" or $model == "iFan") { $eqlogic->ifan($device_id, $apikey, $ch); } elseif ($model == 'POW' && $ch == 'get') { $eqlogic->getpowui($device_id, $apikey); } elseif ($model == 'POW' && $ch == 'get_pow_mensuel') { $eqlogic->pow_stat($device_id, 'get'); } elseif ($model == 'PSF-X67' && $ch == 'get') { $eqlogic->getpowui($device_id, $apikey); } elseif ($model == 'PSF-X67' && $ch == 'get_pow_mensuel') { $url = 'http://localhost:6005/hundredDaysKwh/' . $device_id . '/' . $apikey; $data = file_get_contents($url); } elseif ($model == 'KING-Q4') { switch ($ch) { case 'Q4-setClose-slider': $eqlogic->setClose($device_id, $apikey, $_options['slider']); break; case 'Q4-open': $eqlogic->send_lan($eqlogic, '/zeroconf/switches', '{"switches":[{"outlet":1,"switch":"on"}]}'); break; case 'Q4-close': $eqlogic->send_lan($eqlogic, '/zeroconf/switches', '{"switches":[{"outlet":1,"switch":"off"}]}'); break; case 'Q4-pause': $eqlogic->pause($device_id, $apikey); break; } } elseif ($model == 'zigbee_ON_OFF_SWITCH_1000') { switch ($ch) { case 'simple_key': $url = 'http://localhost:6005/switch/zig/' . $model . '/' . $device_id . '/' . $apikey . '/0'; $data = file_get_contents($url); break; case 'double_key': $url = 'http://localhost:6005/switch/zig/' . $model . '/' . $device_id . '/' . $apikey . '/1'; $data = file_get_contents($url); break; } } switch ($ch[0]) { case '2': // Pour RF Bridge $eqlogic->rfbridge($device_id, $apikey, $ch[2]); break; case '3': // Pour Humidificateur if ($ch == '300' or $ch == '301') { $eqlogic->switch_device_LAN($device_id, $ch); } break; case 'B': //B1 Ampoule $eqlogic->b1($ch, $device_id, $apikey); break; case 'i': //Pour bloquer le double switch ventilo ifan log::add('ewejee', 'debug', 'Case i ok'); break; case 'b': if ($ch === 'bright-slider') { $eqlogic->bright($device_id, $apikey, $_options['slider'], $eqlogic); } break; case 'd': if ($ch === 'dbrightness-slider') { $eqlogic->brightness($device_id, $apikey, $_options['slider']); } break; default: if (strpos($ch, "#") !== FALSE) { if ($model == "B1") // B1 color { $eqlogic->b1($ch, $device_id, $apikey); } else { // WI16 $eqlogic->change_color($device_id, $apikey, $ch); } break; } $name = $this->getName(); // if name onoff alors check valeur si on ou off puis change name on ou off pour compatibilitée actuelle if ($name == 'ON-OFF') { $name = 'old'; } if (strpos($name, 'ON') !== FALSE or strpos($name, 'Allumer') !== FALSE) { $name = 'on'; log::add('ewejee', 'debug', '- ON en on'); } if (strpos($name, 'OFF') !== FALSE or strpos($name, 'Éteindre') !== FALSE) { $name = 'off'; } /** * Allumer 1 * Eteindre 01 * ON 1 = 101 / ON 3 = 103 * OFF 1 = 501 / OFF 3 = 503 * Pow pareil */ /** * Si plusieurs channel : */ if (strlen($ch) === 3) { $channel = $ch[2] - 1; if ($ch[0] === '1') { $onoff = 'on'; } else if ($ch[0] === '5') { $onoff = 'off'; } } if ($channel === 0 || $channel === 1 || $channel === 2 || $channel === 3) { $outlet = $eqlogic->getConfiguration('outlet'); if (!$outlet) { $outlet = 'outlet'; } $eqlogic->send_lan($eqlogic, '/zeroconf/switches', '{"switches":[{"'.$outlet.'":'.$channel.',"switch":"'.$onoff.'"}]}'); } else { /** * Si mono channel */ if ($ch === '1') { $onoff = 'on'; } else if ($ch === '01') { $onoff = "off"; } $eqlogic->send_lan($eqlogic, '/zeroconf/switch', '{"switch":"'.$onoff.'"}'); } //'{"switches":[{"outlet":'.$ch.',"switch":'.$onoff.'}]}'; break; } break; } } } /* * **********************Getteur Setteur*************************** */ }