WattWatch/main.py

129 lines
4.6 KiB
Python

# main.py
#
# Copyright 2023 Nikola Mitrojevic, Emanuel Loos & Tobias Bramböck
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
import kivy
kivy.require('2.1.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.garden.matplotlib import FigureCanvasKivyAgg
from awattar.client import AwattarClient
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.graphics.svg import Svg
from datetime import datetime, date
import numpy as np
import matplotlib.pyplot as plt
client = AwattarClient('AT') # or DE for Germany
date = datetime(datetime.now().year, datetime.now().month, datetime.now().day)
data = client.request(date)
plt.style.use('_mpl-gallery')
hours = np.arange(24)
prices = np.array([item.marketprice/1000 for item in data])
some_data = np.random.random(24)
class DiagramWidget(FigureCanvasKivyAgg):
def __init__(self, figure, **kwargs):
super().__init__(figure, **kwargs)
self.ax = figure.axes[0]
class MyBoxLayout(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = 'vertical'
self.add_widget(Label(text='[b]Strompreise & Stromerzeugung[/b]', markup=True, halign='left', valign='top', font_size='20sp', size_hint=(None, None), size=(0, 100), pos_hint={'x':0.18,'y':1}))
picture_button = Button(size_hint=(None, None), size=(85, 100), pos_hint={'x': 0.8, 'y': 0})
picture_button.background_normal = 'Einstellungsrad.svg'
self.add_widget(picture_button)
hours = np.arange(24)
fig2, ax2 = plt.subplots(figsize=(10, 5))
fig2.subplots_adjust(left=0.075, bottom=0.075)
ax2.bar(hours, some_data, width=0.5, edgecolor='white', linewidth=0.5)
ax2.set(xlim=(-1, 24), xticks=hours)
fig2.canvas.draw()
diagram_widget2 = DiagramWidget(figure=fig2)
diagram_widget2.size_hint_y = 0.9 # Less than 1 to make room for toggle buttons
diagram_box2 = BoxLayout(size_hint_y=0.9)
diagram_box2.add_widget(diagram_widget2)
self.add_widget(diagram_box2)
# Add toggle buttons
toggle_box = BoxLayout(orientation='horizontal', size_hint_y=None, height='50dp')
toggle_button1 = ToggleButton(text='Netto/Brutto')
toggle_button2 = ToggleButton(text='Stromherkunft einblenden')
toggle_box.add_widget(toggle_button1)
toggle_box.add_widget(toggle_button2)
self.add_widget(toggle_box)
prices = np.array([item.marketprice/1000 for item in data])
hours = np.arange(24)
fig, ax = plt.subplots(figsize=(10, 5))
fig.subplots_adjust(left=0.075,bottom=0.075)
ax.bar(hours, prices, width=0.5, edgecolor='white', linewidth=0.5)
ax.set(xlim=(-1, 24), xticks=hours)
fig.canvas.draw()
diagram_widget = DiagramWidget(figure=fig)
diagram_widget.size_hint_y = 0.9 # Less than 1 to make room for toggle buttons
diagram_box = BoxLayout(size_hint_y=0.9)
diagram_box.add_widget(diagram_widget)
self.add_widget(diagram_box)
Builder.load_string('''
<WattWatchMain>:
GridLayout:
cols: 1
size_hint_y: 1
BoxLayout:
id: my_box
size_hint_y: .8
pos_hint: {'top': 1}
BoxLayout:
orientation: 'horizontal'
size_hint_y: .2
pos_hint: {'top': 0.2}
Button:
text: 'Blablub'
Button:
text: 'Blablub'
''')
class WattWatchMain(Screen):
pass
class WattWatchApp(App):
title = 'WattWatch'
icon = 'wwicon.png'
def build(self):
sm = ScreenManager()
my_screen = WattWatchMain(name='WattWatchMain')
my_screen.ids.my_box.add_widget(MyBoxLayout())
sm.add_widget(my_screen)
sm.current = 'WattWatchMain'
return sm
if __name__ == '__main__':
WattWatchApp().run()