Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonread Plugin; update web interface #825

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jsonread/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import pyjq
from lib.model.smartplugin import SmartPlugin
from lib.item import Items

from .webif import WebInterface


class JSONREAD(SmartPlugin):
PLUGIN_VERSION = "1.0.2"
PLUGIN_VERSION = "1.0.3"

def __init__(self, sh):
"""
Expand Down
14 changes: 6 additions & 8 deletions jsonread/locale.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# translations for the web interface
plugin_translations:
# Translations for the plugin specially for the web interface
'Wert 2': {'de': '=', 'en': 'Value 2'}
'Wert 4': {'de': '=', 'en': 'Value 4'}

# Alternative format for translations of longer texts:
'Hier kommt der Inhalt des Webinterfaces hin.':
de: '='
en: 'Here goes the content of the web interface.'
'Item': {'de': '=', 'en': '='}
'jsonread_filter': {'de': '=', 'en': '='}
'Wert': {'de': '=', 'en': 'Value'}
'Letzte Auslesung': {'de': '=', 'en': 'Last Reading'}
'Items definiert': {'de': '=', 'en': 'Items defined'}
'Letzte Auslesung einfache jq Abfragesyntax': {'de': '=', 'en': 'Last Reading simple jq query syntax'}
7 changes: 3 additions & 4 deletions jsonread/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ plugin:
de: 'json Parser Plugin basierend auf jq'
en: 'json parser plugin based on jq'
maintainer: Torsten Dreyer
tester: none (yet)
state: develop
tester: onkelandy
state: ready
keywords: json jq
documentation: http://smarthomeng.de/user/plugins_doc/config/not-yet.html
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/not-yet

version: 1.0.2 # Plugin version
version: 1.0.3 # Plugin version
sh_minversion: 1.4 # minimum shNG version to use this plugin
#sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
#py_minversion: 3.6 # minimum Python version to use for this plugin
Expand Down Expand Up @@ -61,4 +61,3 @@ logic_parameters: NONE

plugin_functions: NONE
# Definition of function interface of the plugin

30 changes: 14 additions & 16 deletions jsonread/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
import datetime
import time
import os
import json

from lib.item import Items
from lib.model.smartplugin import SmartPluginWebIf


# ------------------------------------------
# Webinterface of the plugin
# ------------------------------------------

import cherrypy
import csv
from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -69,8 +66,11 @@ def index(self, reload=None):
:return: contents of the template after beeing rendered
"""
tmpl = self.tplenv.get_template('index.html')
pagelength = self.plugin.get_parameter_value('webif_pagelength')
# add values to be passed to the Jinja2 template eg: tmpl.render(p=self.plugin, interface=interface, ...)
return tmpl.render(p=self.plugin,
webif_pagelength=pagelength,
update_interval=self.plugin._cycle * 1000,
items=self.plugin._items,
item_count=0)

Expand All @@ -87,16 +87,14 @@ def get_data_html(self, dataSet=None):
"""
if dataSet is None:
# get the new data
data = {}

# data['item'] = {}
# for i in self.plugin.items:
# data['item'][i]['value'] = self.plugin.getitemvalue(i)
#
# return it as json the the web page
# try:
# return json.dumps(data)
# except Exception as e:
# self.logger.error("get_data_html exception: {}".format(e))
data = {'items': {}, 'laststr': '', 'lastjq': ''}

for item in self.plugin._items.keys():
data['items'][item.property.path] = item.property.value
data['laststr'] = self.plugin._lastresultstr
data['lastjq'] = self.plugin._lastresultjq
try:
return json.dumps(data)
except Exception as e:
self.logger.error("get_data_html exception: {}".format(e))
return {}

108 changes: 43 additions & 65 deletions jsonread/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% set logo_frame = false %}

<!-- set update_interval to a value > 0 (in milliseconds) to enable periodic data updates -->
{% set update_interval = 0 %}
{% set update_interval = update_interval %}

<!--
Additional script tag for plugin specific javascript code go into this block
Expand All @@ -14,37 +14,44 @@
if (dataSet === 'devices_info' || dataSet === null) {
var objResponse = JSON.parse(response);
myProto = document.getElementById(dataSet);
for (var device in objResponse) {
/*
shngInsertText (device+'_source', objResponse[device]['source']);
shngInsertText (device+'_powerState', objResponse[device]['powerState']);
*/
for (item in objResponse['items']) {
shngInsertText (item+'_value', objResponse['items'][item], "itemtable", 10);
}
shngInsertText ('laststr', objResponse['laststr'], null, 2);
shngInsertText ('lastjq', objResponse['lastjq'], null, 2);
}
}
</script>

/*
* The combined file was created by the DataTables downloader builder:
* https://datatables.net/download
*
* To rebuild or modify this file with the latest versions of the included
* software please visit:
* https://datatables.net/download/#dt/dt-1.10.21/fh-3.1.7/r-2.2.5
*
* Included libraries:
* DataTables 1.10.21, FixedHeader 3.1.7, Responsive 2.2.5
*/
<link rel="stylesheet" type="text/css" href="static/datatables.css">
<script type="text/javascript" charset="utf8" src="static/datatables.js"></script>

<script>
$(document).ready( function () {
$('#itemtable').DataTable( {
"paging": false,
fixedHeader: true
} );
} );
$(document).ready( function () {
$(window).trigger('datatables_defaults');
try {
table = $('#itemtable').DataTable( {
columnDefs: [
{
title: '',
targets: 0, "className": ""
},
{
title: '{{ _("Item") }}',
targets: 1, "className": "item"
},
{
title: '{{ _("jsonread_filter") }}',
targets: 2, "className": "send"
},
{
title: '{{ _("Wert") }}',
targets: 3, "className": "remote"
}
].concat($.fn.dataTable.defaults.columnDefs)
});
}
catch (e) {
console.log("Datatable JS not loaded, showing standard table without reorder option " +e);
}

});
</script>
{% endblock pluginscripts %}

Expand All @@ -66,27 +73,8 @@
</table>
{% endblock headtable %}


<!--
Additional buttons for the web interface (if any are needed) - displayed below the headtable-section
-->
{% block buttons %}
{% if 1==2 %}
<div>
<button id="btn1" class="btn btn-shng btn-sm" name="scan" onclick="shngPost('', {learn: 'on'})"><i class="fas fa-question"></i>&nbsp;&nbsp;&nbsp;{{ _('nach Devices suchen') }}&nbsp;</button>
</div>
{% endif %}
{% endblock %}

<!--
Define the number of tabs for the body of the web interface (1 - 3)
-->
{% set tabcount = 3 %}


<!--
Set the tab that will be visible on start, if another tab that 1 is wanted (1 - 3)
-->
{% set item_count = p._items|length %}
{% if item_count==0 %}
{% set start_tab = 1 %}
Expand All @@ -101,7 +89,7 @@
{% set tab1title = "<strong>" ~ _("Letzte Auslesung") ~ "</strong>" %}
{% block bodytab1 %}
<div class="container-fluid m-2">
<pre>{{ p._lastresultstr }}</pre>
<pre id="laststr">{{ p._lastresultstr }}</pre>
</div>
{% endblock bodytab1 %}

Expand All @@ -111,28 +99,19 @@
-->
{% set tab2title = "<strong>" ~ item_count ~ " " ~ _("Items definiert") ~ "</strong>" %}
{% block bodytab2 %}
<div class="table-responsive" style="margin-left: 3px; margin-right: 3px;" class="row">
<div class="col-sm-12">
<table id="itemtable" class="table table-striped table-hover">
<thead>
<tr>
<th style="border-bottom: 1px solid var(--shng-border);">{{ _('Item') }}</th>
<th style="border-bottom: 1px solid var(--shng-border);">{{ _('jsonread_filter') }}</th>
<th style="border-bottom: 1px solid var(--shng-border); width: 100;">{{ _('Wert') }}</th>
</tr>
</thead>

<table id="itemtable" class="dataTableAdditional">
<tbody>
{% for item in p._items %}
<tr>
<td class="py-1">{{ item._path }}</td>
<td class="py-1">{{ p.get_iattr_value(item.conf, 'jsonread_filter') }}</td>
<td class="py-1">{{ item() }}</td>
<tr><td></td>
<td class="py-1" id="{{ item }}_path">{{ item._path }}</td>
<td class="py-1" id="{{ item }}_filter">{{ p.get_iattr_value(item.conf, 'jsonread_filter') }}</td>
<td class="py-1" id="{{ item }}_value">{{ item() }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

{% endblock bodytab2 %}

<!--
Expand All @@ -141,7 +120,6 @@
{% set tab3title = "<strong>" ~ _("Letzte Auslesung einfache jq Abfragesyntax") ~ "</strong>" %}
{% block bodytab3 %}
<div class="container-fluid m-2">
<pre>{{ p._lastresultjq }}</pre>
<pre id="lastjq">{{ p._lastresultjq }}</pre>
</div>
{% endblock bodytab3 %}