Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
fix(mqtt): follow best practices
Browse files Browse the repository at this point in the history
fix #313

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and DIOHz0r committed Feb 22, 2018
1 parent a93b511 commit e8f1f1a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 19 deletions.
8 changes: 4 additions & 4 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ public function getTopic() {
$serial = $computer->getField('serial');
if (strlen($serial)) {
$entity = $this->getField('entities_id');
$this->topic = "/$entity/agent/$serial";
$this->topic = "$entity/agent/$serial";
}
}
}
Expand All @@ -921,10 +921,10 @@ public function getByTopic($topic) {
global $DB;

$mqttPath = explode('/', $topic);
if (isset($mqttPath[3])) {
if ($mqttPath[2] == 'agent') {
if (isset($mqttPath[2])) {
if ($mqttPath[1] == 'agent') {
$entity = intval($mqttPath[1]);
$serial = $DB->escape($mqttPath[3]);
$serial = $DB->escape($mqttPath[2]);
if (strlen($serial)) {
$computerTable = Computer::getTable();
$agentTable = self::getTable();
Expand Down
2 changes: 1 addition & 1 deletion inc/fleet.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public function getTopic() {
return null;
}

return '/' . $this->fields['entities_id'] . '/fleet/' . $this->fields['id'];
return $this->fields['entities_id'] . '/fleet/' . $this->fields['id'];
}

/**
Expand Down
29 changes: 16 additions & 13 deletions inc/mqtthandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ public function publish(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $p
$message = $publish_object->getMessage();
$this->log->saveIngoingMqttMessage($topic, $message);

$mqttPath = explode('/', $topic, 5);
if (isset($mqttPath[4])) {
if ($mqttPath[4] == "Status/Ping" && $message == "!") {
$mqttPath = explode('/', $topic, 4);
if (isset($mqttPath[3])) {
if ($mqttPath[3] == "Status/Ping") {
$this->updateLastContact($topic, $message);
} else if ($mqttPath[4] == "Status/Geolocation" && $message != "?") {
} else if ($mqttPath[3] == "Status/Geolocation" && $message != "?") {
$this->saveGeolocationPosition($topic, $message);
} else if ($mqttPath[4] == "Status/Unenroll") {
} else if ($mqttPath[3] == "Status/Unenroll") {
$this->deleteAgent($topic, $message);
} else if ($mqttPath[4] == "Status/Inventory") {
} else if ($mqttPath[3] == "Status/Inventory") {
$this->updateInventory($topic, $message);
} else if ($mqttPath[4] == "Status/Online") {
} else if ($mqttPath[3] == "Status/Online") {
$this->updateOnlineStatus($topic, $message);
} else if ($mqttPath[4] == "Status/Task") {
} else if ($mqttPath[3] == "Status/Task") {
$this->updateTaskStatus($topic, $message);
} else if ($mqttPath[4] == "FlyvemdmManifest/Status/Version") {
} else if ($mqttPath[3] == "FlyvemdmManifest/Status/Version") {
$this->updateAgentVersion($topic, $message);
} else if (strpos($topic, "/FlyvemdmManifest") === 0) {
if ($topic == '/FlyvemdmManifest/Status/Version') {
Expand Down Expand Up @@ -203,7 +203,7 @@ protected function updateInventory($topic, $message) {
}
}

$this->updateLastContact($topic, $message);
$this->updateLastContact($topic, '!');
}
}

Expand All @@ -216,6 +216,9 @@ protected function updateInventory($topic, $message) {
* @param string $message
*/
protected function updateLastContact($topic, $message) {
if ($message !== '!') {
return;
}
$agent = new \PluginFlyvemdmAgent();
if ($agent->getByTopic($topic)) {

Expand Down Expand Up @@ -280,7 +283,7 @@ protected function saveGeolocationPosition($topic, $message) {
}
}

$this->updateLastContact($topic, $message);
$this->updateLastContact($topic, '!');
}
}

Expand Down Expand Up @@ -324,7 +327,7 @@ protected function updateTaskStatus($topic, $message) {
$taskStatus->updateStatus($policy, $status);
}

$this->updateLastContact($topic, $message);
$this->updateLastContact($topic, '!');
}
}
}
Expand Down Expand Up @@ -355,7 +358,7 @@ protected function updateOnlineStatus($topic, $message) {
'is_online' => $status,
]);

$this->updateLastContact($topic, $message);
$this->updateLastContact($topic, '!');
}
}
}
94 changes: 93 additions & 1 deletion install/upgrade/update_to_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
PluginFlyvemdmInvitation::$rightname => ALLSTANDARDRIGHT ,
PluginFlyvemdmInvitationlog::$rightname => READ,
PluginFlyvemdmGeolocation::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmTask::$rightname => READ,
]);
$profileRight->updateProfileRights($profiles_id, $newRights);

Expand Down Expand Up @@ -303,4 +302,97 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
SET `symbol` = 'maximumTimeToLock'
WHERE `symbol`='MaximumTimeToLock'";
$DB->query($query);

// change MQTT topics tree layout : remove leading slash
$mqttClient = PluginFlyvemdmMqttclient::getInstance();
$request = [
'FIELDS' => [
'glpi_plugin_flyvemdm_agents' => ['entities_id'],
'glpi_computers' => ['serial'],
],
'FROM' => 'glpi_plugin_flyvemdm_agents',
'INNER JOIN' => [
'glpi_computers' => ['FKEY' => [
'glpi_plugin_flyvemdm_agents' => 'computers_id',
'glpi_computers' => 'id'
]]
],
'WHERE' => ['lock' => ['<>' => '0']]
];
$mqttMessage = ['lock' => 'now'];
$mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES);
foreach ($DB->request($request) as $row) {
$topic = implode('/', [
$row['entities_id'],
'agent',
$row['serial'],
'Command',
'Lock',
]);
$mqttClient->publish($topic, $mqttMessage, 0, 1);
$mqttClient->publish('/' . $topic, null, 0, 1);
}

// re-use previous request array
$request['WHERE'] = ['wipe' => ['<>' => '0']];
$mqttMessage = ['wipe' => 'now'];
$mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES);
foreach ($DB->request($request) as $row) {
$topic = implode('/', [
$row['entities_id'],
'agent',
$row['serial'],
'Command',
'Wipe',
]);
$mqttClient->publish($topic, $mqttMessage, 0, 1);
$mqttClient->publish('/' . $topic, null, 0, 1);
}

$request['WHERE'] = ['enroll_status' => ['=' => 'unenrolling']];
$mqttMessage = ['unenroll' => 'now'];
$mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES);
foreach ($DB->request($request) as $row) {
$topic = implode('/', [
$row['entities_id'],
'agent',
$row['serial'],
'Command',
'Unenroll',
]);
$mqttClient->publish($topic, $mqttMessage, 0, 1);
$mqttClient->publish('/' . $topic, null, 0, 1);
}

$request = [
'FIELDS' => [
'glpi_plugin_flyvemdm_tasks' => ['id', 'plugin_flyvemdm_fleets_id'],
'glpi_plugin_flyvemdm_policies' => ['symbol'],
'glpi_plugin_flyvemdm_fleets' => ['entities_id']
],
'FROM' => 'glpi_plugin_flyvemdm_tasks',
'INNER JOIN' => [
'glpi_plugin_flyvemdm_policies' => [
'FKEY' => [
'glpi_plugin_flyvemdm_tasks' => 'plugin_flyvemdm_policies_id', 'glpi_plugin_flyvemdm_policies' => 'id'
]
],
'glpi_plugin_flyvemdm_fleets' => [
'FKEY' => [
'glpi_plugin_flyvemdm_tasks' => 'plugin_flyvemdm_fleets_id', 'glpi_plugin_flyvemdm_fleets' => 'id'
]
]
]
];
foreach ($DB->request($request) as $row) {
$topic = implode('/', [
$row['entities_id'],
'fleet',
$row['plugin_flyvemdm_fleets_id'],
'Policy',
$row['symbol'],
]);
$mqttClient->publish("$topic/Task/" . $row['id'], json_encode($mqttMessage, JSON_UNESCAPED_SLASHES), 0, 1);
$mqttClient->publish('/' . $topic, null, 0, 1);
}
}

0 comments on commit e8f1f1a

Please sign in to comment.