Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_PresenceHandler.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Nov 13, 2019
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
12#include <time/cs_SystemTime.h>
13#include <util/cs_Store.h>
14
15#include <optional>
16
22class PresenceHandler : public EventListener, public Component {
23public:
24 struct __attribute__((__packed__)) ProfileLocation {
25 static const constexpr uint8_t MAX_LOCATION_ID = 63;
26 static const constexpr uint8_t MAX_PROFILE_ID = 7;
27
28 uint8_t profile;
29 uint8_t location;
30
31 bool operator==(const ProfileLocation& other) { return memcmp(this, &other, sizeof(*this)) == 0; }
32
33 bool isValid() { return profile <= MAX_PROFILE_ID && location <= MAX_LOCATION_ID; }
34 };
35
38
42 virtual cs_ret_code_t init() override;
43
49 std::optional<PresenceStateDescription> getCurrentPresenceDescription();
50
54 void registerPresence(ProfileLocation profileLocation);
55
56private:
58 static const constexpr uint8_t PRESENCE_TIMEOUT_SECONDS = 10;
59
61 static const constexpr uint8_t PRESENCE_MESH_SEND_THROTTLE_SECONDS = 10;
62 static const constexpr uint8_t PRESENCE_MESH_SEND_THROTTLE_SECONDS_VARIATION = 20;
63
67 static const constexpr uint32_t PRESENCE_UNCERTAIN_SECONDS_AFTER_BOOT = 30;
68
74 static const constexpr uint8_t MAX_RECORDS = 20;
75
90
93 uint8_t timeoutSeconds = PRESENCE_TIMEOUT_SECONDS,
94 uint8_t meshThrottleSeconds = 0)
96 , timeoutCountdownSeconds(timeoutSeconds)
97 , meshSendCountdownSeconds(meshThrottleSeconds) {}
98
100
101 bool isValid() { return timeoutCountdownSeconds != 0; }
102
104 };
105
110
116
123 void handlePresenceEvent(ProfileLocation profileLocation, bool forwardToMesh);
124
136 PresenceMutation handleProfileLocation(ProfileLocation profileLocation, bool forwardToMesh);
137
142 std::optional<PresenceStateDescription> prevDescription,
143 std::optional<PresenceStateDescription> nextDescription);
144
148 void sendMeshMessage(ProfileLocation profileLocation);
149
157
163
169
170public:
176 virtual void handleEvent(event_t& evt) override;
177};
Helper class to manage decoupling of components.
Definition: cs_Component.h:35
Event listener.
Definition: cs_EventListener.h:17
Keeps up all the locations each profile is present in.
Definition: cs_PresenceHandler.h:22
void registerPresence(ProfileLocation profileLocation)
Validates and administrate a presence update.
virtual void handleEvent(event_t &evt) override
receive background messages indicating where users are, record the time and place and update the curr...
PresenceRecord * clearOldestRecord(ProfileLocation profileLocation)
finds oldest record and default constructs its present record, then returns the pointer to it.
virtual ~PresenceHandler()
void dispatchPresenceChangeEvent(PresenceChange type, ProfileLocation profileLocation={})
Sends presence change event.
PresenceMutation handleProfileLocation(ProfileLocation profileLocation, bool forwardToMesh)
Handle an incoming profile-location combination.
Store< PresenceRecord, MAX_RECORDS > _store
Stores presence records.
Definition: cs_PresenceHandler.h:109
PresenceMutation getMutationType(std::optional< PresenceStateDescription > prevDescription, std::optional< PresenceStateDescription > nextDescription)
Resolves the type of mutation from previous and next descriptions.
std::optional< PresenceStateDescription > getCurrentPresenceDescription()
Returns a simplified description of the current presence knowledge, each bit in the description indic...
void sendMeshMessage(ProfileLocation profileLocation)
Send a mesh message with profile and location.
static const constexpr uint8_t PRESENCE_TIMEOUT_SECONDS
Number of seconds before presence times out.
Definition: cs_PresenceHandler.h:58
static const constexpr uint32_t PRESENCE_UNCERTAIN_SECONDS_AFTER_BOOT
Number of seconds after boot it is assumed to take to receive the location of all devices.
Definition: cs_PresenceHandler.h:67
void handlePresenceEvent(ProfileLocation profileLocation, bool forwardToMesh)
Handle an incoming profile-location combination.
void tickSecond()
To be called every second.
virtual cs_ret_code_t init() override
calls listen();
static const constexpr uint8_t PRESENCE_MESH_SEND_THROTTLE_SECONDS
For each presence entry, send it max every (x + variation) seconds over the mesh.
Definition: cs_PresenceHandler.h:61
static const constexpr uint8_t MAX_RECORDS
Maximum number of presence records that is kept up.
Definition: cs_PresenceHandler.h:74
void dispatchPresenceMutationEvent(PresenceMutation mutation)
Sends presence mutation event.
static const constexpr uint8_t PRESENCE_MESH_SEND_THROTTLE_SECONDS_VARIATION
Definition: cs_PresenceHandler.h:62
A variable size storage utility for objects of type RecordType with absolute maximum MaxItemCount.
Definition: cs_Store.h:25
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
PresenceMutation
Definition: cs_PacketsInternal.h:255
PresenceChange
Definition: cs_Packets.h:378
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
Definition: cs_PresenceHandler.h:76
uint8_t meshSendCountdownSeconds
Used to determine whether to send a mesh message.
Definition: cs_PresenceHandler.h:89
ProfileLocation id()
Definition: cs_PresenceHandler.h:103
bool isValid()
Definition: cs_PresenceHandler.h:101
void invalidate()
Definition: cs_PresenceHandler.h:99
uint8_t timeoutCountdownSeconds
Used to determine when a record is timed out.
Definition: cs_PresenceHandler.h:83
ProfileLocation profileLocation
Definition: cs_PresenceHandler.h:77
PresenceRecord(ProfileLocation profileLocation={}, uint8_t timeoutSeconds=PRESENCE_TIMEOUT_SECONDS, uint8_t meshThrottleSeconds=0)
Definition: cs_PresenceHandler.h:91
Definition: cs_PresenceHandler.h:24
uint8_t location
Definition: cs_PresenceHandler.h:29
bool isValid()
Definition: cs_PresenceHandler.h:33
uint8_t profile
Definition: cs_PresenceHandler.h:28
bool operator==(const ProfileLocation &other)
Definition: cs_PresenceHandler.h:31