Skip to content

Commit

Permalink
fix(delta): fix pagination
Browse files Browse the repository at this point in the history
also protect cache function
  • Loading branch information
stakach committed Apr 3, 2024
1 parent b0d1877 commit 9a37750
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions drivers/delta/api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Delta::API < PlaceOS::Driver
devices.push(Models::Device.new(id: key.to_u32, base: value["$base"].to_s, node_type: value["nodeType"].to_s, display_name: value["displayName"].to_s))
end

break if body.json_unmapped.keys.size < 1000
break unless body.next_req.presence
skip += 1000
end

Expand All @@ -96,7 +96,7 @@ class Delta::API < PlaceOS::Driver
objects.push(Models::Object.new(object_type, instance, base: value["$base"].to_s, display_name: value["displayName"].to_s))
end

break if body.json_unmapped.keys.size < 1000
break unless body.next_req.presence
skip += 1000
end

Expand Down
3 changes: 3 additions & 0 deletions drivers/delta/models/list_devices_by_site_name_response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module Delta
# returns this when there are no more results
@[JSON::Field(key: "Collection")]
property collection : String? = nil

@[JSON::Field(key: "next")]
property next_req : String? = nil
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module Delta

@[JSON::Field(key: "nodeType")]
property node_type : String

@[JSON::Field(key: "next")]
property next_req : String? = nil
end
end
end
14 changes: 5 additions & 9 deletions drivers/delta/uno_next.cr
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,13 @@ class Delta::UNOnext < PlaceOS::Driver

NO_OBJECTS = [] of Models::Object

def cache_sensor_data(zone_id : String? = nil, sensor : SensorType? = nil, device_id : UInt32? = nil) : Nil
protected def cache_sensor_data : Nil
logger.debug { "caching sensor data" }

# grab all the UNONext manager objects
site = site_name
all_objects = manager_mappings.flat_map do |man_map|
if zone = zone_id
next NO_OBJECTS unless zone.in?({man_map.building_zone, man_map.level_zone})
end

man_map.managers.flat_map do |id|
if device = device_id
next NO_OBJECTS unless id == device
end

begin
Array(Models::Object).from_json(delta_api.list_device_objects(site, id).get.to_json)
.select(&.display_name.includes?("UnoNext"))
Expand All @@ -204,6 +198,8 @@ class Delta::UNOnext < PlaceOS::Driver
end
end

logger.debug { "found #{all_objects.size} UnoNext objects" }

# parse them into sensor data
all_objects.each_slice(7) do |objects|
SENSOR_TYPES.each do |index, type|
Expand Down

0 comments on commit 9a37750

Please sign in to comment.