Skip to content

Commit

Permalink
Refactor to use buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunnyka98 committed Aug 20, 2024
1 parent 9abf51f commit 797ce87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
58 changes: 29 additions & 29 deletions PhoneChain/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public function ApplyChanges()
$phoneNumbers = json_decode($this->ReadPropertyString('PhoneNumbers'), true);
//Create the variable
foreach ($phoneNumbers as $key => $number) {
$ident = array_key_exists('VariableIdent', $number) ? $number['VariableIdent'] : 'Position_' . $key + 1;
$ident = array_key_exists('VariableIdent', $number) ? $number['VariableIdent'] : 'PhoneNumber_' . $key;
if (!@$this->GetIDForIdent($ident)) {
$this->MaintainVariable($ident, array_key_exists('Description', $number) && $number['Description'] !== '' ? $number['Description'] : 'Phone Number' . $key + 1, 0, '', $key, true);
$this->EnableAction($ident);
$id = $this->GetIDForIdent($ident);
$this->RegisterReference($id);
$this->RegisterMessage($id, OM_CHANGEDISABLED);
$needsReload = true;
}
$phoneNumbers[$key]['VariableIdent'] = $ident;
Expand All @@ -92,9 +93,6 @@ public function ApplyChanges()

//Delete unnecessary variables
$idents = array_column($phoneNumbers, 'VariableIdent');
$this->SendDebug('numbers', print_r($phoneNumbers, true), 0);
$this->SendDebug('idents', print_r($idents, true), 0);

array_push(
$idents,
'ConfirmNumber',
Expand All @@ -104,10 +102,12 @@ public function ApplyChanges()
foreach (IPS_GetChildrenIDs($this->InstanceID) as $childID) {
if (!in_array($this->GetIdentByID($childID), $idents)) {
$this->UnregisterReference($childID);
$this->UnregisterMessage($childID, OM_CHANGEDISABLED);
$this->UnregisterVariable($this->GetIdentByID($childID));
}
}
}
$this->updateBuffer();

$this->setErrorState();
if ($this->GetStatus() != 102) {
Expand All @@ -133,11 +133,11 @@ public function MessageSink($TimeStamp, $SenderID, $MessageID, $Data)
switch ($MessageID) {
case VM_UPDATE:
if ($Data[0] && ($this->GetStatus() == 102) && ($this->GetValue('Status') == self::WAITING)) {
$this->updateBuffer();
$this->SetTimerInterval('UpdateCall', 1000);
$this->UpdateCalls();
}
break;

case self::VOIP_EVENT:
//Only handle messages for our active calls
if (!array_key_exists($Data[0], json_decode($this->GetBuffer('ActiveCalls'), true))) {
Expand Down Expand Up @@ -234,32 +234,10 @@ public function UpdateCalls()
}

//If maxSyncCalls not reached and not at the end of the number list
$phoneNumbers = json_decode($this->ReadPropertyString('PhoneNumbers'), true);
$phoneNumbers = json_decode($this->GetBuffer('CurrentChain'), true);
$listPosition = json_decode($this->GetBuffer('ListPosition'));

//Lookup if there is a next phone number
$hasNext = false;
if ($this->ReadPropertyBoolean('EditableVisu')) {
$this->SendDebug('Edit', 'visu', 0);
while (($listPosition < count($phoneNumbers) && !$hasNext)) {
if (
@$this->GetIDForIdent('Position_' . $listPosition)
&& !IPS_GetObject($this->GetIDForIdent('Position_' . $listPosition))['ObjectIsDisabled']
&& GetValue($this->GetIDForIdent('Position_' . $listPosition))
) {
$this->SendDebug('Has Next', 'changed', 0);
$hasNext = true;
break;
}

$listPosition++;
}
}else {
$hasNext = ($listPosition < count($phoneNumbers));
}
//if there is a next pos -> get the correct position
$this->SendDebug('Has Next', $hasNext ? 'true' : 'false', 0);
if ((count($activeCalls) < $this->ReadPropertyInteger('MaxSyncCallCount')) && $hasNext) {
if ((count($activeCalls) < $this->ReadPropertyInteger('MaxSyncCallCount')) && ($listPosition < count($phoneNumbers))) {
$call = VoIP_Connect($this->ReadPropertyInteger('VoIP'), $phoneNumbers[$listPosition]['PhoneNumber']);
$this->SetValue('Status', $listPosition + 1);
$this->SendDebug('New Call', json_encode($call), 0);
Expand Down Expand Up @@ -380,4 +358,26 @@ private function GetIdentByID(int $id): string
{
return IPS_GetObject($id)['ObjectIdent'];
}

private function updateBuffer(): void
{
$chain = [];
$newPosition = 0;
$numbers = json_decode($this->ReadPropertyString('PhoneNumbers'), true);
if (!$this->ReadPropertyBoolean('EditableVisu')) {
$this->setBuffer('CurrentChain', json_encode($numbers));
return;
}
foreach ($numbers as $position => $number) {

$ident = array_key_exists('VariableIdent', $number) ? $number['VariableIdent'] : 'PhoneNumber_' . $position;
$id = $this->GetIDForIdent($ident);
if (!IPS_GetObject($id)['ObjectIsDisabled'] && $this->GetValue($ident)) {
$chain[$newPosition] = $number;
$newPosition++;
}

}
$this->SetBuffer('CurrentChain', json_encode($chain));
}
}
22 changes: 12 additions & 10 deletions tests/TelefonketteVisuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testCallNoAnswer()
//Call until one number moves up
$instanceID = $this->TelefonketteID;
$this->setConfiguration();

RequestAction($this->TriggerID, true);
TK_UpdateCalls($instanceID);
TK_setTime($instanceID, strtotime('September 1 2020 13:00:10'));
TK_UpdateCalls($instanceID);
Expand Down Expand Up @@ -243,22 +243,22 @@ private function setConfiguration()
[
[
'PhoneNumber' => '111111',
'VariableIdent' => 'Position_0'
'VariableIdent' => 'PhoneNumber_0'
],
[
'PhoneNumber' => '222222',
'VariableIdent' => 'Position_1'
'VariableIdent' => 'PhoneNumber_1'
],
[
'PhoneNumber' => '333333',
'VariableIdent' => 'Position_2'
'VariableIdent' => 'PhoneNumber_2'
], [
'PhoneNumber' => '444444',
'VariableIdent' => 'Position_3'
'VariableIdent' => 'PhoneNumber_3'
],
[
'PhoneNumber' => '555555',
'VariableIdent' => 'Position_4'
'VariableIdent' => 'PhoneNumber_4'
]
]
),
Expand All @@ -270,14 +270,16 @@ private function setConfiguration()
IPS_SetConfiguration($instanceID, $configuration);
IPS_ApplyChanges($instanceID);
//set the variable (Pos 0 and 3 should skipped)
$id = IPS_GetObjectIDByIdent('Position_1', $instanceID);
$id = IPS_GetObjectIDByIdent('PhoneNumber_1', $instanceID);
SetValue($id, true);
$id = IPS_GetObjectIDByIdent('Position_2', $instanceID);
$id = IPS_GetObjectIDByIdent('PhoneNumber_2', $instanceID);
SetValue($id, true);
$id = IPS_GetObjectIDByIdent('Position_3', $instanceID);
$id = IPS_GetObjectIDByIdent('PhoneNumber_3', $instanceID);
IPS_SetDisabled($id, true);
$id = IPS_GetObjectIDByIdent('Position_4', $instanceID);
$id = IPS_GetObjectIDByIdent('PhoneNumber_4', $instanceID);
SetValue($id, true);

IPS_ApplyChanges($instanceID); //Don't know how to provoke the message sink, so make sure the buffer is correct
$status = IPS\InstanceManager::getInstance($instanceID)['InstanceStatus'];
$this->assertEquals(102, $status);

Expand Down

0 comments on commit 797ce87

Please sign in to comment.