-
Notifications
You must be signed in to change notification settings - Fork 4
/
web.py
239 lines (191 loc) · 8.47 KB
/
web.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
import json
import logging
import os
import threading
import bottle
from bottle import Bottle, request, response, static_file, redirect, template
from config import config
from session import Session
from utils import dictget
from version import __version__
bottle.TEMPLATE_PATH = [config['www_path']] # must be a list !!!
class AppWeb:
"""
Webserverintegration for application
"""
def __init__(self, app, log='web'):
self.app = app # reference to application
self.log = logging.getLogger(log)
self.web = Bottle() # webserver
self.session = Session(password=config['password'])
self.manual_session = None # session of the client which has manual control
# setup routes
self.web.route('/', callback=self.web_index) # hosting static files
self.web.route('/login', callback=self.web_login, method=('GET', 'POST')) # application state in json format
self.web.route('/api/state', callback=self.web_api_state, method=('GET', 'POST'))
self.web.route('/api/state/<state>', callback=self.web_api_state)
self.web.route('/api/set', callback=self.web_api_set, method=('GET', 'POST'))
self.web.route('/api/bms', callback=self.web_api_bms) # full bms data in json format
self.web.route('/debug/<cmd>', callback=self.web_debug_cmd) # debug commands
self.web.route('/log', callback=self.web_log) # access to logfile
self.web.route('/chart', callback=self.web_chart) #
self.web.route('/blackbox', callback=lambda : "\n".join(self.app.blackbox.record_lines)) #
self.web.route('/<filepath:path>', callback=self.web_static) # hosting static files
logging.getLogger('waitress.queue').setLevel(logging.ERROR) # hide waitress info log
# start webserver thread
threading.Thread(target=self.web.run, daemon=True,
kwargs=dict(host='0.0.0.0', port=config['http_port'], server='waitress')).start()
def web_login(self):
"""
Login view
:return:
"""
password = request.forms.get("password", default=None)
remote_addr = request.environ.get('REMOTE_ADDR')
if password is None:
self.log.info("login form request from {}".format(remote_addr))
elif self.session.login(password):
self.log.info("login from {} successful, redirect to /".format(remote_addr))
redirect('/')
return
else:
self.log.error("login attempt from {} with invalid password: {}".format(remote_addr, password))
return static_file('login.html', root=config['www_path'])
def web_index(self, filepath='index.html'):
"""
Webserver interface for static files
"""
session_id = request.get_cookie('session')
remote_addr = request.environ.get('REMOTE_ADDR')
if self.session.is_valid(session_id): # static files only after login (session)
self.log.debug(
"request to: {} from: {} with valid session_id: {}".format(filepath, remote_addr, session_id))
bottle.TEMPLATES.clear() # DEBUG !!!!
# https://pwp.stevecassidy.net/bottle/templating/
d = {
"version": __version__,
"bms_pack_number": 1,
"enable_car": config['enable_car'],
"enable_heat": config['enable_heat'],
"setting": [s['name'] for s in config['setting']]
}
try:
d["bms_pack_number"] = config['bms_us2000']['pack_number']
except:
pass
return template('index.html', d)
# return static_file('index.html', root=self.app.www_path)
else:
if session_id:
self.log.info(
"index request from:{} unknown session_id: {}, redirect to /login".format(remote_addr, session_id))
else:
self.log.info(
"index request from:{} without session_id, redirect to /login".format(filepath, remote_addr))
redirect('/login')
def web_static(self, filepath=None):
"""
Webserver interface for static files
"""
if filepath == 'index.html':
self.web_index()
else:
return static_file(filepath, root=self.app.www_path)
def web_log(self):
"""
/log Webserver interface to access the logfile
"""
response.content_type = 'text/plain'
return open(os.path.join('log', 'log.txt'), 'r').read()
def web_api_state(self):
"""
/api/state Webserver interface to get application state in JSON
"""
try:
post = json.loads(request.body.read()) # manual_set_p
self.app.ui_command = post
# print("web post ui_command", post)
except:
pass
session_id = request.get_cookie('session')
state = self.app.get_state()
# manual auth
if self.app.mode == 'manual' and self.manual_session and session_id == self.manual_session:
state['manual_auth'] = True
# valid session
if not self.session.is_valid(session_id):
state['session_invalid'] = True
return state
def web_api_set(self):
"""
/api/set Webserver interface to set by JSON
"""
session_id = request.get_cookie('session')
if self.session.is_valid(session_id):
try:
post = json.loads(request.body.read()) # manual_set_p
self.log.info("/api/set {}".format(post))
if 'option' in post:
self.app.setting = int(post['option'])
if 'mode' in post and post['mode'] in ('off', 'auto', 'manual'):
self.app.mode = post['mode']
self.app.charge_start_timer.set_expired() # trigger
self.app.feed_start_timer.set_expired() # trigger
if post['mode'] == 'manual':
self.manual_session = session_id
self.log.info('manual control for session: {} started'.format(session_id))
else:
self.manual_session = None
if 'reset_error' in post and post['reset_error'] is True: # init = Error reset
self.log.info("manual error reset !")
self.app.set_fsm_state('init')
except Exception as e:
self.log.error('/api/set exception: {}'.format(e))
else:
self.log.error('/api/set without valid session')
return self.app.get_state()
def web_api_bms(self):
"""
/api/state Webserver interface to get full bms info as json
"""
response.content_type = 'application/json'
return json.dumps(self.app.bms.get_detail())
def web_debug_cmd(self, cmd):
"""
"""
if cmd == 'mp2online':
s = "mp2online debug command"
self.app.multiplus.online = True
else:
s = "unknown debug command: {}".format(cmd)
self.log.info(s)
return s
def web_chart(self):
"""
"""
# chart = self.app.trace.get_chart([('home', ('meterhub', 'home_all_p')), ('grid', ('meterhub', 'grid_p')), ('grid', ('meterhub', 'car_p'))])
chart = {'x': [], 'home': [], 'grid': [], 'bat_feed': [], 'bat_charge': [], 'charge_set': [], 'feed_set': []}
i = 0
for t in self.app.trace.buffer:
grid = dictget(t, ('meterhub', 'grid_p'))
bat = dictget(t, ('meterhub', 'bat_p'))
home_all = dictget(t, ('meterhub', 'home_all_p'))
car = dictget(t, ('meterhub', 'car_p'), 0)
home = dictget(t, ('meterhub', 'home_p'))
set_p = dictget(t, ('ess', 'set_p'))
try:
charge_set = max(set_p, 0)
feed_set = max(-set_p, 0)
except:
charge_set = None
feed_set = None
chart['home'].append(home)
chart['grid'].append(grid)
chart['bat_charge'].append(bat if bat and bat > 0 else 0)
chart['bat_feed'].append(-bat if bat and bat < 0 else 0)
chart['charge_set'].append(charge_set)
chart['feed_set'].append(feed_set)
chart['x'].append(i)
i += 1
response.content_type = 'application/javascript'
return "var chart =" + json.dumps(chart)