Class: Ocpp::Rails::ChargePointChannel

Inherits:
ActionCable::Channel::Base
  • Object
show all
Defined in:
app/channels/ocpp/rails/charge_point_channel.rb

Instance Method Summary collapse

Instance Method Details

#receive(data) ⇒ Object

Receive OCPP messages from charge point



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/channels/ocpp/rails/charge_point_channel.rb', line 49

def receive(data)
  return unless @charge_point

  # Drop before any processing or Message-row write, so a chatty or
  # malicious station cannot grow the database unboundedly.
  unless Ocpp::Rails.message_rate_limiter.allow?(@charge_point.identifier)
    logger.warn "[OCPP][security] ChargePoint #{@charge_point.identifier}: message rate limit exceeded, dropping message"
    return
  end

  message = data["message"]
  MessageHandler.new(@charge_point, message).process
rescue => e
  logger.error "Error receiving message from ChargePoint #{@charge_point&.identifier}: #{e.message}"
  logger.error e.backtrace.join("\n")
end

#subscribedObject

Charge point subscribes with their identifier; the credential travels in the HTTP Basic Authorization header of the WebSocket upgrade (OCPP-J Security Profile 1).



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
# File 'app/channels/ocpp/rails/charge_point_channel.rb', line 7

def subscribed
  charge_point_id = params[:charge_point_id]

  unless Ocpp::Rails.connection_rate_limiter.allow?(charge_point_id.to_s)
    reject
    logger.warn "[OCPP][security] ChargePoint #{charge_point_id} subscription rejected: connection rate limit exceeded"
    return
  end

  result = StationAuthenticator.authenticate(
    identifier: charge_point_id,
    authorization_header: authorization_header
  )

  unless result.success?
    reject
    logger.warn "[OCPP][security] ChargePoint #{charge_point_id} subscription rejected: #{result.failure}"
    return
  end

  @charge_point = result.charge_point
  stream_for @charge_point
  old_connected = @charge_point.connected
  @charge_point.update(connected: true, last_heartbeat_at: Time.current)
  logger.info "ChargePoint #{charge_point_id} connected"

  # Log connection state change if it actually changed
  log_connection_change(old_connected, true, "subscribed")
end

#unsubscribedObject



37
38
39
40
41
42
43
44
45
46
# File 'app/channels/ocpp/rails/charge_point_channel.rb', line 37

def unsubscribed
  if @charge_point
    old_connected = @charge_point.connected
    @charge_point.disconnect!
    logger.info "ChargePoint #{@charge_point.identifier} disconnected"

    # Log connection state change if it actually changed
    log_connection_change(old_connected, false, "unsubscribed")
  end
end