Files
Energiemanager_Symconmodule…/CC100_HW/module.php

125 lines
4.8 KiB
PHP

<?php
class CC100_HW extends IPSModule
{
private $DigOutPID = '/sys/kernel/dout_drv/DOUT_DATA';
private $PT1PID = '/sys/bus/iio/devices/iio:device2/in_voltage13_raw';
private $PT2PID = '/sys/bus/iio/devices/iio:device2/in_voltage1_raw';
private $waitingTime = 1; // Waiting time (in ms) for a new attempt to write a digital output (if file is blocked)
private $maxTime = 5000; // Max time (in ms) for the writing process
public function Create()
{
parent::Create();
$this->RegisterPropertyString('FilePath', $this->DigOutPID);
$this->RegisterVariableBoolean('Bit1', 'DO 1', '~Switch', 1);
$this->RegisterVariableBoolean('Bit2', 'DO 2', '~Switch', 2);
$this->RegisterVariableBoolean('Bit3', 'DO 3', '~Switch', 3);
$this->RegisterVariableBoolean('Bit4', 'DO 4', '~Switch', 4);
$this->RegisterVariableFloat('PT1', 'PT1 Temperatur', '~Temperature', 5);
$this->RegisterVariableFloat('PT2', 'PT2 Temperatur', '~Temperature', 6);
$this->EnableAction('Bit1');
$this->EnableAction('Bit2');
$this->EnableAction('Bit3');
$this->EnableAction('Bit4');
// Timer für PT1 und PT2 einrichten
$this->RegisterTimer("ReadPTValues", 30000, 'IPS_RequestAction(' . $this->InstanceID . ', "DOUT_ReadPTValues", "");');
$this->RegisterTimer("WriteBits", 1000, 'IPS_RequestAction(' . $this->InstanceID . ', "DOUT_ReadPTValues", "");');
}
public function ApplyChanges()
{
parent::ApplyChanges();
// Timer aktivieren
$this->SetTimerInterval('ReadPTValues', 30000);
}
public function RequestAction($Ident, $Value) {
switch ($Ident) {
case "DOUT_ReadPTValues":
$this->DOUT_ReadPTValues();
break;
case "UpdateFile":
$this->UpdateFile();
break;
default:
throw new Exception("Invalid Ident");
}
}
private function UpdateFile()
{
$starttime = microtime(true) * 1000;
$this->TryWriteFile($starttime);
}
private function TryWriteFile($starttime)
{
$currentTime = microtime(true) * 1000;
if (($currentTime - $starttime) <= $this->maxTime) {
$file = $this->ReadPropertyString('FilePath');
if (is_writable($file)) {
$bit1 = GetValueBoolean($this->GetIDForIdent('Bit1'));
$bit2 = GetValueBoolean($this->GetIDForIdent('Bit2'));
$bit3 = GetValueBoolean($this->GetIDForIdent('Bit3'));
$bit4 = GetValueBoolean($this->GetIDForIdent('Bit4'));
$value = ($bit4 ? 8 : 0) + ($bit3 ? 4 : 0) + ($bit2 ? 2 : 0) + ($bit1 ? 1 : 0);
if (@file_put_contents($file, $value) === false) {
IPS_LogMessage("DOUTModule", "Fehler: Datei $file konnte nicht beschrieben werden. Möglicherweise gesperrt.");
usleep($this->waitingTime * 1000);
$this->TryWriteFile($starttime);
} else {
IPS_LogMessage("DOUTModule", "Wert $value erfolgreich in die Datei $file geschrieben.");
}
} else {
IPS_LogMessage("DOUTModule", "Fehler: Datei $file ist nicht schreibbar.");
usleep($this->waitingTime * 1000);
$this->TryWriteFile($starttime);
}
} else {
IPS_LogMessage("DOUTModule", "Fehler: Schreibvorgang für Datei $file hat zu lange gedauert.");
}
}
public function DOUT_ReadPTValues()
{
$pt1Value = $this->ReadPTValue($this->PT1PID);
$pt2Value = $this->ReadPTValue($this->PT2PID);
SetValue($this->GetIDForIdent('PT1'), $pt1Value);
SetValue($this->GetIDForIdent('PT2'), $pt2Value);
}
private function ReadPTValue($file)
{
if (file_exists($file)) {
$data = @file_get_contents($file);
if ($data !== false) {
$data = intval($data);
$scale = 0;
if ($data >= 600 && $data < 3600) { $scale = 37;}
else if ($data >= 3600 && $data < 6700) { $scale = 43;}
else if ($data >= 6700 && $data < 9750) { $scale = 45;}
else if ($data >= 9750 && $data < 12740) { $scale = 49;}
else if ($data >= 12740 && $data < 15700) { $scale = 50.6;}
else if ($data >= 15700 && $data < 21000) { $scale = 52.4;}
else if ($data > 21000) { $scale = 53.7;}
$numb = $data / $scale - 200;
return round($numb, 1);
} else {
IPS_LogMessage("DOUTModule", "Fehler: Datei $file konnte nicht gelesen werden.");
}
} else {
IPS_LogMessage("DOUTModule", "Fehler: Datei $file existiert nicht.");
}
return 0;
}
}
?>