-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendetail.py
285 lines (256 loc) · 7.55 KB
/
sendetail.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'''
This module is for the SenDetail windows opened when the detial window is opened.
'''
import os
import sys
import time
import json
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, GdkPixbuf
sys.path.append(os.path.expanduser('~/lib'))
prog_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
sys.path.append(prog_dir)
if os.uname()[0] == 'Darwin':
data_path = os.path.expanduser('~/Network/sensor')
else:
data_path = '/sensor'
from dflib import widgets, rest, psen
from dflib.debug import debug
''' Colors for dark mode '''
dark_mode_colors = {
'default': 'lightgray',
'humdity': 'cyan',
'time': 'yellow',
'name': '#AD00AD',
'type': 'goldenrod',
'modinfo': '#ebba7a',
'description': '#9aeb7a',
'location': '#b8870b',
'temp_green': '#00ff00',
'temp_red': '#ff0000',
'temp_blue': '#0000ff'
}
''' Colors for light mode '''
light_mode_colors = {
'default': 'black',
'humdity': '#008000',
'time': 'brown',
'name': 'darkblue',
'type': 'darkorange',
'modinfo': '#800080',
'description': '#034f91',
'location': '#002000',
'temp_green': '#004000',
'temp_red': '#400000',
'temp_blue': '#000040'
}
class SenDetail(Gtk.Window):
'''
This class implements the sensor detail window.
The specified sensor is read every config['poll_interval'] seconds.
When the window is created it is positioned based on the position
parameter. Window movement is tracked and reported to the caller.
The keyword arguments for this class are:
config = global configuration dict
host - sensor host (for rest client)
sensor_name - sensor name (for rest client)
title - Window title
position - initial window position
callback - to be called when the window is closed
move_callback - to be called when the window is moved (for saving position)
'''
def __init__(self,*args,**kwargs):
self._use_rest = False
self.config = None
self.host = None
self.sensor_name = None
self.title = None
self.callback = None
self.position = None
self._macos = False
self.move_callback = None
self._initial_position_set = False
self.iconified = False
self._data_thread = None
self.data_path = data_path
for k,v in kwargs.items():
if k in ['config','host','sensor_name','title','callback','position','move_callback','data_path']:
setattr(self,k,v)
else:
raise ValueError(f'Invalid keyword argument {k}')
for p in ['config','host','sensor_name','title','callback','position']:
if not getattr(self,p):
raise AttributeError(p)
if 'Darwin' in os.uname()[0]:
self._macos = True
if not 'poll_interval' in self.config:
self.config['poll_interval'] = 300
debug(self.sensor_name,'interval', self.config['poll_interval'])
self.server = self.config['server']
if self._use_rest:
self.sensor = rest.RestClient(server=self.server,sensor=self.sensor_name,host=self.host)
else:
self.sensor = psen.PsuedoSensor(
base_path = self.data_path,
server=self.server,
sensor=self.sensor_name,
host=self.host)
Gtk.Window.__init__(self, title=self.title)
self.connect("delete-event", self.stopit)
self.keepgoing = True
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.label = Gtk.Label()
self.label.set_selectable(False)
self.label.set_hexpand(True)
self.vbox.pack_start(self.label,True,True,0)
self.add(self.vbox)
window_icon = GdkPixbuf.Pixbuf.new_from_file('icons/humidity.png')
self.set_icon(window_icon)
self.show_all()
self.update()
def change_sensor(self, title, host, sensor):
'''
Change title,host and sensor used.
'''
self.title = title
self.set_title(title)
self.host = host
self.sensor_name = sensor
self.server = self.config['server']
self.sensor = rest.RestClient(server=self.server,sensor=self.sensor_name,host=self.host)
debug(self.server,title, host, sensor)
def do_iconify(self,*args):
'''
Hide a window if not hidden
'''
debug(f'is_iconified',self.iconified)
if not self.iconified:
self.iconified = True
self.hide()
def do_deiconify(self,*args):
'''
unhide a hidden window
'''
debug(f'is_iconified',self.iconified)
if self.iconified:
self.iconified = False
self.show_all()
def set_window_position(self):
'''
position the window on init
'''
debug(self.title,self.position)
if self.position:
self.move(*self.position)
self.connect('configure-event',self.on_window_config)
self._initial_position_set = True
def _xyfixup(self,x,y):
'''
Window management on macos is a little weird. This fixes it
'''
if x + y != 0:
yoffset = 28 if self._macos else 0
y+=yoffset
if x < 0:
x=0
return (x,y)
def move(self,x,y):
'''
move the window
'''
return super().move(*self._xyfixup(x,y))
def on_window_config(self,*args):
'''
when the window is moved this evet is called, in turn we callback to the
caller for the caller to save
'''
if self.keepgoing:
pos = tuple(self.get_position())
if callable(self.move_callback):
self.move_callback(self.title,pos)
def stopit(self,*args):
'''
call this when terminating. If callback is defined call it with title
'''
debug(f'stopit; {self.title}')
self.keepgoing = False
if callable(self.callback):
self.callback(self.title)
self.destroy()
def read_sensor(self):
''' reas the sensor and return data unless there's an error in the data
'''
we = f'{self.sensor.host}::{self.sensor.sensor}'
detail = self.sensor.read()
if type(detail) is dict:
if 'error' in detail:
return None
else:
debug(we,"returned data is not dict",detail)
return None
return detail
def update(self):
'''
This is out main worker.
First we check for dark_mode and set css accordingly.
We read the sensor and if data is returned format it
based on keys and colors. Once complete set a new timeout
to do this all over again.
'''
if 'dark_mode' not in self.config:
self.dark_mode = False
self.dark_mode = self.config['dark_mode']
if self.dark_mode:
self.key_color = '#7f7f7f'
self.keycolors = dark_mode_colors
bgc = 'black'
else:
self.key_color = '#3f3f3f'
self.keycolors = light_mode_colors
bgc = 'white'
css_data = '.sdetail {font-family: Ariel; font-size: 22px; background-color: @bgc; padding: 15px; }'.replace('@bgc',bgc)
widgets._widget_set_css(self.label, 'sdetail', css_data)
key_color = self.key_color
dark_mode = self.dark_mode
detail = self.read_sensor()
if detail and not 'error' in detail:
detail['name'] = self.sensor_name
s = ''
for k,v in detail.items():
if k == 'description' or k == 'modinfo':
v = f'<i>{v}</i>'
if k == 'time':
v = time.strftime('%D %T',time.localtime(v))
if k == 'boot_time':
v = int(time.time()) - int(v)
v = time.strftime('%D %T',time.localtime(v))
v = ':'.join(v.split(':')[:-1])
if k in self.keycolors:
color = self.keycolors[k]
else:
color = self.keycolors['default']
if k == type(v) is list:
v = v[0]
if type(v) is float:
if k in ['high', 'low','temp','tempc']:
if v < 60:
color = self.keycolors['temp_blue']
elif v >= 80:
color = self.keycolors['temp_red']
else:
color = self.keycolors['temp_green']
v = '{:.2f}'.format(v)
s = s + f'<span foreground="{key_color}">{k}</span>: '
s = s + f'<span foreground="{color}">{v}</span>\n'
try:
self.label.set_markup(s)
except:
debug(f'Markup error: {s}')
self.keepgoing = False
return
if not self._initial_position_set:
self.set_window_position()
if self.keepgoing:
interval = self.config['poll_interval']
GLib.timeout_add(interval, self.update)