Skip to content

Commit

Permalink
Fixes for model reset when alternate signature
Browse files Browse the repository at this point in the history
  • Loading branch information
tcharp38 committed Nov 10, 2023
1 parent c44b46d commit 2ffd90a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
2 changes: 1 addition & 1 deletion core/ajax/AbeilleFiles.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
$status = -1;
$error = "Le fichier '".$jsonId."' n'existe pas dans '".$jsonLocation."'";
} else {
$devModel = AbeilleTools::getDeviceModel($jsonId, $jsonLocation, $mode);
$devModel = AbeilleTools::getDeviceModel('', $jsonId, $jsonLocation, $mode);
if ($devModel === false) {
$status = -1;
$error = "Le modèle '".$jsonId."' n'existe pas dans '".$jsonLocation."'";
Expand Down
17 changes: 9 additions & 8 deletions core/class/Abeille.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ public static function message($topic, $payload) {
// Any user or official model ?
$modelInfos = self::findModel($eqSig['modelId'], $eqSig['manufId']);
if ($modelInfos !== false) {
$modelSig = $modelInfos['modelSignature'];
$modelSig = $modelInfos['modelSig'];
$jsonId = $modelInfos['jsonId'];
$jsonLocation = $modelInfos['location']; // TODO: rename to jsonLocation
$eqHName = $eqLogic->getHumanName();
Expand All @@ -1491,9 +1491,9 @@ public static function message($topic, $payload) {
$dev = array(
'net' => $dest,
'addr' => $addr,
'modelSignature' => $modelSig,
'jsonId' => $jsonId,
'jsonLocation' => $jsonLocation,
'modelSig' => $modelSig, // Model signature
'jsonId' => $jsonId, // Model file name
'jsonLocation' => $jsonLocation, // Model file location
'ieee' => $eqLogic->getConfiguration('IEEE'),
);
Abeille::createDevice($action, $dev);
Expand Down Expand Up @@ -2777,8 +2777,9 @@ public static function createDevice($action, $dev) {
);
*/

$jsonId = (isset($dev['jsonId']) ? $dev['jsonId']: '');
$jsonLocation = (isset($dev['jsonLocation']) ? $dev['jsonLocation']: '');
$modelSig = isset($dev['modelSig']) ? $dev['modelSig']: '';
$jsonId = isset($dev['jsonId']) ? $dev['jsonId']: '';
$jsonLocation = isset($dev['jsonLocation']) ? $dev['jsonLocation']: '';
if ($jsonLocation == '')
$jsonLocation = 'Abeille';

Expand All @@ -2790,15 +2791,15 @@ public static function createDevice($action, $dev) {
$isModelForcedByUser = false;
if(is_object($eqLogic)){
$jEqModel = $eqLogic->getConfiguration('ab::eqModel', []); // Eq model from Jeedom DB
if(isset($jEqModel['id']) && isset($jEqModel['location']) && isset($jEqModel['forcedByUser']) && $jEqModel['forcedByUser'] == true){
if(isset($jEqModel['id']) && isset($jEqModel['location']) && isset($jEqModel['forcedByUser']) && ($jEqModel['forcedByUser'] == true)){
$jsonLocation = $jEqModel['location'];
$jsonId = $jEqModel['id'];
$isModelForcedByUser = true;
}
}

if ($jsonId != '' && $jsonLocation != '') {
$model = AbeilleTools::getDeviceModel($jsonId, $jsonLocation);
$model = AbeilleTools::getDeviceModel($modelSig, $jsonId, $jsonLocation);
if ($model === false) {
// log::add('Abeille', 'error', " createDevice(jsonId=".$jsonId.", location=".$jsonLocation."): Unknown model");
return;
Expand Down
2 changes: 1 addition & 1 deletion core/class/AbeilleParser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ function deviceConfigure($net, $addr) {
parserLog('debug', " deviceConfigure(".$net.", ".$addr.", jsonId=".$eq['jsonId'].")");

// Read JSON to get list of commands to execute
$eqModel = AbeilleTools::getDeviceModel($eq['jsonId'], $eq['jsonLocation']);
$eqModel = AbeilleTools::getDeviceModel('', $eq['jsonId'], $eq['jsonLocation']);
if ($eqModel === false)
return;

Expand Down
84 changes: 63 additions & 21 deletions core/class/AbeilleTools.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ public static function deamonlogFilter($loglevel = 'NONE', $pluginName, $loggerN
}

/* Get list of supported devices ($from="Abeille"), or user/custom ones ($from="local")
Returns: Associative array; $devicesList[$identifier] = array(), or false if error */
Returns: false if error
or associative array $devicesList[$identifier] = array(
'modelSig' => model signature
'jsonId' => model file
'location' => model file location // TODO: rename to jsonLocation
) */
public static function getDevicesList($from = "Abeille") {
$devicesList = [];

Expand Down Expand Up @@ -112,46 +117,50 @@ public static function getDevicesList($from = "Abeille") {
continue; // No local JSON model. Maybe just an auto-discovery ?
$dev = array(
'modelSig' => $dirEntry,
'jsonId' => $dirEntry,
'location' => $from
);

/* Check if config is compliant with other device identification */
$content = file_get_contents($fullPath);
$devConf = json_decode($content, true);
$devConf = $devConf[$dirEntry]; // Removing top key
$dev['manufacturer'] = isset($devConf['manufacturer']) ? $devConf['manufacturer'] : '';
$dev['model'] = isset($devConf['model']) ? $devConf['model']: '';
$dev['type'] = $devConf['type'];
$dev['icon'] = $devConf['configuration']['icon'];
$devMod = json_decode($content, true); // Device model
$devMod = $devMod[$dirEntry]; // Removing top key
// $dev['manufacturer'] = isset($devMod['manufacturer']) ? $devMod['manufacturer'] : '';
// $dev['model'] = isset($devMod['model']) ? $devMod['model']: '';
// $dev['type'] = $devMod['type'];
// $dev['icon'] = $devMod['configuration']['icon'];
$devicesList[$dirEntry] = $dev;

if (isset($devConf['alternateIds'])) {
$ai = $devConf['alternateIds'];
if (isset($devMod['alternateIds'])) {
$ai = $devMod['alternateIds'];
/* Reminder:
"alternateIds": {
"sigX": {
"alternateSigX": {
"manufacturer": "manufX", // Optional
"model": "modelX", // Optional
"type": "typeX" // Optional
"icon": "iconX" // Optional
}
},
"alternateSigY": {},
"alternateSigZ": {}
} */
foreach ($ai as $aId => $aIdVal) {
log::add('Abeille', 'debug', "getDevicesList(): Alternate ID '".$aId."' for '".$dirEntry."'");
$dev = array(
'modelSig' => $aId,
'jsonId' => $dirEntry,
'location' => $from
);
// manufacturer, model, type or icon overload
if (isset($aIdVal['manufacturer']))
$dev['manufacturer'] = $aIdVal['manufacturer'];
if (isset($aIdVal['model']))
$dev['model'] = $aIdVal['model'];
if (isset($aIdVal['type']))
$dev['type'] = $aIdVal['type'];
if (isset($aIdVal['icon']))
$dev['icon'] = $aIdVal['icon'];
// if (isset($aIdVal['manufacturer']))
// $dev['manufacturer'] = $aIdVal['manufacturer'];
// if (isset($aIdVal['model']))
// $dev['model'] = $aIdVal['model'];
// if (isset($aIdVal['type']))
// $dev['type'] = $aIdVal['type'];
// if (isset($aIdVal['icon']))
// $dev['icon'] = $aIdVal['icon'];
$devicesList[$aId] = $dev;
}
}
Expand Down Expand Up @@ -336,12 +345,13 @@ public static function getCommandModel($modelName, $cmdFName, $newJCmdName = '')

/*
* Read given device configuration from JSON file and associated commands.
* 'modelSig': Model signature (!= modelName if alternate signature)
* 'modelName': JSON file name without extension
* 'from': JSON file location (default=Abeille, or 'local')
* 'mode': 0/default=load commands too, 1=split cmd call & file
* Return: device associative array without top level key (jsonId) or false if error.
* Return: device associative array WITHOUT top level key (modelSig) or false if error.
*/
public static function getDeviceModel($modelName, $from="Abeille", $mode=0) {
public static function getDeviceModel($modelSig, $modelName, $from="Abeille", $mode=0) {
// log::add('Abeille', 'debug', 'getDeviceModel start, modelName='.$modelName.", from=".$from);

if ($modelName == '') {
Expand Down Expand Up @@ -371,9 +381,41 @@ public static function getDeviceModel($modelName, $from="Abeille", $mode=0) {
}

$device = $device[$modelName]; // Removing top key
$device['modelSig'] = ($modelSig != '') ? $modelSig : $modelName;
$device['jsonId'] = $modelName;
$device['jsonLocation'] = $from; // Official device or local one ?
/* Alternate signature support */
if ($device['modelSig'] != $modelName) {
/* Reminder:
"alternateIds": {
"alternateSigX": {
"manufacturer": "manufX", // Optional
"model": "modelX", // Optional
"type": "typeX" // Optional
"icon": "iconX" // Optional
}
} */
if (!isset($device['alternateIds'][$modelSig])) {
// Internal error
log::add('Abeille', 'error', "getDeviceModel(): Unexpected alternate sig '${modelSig}'");
} else {
$alt = $device['alternateIds'][$modelSig];
// manufacturer, model, type or icon overload
if (isset($alt['manufacturer']))
$device['manufacturer'] = $alt['manufacturer'];
if (isset($alt['model']))
$device['model'] = $alt['model'];
if (isset($alt['type']))
$device['type'] = $alt['type'];
if (isset($alt['icon']))
$device['configuration']['icon'] = $alt['icon'];
if (isset($alt['batteryType']))
$device['configuration']['batteryType'] = $alt['batteryType'];
unset($device['alternateIds']); // Cleanup
}
}

if (isset($device['commands'])) {
if ($mode == 0) {
$deviceCmds = array();
Expand Down
2 changes: 1 addition & 1 deletion core/php/AbeilleCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function signalHandler($signal) {
);
if ($eq['jsonId'] != '') {
// Read JSON to get list of commands to execute
$model = AbeilleTools::getDeviceModel($eq['jsonId'], $eq['jsonLocation']);
$model = AbeilleTools::getDeviceModel('', $eq['jsonId'], $eq['jsonLocation']);
if ($model !== false) {
$eq['mainEp'] = isset($model['mainEP']) ? $model['mainEP'] : "01";
$eq['commands'] = isset($model['commands']) ? $model['commands'] : [];
Expand Down
2 changes: 2 additions & 0 deletions docs/fr_FR/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ChangeLog
- Tuya ZS08 télécommande universelle alimentée par USB: Ajout support.
- Meilleur support des modèles avec signature alternative.
- Abeille remote control: Commandes 'on all' & 'off all' cachées par défaut. Trop dangereux.
- Interne: Mise-à-jour 'getDevicesList()' + 'getDeviceModel()'.
- Reset de modele: Correction pour signatures alternatives.

231107-BETA-2
-------------
Expand Down

0 comments on commit 2ffd90a

Please sign in to comment.