如何在使用Node.js的OCPP服务器中启动远程事务

huangapple go评论54阅读模式
英文:

How to start remote transection in OCPP server using node js

问题

我们使用Node.js创建了OCPP服务器,使用ocpp-rpc的帮助,并从OCPP服务器获得了WebSocket连接。以下是我的示例代码:

const { RPCServer, createRPCError } = require('ocpp-rpc');

const server = new RPCServer({
  protocols: ['ocpp1.6'], // 服务器接受ocpp1.6子协议
  strictMode: true,       // 启用请求和响应的严格验证
 });

server.auth((accept, reject, handshake) => {
  // 接受传入的客户端
  accept({
      // 传递给accept()的任何内容将附加为客户端的'session'属性。
      sessionId: 'XYZ123'
  });
});

server.on('client', async (client) => {
  console.log(`${client.session.sessionId} connected!`); // 'XYZ123 connected!'

  // 为处理BootNotification请求创建特定的处理程序
  client.handle('BootNotification', ({params}) => {
      console.log(`服务器从${client.identity}接收到BootNotification:`, params);

      // 响应以接受客户端
      return {
          status: "Accepted",
          interval: 300,
          currentTime: new Date().toISOString()
      };
  });
  
  // 为处理Heartbeat请求创建特定的处理程序
  client.handle('Heartbeat', ({params}) => {
      console.log(`服务器从${client.identity}接收到Heartbeat:`, params);

      // 用服务器的当前时间作出响应。
      return {
          currentTime: new Date().toISOString()
      };
  });
  
  // 为处理StatusNotification请求创建特定的处理程序
  client.handle('StatusNotification', ({params}) => {
      console.log(`服务器从${client.identity}接收到StatusNotification:`, params);
      return {};
  });

  // 创建一个通配符处理程序以处理任何RPC方法
  client.handle(({method, params}) => {
      // 如果无法在其他地方处理传入的方法,将调用此处理程序。
      console.log(`服务器从${client.identity}接收到${method}:`, params);

      // 抛出RPC错误以通知服务器我们不理解请求。
      throw createRPCError("NotImplemented");
  });
});

await server.listen(3000);

现在我的问题是如何从移动应用程序启动远程交易?我是否需要调用自己的OCPP服务器?如何从充电站点获取数据?根据我们的需求,我们将Node.js OCPP服务器链接添加到ABB充电站配置中。如果有任何公共存储库、博客或示例代码可用,请提供帮助。提前感谢。

英文:

We have created OCPP server using node js with help of ocpp-rpc and we got web socket connection from OCPP server here is the my sample code

const { RPCServer, createRPCError } = require('ocpp-rpc');
const server = new RPCServer({
protocols: ['ocpp1.6'], // server accepts ocpp1.6 subprotocol
strictMode: true,       // enable strict validation of requests & responses
});
server.auth((accept, reject, handshake) => {
// accept the incoming client
accept({
// anything passed to accept() will be attached as a 'session' property of the client.
sessionId: 'XYZ123'
});
});
server.on('client', async (client) => {
console.log(`${client.session.sessionId} connected!`); // `XYZ123 connected!`
// create a specific handler for handling BootNotification requests
client.handle('BootNotification', ({params}) => {
console.log(`Server got BootNotification from ${client.identity}:`, params);
// respond to accept the client
return {
status: "Accepted",
interval: 300,
currentTime: new Date().toISOString()
};
});
// create a specific handler for handling Heartbeat requests
client.handle('Heartbeat', ({params}) => {
console.log(`Server got Heartbeat from ${client.identity}:`, params);
// respond with the server's current time.
return {
currentTime: new Date().toISOString()
};
});
// create a specific handler for handling StatusNotification requests
client.handle('StatusNotification', ({params}) => {
console.log(`Server got StatusNotification from ${client.identity}:`, params);
return {};
});
// create a wildcard handler to handle any RPC method
client.handle(({method, params}) => {
// This handler will be called if the incoming method cannot be handled elsewhere.
console.log(`Server got ${method} from ${client.identity}:`, params);
// throw an RPC error to inform the server that we don't understand the request.
throw createRPCError("NotImplemented");
});
});
await server.listen(3000);

now My question is how to start remote transaction from mobile app ? should i need to call my own OCPP server ? and how i will get data from the charging station point. as per our need we are adding node js OCPP server link to the ABB charging station config. please help if there any public repo or any blog or any sample code is available.
Thanks in advance

答案1

得分: 0

不要回答我要翻译的问题。

以下是要翻翻译的内容:

"To make an OCPP call to a charging station using ocpp-rpc, first you'll need a reference to the charging station's client connection. Adapting your example, here's one way you could set this up based on the charging station's OCPP identity:

const allClients = new Map();

server.on('client', async (client) => {
    console.log(`${client.identity} connected!`);

    allClients set(client.identity, client); // store client reference
});

Next, you need a way to trigger and make your call. Let's assume for a moment that you are using a http framework like express to create a REST API for this purpose. You could route a request like so:

app.post('/charging-station/:identity/start-transaction', async (req, res, next) => {
    try {

        const client = allClients.get(req.params.identity);

        if (!client) {
            throw Error("Client not found");
        }

        const response = await client.call('RemoteStartTransaction', {
            connectorId: 1, // start on connector 1
            idTag: 'XXXXXXXX', // using an idTag with identity 'XXXXXXXX'
        });

        if (response.status === 'Accepted') {
            console.log('Remote start worked!');
        } else {
            console.log('Remote start rejected.');
        }

    } catch (err) {
        next(err);
    }
});

Obviously if you're not using express, you'll need to adapt this to work for your situation.

Hope that gives you a good starting point to go from."

英文:

To make an OCPP call to a charging station using ocpp-rpc, first you'll need a reference to the charging station's client connection. Adapting your example, here's one way you could set this up based on the charging station's OCPP identity:

const allClients = new Map();

server.on('client', async (client) => {
    console.log(`${client.identity} connected!`);

    allClients.set(client.identity, client); // store client reference
});

Next, you need a way to trigger and make your call. Let's assume for a moment that you are using a http framework like express to create a REST API for this purpose. You could route a request like so:

app.post('/charging-station/:identity/start-transaction', async (req, res, next) => {
    try {

        const client = allClients.get(req.params.identity);

        if (!client) {
            throw Error("Client not found");
        }

        const response = await client.call('RemoteStartTransaction', {
            connectorId: 1, // start on connector 1
            idTag: 'XXXXXXXX', // using an idTag with identity 'XXXXXXXX'
        });

        if (response.status === 'Accepted') {
            console.log('Remote start worked!');
        } else {
            console.log('Remote start rejected.');
        }

    } catch (err) {
        next(err);
    }
});

Obviously if you're not using express, you'll need to adapt this to work for your situation.

Hope that gives you a good starting point to go from.

huangapple
  • 本文由 发表于 2023年4月4日 13:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定