summaryrefslogtreecommitdiff
path: root/wasp/apps/weather.py
blob: 15e8758eb802fa42fd22ef17382dbd2a3fe512ca (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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2020 Daniel Thompson
# Copyright (C) 2020 Carlos Gil

"""Weather for GadgetBridge and wasp-os companion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    .. figure:: res/WeatherApp.png
        :width: 179

        Screenshot of the Weather application

"""

import wasp

import icons
import time
import fonts.sans36

# 2-bit RLE, generated from res/weather_icon.png, 309 bytes
icon = (
    b'\x02'
    b'`@'
    b'\x1e\xa4<\xa4<\xa4;\xa5?Y\xec2\xf0/\xf2-'
    b'\xf4,\xc3@;n\xc3,\xc3n\xc3,\xc3n\xc3,'
    b'\xc3n\xc3,\xc3n\xc3,\xc3n\xc3,\xc3n\xc3,'
    b'\xc3n\xc3,\xc3n\xc3,\xc3O\x80\xd3\x87X\xc3,'
    b'\xc3M\x8bV\xc3,\xc3K\x8fT\xc3,\xc3J\x91S'
    b'\xc3,\xc3I\x93R\xc3,\xc3H\x95Q\xc3,\xc3H'
    b'\x95Q\xc3,\xc3G\x97P\xc3,\xc3G\x97P\xc3+'
    b'\xc4F\x99O\xc3*\xc5F\x99O\xc3*\xc5F\x99O'
    b'\xc3*\xc5F\x99O\xc3*\xc5F\x99O\xc3*\xc5F'
    b'\x99\xc4K\xc3*\xc5F\x8e\xc5\x84\xc1\x81\xc5J\xc3*'
    b'\xc5G\x8c\xc8\x81\xcaH\xc3+\xc4G\x83\xc5\x83\xd6F'
    b'\xc3,\xc3H\x81\xe0E\xc3,\xc3G\xe2E\xc3,\xc3'
    b'G\xe3D\xc3,\xc3F\xe4D\xc3,\xc3F\xe4D\xc3'
    b',\xc3F\xe4D\xc3,\xc3F\xe4D\xc3,\xc3F\xe3'
    b'E\xc3,\xc3G\xe2E\xc3,\xc3G\xe1F\xc3,\xc3'
    b'H\xc9A\xccC\xc5H\xc3,\xc3J\xc5E\xc9Q\xc3'
    b',\xc3V\xc5S\xc3,\xc3n\xc3,\xc3n\xc3,\xc3'
    b'n\xc3,\xc3n\xc3,\xc3n\xc3,\xc3n\xc3,\xf4'
    b'-\xf2/\xf02\xec?X\xc0\xdb\xe6;\xe4<\xe4<'
    b'\xe4\x1e'
)

class WeatherApp(object):
    """ Weather application."""
    NAME = 'Weather'
    ICON = icon
    
    def __init__(self):
        self._temp = -1
        self._hum = 0
        self._txt = ''
        self._wind = 0
        self._loc = ''
        self._temp_changed = True
        self._hum_changed = True
        self._txt_changed = True
        self._wind_changed = True
        self._loc_changed = True

    def foreground(self):
        """Activate the application."""
        get_info = wasp.system.weatherinfo.get
        temp = get_info('temp')
        hum = get_info('hum')
        txt = get_info('txt')
        wind = get_info('wind')
        loc = get_info('loc')
        if temp:
            self._temp = temp
        if hum:
            self._hum = hum
        if txt:
            self._txt = txt
        if wind:
            self._wind = wind
        if loc:
            self._loc = loc
        wasp.watch.drawable.fill()
        self.draw()
        wasp.system.request_tick(1000)

    def background(self):
        """De-activate the application (without losing state)."""
        self._temp_changed = True
        self._hum_changed = True
        self._txt_changed = True
        self._wind_changed = True
        self._loc_changed = True

    def tick(self, ticks):
        wasp.system.keep_awake()
        get_info = wasp.system.weatherinfo.get
        temp_now = get_info('temp')
        hum_now = get_info('hum')
        txt_now = get_info('txt')
        wind_now = get_info('wind')
        loc_now = get_info('loc')
        if temp_now:
            if temp_now != self._temp:
                self._temp = temp_now
                self._temp_changed = True
        else:
            self._temp_changed = False
        if hum_now:
            if hum_now != self._hum:
                self._hum = hum_now
                self._hum_changed = True
        else:
            self._hum_changed = False
        if txt_now:
            if txt_now != self._txt:
                self._txt = txt_now
                self._txt_changed = True
        else:
            self._txt_changed = False
        if wind_now:
            if wind_now != self._wind:
                self._wind = wind_now
                self._wind_changed = True
        else:
            self._wind_changed = False
        if loc_now:
            if loc_now != self._loc:
                self._loc = loc_now
                self._loc_changed = True
        else:
            self._loc_changed = False
        wasp.system.weatherinfo = {}
        self._update()

    def draw(self):
        """Redraw the display from scratch."""
        self._draw()

    def _draw(self):
        """Redraw the updated zones."""
        draw = wasp.watch.drawable
        if self._temp != -1:
            units = wasp.system.units
            temp = self._temp - 273.15
            wind = self._wind
            wind_units = "km/h"
            if units == "Imperial":
                temp = (temp * 1.8) + 32
                wind = wind / 1.609
                wind_units = "mph"
            temp = round(temp)
            wind = round(wind)
            if self._temp_changed:
                self._draw_label(str(temp), 54, 36)
            if self._hum_changed:
                self._draw_label("Humidity: {}%".format(self._hum), 160)
            if self._txt_changed:
                self._draw_label(self._txt, 12)
            if self._wind_changed:
                self._draw_label("Wind: {}{}".format(wind, wind_units), 120)
            if self._loc_changed:
                self._draw_label(self._loc, 200)
        else:
            if self._temp_changed:
                draw.fill()
                self._draw_label("No weather data.", 120)

    def _draw_label(self, label, pos, size = 24):
        """Redraw label info"""
        if label:
            draw = wasp.watch.drawable
            draw.reset()
            if size == 36:
                draw.set_font(fonts.sans36)
            
            draw.string(label, 0, pos, 240)

    def _update(self):
        self._draw()

    def update(self):
        pass