-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCSMS.py
More file actions
150 lines (121 loc) · 4.88 KB
/
Copy pathTCSMS.py
File metadata and controls
150 lines (121 loc) · 4.88 KB
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
### CSMS Server (Central System Management)
'''
Follow the encryption.md to setup the encryption chargers
'''
import socket
import json
import time
import ssl
class CSMS:
def __init__(self):
self.host = "0.0.0.0"
self.port = 12345
self.server = None
self.conn = None
self.addr = None
self.transaction_id = 1 # Simple transaction ID management
#TLS configuration
self.context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
self.context.load_cert_chain(certfile="csms_cert.pem",keyfile="csms_key.pem")
self.context.load_verify_locations(cafile="evcs_cert.pem") #verify EVSE certificate
self.context.verify_mode = ssl.CERT_REQUIRED # require client certificate
def start(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((self.host, self.port))
self.server.listen(1)
print("CSMS is waiting for 🔋EVSE🔋 connection...")
#Wrap socket with TLS
self.conn, self.addr = self.server.accept()
self.conn = self.context.wrap_socket(self.conn, server_side=True)
print(f"EVSE connected from {self.addr}")
self.communicate()
def communicate(self):
while True:
data = self.conn.recv(1024)
if not data:
break
try:
message = json.loads(data.decode('utf-8'))
print(f"Received from EVSE: {json.dumps(message, indent=2)}")
if message["type"] == "StartTransaction":
self.handle_start_transaction(message)
elif message["type"] == "StopTransaction":
self.handle_stop_transaction(message)
else:
self.send_error_response("Invalid request type")
except json.JSONDecodeError:
print("Invalid JSON received from EVSE")
self.send_error_response("Invalid JSON format")
except KeyError as e:
print(f"Missing key in message: {e}")
self.send_error_response("Missing required fields")
self.stop()
def handle_start_transaction(self, message):
try:
# Validate input fields
connector_id = message.get("connectorId")
id_tag = message.get("idTag")
if not connector_id or not id_tag:
self.send_error_response("Missing connectorId or idTag")
return
# Simulate transaction start
print(f"Starting transaction for ID Tag: {id_tag}")
time.sleep(1) # Simulate processing time
# Send transaction started response
response = {
"type": "TransactionStarted",
"status": "Accepted",
"transactionId": self.transaction_id,
"connectorId": connector_id
}
self.send_response(response)
self.transaction_id += 1 # Increment transaction ID
except Exception as e:
self.send_error_response(str(e))
def handle_stop_transaction(self, message):
try:
transaction_id = message.get("transactionId")
if not transaction_id:
self.send_error_response("Missing transactionId")
return
# Simulate transaction stop
print(f"Stopping transaction ID: {transaction_id}")
time.sleep(1) # Simulate processing time
# Send transaction stopped response
response = {
"type": "TransactionStopped",
"status": "Accepted",
"transactionId": transaction_id
}
self.send_response(response)
except Exception as e:
self.send_error_response(str(e))
def send_response(self, response):
self.conn.send(json.dumps(response).encode('utf-8'))
print(f"Sent response: {json.dumps(response, indent=2)}")
def send_error_response(self, message):
error_response = {
"type": "Error",
"message": message
}
self.send_response(error_response)
def stop(self):
if self.conn:
self.conn.close()
if self.server:
self.server.close()
print("CSMS stopped")
def main():
csms = CSMS()
try:
csms.start()
except KeyboardInterrupt:
print("CSMS shutdown requested...")
csms.stop()
except Exception as e:
print(f"Error occurred: {e}")
csms.stop()
if __name__ == "__main__":
main()
#openssl req -x509 -newkey rsa:4096 -keyout csms_key.pem -out csms_cert.pem -days 365 -nodes -subj "/CN=CSMS"
#scp csms_cert.pem user@EVSE_VM:/path/to/csms_cert.pem