forked from katsumin/python-echonet-lite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
view_manager.py
206 lines (182 loc) · 7.52 KB
/
view_manager.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
# coding: utf-8
from abc import ABCMeta, abstractmethod
import sys
import math
import os
import datetime
import time
from luma.core import cmdline, error
from luma.core.render import canvas
from PIL import ImageFont
import netifaces
from property_manager import PropertyManager
from configparser import ConfigParser
class ViewManager(metaclass=ABCMeta):
"Viewマネージャ"
# 初期化
def __init__(self, iniFile):
self._device = self.get_device(
['--config', iniFile.get('view', 'config_file')])
self._device._state = True
self._width = min(self._device.width, 240)
self._height = min(self._device.height, 240)
self._font = self.make_font(iniFile.get(
'view', 'header_font'), int(iniFile.get('view', 'font_small')))
self._date = None
self._pm = None
self._dm = None
with canvas(self._device) as draw:
text_w, text_h = draw.textsize('t', self._font)
self._payloadArea = (0, text_h, self._width -
1, self._height - 1 - text_h)
# Propertyマネージャ設定
def setPropertyManager(self, pm):
self._pm = pm
# Displayマネージャー設定
def setDisplayManager(self, dm):
if self._dm is None:
dm.notify(self._device._state)
self._dm = dm
# フォント生成
def make_font(self, name, size):
font_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), 'fonts', name))
return ImageFont.truetype(font_path, size)
# 表示更新
def reflesh(self):
if not self._device._state:
# ディスプレイOFF時は必要ない
return
with canvas(self._device) as draw:
# 外枠
# draw.rectangle((0, 0, self._width - 1, self._height - 1), outline="blue")
# draw.rectangle((self._payloadArea[0],self._payloadArea[1]-1,self._payloadArea[2],self._payloadArea[3]+1), outline="blue")
draw.line((self._payloadArea[0], self._payloadArea[1]-1,
self._payloadArea[2], self._payloadArea[1]-1), fill="blue")
draw.line((self._payloadArea[0], self._payloadArea[3]+1,
self._payloadArea[2], self._payloadArea[3]+1), fill="blue")
# 日付
now = datetime.datetime.now()
self._date = now
dt = u"'{0:02}/{1:02}/{2:02} {3:02}:{4:02}:{5:02}".format(
now.year % 100, now.month, now.day, now.hour, now.minute, now.second)
# date_w, date_h = draw.textsize("'88/88/88 88:88:88", self._font)
date_w, date_h = draw.textsize(dt, self._font)
draw.text(((self._width - date_w)/2, self._height -
date_h), dt, fill="yellow", font=self._font)
# IP Address更新
self.refleshIpAddr(draw)
# ペイロード更新
self.refleshPayload(draw)
# IPアドレスの表示
def refleshIpAddr(self, draw):
addr = None
try:
addr = netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
except Exception:
try:
addr = netifaces.ifaddresses(
'wlan0')[netifaces.AF_INET][0]['addr']
except Exception:
pass
if addr is not None:
addr_w, addr_h = draw.textsize(addr, self._font)
draw.text(((self._width - addr_w)/2, 0), addr,
fill="yellow", font=self._font)
# ペイロード表示のクリア
def clearPayload(self):
self._device.clear()
with canvas(self._device) as draw:
draw.rectangle((self._payloadArea[0], self._payloadArea[1],
self._payloadArea[2], self._payloadArea[3]), fill="black", outline="black")
@abstractmethod
# ペイロードの表示
def refleshPayload(self, draw):
pass
# 表示デバイス取得
def get_device(self, actual_args):
# if actual_args is None:
# actual_args = sys.argv[1:]
# print(actual_args)
parser = cmdline.create_parser(description='luma.examples arguments')
args = parser.parse_args(actual_args)
if args.config:
# load config from file
config = cmdline.load_config(args.config)
args = parser.parse_args(config + actual_args)
# create device
try:
device = cmdline.create_device(args)
except error.Error as e:
parser.error(e)
return device
def dispose(self):
pass
# 表示状態設定
def set_display_state(self, state):
if state:
self.clearPayload()
self.reflesh()
#self._device.show()
self._device.backlight(True)
self._device._state = True
if self._dm is not None:
self._dm.notify(True)
else:
self._device._state = False
time.sleep(0.3) # 表示中に落とすと以後の表示ができなくなる模様?なので待つ
#self._device.hide()
self._device.backlight(False)
if self._dm is not None:
self._dm.notify(False)
# 表示状態取得
def get_display_state(self):
return self._device._state
class ViewManagerAnalog(ViewManager):
def __init__(self, iniFile):
super().__init__(iniFile)
self._offset_x = self._payloadArea[0]
self._offset_y = self._payloadArea[1]
width = self._payloadArea[2] - self._offset_x
height = self._payloadArea[3] - self._offset_y
self._cy = height / 2 + self._offset_y
self._radius = (min(width, height) - 1) / 2
self._cx = width / 2
def posn(self, angle, arm_length):
dx = int(math.cos(math.radians(angle)) * arm_length)
dy = int(math.sin(math.radians(angle)) * arm_length)
return (dx, dy)
def refleshPayload(self, draw):
if self._date is not None:
cx = self._cx
cy = self._cy
radius = self._radius
# calc. angles
now = self._date
hrs_angle = 270 + (30 * (now.hour + (now.minute / 60.0)))
# hrs = posn(hrs_angle, cy - margin - 7)
hrs = self.posn(hrs_angle, radius - 7)
min_angle = 270 + (6 * now.minute)
# mins = posn(min_angle, cy - margin - 2)
mins = self.posn(min_angle, radius - 2)
sec_angle = 270 + (6 * now.second)
# secs = posn(sec_angle, cy - margin - 2)
secs = self.posn(sec_angle, radius - 2)
# reflet view
draw.ellipse((cx - radius, cy - radius, cx +
radius, cy + radius), outline="white")
draw.line((cx, cy, cx + hrs[0], cy + hrs[1]), fill="white")
draw.line((cx, cy, cx + mins[0], cy + mins[1]), fill="white")
draw.line((cx, cy, cx + secs[0], cy + secs[1]), fill="red")
draw.ellipse((cx - 2, cy - 2, cx + 2, cy + 2),
fill="white", outline="white")
if __name__ == "__main__":
iniFile = ConfigParser()
iniFile.read('/home/pi/wisun-gateway/config.ini')
vm = ViewManagerAnalog(iniFile)
try:
while True:
vm.reflesh()
time.sleep(0.1)
except KeyboardInterrupt:
vm.dispose()