Skip to content

Commit

Permalink
Merge pull request #1 from symcon/Fixes/start-end-time-typos
Browse files Browse the repository at this point in the history
Fix für Typos und hinzufügen von Tests
  • Loading branch information
paresy committed Jun 9, 2023
2 parents fb4e2fd + 6ee21ab commit 147effd
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

Folgende Module beinhaltet das Verbrauch in Kategorie Repository:

- __Verbrauch in Kategorie__ ([Dokumentation](Verbrauch%20in%20Kategorie))
- __Verbrauch in Kategorie__ ([Dokumentation](VerbrauchInKategorie))
Zeigt den Verbrauch in Prozent in Kategorie.
6 changes: 4 additions & 2 deletions VerbrauchInKategorie/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Verbrauch in Kategorie
Berechnet den Verbrauch in Prozent pro freiwahlbarer Kategorie nach angegebener Start- und Endzeit.
Berechnet den Verbrauch in Prozent pro frei wählbarer Kategorie nach angegebener Start- und Enddatum. Die Berechnung basiert auf der täglichen Aggregation.

### Inhaltsverzeichnis

Expand All @@ -13,7 +13,7 @@ Berechnet den Verbrauch in Prozent pro freiwahlbarer Kategorie nach angegebener

### 1. Funktionsumfang

* Auswahl mehrer Variablen
* Auswahl mehrere Variablen
* Freitext für Kategorie

### 2. Voraussetzungen
Expand Down Expand Up @@ -46,6 +46,8 @@ Die Statusvariablen/Kategorien werden automatisch angelegt. Das Löschen einzeln
Name | Typ | Beschreibung
--------- | ------- | ------------
Kategorie | Float | Pro Kategorie wird eine Variable angelegt, in welchem der Prozentuale Verbrauch angezeigt wird.
Startzeit | Integer | Datum von wann die Berechnung starten soll
Endzeit | Integer | Datum bis wann die Berechnung gehen soll

### 7. PHP-Befehlsreferenz

Expand Down
19 changes: 15 additions & 4 deletions VerbrauchInKategorie/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
},
{
"type": "List",
"name": "Sources",
"caption": "Sources",
"name": "SourceVariables",
"caption": "Source variables",
"add": true,
"delete": true,
"columns": [
Expand All @@ -48,17 +48,28 @@
]
}
],
"actions": [],
"actions": [
{
"type": "Button",
"onClick": "Vik_CalculateConsumption($id);",
"caption": "Recalculate"
}
],
"status": [
{
"code": 200,
"icon": "error",
"caption": "A Category is missing"
"caption": "A category is missing"
},
{
"code": 201,
"icon": "error",
"caption": "A variable is invalid"
},
{
"code": 202,
"icon": "error",
"caption": "The start time ist greater then the end time"
}
]
}
12 changes: 7 additions & 5 deletions VerbrauchInKategorie/locale.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"translations": {
"de": {
"Start time": "Startzeit",
"End time": "Endzeit",
"Start Time": "Startzeit",
"End Time": "Endzeit",
"Enable cyclic calculation": "Zyklische Berechnung aktivieren",
"Source": "Quelle",
"Source variables": "Quellevariablen",
"Source variable": "Quellvariable",
"Category": "Kategorie",
"A Category is missing": "Eine Kategorie fehlt",
"A category is missing": "Eine Kategorie fehlt",
"A variable is invalid": "Eine Variable ist nicht valide",
"Minutes": "Minuten"
"Minutes": "Minuten",
"The start time ist greater then the end time": "Die Startzeit ist größer als die Endzeit",
"Recalculate": "Neu berechnen"
}
}
}
66 changes: 57 additions & 9 deletions VerbrauchInKategorie/module.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
<?php

declare(strict_types=1);
//include_once __DIR__ . '/timetest.php';
include_once __DIR__ . '/timetest.php';
class VerbrauchInKategorie extends IPSModule
{
//use TestTime;
use TestTime;

public function Create()
{
//Never delete this line!
parent::Create();

$this->RegisterVariableInteger('StartTime', $this->Translate('Start Time'), '~UnixTimestamp', 0);
$this->RegisterVariableInteger('EndTime', $this->Translate('End Time'), '~UnixTimestamp', 1);
//Register Variables
$this->RegisterVariableInteger('StartTime', $this->Translate('Start Time'), '~UnixTimestampDate', 100);
$this->EnableAction('StartTime');
$this->RegisterVariableInteger('EndTime', $this->Translate('End Time'), '~UnixTimestampDate', 101);
$this->EnableAction('EndTime');

$this->RegisterPropertyString('Sources', '[]');
//Register Properties
$this->RegisterPropertyString('SourceVariables', '[]');
$this->RegisterPropertyBoolean('CheckIntervall', false);
$this->RegisterPropertyInteger('Intervall', 0);

//For compatibility check if the ProgressProfile exist
if (!IPS_VariableProfileExists('~Progress')) {
IPS_CreateVariableProfile('~Progress', VARIABLETYPE_FLOAT);
IPS_SetVariableProfileValues('~Progress', 0, 100, 0.1);
IPS_SetVariableProfileDigits('~Progress', 1);
IPS_SetVariableProfileText('~Progress', '', ' %');
}

$this->RegisterTimer('UpdateCalculation', 0, 'VIK_CalculateConsumption($_IPS[\'TARGET\']);');

//set an initial time
$this->SetValue('StartTime', strtotime('yesterday'));
$this->SetValue('EndTime', $this->getTime());
}

public function Destroy()
Expand All @@ -32,7 +48,7 @@ public function ApplyChanges()
//Never delete this line!
parent::ApplyChanges();

$source = json_decode($this->ReadPropertyString('Sources'), true);
$source = json_decode($this->ReadPropertyString('SourceVariables'), true);
$currentCategories = array_diff(IPS_GetChildrenIDs($this->InstanceID), [$this->GetIDForIdent('StartTime'), $this->GetIDForIdent('EndTime')]);
//change IDs to Idents
foreach ($currentCategories as $key => $category) {
Expand Down Expand Up @@ -62,6 +78,20 @@ public function ApplyChanges()
$this->CalculateConsumption();
}

public function RequestAction($Ident, $Value)
{
switch ($Ident) {
case 'StartTime':
case 'EndTime':
$this->SetValue($Ident, $Value);
$this->CalculateConsumption();
break;
default:
$this->SendDebug($Ident, 'You try to set an automatic variable', 0);
break;
}
}

public function GetConfigurationForm()
{
$form = json_decode(file_get_contents(__DIR__ . '/form.json'), true);
Expand All @@ -77,20 +107,38 @@ public function UIVisible(bool $value)
public function CalculateConsumption()
{
$archiveID = IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
$sources = json_decode($this->ReadPropertyString('Sources'), true);
$sources = json_decode($this->ReadPropertyString('SourceVariables'), true);

//Validate that the startTime is lower than endTime
$startTime = $this->GetValue('StartTime');
$endTime = $this->GetValue('EndTime');
if ($endTime > 0 && $endTime < $startTime) {
$this->SetStatus(202);
return;
} else {
$this->SetStatus(102);
}

//Get Values
foreach ($sources as $key => $source) {
if (IPS_VariableExists($source['SourceVariable'])) {
$loggedValue = AC_GetLoggedValues($archiveID, $source['SourceVariable'], $this->GetValue('StartTime'), $this->GetValue('EndTime'), 0);
$sources[$key]['Value'] = array_sum(array_column($loggedValue, 'Value'));
$loggedValue = AC_GetAggregatedValues($archiveID, $source['SourceVariable'], 1 /*Daily*/, $startTime, $endTime, 0);
$sources[$key]['Value'] = array_sum(array_column($loggedValue, 'Avg'));

//Debugs
//$this->SendDebug('Aggregated Values of ' . $source['SourceVariable'], print_r($loggedValue, true), 0);
$this->SendDebug('Sum of ' . $source['SourceVariable'], '' . $sources[$key]['Value'], 0);
} else {
$this->SetStatus(201);
return;
}
}
$this->SetStatus(102);
$totalConsumption = array_sum(array_column($sources, 'Value'));

//Debugs
$this->SendDebug('Total Consumption', '' . $totalConsumption, 0);

//Get Values per Category
$categories = [];
foreach ($sources as $key => $source) {
Expand Down
28 changes: 28 additions & 0 deletions VerbrauchInKategorie/timetest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

if (defined('PHPUNIT_TESTSUITE')) {
trait TestTime
{
private $currentTime = 989884800; //May 15 2001

public function setTime(int $Time)
{
$this->currentTime = $Time;
}

protected function getTime()
{
return $this->currentTime;
}
}
} else {
trait TestTime
{
protected function getTime()
{
return time();
}
}
}
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"author": "Symcon GmbH",
"url": "https://github.com/symcon/VerbrauchInKategorie",
"compatibility": {
"version": "6.0"
"version": "6.4"
},
"version": "1.0",
"version": "1.1",
"build": 0,
"date": 0
}
43 changes: 43 additions & 0 deletions tests/TestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

define('VAR_BOOL', 0);
define('VAR_INT', 1);
define('VAR_FLOAT', 2);
define('VAR_STRING', 3);

include_once __DIR__ . '/stubs/GlobalStubs.php';
include_once __DIR__ . '/stubs/KernelStubs.php';
include_once __DIR__ . '/stubs/ModuleStubs.php';
include_once __DIR__ . '/stubs/MessageStubs.php';
include_once __DIR__ . '/stubs/ConstantStubs.php';

use PHPUnit\Framework\TestCase;

class TestBase extends TestCase
{
protected $archiveControlID;
protected $categoryInstanceID;

protected function setUp(): void
{
//Reset
IPS\Kernel::reset();

//Register our core stubs for testing
IPS\ModuleLoader::loadLibrary(__DIR__ . '/stubs/CoreStubs/library.json');

//Register our library we need for testing
IPS\ModuleLoader::loadLibrary(__DIR__ . '/../library.json');

//Register required profiles
if (!IPS_VariableProfileExists('~UnixTimestampDate')) {
IPS_CreateVariableProfile('~UnixTimestampDate', VARIABLETYPE_INTEGER);
}
$this->archiveControlID = IPS_CreateInstance('{43192F0B-135B-4CE7-A0A7-1475603F3060}');
$this->categoryInstanceID = IPS_CreateInstance('{51CA1560-4725-A7F2-A449-94B7812E1986}');

parent::setUp();
}
}
Loading

0 comments on commit 147effd

Please sign in to comment.