Class: Ocpp::Rails::Actions::StopTransactionHandler

Inherits:
Object
  • Object
show all
Defined in:
app/services/ocpp/rails/actions/stop_transaction_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(charge_point, message_id, payload) ⇒ StopTransactionHandler

Returns a new instance of StopTransactionHandler.



5
6
7
8
9
# File 'app/services/ocpp/rails/actions/stop_transaction_handler.rb', line 5

def initialize(charge_point, message_id, payload)
  @charge_point = charge_point
  @message_id = message_id
  @payload = payload
end

Instance Method Details

#callObject



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

def call
  # Find the session by the wire transaction ID
  session = @charge_point.charging_sessions.find_by(transaction_id: @payload["transactionId"])

  unless session
    ::Rails.logger.error("[OCPP] StopTransaction: Session not found for transaction ID #{@payload['transactionId']}")
    return {
      "idTagInfo" => {
        "status" => "Invalid"
      }
    }
  end

  # Stop the session
  session.stop!(
    reason: @payload["reason"] || "Local",
    meter_value: @payload["meterStop"]
  )

  ::Rails.logger.info("[OCPP] StopTransaction from #{@charge_point.identifier}: Transaction ID #{session.transaction_id}, Energy: #{session.energy_consumed} Wh")

  # Process transaction data (meter values during charging) if provided
  if @payload["transactionData"]
    process_transaction_data(session, @payload["transactionData"])
  end

  # Connector status is owned by StatusNotification (OCPP 1.6); the
  # station reports Finishing/Available itself after the transaction.

  # Fired after transactionData so hooks see the Transaction.End
  # meter values. Stations recovering from an offline period may
  # still deliver queued MeterValues after this point.
  Ocpp::Rails::SessionHookManager.execute_hooks(session, "stopped")

  # Broadcast session stopped event for real-time UI updates
  broadcast_session_stopped(session)

  # Return authorization status
  {
    "idTagInfo" => {
      "status" => "Accepted"
    }
  }
end