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

Make scenes editable in the console #22

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
13 changes: 0 additions & 13 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,3 @@ jobs:
submodules: true
- name: Run tests
uses: symcon/action-tests@master

test-legacy:
runs-on: ubuntu-latest
steps:
- name: Checkout module
uses: actions/checkout@master
with:
submodules: true
- name: Run tests
uses: symcon/action-tests@master
with:
phpunit-version: '9.2.6'
php-version: '7.3' #This version is currently used by Symcon | In later versions the tested wddx function is removed
2 changes: 1 addition & 1 deletion SceneControl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

[Link zur deutschen Dokumentation](https://www.symcon.de/de/service/dokumentation/modulreferenz/szenensteuerung/)

[Link to the english documentation](https://www.symcon.de/en/service/documentation/module-reference/scene-control/)
[Link to the english documentation](https://www.symcon.de/en/service/documentation/module-reference/scene-control/)
3 changes: 2 additions & 1 deletion SceneControl/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"width": "auto",
"add": 0,
"edit": {
"type": "SelectVariable"
"type": "SelectVariable",
"requiredAction": 1
}
}
]
Expand Down
1 change: 1 addition & 0 deletions SceneControl/locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Active Scene": "Aktive Szene",
"Unknown": "Unbekannt",
"'%s' is called": "'%s' wird aufgerufen",
"Set Scenes": "Setze Szenen",
"Scene Control": "Szenensteuerung",
"https://www.symcon.de/en/service/documentation/module-reference/scene-control/": "https://www.symcon.de/de/service/dokumentation/modulreferenz/szenensteuerung/"
}
Expand Down
112 changes: 73 additions & 39 deletions SceneControl/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,6 @@ public function ApplyChanges()

$targets = json_decode($this->ReadPropertyString('Targets'), true);

//Transfer data from Target Category(legacy) to recent List
if ($targets == []) {
$targetCategoryID = @$this->GetIDForIdent('Targets');

if ($targetCategoryID) {
foreach (IPS_GetChildrenIDs($targetCategoryID) as $childID) {
$targetID = IPS_GetLink($childID)['TargetID'];
$line = [
'VariableID' => $targetID
];
array_push($targets, $line);
IPS_DeleteLink($childID);
}

IPS_DeleteCategory($targetCategoryID);
$needsReload = true;
}
}

//Add GUID if none set
$needsReload = false;
foreach ($targets as $index => $target) {
Expand Down Expand Up @@ -93,26 +74,6 @@ public function ApplyChanges()
}
}

//Getting data from legacy SceneData to put them in SceneData attribute (including wddx, JSON)
for ($i = 1; $i <= $sceneCount; $i++) {
$sceneDataID = @$this->GetIDForIdent('Scene' . $i . 'Data');
if ($sceneDataID) {
$decodedSceneData = null;
if (function_exists('wddx_deserialize')) {
$decodedSceneData = wddx_deserialize(GetValue($sceneDataID));
}

if ($decodedSceneData == null) {
$decodedSceneData = json_decode(GetValue($sceneDataID));
}

if ($decodedSceneData) {
$sceneData[$i - 1] = $decodedSceneData;
}
$this->UnregisterVariable('Scene' . $i . 'Data');
}
}

//Deleting surplus data in SceneData
$sceneData = array_slice($sceneData, 0, $sceneCount);
$this->WriteAttributeString('SceneData', json_encode($sceneData));
Expand Down Expand Up @@ -251,9 +212,82 @@ public function GetConfigurationForm()
{
$form = json_decode(file_get_contents(__DIR__ . '/form.json'), true);
$form['elements'][1]['columns'][0]['add'] = $this->generateGUID();

// //generate the Lists for the action section
$targets = json_decode($this->ReadPropertyString('Targets'), true);
if (count($targets) === 0) {
return json_encode($form);
}

$actions = [['type' => 'Label', 'caption' => $this->translate('Set Scenes')]];
$sceneCount = $this->ReadPropertyInteger('SceneCount');
$sceneData = json_decode($this->ReadAttributeString('SceneData'), true);
for ($i = 1; $i <= $sceneCount; $i++) {
$selectTargets = [];
$dataNames = [];
$sceneGuids = [];

foreach ($targets as $key => $value) {
$this->SendDebug($key, print_r($value, true), 0);
$variableID = $value['VariableID'];
if (!IPS_VariableExists($variableID)) {
continue; // Maybe alternativ field
}
$targetValue = json_encode(array_key_exists($i - 1, $sceneData) && array_key_exists($value['GUID'], $sceneData[$i - 1]) ? $sceneData[$i - 1][$value['GUID']] : GetValue($variableID));
$selectTargets[] = [
'type' => 'SelectValue',
'name' => 'Scene' . $i . 'ID' . $variableID,
'caption' => IPS_GetName($variableID),
'value' => $targetValue,
'variableID' => $variableID
];
$dataNames[] = '$Scene' . $i . 'ID' . $variableID;
$sceneGuids[] = $value['GUID'];
}
$selectTargets[] = [
'type' => 'Button',
'caption' => $this->Translate('Save'),
'onClick' => 'SZS_SaveSceneEx($id, ' . $i . ', ' . json_encode($dataNames, JSON_UNESCAPED_SLASHES) . ', ' . json_encode($sceneGuids) . ');'
];
$actions[$i] = [
'type' => 'ExpansionPanel',
'name' => 'Scene' . $i,
'caption' => $this->getSceneName($i),
'items' => $selectTargets
];
}

$form['actions'] = $actions;
$this->SendDebug('actions', json_encode($actions), 0);
return json_encode($form);
}

public function SaveSceneEx(int $sceneNumber, array $sceneValues, array $sceneGuids): bool
{
$unsavedData = array_combine($sceneGuids, $sceneValues);
//fix datatype
foreach ($unsavedData as $guid => $value) {
$id = $this->getVariable($guid);
if (!IPS_VariableExists($id)) {
continue;
}
$type = IPS_GetVariable($id)['VariableType'];
$value = match ($type) {
0 => $value == 'true',
1 => intval($value),
2 => floatval($value),
3 => trim($value, '"'),
};
$unsavedData[$guid] = $value;
}
$sceneData = json_decode($this->ReadAttributeString('SceneData'), true);

// set the value in the correct scene
$sceneData[$sceneNumber - 1] = $unsavedData;
$this->WriteAttributeString('SceneData', json_encode($sceneData));
return true;
}

private function generateGUID()
{
return sprintf('{%04X%04X-%04X-%04X-%04X-%04X%04X%04X}', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"name": "SceneControl",
"url": "https://www.symcon.de",
"compatibility": {
"version": "5.0"
"version": "7.0"
},
"version": "1.4",
"version": "1.5",
"build": 0,
"date": 0
}
75 changes: 0 additions & 75 deletions tests/SzenenSteuerungMigrationTest.php

This file was deleted.