-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e837349
commit 8a26897
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#!/usr/bin/env python3 | ||
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab | ||
######################################################################### | ||
# Copyright 2020- <AUTHOR> <EMAIL> | ||
######################################################################### | ||
# This file is part of SmartHomeNG. | ||
# https://www.smarthomeNG.de | ||
# https://knx-user-forum.de/forum/supportforen/smarthome-py | ||
# | ||
# Sample plugin for new plugins to run with SmartHomeNG version 1.5 and | ||
# upwards. | ||
# | ||
# SmartHomeNG is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# SmartHomeNG is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with SmartHomeNG. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
|
||
|
||
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 | ||
|
||
|
||
class WebInterface(SmartPluginWebIf): | ||
|
||
def __init__(self, webif_dir, plugin): | ||
""" | ||
Initialization of instance of class WebInterface | ||
:param webif_dir: directory where the webinterface of the plugin resides | ||
:param plugin: instance of the plugin | ||
:type webif_dir: str | ||
:type plugin: object | ||
""" | ||
self.logger = plugin.logger | ||
self.webif_dir = webif_dir | ||
self.plugin = plugin | ||
self.items = Items.get_instance() | ||
|
||
self.tplenv = self.init_template_environment() | ||
|
||
@cherrypy.expose | ||
def index(self, reload=None): | ||
""" | ||
Build index.html for cherrypy | ||
Render the template and return the html file to be delivered to the browser | ||
:return: contents of the template after beeing rendered | ||
""" | ||
tmpl = self.tplenv.get_template('index.html') | ||
# Setting pagelength (max. number of table entries per page) for web interface | ||
try: | ||
pagelength = self.plugin.webif_pagelength | ||
except Exception: | ||
pagelength = 100 | ||
# 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, | ||
items=sorted(self.plugin.item_list, key=lambda k: str.lower(k['_path'])), | ||
item_count=len(self.plugin.item_list), | ||
plugin_shortname=self.plugin.get_shortname(), | ||
plugin_version=self.plugin.get_version(), | ||
plugin_info=self.plugin.get_info(), | ||
maintenance=True if self.plugin.log_level <= 20 else False, | ||
) | ||
|
||
@cherrypy.expose | ||
def get_data_html(self, dataSet=None): | ||
""" | ||
Return data to update the webpage | ||
For the standard update mechanism of the web interface, the dataSet to return the data for is 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 dataSet is None: | ||
# get the new data | ||
data = dict() | ||
|
||
data['items'] = {} | ||
for item in self.plugin.item_list: | ||
data['items'][item.property.path] = {} | ||
data['items'][item.property.path]['value'] = item.property.value | ||
data['items'][item.property.path]['last_update'] = item.property.last_update.strftime('%d.%m.%Y %H:%M:%S') | ||
data['items'][item.property.path]['last_change'] = item.property.last_change.strftime('%d.%m.%Y %H:%M:%S') | ||
data['plugin_suspended'] = self.plugin.suspended | ||
data['maintenance'] = True if self.plugin.log_level <= 20 else False | ||
try: | ||
return json.dumps(data, default=str) | ||
except Exception as e: | ||
self.logger.error(f"get_data_html exception: {e}") | ||
|
||
@cherrypy.expose | ||
def activate(self): | ||
self.logger.debug(f"active called") | ||
self.plugin.suspend(False) | ||
|
||
@cherrypy.expose | ||
def suspend(self): | ||
self.logger.debug(f"suspend called") | ||
self.plugin.suspend(True) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
This directory is for storing images that are used by the web interface. | ||
|
||
If you want to have your own logo on the top of the web interface, store it here and name it plugin_logo.<extension>. | ||
|
||
Extension can be png, svg or jpg | ||
|