RegisterPropertyInteger("Peakleistung", 0); $this->RegisterPropertyInteger("Ueberschussleistung", 0); $this->RegisterPropertyInteger("Netzbezug", 0); // Initialisierung mit 0 $this->RegisterPropertyString("Verbraucher_Liste", "[]"); // 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 WebSocketHandler($this), ['*']); $server->run(); } public function AddClient($conn) { $this->clients[] = $conn; $this->UpdateClientList(); } public function RemoveClient($conn) { $index = array_search($conn, $this->clients); if ($index !== false) { unset($this->clients[$index]); $this->clients = array_values($this->clients); // Reindizieren des Arrays $this->UpdateClientList(); } } private function UpdateClientList() { $clientList = []; foreach ($this->clients as $client) { $clientList[] = ['Client' => (string)$client->resourceId]; } $this->UpdateFormField('ClientList', 'values', json_encode($clientList)); } } class WebSocketHandler 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 } } ?>