如何在OCPP SOAP客户端/服务器中实现Heartbeat等方法及通信约束?
实现OCPP 1.5的Heartbeat/BootNotification函数及通信约束
一、实现Heartbeat和BootNotification的具体步骤
你的代码目前缺少核心的服务实现对象(service),这是处理OCPP请求的关键。结合strong-soap工具,按以下步骤完成开发:
1. 定义Central System的服务实现
OCPP的WSDL已规定Central System需要处理的操作(如BootNotification、Heartbeat),你需要在service对象中实现对应处理函数,返回完全符合OCPP规范的响应:
// 定义Central System的服务逻辑,对应WSDL中的操作 const service = { CentralSystemService: { CentralSystemServiceSoap12: { // 处理ChargePoint发送的BootNotification请求 BootNotification: (args, callback) => { // args包含ChargePoint上报的厂商、型号等信息 console.log('收到BootNotification请求:', args); // 构造OCPP 1.5标准响应 const response = { status: 'Accepted', // 可选值:Accepted/Pending/Rejected currentTime: new Date().toISOString(), interval: 300 // 指定心跳间隔(单位:秒) }; callback(null, response); }, // 处理ChargePoint的心跳请求 Heartbeat: (args, callback) => { console.log('收到Heartbeat请求'); // 仅需返回当前时间即可 const response = { currentTime: new Date().toISOString() }; callback(null, response); } } } };
2. 修正服务器监听配置
确保soap.listen使用正确的路径和Central System的WSDL文件:
app.listen(port, () => { console.log(`Listening on port ${port}`); // 绑定Central System的服务路径,与ChargePoint的请求Endpoint对应 soap.listen(app, '/Ocpp/CentralSystemService/', service, xml); });
3. 客户端(模拟ChargePoint)调用OCPP方法
调整客户端代码,按OCPP时序发送请求:
soap.createClient(url, {}, (err, client) => { if(err) throw err; // 指向Central System的服务地址 client.setEndpoint('http://localhost:9330/Ocpp/CentralSystemService/'); // 启动后先发送BootNotification client.CentralSystemService.CentralSystemServiceSoap12.BootNotification( { chargePointVendor: 'TestVendor', chargePointModel: 'TestModel' }, (err, result) => { if(err) console.error('BootNotification错误:', err); else { console.log('BootNotification响应:', result); // 根据返回的interval定时发送Heartbeat setInterval(() => { client.CentralSystemService.CentralSystemServiceSoap12.Heartbeat({}, (err, hbResult) => { if(err) console.error('Heartbeat错误:', err); else console.log('Heartbeat响应:', hbResult); }); }, result.interval * 1000); } } ); });
二、编写OCPP通信函数的核心约束
OCPP是标准化的SOAP协议,编写相关函数必须严格遵循以下规则:
消息格式约束:
- 请求/响应的字段名、数据类型必须完全匹配OCPP 1.5 WSDL定义,比如
BootNotification请求必须包含chargePointVendor、chargePointModel,响应必须包含status、currentTime、interval。 - SOAP消息必须使用OCPP指定的命名空间:
http://www.iccp.eu/2010/08/ICCP(1.5版本)。
- 请求/响应的字段名、数据类型必须完全匹配OCPP 1.5 WSDL定义,比如
通信时序约束:
- ChargePoint启动后必须先发送
BootNotification,仅当收到Accepted状态的响应后,才能发送其他请求。 - Heartbeat必须严格按照Central System返回的
interval定时发送,不能随意调整间隔。
- ChargePoint启动后必须先发送
响应规则约束:
- 所有请求必须在30秒内返回响应(OCPP建议超时阈值)。
- 错误响应必须使用OCPP预定义的SOAP Fault代码,比如
ActionNotSupported、InternalError,禁止自定义错误码。
角色职责约束:
- Central System只能处理WSDL中定义的操作,拒绝未规范的请求;ChargePoint只能发送WSDL中定义的客户端操作。
- 部分操作(如
Reset)需要Central System返回确认响应后,ChargePoint才能执行对应动作。
内容的提问来源于stack exchange,提问作者Mustafa Mustafa Neverrun
相关产品推荐
相关产品推荐

