Skip to content

Commit

Permalink
fix(gobright): apply desk mappings when locating users
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Aug 29, 2023
1 parent 754a6ef commit 77ebf45
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
23 changes: 19 additions & 4 deletions drivers/gobright/location_service.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GoBright::LocationService < PlaceOS::Driver

accessor staff_api : StaffAPI_1
accessor gobright : GoBright_1
accessor area_management : AreaManagement_1

default_settings({
gobright_floor_mappings: {
Expand Down Expand Up @@ -38,6 +39,12 @@ class GoBright::LocationService < PlaceOS::Driver
getter location_id : String
end

struct LevelCapacity
include JSON::Serializable

getter desk_mappings : Hash(String, String)
end

def on_update
@return_empty_spaces = setting?(Bool, :return_empty_spaces) || false
@desk_space_types = setting?(Array(SpaceType), :desk_space_types) || [SpaceType::Desk]
Expand All @@ -48,6 +55,11 @@ class GoBright::LocationService < PlaceOS::Driver
timezone = Time::Location.load(system.timezone.presence || "Australia/Sydney")
schedule.clear
schedule.cron(setting?(String, :space_cache_cron) || "0 5 * * *", timezone) { cache_space_details }
schedule.every(10.minutes) { @level_details = nil }
end

getter level_details : Hash(String, LevelCapacity) do
Hash(String, LevelCapacity).from_json area_management.level_details.get.to_json
end

# Finds the building ID for the current location services object
Expand Down Expand Up @@ -94,7 +106,8 @@ class GoBright::LocationService < PlaceOS::Driver

# return the data in the correct format
matches.compact_map do |booking|
map_booking(booking, booking.matched_space, booking.zone_id)
zone_id = booking.zone_id
map_booking(booking, booking.matched_space, zone_id, level_details[zone_id]?.try(&.desk_mappings))
end
end

Expand Down Expand Up @@ -204,10 +217,12 @@ class GoBright::LocationService < PlaceOS::Driver
booking_locs.map(&.as(typeof(booking_locs[0]) | typeof(occupancy_locs[0]))) + occupancy_locs.map(&.as(typeof(booking_locs[0]) | typeof(occupancy_locs[0])))
end

protected def map_booking(occurrence, space, zone_id)
protected def map_booking(occurrence, space, zone_id, mappings = nil)
owner = occurrence.organizer || occurrence.attendees.first?
starting = occurrence.start_date.to_unix
ending = occurrence.end_date.to_unix
space_name = space.name
map_id = mappings ? (mappings[space_name]? || space_name) : space_name

{
location: :booking,
Expand All @@ -216,8 +231,8 @@ class GoBright::LocationService < PlaceOS::Driver

# We supply map_id here as this will be mapped to the correct id
# and the frontend preferences map_id over asset_id
asset_id: space.name,
map_id: space.name,
asset_id: space_name,
map_id: map_id,

booking_id: occurrence.id,
building: building_id,
Expand Down
12 changes: 10 additions & 2 deletions drivers/gobright/location_service_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ require "./models"

DriverSpecs.mock_driver "GoBright::LocationService" do
system({
GoBright: {GoBrightMock},
StaffAPI: {StaffAPIMock},
GoBright: {GoBrightMock},
StaffAPI: {StaffAPIMock},
AreaManagement: {AreaManagementMock},
})

exec(:device_locations, "placeos_zone_id").get.should eq([
Expand Down Expand Up @@ -81,3 +82,10 @@ class StaffAPIMock < DriverSpecs::MockDriver
[{id: "zone-1234"}]
end
end

# :nodoc:
class AreaManagementMock < DriverSpecs::MockDriver
def level_details
{} of String => String
end
end
2 changes: 1 addition & 1 deletion drivers/place/area_management.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Place::AreaManagement < PlaceOS::Driver

# zone_id => desk_ids
@duplication_factor : Float64 = 0.8
@level_details : Hash(String, LevelCapacity) = {} of String => LevelCapacity
getter level_details : Hash(String, LevelCapacity) = {} of String => LevelCapacity

# PlaceOS client config
getter building_id : String { get_building_id.not_nil! }
Expand Down

0 comments on commit 77ebf45

Please sign in to comment.