-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEVCS.py
More file actions
114 lines (94 loc) · 3.75 KB
/
Copy pathEVCS.py
File metadata and controls
114 lines (94 loc) · 3.75 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
"""
EVSE (Electric Vehicle Supply Equipment) Client Implementation
This module implements a simplified version of an OCPP (Open Charge Point Protocol) client
that simulates an EV charging station communicating with a central management system.
The client simulates basic charging operations:
- Connecting to central system
- Starting charging sessions
- Simulating charging time
- Stopping charging sessions
"""
import socket
import json
import time
class EVSE:
"""
Electric Vehicle Supply Equipment client class that simulates a charging station.
Implements a TCP client to communicate with the CSMS (Central System Management Server).
"""
def __init__(self):
"""
Initialize EVSE client with default connection settings and identification parameters.
Sets up connection details and basic charging station parameters.
"""
self.host = "127.0.0.1"
self.port = 12345
self.client = None
self.connector_id = 1 # Simple connector ID
self.id_tag = "EV12345" # Example ID tag
def connect(self):
"""
Establish connection with the CSMS server and initiate a charging sequence.
Performs a complete charging cycle (start -> simulate charging -> stop).
"""
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.connect((self.host, self.port))
print("EVSE connected to CSMS")
self.start_transaction()
self.stop_transaction()
def start_transaction(self):
"""
Initiate a charging transaction with the CSMS.
Sends a StartTransaction request with:
- connectorId: Identifier for the charging connector
- idTag: Authentication tag for the charging session
Simulates a charging session with a 10-second duration.
"""
start_transaction = {
"type": "StartTransaction",
"connectorId": self.connector_id,
"idTag": self.id_tag
}
print(f"Sending start transaction: {json.dumps(start_transaction, indent=2)}")
self.client.send(json.dumps(start_transaction).encode('utf-8'))
response = self.client.recv(1024).decode('utf-8')
print(f"Response from CSMS: {json.dumps(json.loads(response), indent=2)}")
# Simulate charging time
print("Charging in progress...")
time.sleep(10) # Simulate charging time
def stop_transaction(self):
"""
End a charging transaction with the CSMS.
Sends a StopTransaction request with:
- transactionId: Identifier for the charging session to stop
Performs a complete charging cycle (start -> simulate charging -> stop).
"""
stop_transaction = {
"type": "StopTransaction",
"transactionId": 1 # Assuming the transaction ID from start response
}
print(f"Sending stop transaction: {json.dumps(stop_transaction, indent=2)}")
self.client.send(json.dumps(stop_transaction).encode('utf-8'))
response = self.client.recv(1024).decode('utf-8')
print(f"Response from CSMS: {json.dumps(json.loads(response), indent=2)}")
def close(self):
"""
Clean up client resources and close the connection to CSMS.
"""
if self.client:
self.client.close()
print("EVSE disconnected from CSMS")
def main():
"""
Main entry point for the EVSE client.
Handles client startup, charging simulation, and graceful shutdown.
"""
evse = EVSE()
try:
evse.connect()
except Exception as e:
print(f"Error occurred: {e}")
finally:
evse.close()
if __name__ == "__main__":
main()