Module: Ocpp::Rails::Protocol

Defined in:
app/services/ocpp/rails/protocol.rb

Class Method Summary collapse

Class Method Details

.build_call(message_id, action, payload) ⇒ Object

Build a CALL message (request from server to charge point or charge point to server) Format: [2, "message_id", "Action", payload]



7
8
9
# File 'app/services/ocpp/rails/protocol.rb', line 7

def build_call(message_id, action, payload)
  [ 2, message_id, action, payload ].to_json
end

.build_callerror(message_id, error_code, error_description, details = {}) ⇒ Object

Build a CALLERROR message (error response) Format: [4, "message_id", "error_code", "error_description", details]



19
20
21
# File 'app/services/ocpp/rails/protocol.rb', line 19

def build_callerror(message_id, error_code, error_description, details = {})
  [ 4, message_id, error_code, error_description, details ].to_json
end

.build_callresult(message_id, payload) ⇒ Object

Build a CALLRESULT message (successful response) Format: [3, "message_id", payload]



13
14
15
# File 'app/services/ocpp/rails/protocol.rb', line 13

def build_callresult(message_id, payload)
  [ 3, message_id, payload ].to_json
end

.parse(raw_message) ⇒ Object

Parse incoming JSON-RPC 2.0 message



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
# File 'app/services/ocpp/rails/protocol.rb', line 24

def parse(raw_message)
  data = JSON.parse(raw_message)

  case data[0]
  when 2
    {
      type: "CALL",
      message_id: data[1],
      action: data[2],
      payload: data[3] || {}
    }
  when 3
    {
      type: "CALLRESULT",
      message_id: data[1],
      payload: data[2] || {}
    }
  when 4
    {
      type: "CALLERROR",
      message_id: data[1],
      error_code: data[2],
      error_description: data[3],
      details: data[4] || {}
    }
  else
    {
      type: "UNKNOWN",
      raw: data
    }
  end
rescue JSON::ParserError => e
  {
    type: "PARSE_ERROR",
    error: e.message,
    raw: raw_message
  }
end