From 9b90c04d18644addbb079b4157f3d7878f4bdebe Mon Sep 17 00:00:00 2001 From: Morg42 <43153739+Morg42@users.noreply.github.com> Date: Sun, 15 Dec 2024 16:37:54 +0100 Subject: [PATCH] smartmeter: update webif --- smartmeter/webif/__init__.py | 41 ++++++++--- smartmeter/webif/index.html | 129 +++++++++++++++++++++++++++++------ 2 files changed, 139 insertions(+), 31 deletions(-) diff --git a/smartmeter/webif/__init__.py b/smartmeter/webif/__init__.py index c530db5b1..1a39ac774 100644 --- a/smartmeter/webif/__init__.py +++ b/smartmeter/webif/__init__.py @@ -24,9 +24,12 @@ # ######################################################################### +import datetime +import time +import os import json -import cherrypy +from lib.item import Items from lib.model.smartplugin import SmartPluginWebIf @@ -34,6 +37,10 @@ # Webinterface of the plugin # ------------------------------------------ +import cherrypy +import csv +from jinja2 import Environment, FileSystemLoader + class WebInterface(SmartPluginWebIf): @@ -49,6 +56,7 @@ def __init__(self, webif_dir, plugin): self.logger = plugin.logger self.webif_dir = webif_dir self.plugin = plugin + self.items = Items.get_instance() self.tplenv = self.init_template_environment() @@ -78,18 +86,11 @@ def get_data_html(self, dataSet=None): :param dataSet: Dataset for which the data should be returned (standard: None) :return: dict with the data needed to update the web page. """ + # if dataSets are used, define them here if dataSet == 'overview': - # get the new data from the plugin variable called _webdata - - data = {} - for obis, value in self.plugin.obis_results.items(): - if isinstance(value, list): - value = value[0] - data[obis] = value - try: - data = json.dumps(data) + data = json.dumps(self.plugin.obis_results) return data except Exception as e: self.logger.error(f"get_data_html overview exception: {e}") @@ -116,6 +117,26 @@ def get_data_html(self, dataSet=None): if dataSet is None: return + @cherrypy.expose + def submit(self, cmd=None): + + self.logger.warning(f"submit: {cmd=}") + result = None + + if cmd == "detect": + result = {'discovery_successful': self.plugin.discover(), 'protocol': self.plugin.protocol} + + elif cmd == 'query': + result = self.plugin.query(assign_values=False) + + self.logger.warning(f"submit: {result=}") + + if result is not None: + # JSON zurücksenden + cherrypy.response.headers['Content-Type'] = 'application/json' + self.logger.debug(f"Result for web interface: {result}") + return json.dumps(result).encode('utf-8') + @cherrypy.expose def read_data(self): self.plugin.query(assign_values=False) diff --git a/smartmeter/webif/index.html b/smartmeter/webif/index.html index b19ab812b..9577a258c 100644 --- a/smartmeter/webif/index.html +++ b/smartmeter/webif/index.html @@ -80,9 +80,17 @@ shngInsertText(item+'_value', objResponse['items'][item]['value'], 'maintable', 5); shngInsertText(item+'_last_update', objResponse['items'][item]['last_update'], 'maintable', 5); shngInsertText(item+'_last_change', objResponse['items'][item]['last_change'], 'maintable', 5); + + for (obis in objResponse['obis_results']) { + var obis_data = objResponse['obis_results'][obis][0]; + console.log("DOM: " + obis+'_value'); + console.log("VAL: " + JSON.stringify(obis_data)); + shngInsertText(obis+'_value', JSON.stringify(obis_data), 'obis_data_table', 5); + } } // Redraw datatable after cell updates // $('#maintable').DataTable().draw(false); + $('#obis_data_table').DataTable().draw(false); } } @@ -92,6 +100,80 @@ -->