2e5960fae3
- Created initial wiki structure with CLAUDE.md schema, index.md, and log.md. - Ingested findings and requirements document into the wiki. - Added concepts for authorization methods, network topology, OCPP protocol, and troubleshooting. - Documented entities including the Anfu charger, JC-6620 controller, and CSMS toolkit. - Established configuration files for Obsidian app settings and workspace layout. - Implemented core plugins and appearance settings for the wiki.
159 lines
5.4 KiB
Python
159 lines
5.4 KiB
Python
"""Minimal OCPP 1.6J central system for discovering/testing a charge point.
|
|
|
|
Accepts any charge point ID on ws://0.0.0.0:9000/<id>, answers the standard
|
|
message set with "Accepted", and logs every raw frame in both directions.
|
|
|
|
Run: .venv/bin/python csms.py
|
|
Then point the charger's OCPP backend URL at ws://<host-ip>:9000/
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
from datetime import datetime, timezone
|
|
|
|
import websockets
|
|
|
|
from ocpp.routing import on
|
|
from ocpp.v16 import ChargePoint as OcppChargePoint
|
|
from ocpp.v16 import call_result
|
|
from ocpp.v16.enums import (
|
|
Action,
|
|
AuthorizationStatus,
|
|
DataTransferStatus,
|
|
RegistrationStatus,
|
|
)
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s %(levelname)s %(name)s | %(message)s",
|
|
)
|
|
log = logging.getLogger("csms")
|
|
|
|
|
|
def now() -> str:
|
|
return datetime.now(timezone.utc).isoformat()
|
|
|
|
|
|
class ChargePoint(OcppChargePoint):
|
|
@on(Action.boot_notification)
|
|
async def on_boot_notification(self, charge_point_vendor, charge_point_model, **kwargs):
|
|
log.info(
|
|
"BootNotification: vendor=%r model=%r extra=%r",
|
|
charge_point_vendor, charge_point_model, kwargs,
|
|
)
|
|
return call_result.BootNotification(
|
|
current_time=now(), interval=30, status=RegistrationStatus.accepted
|
|
)
|
|
|
|
@on(Action.heartbeat)
|
|
async def on_heartbeat(self):
|
|
return call_result.Heartbeat(current_time=now())
|
|
|
|
@on(Action.status_notification)
|
|
async def on_status_notification(self, connector_id, error_code, status, **kwargs):
|
|
log.info(
|
|
"StatusNotification: connector=%s status=%s error=%s extra=%r",
|
|
connector_id, status, error_code, kwargs,
|
|
)
|
|
return call_result.StatusNotification()
|
|
|
|
@on(Action.authorize)
|
|
async def on_authorize(self, id_tag, **kwargs):
|
|
log.info("Authorize: id_tag=%r", id_tag)
|
|
return call_result.Authorize(id_tag_info={"status": AuthorizationStatus.accepted})
|
|
|
|
@on(Action.start_transaction)
|
|
async def on_start_transaction(self, connector_id, id_tag, meter_start, timestamp, **kwargs):
|
|
log.info(
|
|
"StartTransaction: connector=%s id_tag=%r meter_start=%s",
|
|
connector_id, id_tag, meter_start,
|
|
)
|
|
return call_result.StartTransaction(
|
|
transaction_id=1,
|
|
id_tag_info={"status": AuthorizationStatus.accepted},
|
|
)
|
|
|
|
@on(Action.stop_transaction)
|
|
async def on_stop_transaction(self, meter_stop, timestamp, transaction_id, **kwargs):
|
|
log.info(
|
|
"StopTransaction: tx=%s meter_stop=%s extra=%r",
|
|
transaction_id, meter_stop, kwargs,
|
|
)
|
|
return call_result.StopTransaction(
|
|
id_tag_info={"status": AuthorizationStatus.accepted}
|
|
)
|
|
|
|
@on(Action.meter_values)
|
|
async def on_meter_values(self, connector_id, meter_value, **kwargs):
|
|
log.info("MeterValues: connector=%s values=%r", connector_id, meter_value)
|
|
return call_result.MeterValues()
|
|
|
|
@on(Action.data_transfer)
|
|
async def on_data_transfer(self, vendor_id, **kwargs):
|
|
log.info("DataTransfer: vendor_id=%r extra=%r", vendor_id, kwargs)
|
|
return call_result.DataTransfer(status=DataTransferStatus.accepted)
|
|
|
|
@on(Action.firmware_status_notification)
|
|
async def on_firmware_status(self, status, **kwargs):
|
|
log.info("FirmwareStatusNotification: %s", status)
|
|
return call_result.FirmwareStatusNotification()
|
|
|
|
@on(Action.diagnostics_status_notification)
|
|
async def on_diagnostics_status(self, status, **kwargs):
|
|
log.info("DiagnosticsStatusNotification: %s", status)
|
|
return call_result.DiagnosticsStatusNotification()
|
|
|
|
|
|
async def on_connect(connection):
|
|
path = connection.request.path
|
|
charge_point_id = path.strip("/").split("/")[-1] or "unknown"
|
|
log.info(
|
|
"Connection from %s | path=%r -> charge_point_id=%r | subprotocol=%r",
|
|
connection.remote_address, path, charge_point_id, connection.subprotocol,
|
|
)
|
|
if connection.subprotocol is None:
|
|
log.warning(
|
|
"Charge point %r connected WITHOUT an ocpp subprotocol header. "
|
|
"Handling it anyway so we can see its traffic.",
|
|
charge_point_id,
|
|
)
|
|
cp = ChargePoint(charge_point_id, connection)
|
|
try:
|
|
await cp.start()
|
|
except websockets.exceptions.ConnectionClosed:
|
|
log.info("Charge point %r disconnected", charge_point_id)
|
|
|
|
|
|
async def process_request(connection, request):
|
|
"""Log every inbound HTTP/WS request line so we see even failed attempts."""
|
|
log.info(
|
|
"HTTP request from %s: %s %s | headers=%s",
|
|
connection.remote_address,
|
|
request.method if hasattr(request, "method") else "?",
|
|
request.path,
|
|
dict(request.headers),
|
|
)
|
|
return None # let the handshake proceed normally
|
|
|
|
|
|
async def main():
|
|
# select_subprotocol tolerates clients that omit the ocpp1.6 header
|
|
# (some cheap firmwares do), so we still see their traffic instead of a 400.
|
|
def select_subprotocol(connection, subprotocols):
|
|
return "ocpp1.6"
|
|
|
|
server = await websockets.serve(
|
|
on_connect,
|
|
"0.0.0.0",
|
|
9000,
|
|
subprotocols=["ocpp1.6"],
|
|
select_subprotocol=select_subprotocol,
|
|
process_request=process_request,
|
|
)
|
|
log.info("OCPP 1.6J CSMS listening on ws://0.0.0.0:9000/<charge_point_id>")
|
|
await server.wait_closed()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|