Skip to content

Commit

Permalink
feat(kontakt_io): collect map room_ids to map_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed May 8, 2024
1 parent b67249e commit 81a001e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
29 changes: 28 additions & 1 deletion drivers/kontakt_io/sensor_service.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class KontaktIO::SensorService < PlaceOS::Driver
accessor kontakt_io : KontaktIO_1
bind KontaktIO_1, :occupancy_cached_at, :update_cache

accessor staff_api : StaffAPI_1
accessor location_service : LocationServices_1

default_settings({
floor_mappings: {
"KontaktIO_floor_id": {
Expand Down Expand Up @@ -61,6 +64,30 @@ class KontaktIO::SensorService < PlaceOS::Driver
@occupancy_cache = Hash(Int64, RoomOccupancy).from_json kontakt_io.occupancy_cache.get.to_json
end

# System id => Map ID
getter system_map_ids : Hash(String, String) do
building_zone = location_service.building_id.get.as_s
map_ids = {} of String => String
staff_api.systems(zone_id: building_zone).get.as_a.each do |sys|
map_id = sys["map_id"]?.try(&.as_s?)
next unless map_id
map_ids[sys["id"].as_s] = map_id
end
map_ids
end

# KIO room id => Map ID
getter map_ids : Hash(Int64, String) do
ids = {} of Int64 => String
system_map_ids.each do |sys_id, map_id|
resp = staff_api.system_settings(sys_id, "space_ref_id").get
value = resp.as_s?.try(&.to_i64?) || resp.as_i64?
next unless value
ids[value] = map_id
end
ids
end

# ===================================
# Locatable Interface functions
# ===================================
Expand Down Expand Up @@ -111,7 +138,7 @@ class KontaktIO::SensorService < PlaceOS::Driver
{
location: loc_type,
at_location: people_count,
map_id: "room-#{space.room_id}",
map_id: map_ids[space.room_id]? || "room-#{space.room_id}",
level: zone_id,
building: @floor_mappings[space.floor_id.to_s]?.try(&.[](:building_id)),
capacity: capacity,
Expand Down
30 changes: 29 additions & 1 deletion drivers/kontakt_io/sensor_service_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ require "placeos-driver/spec"

DriverSpecs.mock_driver "KontaktIO::SensorService" do
system({
KontaktIO: {KontaktIOMock},
KontaktIO: {KontaktIOMock},
LocationServices: {LocationServicesMock},
StaffAPI: {StaffAPIMock},
})
settings({
floor_mappings: {
Expand Down Expand Up @@ -60,3 +62,29 @@ class KontaktIOMock < DriverSpecs::MockDriver
}
end
end

# :nodoc:
class LocationServicesMock < DriverSpecs::MockDriver
def building_id : String
"zone-building"
end
end

# :nodoc:
class StaffAPIMock < DriverSpecs::MockDriver
def systems(
q : String? = nil,
zone_id : String? = nil,
capacity : Int32? = nil,
bookable : Bool? = nil,
features : String? = nil,
limit : Int32 = 1000,
offset : Int32 = 0
)
[] of String
end

def system_settings(id : String, key : String)
nil
end
end

0 comments on commit 81a001e

Please sign in to comment.