Class: Ocpp::Rails::Actions::StartTransactionHandler
- Inherits:
-
Object
- Object
- Ocpp::Rails::Actions::StartTransactionHandler
- Defined in:
- app/services/ocpp/rails/actions/start_transaction_handler.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(charge_point, message_id, payload) ⇒ StartTransactionHandler
constructor
A new instance of StartTransactionHandler.
Constructor Details
#initialize(charge_point, message_id, payload) ⇒ StartTransactionHandler
Returns a new instance of StartTransactionHandler.
5 6 7 8 9 |
# File 'app/services/ocpp/rails/actions/start_transaction_handler.rb', line 5 def initialize(charge_point, , payload) @charge_point = charge_point @message_id = @payload = payload end |
Instance Method Details
#call ⇒ Object
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 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 62 63 64 65 |
# File 'app/services/ocpp/rails/actions/start_transaction_handler.rb', line 11 def call id_tag = @payload["idTag"] = (id_tag) unless [:status] == "Accepted" ::Rails.logger.warn("[OCPP] StartTransaction from #{@charge_point.identifier} rejected for idTag #{id_tag}: #{[:status]}") # OCPP 1.6 requires transactionId in the response; the station # must ignore it when the idTag was not accepted. return { "idTagInfo" => { "status" => [:status] }, "transactionId" => 0 } end # A duplicate/replayed StartTransaction must not open a second # concurrent session on the connector; resume the open one instead. existing = active_session if existing ::Rails.logger.warn("[OCPP] StartTransaction from #{@charge_point.identifier}: Connector #{@payload['connectorId']} already has active transaction #{existing.transaction_id}, resuming it") return accepted_response(existing, ) end started_at = TimestampParser.parse(@payload["timestamp"]) begin session = @charge_point.charging_sessions.create!( connector_id: @payload["connectorId"], id_tag: id_tag, start_meter_value: @payload["meterStart"], started_at: started_at.time, status: "Charging", metadata: (started_at) ) rescue ActiveRecord::RecordNotUnique # Lost a race against a concurrent StartTransaction; the winner's # session is the one to resume. session = active_session raise unless session return accepted_response(session, ) end ::Rails.logger.info("[OCPP] StartTransaction from #{@charge_point.identifier}: Connector #{@payload['connectorId']}, Transaction ID: #{session.transaction_id}") # Connector status is owned by StatusNotification (OCPP 1.6); a # transaction starting says nothing about ChargePoint#status. # Duplicate/replayed StartTransactions resume the open session # above and intentionally do not re-fire this hook. Ocpp::Rails::SessionHookManager.execute_hooks(session, "started") # Broadcast session started event for real-time UI updates broadcast_session_started(session) accepted_response(session, ) end |