This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
module.php
74 lines (54 loc) · 2.18 KB
/
module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?
class EnergiezaehlerImpuls extends IPSModule
{
public function Create()
{
//Never delete this line!
parent::Create();
$this->RegisterPropertyInteger("SourceVariable", 0);
$this->RegisterPropertyInteger("Interval", 300);
$this->RegisterPropertyInteger("Impulses", 1000);
$this->RegisterTimer("UpdateTimer", 0, 'EZI_Update($_IPS[\'TARGET\']);');
$this->RegisterVariableFloat("Current", "Current", "Watt.3680", 0);
$this->RegisterVariableFloat("Counter", "Counter", "Electricity", 1);
$this->RegisterVariableFloat("LastSourceValue", "Last Value (Temporary)", "", 2);
}
public function ApplyChanges()
{
//Never delete this line!
parent::ApplyChanges();
//Reset source value
if(IPS_VariableExists($this->ReadPropertyInteger("SourceVariable"))) {
SetValue($this->GetIDForIdent("LastSourceValue"), GetValue($this->ReadPropertyInteger("SourceVariable")));
}
$this->SetTimerInterval("UpdateTimer", $this->ReadPropertyInteger("Interval")*1000);
//Always hide this variable
IPS_SetHidden($this->GetIDForIdent("LastSourceValue"), true);
}
/**
* This function will be available automatically after the module is imported with the module control.
* Using the custom prefix this function will be callable from PHP and JSON-RPC through:
*
* EZI_Update($id);
*
*/
public function Update()
{
if(IPS_VariableExists($this->ReadPropertyInteger("SourceVariable"))) {
$value = GetValue($this->ReadPropertyInteger("SourceVariable"));
//we only count positive deltas
$diff = max(0, $value - GetValue($this->GetIDForIdent("LastSourceValue")));
//add to our counter
SetValue($this->GetIDForIdent("Counter"), GetValue($this->GetIDForIdent("Counter"))+($diff/$this->ReadPropertyInteger("Impulses")));
//calculate consumption
if($diff == 0) {
SetValue($this->GetIDForIdent("Current"), 0);
} else {
SetValue($this->GetIDForIdent("Current"), ($diff/$this->ReadPropertyInteger("Interval"))*3600/($this->ReadPropertyInteger("Impulses")/1000));
}
//update last source value
SetValue($this->GetIDForIdent("LastSourceValue"), $value);
}
}
}
?>