Commit 8e6c4c131602ae8800c697c6d4a4ee169f3890be
Committed by
Moritz Wirger
1 parent
72f9bfd1
Update documentation for shared state.
Showing
4 changed files
with
59 additions
and
4 deletions
README.md
| ... | ... | @@ -8,10 +8,10 @@ A simple and easy to use library for Philips Hue Lights |
| 8 | 8 | |
| 9 | 9 | ## Features |
| 10 | 10 | * find bridges with SSDP or set an ip manually |
| 11 | -* function to assign a username or set one manually | |
| 12 | 11 | * all common light functions (brightness, color, temperature) |
| 13 | 12 | * extended alert() functions, which alert in a specific color (good for notifications) |
| 14 | -* creating custom sensors and reading sensor values | |
| 13 | +* supports sensors, rules, groups, scenes and schedules | |
| 14 | +* streaming with entertainment mode | |
| 15 | 15 | * [documented with doxygen](https://enwi.github.io/hueplusplus/) |
| 16 | 16 | * tested with google test, google mock and gcov/lcov |
| 17 | 17 | ... | ... |
doc/markdown/Getting_Started.md
| ... | ... | @@ -38,6 +38,7 @@ Here you will need to provide the ip address, the port number, a username and an |
| 38 | 38 | auto handler = std::make_shared<hueplusplus::LinHttpHandler>(); |
| 39 | 39 | hueplusplus::Bridge bridge("192.168.2.102", 80, "<username>", handler); |
| 40 | 40 | ``` |
| 41 | +At this point you may want to decide whether to use a [shared state](@ref shared-state) cache model or keep the default settings. | |
| 41 | 42 | |
| 42 | 43 | ### Controlling lights |
| 43 | 44 | ... | ... |
doc/markdown/Mainpage.md
| ... | ... | @@ -3,10 +3,10 @@ A simple and easy to use library for Philips Hue Lights. |
| 3 | 3 | |
| 4 | 4 | ## Features |
| 5 | 5 | * find bridges with SSDP or set an ip manually |
| 6 | -* function to assign a username or set one manually | |
| 7 | 6 | * all common light functions (brightness, color, temperature) |
| 8 | 7 | * extended alert() functions, which alert in a specific color (good for notifications) |
| 9 | -* creating custom sensors and reading sensor values | |
| 8 | +* supports sensors, rules, groups, scenes and schedules | |
| 9 | +* streaming with entertainment mode | |
| 10 | 10 | * documented with doxygen |
| 11 | 11 | * tested with google test, google mock and gcov/lcov |
| 12 | 12 | ... | ... |
doc/markdown/Shared_State.md
0 → 100644
| 1 | +# Shared state cache {#shared-state} | |
| 2 | + | |
| 3 | +## What shared state means | |
| 4 | +There are two ways in which the API state (internally JSON) can be handled: | |
| 5 | +1. Every resource instance holds its own cache of the state (default). | |
| 6 | +2. All instances share the cache for the entire bridge. | |
| 7 | + | |
| 8 | +### Advantages of shared state | |
| 9 | +* Different resources are always consistent on the library level. | |
| 10 | +If one part of the code uses the light with id 1 and turns it off, | |
| 11 | +light 1 is also off when using a different variable to access it. | |
| 12 | +* The number of requests can be reduced, because they can be bundled together on a higher cache level. | |
| 13 | + | |
| 14 | +### Disadvantages of shared state | |
| 15 | +* Different objects are no longer thread safe, you cannot use **any** parts of the library | |
| 16 | +from multiple threads (without locking). | |
| 17 | +* Changes are not transparent. For example, a `const Light` can suddenly change its name, because the | |
| 18 | +name was changed somewhere else in the code. | |
| 19 | + | |
| 20 | +Because of these considerations, shared state is disabled by default. | |
| 21 | + | |
| 22 | +## Enabling shared state | |
| 23 | +Shared state can be configured when the bridge is first constructed, either in [getBridge()](@ref hueplusplus::BridgeFinder::getBridge) | |
| 24 | +or in the [constructor](@ref hueplusplus::Bridge::Bridge). Set `sharedState` to `true` to keep all resources | |
| 25 | +connected to the bridge cache. | |
| 26 | +```{.cpp} | |
| 27 | +hueplusplus::Bridge bridge = finder.getBridge(bridges[0], true); | |
| 28 | +``` | |
| 29 | +```{.cpp} | |
| 30 | +hueplusplus::Bridge bridge("192.168.2.102", 80, "<username>", handler, std::chrono::seconds(10), true); | |
| 31 | +``` | |
| 32 | + | |
| 33 | +## Shared state and refreshing | |
| 34 | +When shared cache is used, refreshes use a hierarchichal structure to determine how much should be requested from the bridge. | |
| 35 | +Every level has its own last update time and refresh duration. | |
| 36 | +First, it is checked whether the higher level is up to date and refresh everything if not. | |
| 37 | +Otherwise, only the lowest necessary level is requested from the bridge to be more efficient. | |
| 38 | + | |
| 39 | +### Example: | |
| 40 | + | |
| 41 | +```{.cpp} | |
| 42 | +bridge.setRefreshDuration(std::chrono::minutes(1)); | |
| 43 | +bridge.lights().setRefreshDuration(std::chrono::seconds(30)); | |
| 44 | +hueplusplus::Light light = bridge.lights().get(1); | |
| 45 | +// ... wait some time | |
| 46 | +bool on = light.isOn(); | |
| 47 | +``` | |
| 48 | +[isOn()](@ref hueplusplus::Light::isOn) is a non-const method (in this case). That means it will refresh the | |
| 49 | +state if it is outdated. The default refresh time is inherited from `bridge.lights()`, so it is 30 seconds. | |
| 50 | +After 30 seconds, the state of `light` *and* `bridge.lights()` is outdated. Therefore, the entire list of lights is | |
| 51 | +updated at this point. | |
| 52 | + | |
| 53 | +After more than one minute, the bridge state is considered outdated. This means that `isOn()` causes an update of | |
| 54 | +the entire bridge. | ... | ... |