aboutsummaryrefslogtreecommitdiff
path: root/foris/__init__.py
blob: a70f760ebf5bf80545d525650dded5c49430fcb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import time

from foris.config import ConfigPageMixin, add_config_page
from foris.config_handlers import BaseConfigHandler
from foris.core import gettext_dummy as gettext, ugettext as _
from foris.plugins import ForisPlugin
from foris.ubus import call as ubus_call


class SamplePluginConfigHandler(BaseConfigHandler):
    pass


class SamplePluginPage(ConfigPageMixin, SamplePluginConfigHandler):
    menu_order = 70
    template = "i2c_collect/i2c_collect.tpl"
    userfriendly_title = gettext("I2C collect")

    def _prepare_render_args(self, args):
        args['PLUGIN_NAME'] = SamplePluginPlugin.PLUGIN_NAME
        args['PLUGIN_STYLES'] = SamplePluginPlugin.PLUGIN_STYLES
        args['PLUGIN_STATIC_SCRIPTS'] = SamplePluginPlugin.PLUGIN_STATIC_SCRIPTS
        args['PLUGIN_DYNAMIC_SCRIPTS'] = SamplePluginPlugin.PLUGIN_DYNAMIC_SCRIPTS
        args['data'] = {}
        for fld in ["voltage", "light", "temperature", "trimmer"]:
            args['data'][fld] = ubus_call("i2c-collect", "read_data", {"range": 172800, "field": fld})[0]['data']
            for _, dt in enumerate(args['data'][fld]):
                dt[0] = time.strftime('%H:%M:%S', time.gmtime(int(dt[0])))
                if fld == "voltage":
                    dt[1] = float(dt[1]) * 0.0593
                elif fld == "light":
                    dt[1] = float(255 - dt[1]) # TODO calibrate
                elif fld == "temperature":
                    dt[1] = float(255 - dt[1]) # TODO calibrate
                elif fld == "trimmer":
                    dt[1] = float(dt[1]) / 2.55

    def render(self, **kwargs):
        self._prepare_render_args(kwargs)
        return super(SamplePluginPage, self).render(**kwargs)


class SamplePluginPlugin(ForisPlugin):
    PLUGIN_NAME = "i2c_collect"
    DIRNAME = os.path.dirname(os.path.abspath(__file__))

    PLUGIN_STYLES = [
    ]
    PLUGIN_STATIC_SCRIPTS = [
        "js/Chart.bundle.min.js"
    ]
    PLUGIN_DYNAMIC_SCRIPTS = [
        "i2c_collect.js"
    ]

    def __init__(self, app):
        super(SamplePluginPlugin, self).__init__(app)
        add_config_page("i2c_collect", SamplePluginPage, top_level=True)