|
| 1 | +require "placeos-driver" |
| 2 | +require "simple_retry" |
| 3 | + |
| 4 | +class Cisco::SpacesRoom < PlaceOS::Driver |
| 5 | + include Interface::Sensor |
| 6 | + |
| 7 | + descriptive_name "Cisco Spaces Room Sensors" |
| 8 | + generic_name :SpacesRoomSensors |
| 9 | + description "exposes sensor information to the room" |
| 10 | + |
| 11 | + default_settings({ |
| 12 | + _cisco_spaces_system: "sys-12345", |
| 13 | + _cisco_spaces_module: "Cisco_Spaces", |
| 14 | + |
| 15 | + space_room_id: "a410b6d676", |
| 16 | + }) |
| 17 | + |
| 18 | + getter system_id : String = "" |
| 19 | + getter module_name : String = "" |
| 20 | + getter room_id : String = "" |
| 21 | + |
| 22 | + def on_load |
| 23 | + on_update |
| 24 | + end |
| 25 | + |
| 26 | + def on_update |
| 27 | + @system_id = setting?(String, :cisco_spaces_system).presence || config.control_system.not_nil!.id |
| 28 | + @module_name = setting?(String, :cisco_spaces_module).presence || "Cisco_Spaces" |
| 29 | + @room_id = setting(String, :space_room_id) |
| 30 | + end |
| 31 | + |
| 32 | + private def cisco_spaces |
| 33 | + system(system_id)[module_name] |
| 34 | + end |
| 35 | + |
| 36 | + # ====================== |
| 37 | + # Sensor interface |
| 38 | + # ====================== |
| 39 | + |
| 40 | + SENSOR_TYPES = {SensorType::PeopleCount, SensorType::Presence, SensorType::Humidity, SensorType::Temperature, SensorType::AirQuality, SensorType::SoundPressure} |
| 41 | + NO_MATCH = [] of Interface::Sensor::Detail |
| 42 | + |
| 43 | + def sensors(type : String? = nil, mac : String? = nil, zone_id : String? = nil) : Array(Interface::Sensor::Detail) |
| 44 | + logger.debug { "sensors of type: #{type}, mac: #{mac}, zone_id: #{zone_id} requested" } |
| 45 | + |
| 46 | + return NO_MATCH if mac && mac != @room_id |
| 47 | + if type |
| 48 | + sensor_type = SensorType.parse(type) |
| 49 | + return NO_MATCH unless SENSOR_TYPES.includes?(sensor_type) |
| 50 | + end |
| 51 | + return NO_MATCH if zone_id && !system.zones.includes?(zone_id) |
| 52 | + |
| 53 | + Array(Interface::Sensor::Detail).from_json cisco_spaces.sensors(type, mac, zone_id).get.to_json |
| 54 | + end |
| 55 | + |
| 56 | + def sensor(mac : String, id : String? = nil) : Interface::Sensor::Detail? |
| 57 | + logger.debug { "sensor mac: #{mac}, id: #{id} requested" } |
| 58 | + return nil unless id |
| 59 | + return nil unless mac == @room_id |
| 60 | + |
| 61 | + Interface::Sensor::Detail?.from_json(cisco_spaces.sensors(mac, id).get.to_json) |
| 62 | + end |
| 63 | +end |
0 commit comments