110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
|
|
class HauptManager extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Systemvariablen registrieren
|
|
$this->RegisterPropertyInteger("Peakleistung", 0);
|
|
$this->RegisterPropertyInteger("Ueberschussleistung", 0);
|
|
$this->RegisterPropertyInteger("Netzbezug", 0); // Initialisierung mit 0
|
|
$this->RegisterPropertyString("Verbraucher_Liste", "[]");
|
|
$this->RegisterVariableString("Clients", "Clients", "", 0);
|
|
|
|
// Timer registrieren
|
|
$this->RegisterTimer("Timer_DistributeEnergy", 5000, "IPS_RequestAction(" .$this->InstanceID .', "DistributeEnergy", "");');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
// Liste aller Verbraucher einlesen
|
|
$Verbraucher_Liste = $this->ReadPropertyString("Verbraucher_Liste");
|
|
|
|
// WebSocket-Server starten
|
|
$this->StartWebSocketServer();
|
|
|
|
// Initialisiere die Client-Liste im Formular
|
|
$this->UpdateClientList();
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
switch ($Ident) {
|
|
case "DistributeEnergy":
|
|
$this->DistributeEnergy();
|
|
break;
|
|
case "ApplyChanges":
|
|
$this->ApplyChanges();
|
|
break;
|
|
default:
|
|
throw new Exception("Invalid Ident");
|
|
}
|
|
}
|
|
|
|
public function DistributeEnergy()
|
|
{
|
|
// Energieverteilung implementieren
|
|
}
|
|
|
|
private function StartWebSocketServer()
|
|
{
|
|
$server = new PHPWebSocket();
|
|
$server->bind('message', function($clientID, $message, $messageLength, $binary) {
|
|
$this->onMessage($clientID, $message);
|
|
});
|
|
$server->bind('open', function($clientID) {
|
|
$this->AddClient($clientID);
|
|
});
|
|
$server->bind('close', function($clientID) {
|
|
$this->RemoveClient($clientID);
|
|
});
|
|
$server->startServer('localhost', 8080);
|
|
}
|
|
|
|
public function AddClient($clientID)
|
|
{
|
|
$clients = json_decode($this->GetValue("Clients"), true);
|
|
if ($clients === null) {
|
|
$clients = [];
|
|
}
|
|
$clients[] = (string)$clientID;
|
|
$this->SetValue("Clients", json_encode($clients));
|
|
$this->UpdateClientList();
|
|
}
|
|
|
|
public function RemoveClient($clientID)
|
|
{
|
|
$clients = json_decode($this->GetValue("Clients"), true);
|
|
if ($clients !== null) {
|
|
$index = array_search((string)$clientID, $clients);
|
|
if ($index !== false) {
|
|
unset($clients[$index]);
|
|
$clients = array_values($clients); // Reindizieren des Arrays
|
|
$this->SetValue("Clients", json_encode($clients));
|
|
$this->UpdateClientList();
|
|
}
|
|
}
|
|
}
|
|
|
|
private function UpdateClientList()
|
|
{
|
|
$clients = json_decode($this->GetValue("Clients"), true);
|
|
if ($clients === null) {
|
|
$clients = [];
|
|
}
|
|
$clientList = [];
|
|
foreach ($clients as $client) {
|
|
$clientList[] = ['Client' => $client];
|
|
}
|
|
$this->UpdateFormField('ClientList', 'values', json_encode($clientList));
|
|
}
|
|
|
|
public function onMessage($clientID, $message)
|
|
{
|
|
// Nachrichtenverarbeitung implementieren
|
|
}
|
|
}
|
|
?>
|