feat(ntfy): dispatch gateway_online/gateway_offline from model
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
class Gateway < ApplicationRecord
|
||||
include NtfyDispatchable
|
||||
|
||||
# Normalize metadata to always be a Hash
|
||||
attribute :metadata, :jsonb, default: {}
|
||||
|
||||
@@ -35,12 +37,31 @@ class Gateway < ApplicationRecord
|
||||
|
||||
# Update heartbeat timestamp and status
|
||||
def heartbeat!
|
||||
was_offline = status == "offline"
|
||||
update!(status: "online", last_heartbeat_at: Time.current)
|
||||
|
||||
if was_offline
|
||||
self.class.dispatch_ntfy("gateway_online",
|
||||
title: "Gateway online",
|
||||
message: "#{name} (#{device_id}) came back online",
|
||||
priority: 3,
|
||||
tags: ["white_check_mark"],
|
||||
click: nil)
|
||||
end
|
||||
end
|
||||
|
||||
# Mark gateway as offline
|
||||
def mark_offline!
|
||||
was_online = status == "online"
|
||||
update!(status: "offline")
|
||||
|
||||
if was_online
|
||||
self.class.dispatch_ntfy("gateway_offline",
|
||||
title: "Gateway offline",
|
||||
message: "#{name} (#{device_id}) went offline",
|
||||
priority: 4,
|
||||
tags: ["rotating_light"])
|
||||
end
|
||||
end
|
||||
|
||||
# Increment message counters
|
||||
|
||||
Reference in New Issue
Block a user