Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divers améliorations #106

Merged
merged 3 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 51 additions & 40 deletions core/class/worxLandroidS.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public static function refresh_values($checkMowingTime = "false")
);
$count++;
if (!empty($eqptlist[0])) {
$mosqId = config::byKey('mqtt_client_id', 'worxLandroidS') . substr(md5(rand()), 0, 8);
$client = new Mosquitto\Client($mosqId, true);
self::connect_and_publish($eqptlist, $client, '{}');
self::connect_and_publish($eqptlist, '{}');
}
}
}
Expand Down Expand Up @@ -455,9 +453,13 @@ public static function daemon()

public static function daemonRefresh()
{
//log::add('worxLandroidS', 'debug', 'Début du Refresh');
self::refresh_values();
log::add('worxLandroidS', 'info', 'Fin du Refresh');
if (config::byKey('automaticRefresh', 'worxLandroidS') == true) {
//log::add('worxLandroidS', 'debug', 'Début du Refresh');
self::refresh_values();
log::add('worxLandroidS', 'info', 'Fin du Refresh');
} else {
log::add('worxLandroidS', 'info', 'Le rafraîchissement forcé est désactivé : pas de Refresh');
}

}

Expand Down Expand Up @@ -536,7 +538,7 @@ public static function create_equipement($product, $MowerType, $mowerDescription
$elogic->save();
}

public static function connect_and_publish($eqptlist, $client, $msg)
public static function old_connect_and_publish($eqptlist, $client, $msg)
{

$RESOURCE_PATH = realpath(dirname(__FILE__) . '/../../resources/');
Expand Down Expand Up @@ -602,6 +604,47 @@ public static function connect_and_publish($eqptlist, $client, $msg)

}


public function connect_and_publish($eqptlist, $msg) {
$RESOURCE_PATH = realpath(dirname(__FILE__) . '/../../resources/');
$CERTFILE = $RESOURCE_PATH . '/cert.pem';
$PKEYFILE = $RESOURCE_PATH . '/pkey.pem';
$ROOT_CA = $RESOURCE_PATH . '/vs-ca.pem';

$mosqId = config::byKey('mqtt_client_id', 'worxLandroidS') . substr(md5(rand()), 0, 8);
$client = new Mosquitto\Client($mosqId, true);

$client->clearWill();
$client->onDisconnect('worxLandroidS::disconnect');
$client->onSubscribe('worxLandroidS::subscribe');
$client->onMessage('worxLandroidS::message');
$client->onLog('worxLandroidS::logmq');
$client->setTlsCertificates($ROOT_CA, $CERTFILE, $PKEYFILE, null);
$client->setTlsOptions(Mosquitto\Client::SSL_VERIFY_NONE, "tlsv1.2", null);
$client->onConnect(
function () use ($client, $eqptlist, $msg) {
config::save('status', '1', 'worxLandroidS');
foreach ($eqptlist as $key => $value) {
$topic = $eqptlist[0][0] . '/' . $eqptlist[0][1] . "/commandIn";
log::add('worxLandroidS', 'debug', 'Publication du message : ' . $topic . ', msg : ' . $msg);
$client->publish($topic, $eqptlist[0][2], 0, 0);
}
$client->exitLoop();

});

$client->connect(config::byKey('mqtt_endpoint', 'worxLandroidS'), 8883, 300);
$client->loopForever();
config::save('status', '0', 'worxLandroidS');
$client->disconnect();
log::add('worxLandroidS', 'debug', 'Message publié');
}
public static function onConnectRefresh($r, $message)
{
log::add('worxLandroidS', 'debug', 'Connexion à Mosquitto avec code ' . $r . ' ' . $message);
//config::save('status', '1', 'worxLandroidS');
}

public static function connect($r, $message)
{
log::add('worxLandroidS', 'debug', 'Connexion à Mosquitto avec code ' . $r . ' ' . $message);
Expand Down Expand Up @@ -1142,7 +1185,6 @@ public static function publishMosquitto($_id, $_subject, $_message, $_retain)

// send cutedge
if ($cmd->getName() == 'cutEdge') {
// $_message = '{"cmd":4}';
$_message = '{"sc":{"ots":{"bc":1,"wtm":0}}}';
}

Expand All @@ -1167,46 +1209,15 @@ public static function publishMosquitto($_id, $_subject, $_message, $_retain)
log::add('worxLandroidS', 'debug', 'Envoi du message rain delay: ' . $_message);
}

$mosqId = config::byKey('mqtt_client_id', 'worxLandroidS') . substr(md5(rand()), 0, 8);
// if ( config::byKey('mowingTime', 'worxLandroidS') == '0' ){
$client = new Mosquitto\Client($mosqId, true);
$eqptlist[] = array();
$eqptlist[0] = array(
$eqlogic->getConfiguration('MowerType'),
$eqlogic->getLogicalId(),
$_message
);
self::connect_and_publish($eqptlist, $client, $_message);
//self::connect_and_publish($eqlogic, $client, $_message);

self::connect_and_publish($eqptlist, $_message);
// send cutedger
if ($cmd->getName() == 'cutEdge') {
sleep(7);
$_message = '{"cmd":2}';
$mosqId = config::byKey('mqtt_client_id', 'worxLandroidS') . substr(md5(rand()), 0, 8);
// if ( config::byKey('mowingTime', 'worxLandroidS') == '0' ){
$client2 = new Mosquitto\Client($mosqId, true);
$eqptlist[] = array();
$eqptlist[0] = array(
$eqlogic->getConfiguration('MowerType'),
$eqlogic->getLogicalId(),
$_message
);
self::connect_and_publish($eqptlist, $client, $_message);
sleep(5);
$_message = '{"cmd":3}';
$mosqId = config::byKey('mqtt_client_id', 'worxLandroidS') . substr(md5(rand()), 0, 8);
// if ( config::byKey('mowingTime', 'worxLandroidS') == '0' ){
$client3 = new Mosquitto\Client($mosqId, true);
$eqptlist[] = array();
$eqptlist[0] = array(
$eqlogic->getConfiguration('MowerType'),
$eqlogic->getLogicalId(),
$_message
);
self::connect_and_publish($eqptlist, $client, $_message);

}

}

Expand Down
6 changes: 6 additions & 0 deletions plugin_info/configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<input id="automaticWidget" type="checkbox" class="configKey autoCheck" data-l1key="automaticWidget"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{{Activer le rafraîchissement forcé : }}</label>
<div class="col-sm-2">
<input id="automaticRefresh" type="checkbox" class="configKey autoCheck" data-l1key="automaticRefresh"/>
</div>
</div>
</fieldset>
</form>
<legend>
Expand Down
2 changes: 2 additions & 0 deletions plugin_info/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
function worxLandroidS_install() {
$cron = cron::byClassAndFunction('worxLandroidS', 'daemon');
config::save('initCloud', 1 ,'worxLandroidS');
config::save('automaticRefresh', 0 ,'worxLandroidS');
if (!is_object($cron)) {
$cron = new cron();
$cron->setClass('worxLandroidS');
Expand Down Expand Up @@ -58,6 +59,7 @@ function worxLandroidS_update() {
}
$cron = cron::byClassAndFunction('worxLandroidS', 'daemon');
config::save('initCloud', 1 ,'worxLandroidS');
config::save('automaticRefresh', 0 ,'worxLandroidS');
if (!is_object($cron)) {
$cron = new cron();
$cron->setClass('worxLandroidS');
Expand Down