Skip to content

Commit 643b771

Browse files
authored
Merge pull request #44 from jkurz78/beta
fix: résoudre la collision des logicalId des groupes en multi-hubs et gérer le retour false de l'API
2 parents be81f68 + 8007cd1 commit 643b771

1 file changed

Lines changed: 53 additions & 21 deletions

File tree

core/class/ajaxSystem.class.php

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,40 @@ public static function sync() {
203203

204204
$groups = self::request('/user/{userId}/hubs/' . $hub['hubId'] . '/groups');
205205
log::add('ajaxSystem', 'debug', json_encode($groups));
206-
foreach ($groups as $group) {
207-
if($group['groupName'] == ''){
208-
continue;
206+
if (!is_array($groups)) {
207+
log::add('ajaxSystem', 'warning', __('Impossible de récupérer les groupes pour le hub ', __FILE__) . $hub['hubId'] . ' : ' . json_encode($groups));
208+
} else {
209+
foreach ($groups as $group) {
210+
if($group['groupName'] == ''){
211+
continue;
212+
}
213+
$groupLogicalId = $hub['hubId'] . '_' . $group['id'];
214+
$eqLogic = eqLogic::byLogicalId($groupLogicalId, 'ajaxSystem');
215+
// Migration: check for old-style logicalId without hub prefix
216+
if (!is_object($eqLogic)) {
217+
$eqLogic = eqLogic::byLogicalId($group['id'], 'ajaxSystem');
218+
if (is_object($eqLogic) && $eqLogic->getConfiguration('type') == 'group') {
219+
log::add('ajaxSystem', 'info', __('Migration du groupe ', __FILE__) . $group['groupName'] . ' vers le nouveau logicalId ' . $groupLogicalId);
220+
} else {
221+
$eqLogic = null;
222+
}
223+
}
224+
if (!is_object($eqLogic)) {
225+
$eqLogic = new ajaxSystem();
226+
$eqLogic->setEqType_name('ajaxSystem');
227+
$eqLogic->setIsEnable(1);
228+
$eqLogic->setName($group['groupName']);
229+
$eqLogic->setCategory('security', 1);
230+
$eqLogic->setIsVisible(1);
231+
}
232+
$eqLogic->setConfiguration('hub_id', $hub['hubId']);
233+
$eqLogic->setConfiguration('type', 'group');
234+
$eqLogic->setConfiguration('device', 'group');
235+
$eqLogic->setConfiguration('group_id', $group['id']);
236+
$eqLogic->setLogicalId($groupLogicalId);
237+
$eqLogic->save();
238+
$eqLogic->refreshData($group);
209239
}
210-
$eqLogic = eqLogic::byLogicalId($group['id'], 'ajaxSystem');
211-
if (!is_object($eqLogic)) {
212-
$eqLogic = new ajaxSystem();
213-
$eqLogic->setEqType_name('ajaxSystem');
214-
$eqLogic->setIsEnable(1);
215-
$eqLogic->setName($group['groupName']);
216-
$eqLogic->setCategory('security', 1);
217-
$eqLogic->setIsVisible(1);
218-
}
219-
$eqLogic->setConfiguration('hub_id', $hub['hubId']);
220-
$eqLogic->setConfiguration('type', 'group');
221-
$eqLogic->setConfiguration('device', 'group');
222-
$eqLogic->setLogicalId($group['id']);
223-
$eqLogic->save();
224-
$eqLogic->refreshData($group);
225240
}
226241
}
227242
}
@@ -295,6 +310,22 @@ public function refreshData($_data = null) {
295310
if ($this->getConfiguration('type') == 'device') {
296311
$datas = self::request('/user/{userId}/hubs/' . $this->getConfiguration('hub_id') . '/devices/' . $this->getLogicalId());
297312
}
313+
if ($this->getConfiguration('type') == 'group') {
314+
$groupId = $this->getConfiguration('group_id', $this->getLogicalId());
315+
$allGroups = self::request('/user/{userId}/hubs/' . $this->getConfiguration('hub_id') . '/groups');
316+
$datas = null;
317+
if (is_array($allGroups)) {
318+
foreach ($allGroups as $g) {
319+
if (isset($g['id']) && $g['id'] == $groupId) {
320+
$datas = $g;
321+
break;
322+
}
323+
}
324+
}
325+
if ($datas === null) {
326+
return;
327+
}
328+
}
298329
}else{
299330
$datas = $_data;
300331
}
@@ -400,12 +431,13 @@ public function execute($_options = array()) {
400431
log::add('ajaxSystem','debug','Command send to ajax : '.json_encode($command));
401432
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/devices/' . $eqLogic->getLogicalId() . '/command', $command, 'POST');
402433
} else if ($eqLogic->getConfiguration('type') == 'group') {
434+
$groupId = $eqLogic->getConfiguration('group_id', $eqLogic->getLogicalId());
403435
if ($this->getLogicalId() == 'ARM') {
404-
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/groups/' . $eqLogic->getLogicalId() . '/commands/arming', array('command' => 'ARM', 'ignoreProblems' => true), 'PUT');
436+
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/groups/' . $groupId . '/commands/arming', array('command' => 'ARM', 'ignoreProblems' => true), 'PUT');
405437
} else if ($this->getLogicalId() == 'DISARM') {
406-
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/groups/' . $eqLogic->getLogicalId() . '/commands/arming', array('command' => 'DISARM', 'ignoreProblems' => true), 'PUT');
438+
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/groups/' . $groupId . '/commands/arming', array('command' => 'DISARM', 'ignoreProblems' => true), 'PUT');
407439
} else if ($this->getLogicalId() == 'NIGHT_MODE') {
408-
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/groups/' . $eqLogic->getLogicalId() . '/commands/arming', array('command' => 'NIGHT_MODE_ON', 'ignoreProblems' => true), 'PUT');
440+
ajaxSystem::request('/user/{userId}/hubs/' . $eqLogic->getConfiguration('hub_id') . '/groups/' . $groupId . '/commands/arming', array('command' => 'NIGHT_MODE_ON', 'ignoreProblems' => true), 'PUT');
409441
}
410442
}
411443
}

0 commit comments

Comments
 (0)