-
Notifications
You must be signed in to change notification settings - Fork 284
/
charge_point.py
296 lines (229 loc) · 10.2 KB
/
charge_point.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
286
287
288
289
290
291
292
293
294
295
296
import re
import time
import logging
import asyncio
import uuid
from dataclasses import asdict
from ocpp.routing import create_route_map
from ocpp.messages import Call, validate_payload, MessageType
from ocpp.exceptions import OCPPError, NotImplementedError
from ocpp.messages import unpack
LOGGER = logging.getLogger('ocpp')
def camel_to_snake_case(data):
"""
Convert all keys of all dictionaries inside the given argument from
camelCase to snake_case.
Inspired by: https://stackoverflow.com/a/1176023/1073222
"""
if isinstance(data, dict):
snake_case_dict = {}
for key, value in data.items():
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', key)
key = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
snake_case_dict[key] = camel_to_snake_case(value)
return snake_case_dict
if isinstance(data, list):
snake_case_list = []
for value in data:
snake_case_list.append(camel_to_snake_case(value))
return snake_case_list
return data
def snake_to_camel_case(data):
"""
Convert all keys of a all dictionaries inside given argument from
snake_case to camelCase.
Inspired by: https://stackoverflow.com/a/19053800/1073222
"""
if isinstance(data, dict):
camel_case_dict = {}
for key, value in data.items():
components = key.split('_')
key = components[0] + ''.join(x.title() for x in components[1:])
camel_case_dict[key] = snake_to_camel_case(value)
return camel_case_dict
if isinstance(data, list):
camel_case_list = []
for value in data:
camel_case_list.append(snake_to_camel_case(value))
return camel_case_list
return data
def remove_nones(dict_to_scan):
dict_to_scan = {
k: v for k, v in dict_to_scan.items()
if v is not None
}
return dict_to_scan
class ChargePoint:
"""
Base Element containing all the necessary OCPP1.6J messages for messages
initiated and received by the Central System
"""
def __init__(self, id, connection, response_timeout=30):
"""
Args:
charger_id (str): ID of the charger.
connection: Connection to CP.
response_timeout (int): When no response on a request is received
within this interval, a asyncio.TimeoutError is raised.
"""
self.id = id
# The maximum time in seconds it may take for a CP to respond to a
# CALL. An asyncio.TimeoutError will be raised if this limit has been
# exceeded.
self._response_timeout = response_timeout
# A connection to the client. Currently this is an instance of gh
self._connection = connection
# A dictionary that hooks for Actions. So if the CS receives a it will
# look up the Action into this map and execute the corresponding hooks
# if exists.
self.route_map = create_route_map(self)
self._call_lock = asyncio.Lock()
# A queue used to pass CallResults and CallErrors from
# the self.serve() task to the self.call() task.
self._response_queue = asyncio.Queue()
# Function used to generate unique ids for CALLs. By default
# uuid.uuid4() is used, but it can be changed. This is meant primarily
# for testing purposes to have predictable unique ids.
self._unique_id_generator = uuid.uuid4
async def start(self):
while True:
message = await self._connection.recv()
LOGGER.info('%s: receive message %s', self.id, message)
await self.route_message(message)
async def route_message(self, raw_msg):
"""
Route a message received from a CP.
If the message is a of type Call the corresponding hooks are executed.
If the message is of type CallResult or CallError the message is passed
to the call() function via the response_queue.
"""
try:
msg = unpack(raw_msg)
except OCPPError as e:
LOGGER.exception("Unable to parse message: '%s', it doesn't seem "
"to be valid OCPP: %s", raw_msg, e)
return
if msg.message_type_id == MessageType.Call:
await self._handle_call(msg)
elif msg.message_type_id in \
[MessageType.CallResult, MessageType.CallError]:
self._response_queue.put_nowait(msg)
async def _handle_call(self, msg):
"""
Execute all hooks installed for based on the Action of the message.
First the '_on_action' hook is executed and its response is returned to
the client. If there is no '_on_action' hook for Action in the message
a CallError with a NotImplemtendError is returned.
Next the '_after_action' hook is executed.
"""
try:
handlers = self.route_map[msg.action]
except KeyError:
raise NotImplementedError(f"No handler for '{msg.action}' "
"registered.")
if not handlers.get('_skip_schema_validation', False):
validate_payload(msg, self._ocpp_version)
# OCPP uses camelCase for the keys in the payload. It's more pythonic
# to use snake_case for keyword arguments. Therefore the keys must be
# 'translated'. Some examples:
#
# * chargePointVendor becomes charge_point_vendor
# * firmwareVersion becomes firmwareVersion
snake_case_payload = camel_to_snake_case(msg.payload)
try:
handler = handlers['_on_action']
except KeyError:
raise NotImplementedError(f"No handler for '{msg.action}' "
"registered.")
try:
response = await asyncio.coroutine(handler)(**snake_case_payload)
except Exception as e:
LOGGER.exception("Error while handling request '%s'", msg)
response = msg.create_call_error(e).to_json()
await self._send(response)
return
temp_response_payload = asdict(response)
# Remove nones ensures that we strip out optional arguments
# which were not set and have a default value of None
response_payload = remove_nones(temp_response_payload)
# The response payload must be 'translated' from snake_case to
# camelCase. So:
#
# * charge_point_vendor becomes chargePointVendor
# * firmware_version becomes firmwareVersion
camel_case_payload = snake_to_camel_case(response_payload)
response = msg.create_call_result(camel_case_payload)
if not handlers.get('_skip_schema_validation', False):
validate_payload(response, self._ocpp_version)
await self._send(response.to_json())
try:
handler = handlers['_after_action']
# Create task to avoid blocking when making a call inside the
# after handler
asyncio.ensure_future(
asyncio.coroutine(handler)(**snake_case_payload))
except KeyError:
# '_on_after' hooks are not required. Therefore ignore exception
# when no '_on_after' hook is installed.
pass
async def call(self, payload):
"""
Send Call message to client and return payload of response.
The given payload is transformed into a Call object by looking at the
type of the payload. A payload of type BootNotificationPayload will
turn in a Call with Action BootNotification, a HeartbeatPayload will
result in a Call with Action Heartbeat etc.
A timeout is raised when no response has arrived before expiring of
the configured timeout.
When waiting for a response no other Call message can be send. So this
function will wait before response arrives or response timeout has
expired. This is in line the OCPP specification
"""
camel_case_payload = snake_to_camel_case(asdict(payload))
call = Call(
unique_id=str(self._unique_id_generator()),
action=payload.__class__.__name__[:-7],
payload=remove_nones(camel_case_payload)
)
# Use a lock to prevent make sure that only 1 message can be send at a
# a time.
async with self._call_lock:
await self._send(call.to_json())
response = \
await self._get_specific_response(call.unique_id,
self._response_timeout)
if response.message_type_id == MessageType.CallError:
LOGGER.warning("Received a CALLError: %s'", response)
return
else:
response.action = call.action
validate_payload(response, self._ocpp_version)
snake_case_payload = camel_to_snake_case(response.payload)
# Create the correct Payload instance based on the received payload. If
# this method is called with a call.BootNotificationPayload, then it
# will create a call_result.BootNotificationPayload. If this method is
# called with a call.HeartbeatPayload, then it will create a
# call_result.HeartbeatPayload etc.
cls = getattr(self._call_result, payload.__class__.__name__) # noqa
return cls(**snake_case_payload)
async def _get_specific_response(self, unique_id, timeout):
"""
Return response with given unique ID or raise an asyncio.TimeoutError.
"""
wait_until = time.time() + timeout
try:
# Wait for response of the Call message.
response = await asyncio.wait_for(self._response_queue.get(),
timeout)
except asyncio.TimeoutError:
raise
if response.unique_id == unique_id:
return response
LOGGER.error('Ignoring response with unknown unique id: %s', response)
timeout_left = wait_until - time.time()
if timeout_left < 0:
raise asyncio.TimeoutError
return await self._get_specific_response(unique_id, timeout_left)
async def _send(self, message):
LOGGER.info('%s: send %s', self.id, message)
await self._connection.send(message)