Class: Ocpp::Rails::ChargePoint
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Ocpp::Rails::ChargePoint
- Defined in:
- app/models/ocpp/rails/charge_point.rb
Instance Method Summary collapse
-
#auth_password=(password) ⇒ Object
Stores the station's Basic Auth password as a SHA-256 digest.
- #authenticate_password?(password) ⇒ Boolean
- #available? ⇒ Boolean
-
#connector_charging?(connector_id) ⇒ Boolean
Whether a transaction is running on the connector.
- #connector_error_code(connector_id) ⇒ Object
-
#connector_status(connector_id) ⇒ Object
Last status the station reported for this connector via StatusNotification; nil until the connector has reported once.
- #current_session ⇒ Object
- #disconnect! ⇒ Object
- #heartbeat! ⇒ Object
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.
26 27 28 |
# File 'app/models/ocpp/rails/charge_point.rb', line 26 def auth_password=(password) self.auth_password_digest = password.nil? ? nil : Digest::SHA256.hexdigest(password) end |
#authenticate_password?(password) ⇒ Boolean
30 31 32 33 34 35 36 37 |
# File 'app/models/ocpp/rails/charge_point.rb', line 30 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
67 68 69 |
# File 'app/models/ocpp/rails/charge_point.rb', line 67 def available? status == "Available" && connected? end |
#connector_charging?(connector_id) ⇒ Boolean
Whether a transaction is running on the connector. Authoritative by definition, unlike connector_status which is only as fresh as the station's last StatusNotification.
84 85 86 |
# File 'app/models/ocpp/rails/charge_point.rb', line 84 def connector_charging?(connector_id) charging_sessions.active.where(connector_id: connector_id).exists? end |
#connector_error_code(connector_id) ⇒ Object
77 78 79 |
# File 'app/models/ocpp/rails/charge_point.rb', line 77 def connector_error_code(connector_id) connector_statuses.find_by(connector_id: connector_id)&.error_code end |
#connector_status(connector_id) ⇒ Object
Last status the station reported for this connector via StatusNotification; nil until the connector has reported once.
73 74 75 |
# File 'app/models/ocpp/rails/charge_point.rb', line 73 def connector_status(connector_id) connector_statuses.find_by(connector_id: connector_id)&.status end |
#current_session ⇒ Object
63 64 65 |
# File 'app/models/ocpp/rails/charge_point.rb', line 63 def current_session charging_sessions.where(stopped_at: nil).order(started_at: :desc).first end |
#disconnect! ⇒ Object
59 60 61 |
# File 'app/models/ocpp/rails/charge_point.rb', line 59 def disconnect! update(connected: false) end |
#heartbeat! ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/ocpp/rails/charge_point.rb', line 39 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.}") end end end |