mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 02:04:57 +00:00
Add heartbeat model, next_heartbeat_time and update_heartbeat_time on device registration
This commit is contained in:
@@ -2,8 +2,19 @@ class Device < ActiveRecord::Base
|
||||
validates :unique_id, :model, presence: true
|
||||
validates :unique_id, uniqueness: true
|
||||
before_create :generate_access_token
|
||||
after_create :update_last_heartbeats_time
|
||||
has_many :heartbeats, dependent: :destroy
|
||||
|
||||
def generate_access_token
|
||||
self.access_token = SecureRandom.uuid
|
||||
end
|
||||
|
||||
def update_last_heartbeats_time
|
||||
self.last_heartbeat_recd_time = self.updated_at
|
||||
self.save
|
||||
end
|
||||
|
||||
def next_heartbeat_time
|
||||
(self.last_heartbeat_recd_time + HEARTBEAT_INTERVAL).to_i
|
||||
end
|
||||
end
|
||||
|
||||
14
app/models/heartbeat.rb
Normal file
14
app/models/heartbeat.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
class Heartbeat < ActiveRecord::Base
|
||||
belongs_to :device, counter_cache: :heartbeats_count
|
||||
|
||||
after_create :update_device_latest_heartbeat_recd_time
|
||||
|
||||
def update_device_latest_heartbeat_recd_time
|
||||
self.device.last_heartbeat_recd_time = self.created_at
|
||||
self.device.save
|
||||
end
|
||||
|
||||
def next_heartbeat_time
|
||||
device.next_heartbeat_time
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user