Class: Ocpp::Rails::ChargePoint

Inherits:
ApplicationRecord show all
Defined in:
app/models/ocpp/rails/charge_point.rb

Instance Method Summary collapse

Instance Method Details

#auth_password=(password) ⇒ Object

Stores the station's Basic Auth password as a SHA-256 digest. OCPP-J passwords are high-entropy machine credentials (the spec mandates 16-40 random bytes), so a fast unsalted hash is appropriate, like for API tokens.



23
24
25
# File 'app/models/ocpp/rails/charge_point.rb', line 23

def auth_password=(password)
  self.auth_password_digest = password.nil? ? nil : Digest::SHA256.hexdigest(password)
end

#authenticate_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'app/models/ocpp/rails/charge_point.rb', line 27

def authenticate_password?(password)
  return false if auth_password_digest.blank? || password.blank?

  ActiveSupport::SecurityUtils.fixed_length_secure_compare(
    Digest::SHA256.hexdigest(password),
    auth_password_digest
  )
end

#available?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/ocpp/rails/charge_point.rb', line 64

def available?
  status == "Available" && connected?
end

#current_sessionObject



60
61
62
# File 'app/models/ocpp/rails/charge_point.rb', line 60

def current_session
  charging_sessions.where(stopped_at: nil).order(started_at: :desc).first
end

#disconnect!Object



56
57
58
# File 'app/models/ocpp/rails/charge_point.rb', line 56

def disconnect!
  update(connected: false)
end

#heartbeat!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/ocpp/rails/charge_point.rb', line 36

def heartbeat!
  old_connected = connected
  update(last_heartbeat_at: Time.current, connected: true)

  # Log connection state change only if reconnecting (false -> true)
  if old_connected == false
    begin
      state_changes.create!(
        change_type: "connection",
        connector_id: nil,
        old_value: "false",
        new_value: "true",
        metadata: { source: "heartbeat" }
      )
    rescue => error
      ::Rails.logger.error("Failed to log state change: #{error.message}")
    end
  end
end