no message

This commit is contained in:
dh
2025-06-18 10:42:34 +02:00
parent 2503bb5628
commit 09e6b80ede
2 changed files with 182 additions and 117 deletions
+65
View File
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<style>
.bar-container {
width: 100%;
background-color: #eee;
border-radius: 5px;
overflow: hidden;
height: 20px;
margin-bottom: 5px;
}
.bar {
height: 100%;
float: left;
}
.bar-cons { background-color: #4CAF50; }
.bar-feed { background-color: #8BC34A; }
.bar-pv { background-color: #FF9800; }
.bar-grid { background-color: #FF5722; }
.value-label {
font-size: 0.9em;
margin: 4px 0;
}
</style>
</head>
<body>
<div class="value-label" id="prodLabel"></div>
<div class="bar-container">
<div class="bar bar-cons" id="barCons"></div>
<div class="bar bar-feed" id="barFeed"></div>
</div>
<div class="value-label" id="consLabel"></div>
<div class="bar-container">
<div class="bar bar-pv" id="barPV"></div>
<div class="bar bar-grid" id="barGrid"></div>
</div>
<script>
// Daten in die Balken und Beschriftungen einfüllen
function Apply(data) {
document.getElementById('prodLabel').innerText =
"Produktion: " + data.value.prod + " kWh";
document.getElementById('barCons').style.width = data.prodCons + "%";
document.getElementById('barFeed').style.width = data.prodFeed + "%";
document.getElementById('consLabel').innerText =
"Verbrauch: " + data.value.cons + " kWh";
document.getElementById('barPV').style.width = data.consPV + "%";
document.getElementById('barGrid').style.width = data.consGrid + "%";
}
// HTML-SDK: hier kommen die Nachrichten vom Modul an
function handleMessage(msg) {
if (msg) {
Apply(msg);
}
}
// beim Laden gleich initial Daten anfordern
requestAction('update', true);
</script>
</body>
</html>
+110 -110
View File
@@ -1,126 +1,126 @@
<?php <?php declare(strict_types=1);
class Belevo_Bezahl_Modul extends IPSModule
class Belevo_PV_Visu extends IPSModule
{ {
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
$this->RegisterPropertyInteger("Reservate", 0); // vier Properties, in denen später die vier Zähler-Variablen gewählt werden
$this->RegisterPropertyInteger("GetAmount", 0); $this->RegisterPropertyInteger('VarProduction', 0);
$this->RegisterPropertyInteger("HTMLBox", 0); $this->RegisterPropertyInteger('VarConsumption', 0);
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0); $this->RegisterPropertyInteger('VarFeedIn', 0);
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false); $this->RegisterPropertyInteger('VarGrid', 0);
// HTML-SDK aktivieren
$this->SetVisualizationType(3);
} }
public function ApplyChanges() public function GetConfigurationForm(): string
{ {
parent::ApplyChanges(); return json_encode([
$this->SetHTMLContent(); 'elements' => [
} [
'type' => 'SelectVariable',
private function SetHTMLContent() 'name' => 'VarProduction',
{ 'caption' => 'Produktion (kWh)'
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel ],
[
$html = "<html lang='en'> 'type' => 'SelectVariable',
<head> 'name' => 'VarConsumption',
<meta charset='UTF-8'> 'caption' => 'Verbrauch (kWh)'
<meta name='viewport' content='width=device-width, initial-scale=1.0'> ],
<title>Google Pay mit Stripe</title> [
<!-- Stripe.js --> 'type' => 'SelectVariable',
<script src='https://js.stripe.com/v3/'></script> 'name' => 'VarFeedIn',
<!-- Google Pay API --> 'caption' => 'Einspeisung (kWh)'
<script src='https://pay.google.com/gp/p/js/pay.js' async></script> ],
</head> [
<body> 'type' => 'SelectVariable',
<h1>Google Pay Integration mit Stripe</h1> 'name' => 'VarGrid',
<button id='google-pay-button'>Mit Google Pay bezahlen</button> 'caption' => 'Bezug Netz (kWh)'
]
<script>
// Stripe initialisieren
const stripe = Stripe('pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm'); // Ersetze mit deinem Stripe-Publishable-Key
const googlePayButton = document.getElementById('google-pay-button');
// Google Pay-Konfiguration
const googlePayConfig = {
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['VISA', 'MASTERCARD'],
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'stripe',
'stripe:version': '2020-08-27',
'stripe:publishableKey': 'pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm', // Ersetze hier ebenfalls
},
},
},
], ],
merchantInfo: { 'actions' => []
merchantId: 'TEST', // Für Testumgebungen ]);
merchantName: 'Dein Unternehmen', }
},
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPrice: '100.00', // Betrag in CHF
currencyCode: 'CHF',
},
};
// Eventlistener für den Google Pay-Button /**
googlePayButton.addEventListener('click', async () => { * Wird automatisch aufgerufen, wenn die Visu die Tile anfordert
try { */
const googlePayClient = new google.payments.api.PaymentsClient({ environment: 'TEST' }); public function GetVisualizationTile(int $InstanceID): string
{
$file = __DIR__ . '/module.html';
if (!file_exists($file)) {
return '';
}
// Übersetzungen einbinden
return $this->Translate(file_get_contents($file));
}
// Prüfen, ob Google Pay verfügbar ist /**
const isReadyToPay = await googlePayClient.isReadyToPay(googlePayConfig); * JS-requestAction ruft das auf, um frische Daten anzufordern
if (!isReadyToPay.result) { */
alert('Google Pay ist auf diesem Gerät nicht verfügbar.'); public function RequestAction(string $Ident, $Value): void
return; {
} if ($Ident === 'update') {
$this->Update();
} else {
throw new Exception('Unknown Ident');
}
}
// Google Pay PaymentData abrufen /**
const paymentData = await googlePayClient.loadPaymentData(googlePayConfig); * Berechnet die Tages-Summen und sendet sie an die Visu
*/
private function Update(): void
{
$start = strtotime('today 00:00');
$end = time();
$prod = $this->GetDailyTotal($this->ReadPropertyInteger('VarProduction'), $start, $end);
$cons = $this->GetDailyTotal($this->ReadPropertyInteger('VarConsumption'), $start, $end);
$feed = $this->GetDailyTotal($this->ReadPropertyInteger('VarFeedIn'), $start, $end);
$grid = $this->GetDailyTotal($this->ReadPropertyInteger('VarGrid'), $start, $end);
// Token aus dem PaymentData extrahieren // Quoten berechnen
const paymentToken = paymentData.paymentMethodData.tokenizationData.token; $prodCons = $prod > 0 ? ($cons / $prod) * 100 : 0;
$prodFeed = $prod > 0 ? ($feed / $prod) * 100 : 0;
$consPV = $cons > 0 ? (min($cons, $prod) / $cons) * 100 : 0;
$consGrid = $cons > 0 ? ($grid / $cons) * 100 : 0;
// Token an das Backend senden $data = [
const response = await fetch('/path-to-your-backend.php', { 'prodCons' => round($prodCons, 2),
method: 'POST', 'prodFeed' => round($prodFeed, 2),
headers: { 'Content-Type': 'application/json' }, 'consPV' => round($consPV, 2),
body: JSON.stringify({ token: paymentToken }), 'consGrid' => round($consGrid, 2),
}); 'value' => [
'prod' => round($prod, 2),
'cons' => round($cons, 2),
'feed' => round($feed, 2),
'grid' => round($grid, 2)
]
];
$this->UpdateVisualizationTile($data);
}
const result = await response.json(); /**
* Ruft den Tages-Gesamtwert aus dem Archiv ab (tägliche Aggregation)
if (result.clientSecret) { */
// Zahlung mit Stripe abschließen private function GetDailyTotal(int $varID, int $start, int $end): float
const stripeResult = await stripe.confirmCardPayment(result.clientSecret); {
if ($varID <= 0) {
if (stripeResult.error) { return 0.0;
alert('Zahlung fehlgeschlagen: ' + stripeResult.error.message); }
} else if (stripeResult.paymentIntent && stripeResult.paymentIntent.status === 'succeeded') { // Erste Archive-Instanz finden
alert('Zahlung erfolgreich abgeschlossen!'); $archives = IPS_GetInstanceListByModuleID('{43192F11-5B02-4B5D-9B53-8B4DBD4769E9}');
} if (empty($archives)) {
} else { return 0.0;
alert('Ein Fehler ist im Backend aufgetreten.'); }
} $archiveID = $archives[0];
} catch (error) { // AC_GetAggregatedValues( InstanzID, VarID, AggregationLevel(1=Täglich), Start, Ende, Limit=1 )
console.error('Fehler bei der Zahlung:', error); $values = AC_GetAggregatedValues($archiveID, $varID, 1, $start, $end, 1);
alert('Ein Fehler ist aufgetreten: ' + error.message); if (empty($values)) {
} return 0.0;
}); }
</script> // Bei Counter-Variablen enthält 'Avg' die Summe der Deltas
</body> return (float)$values[0]['Avg'];
</html>";
SetValue($this->ReadPropertyInteger("HTMLBox"), $html);
} }
} }