126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
<?php
|
|
|
|
require __DIR__ . '/vendor/autoload.php'; // Autoload-Datei einbinden
|
|
|
|
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 \Ratchet\App('localhost', 8080);
|
|
$server->route('/ws', new class($this) implements \Ratchet\MessageComponentInterface {
|
|
private $module;
|
|
|
|
public function __construct($module)
|
|
{
|
|
$this->module = $module;
|
|
}
|
|
|
|
public function onOpen(\Ratchet\ConnectionInterface $conn)
|
|
{
|
|
$this->module->AddClient($conn);
|
|
}
|
|
|
|
public function onClose(\Ratchet\ConnectionInterface $conn)
|
|
{
|
|
$this->module->RemoveClient($conn);
|
|
}
|
|
|
|
public function onError(\Ratchet\ConnectionInterface $conn, \Exception $e)
|
|
{
|
|
$conn->close();
|
|
}
|
|
|
|
public function onMessage(\Ratchet\ConnectionInterface $from, $msg)
|
|
{
|
|
// Nachrichtenverarbeitung implementieren
|
|
}
|
|
}, ['*']);
|
|
$server->run();
|
|
}
|
|
|
|
public function AddClient($conn)
|
|
{
|
|
$clients = json_decode($this->GetValue("Clients"), true);
|
|
if ($clients === null) {
|
|
$clients = [];
|
|
}
|
|
$clients[] = (string)$conn->resourceId;
|
|
$this->SetValue("Clients", json_encode($clients));
|
|
$this->UpdateClientList();
|
|
}
|
|
|
|
public function RemoveClient($conn)
|
|
{
|
|
$clients = json_decode($this->GetValue("Clients"), true);
|
|
if ($clients !== null) {
|
|
$index = array_search((string)$conn->resourceId, $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));
|
|
}
|
|
}
|
|
?>
|