-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
90 lines (68 loc) · 2.65 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/python3
"""
Data Logger application.
"""
__author__ = 'Holger Fleischmann'
__copyright__ = 'Copyright 2021, Holger Fleischmann, Bavaria/Germany'
__license__ = 'Apache License 2.0'
# initialize custom logging:
import log
import logging
from datetime import datetime
import time
from kivy.config import Config
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from mqttclient import MqttClient
from signalsourcesconfig import signal_sources_config
# from mocksignalsourcesconfig import signal_sources_config
logger = logging.getLogger().getChild(__name__)
class DataLoggerWidget(BoxLayout):
"""
Main widget of the application.
"""
status_text = StringProperty()
time_text = StringProperty()
date_text = StringProperty()
SCREENSAVER_DELAY = 600
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.clock_update_event = Clock.schedule_interval(self.update_clock, 1.)
self.ids.measurements_screen.use_signals_config(signal_sources_config)
self.ids.graphs_screen.use_signals_config(signal_sources_config)
self._last_user_activity = time.time()
self._screensaver_active = None
self._activate_screensaver(False)
def update_clock(self, *args):
now = time.time()
dt = datetime.fromtimestamp(now)
self.time_text = '{:%H:%M:%S}'.format(dt)
self.date_text = '{:%d.%m.%Y}'.format(dt)
if self._last_user_activity is not None and self._last_user_activity < now - DataLoggerWidget.SCREENSAVER_DELAY:
self._activate_screensaver(True)
self._last_user_activity = None
def on_touch_down(self, touch):
self._last_user_activity = time.time()
self._activate_screensaver(False)
return super().on_touch_down(touch)
def _activate_screensaver(self, activate):
if self._screensaver_active != activate:
self._screensaver_active = activate
try:
with open('/sys/class/backlight/rpi_backlight/bl_power', 'w') as file:
file.write('1' if activate else '0')
except:
logger.error('Could not ' + ('activate' if activate else 'deactivate') + ' screensaver')
class DataLoggerApp(App):
def build(self):
return DataLoggerWidget()
if __name__ == '__main__':
logger.error('Starting application')
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
mqtt_client = MqttClient()
mqtt_client.use_signals_config(signal_sources_config)
mqtt_client.start()
DataLoggerApp().run()