Bridge.h
Go to the documentation of this file.
1 /****
2  * Bridge.h
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the HueEmulator Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Device.h"
23 #include "Stats.h"
24 #include <Network/HttpServer.h>
25 #include <Data/WebConstants.h>
26 #include <SimpleTimer.h>
27 #include <Network/UPnP/schemas-upnp-org/ClassGroup.h>
28 
29 namespace Hue
30 {
31 enum class Model {
32  LWB004,
33  LWB007,
34 };
35 
39 struct User {
41  uint16_t count{0};
42  bool authorized{false};
43 };
44 
49 
51 {
52 public:
56  struct Config {
57  enum class Type {
60  };
61 
62  Type type;
64  String name;
65  };
66 
73  using ConfigDelegate = Delegate<void(const Config& config)>;
74 
84  using StateChangeDelegate = Delegate<void(const Hue::Device& device, Hue::Device::Attributes attr)>;
85 
90  Bridge(Hue::Device::Enumerator& devices) : Basic1Template(), devices(devices)
91  {
92  }
93 
98  void configure(const Config& config);
99 
108  void enablePairing(bool enable)
109  {
110  pairingEnabled = enable;
111  }
112 
113  void onConfigChange(ConfigDelegate delegate)
114  {
115  configDelegate = delegate;
116  }
117 
118  void onStateChanged(StateChangeDelegate delegate)
119  {
120  stateChangeDelegate = delegate;
121  }
122 
123  void begin();
124 
129  const Stats& getStats()
130  {
131  return stats;
132  }
133 
137  void resetStats()
138  {
139  memset(&stats, 0, sizeof(stats));
140  }
141 
146  const UserMap& getUsers() const
147  {
148  return users;
149  }
150 
155  void getStatusInfo(JsonObject json);
156 
161  void deviceStateChanged(const Hue::Device& device, Hue::Device::Attributes changed)
162  {
163  if(stateChangeDelegate) {
164  stateChangeDelegate(device, changed);
165  }
166  }
167 
168  /* UPnP::Device */
169 
170  String getField(Field desc) const override;
171 
172  bool formatMessage(SSDP::Message& msg, SSDP::MessageSpec& ms) override;
173  bool onHttpRequest(HttpServerConnection& connection) override;
174 
175 private:
176  void createUser(JsonObjectConst request, JsonDocument& result, const String& path);
177  bool validateUser(const char* userName);
178  void handleApiRequest(HttpServerConnection& connection);
179 
180 private:
181  UserMap users;
182  bool pairingEnabled = false;
183  Hue::Device::Enumerator& devices;
184  ConfigDelegate configDelegate;
185  StateChangeDelegate stateChangeDelegate;
186  Stats stats;
187 };
188 
189 } // namespace Hue
uint16_t count
Number of requests received from this user.
Definition: Bridge.h:58
Message using regular HTTP header management class.
Definition: SSDP/src/include/Network/SSDP/Message.h:71
Definition: Stats.h:44
void getStatusInfo(JsonObject json)
Get bridge status information in JSON format.
const UserMap & getUsers() const
Access the list of users.
Definition: Bridge.h:163
The String class.
Definition: WString.h:136
Definition: Bridge.h:67
void configure(const Config &config)
Perform a configuration action.
void onStateChanged(StateChangeDelegate delegate)
Definition: Bridge.h:135
const Stats & getStats()
Get bridge statistics.
Definition: Bridge.h:146
String deviceType
How device identifies itself.
Definition: Bridge.h:80
void deviceStateChanged(const Hue::Device &device, Hue::Device::Attributes changed)
Devices call this method when their state has been updated.
Definition: Bridge.h:178
Defines the information used to create an outgoing message.
Definition: MessageSpec.h:74
Definition: Bridge.h:73
Bridge(Hue::Device::Enumerator &devices)
Constructor.
Definition: Bridge.h:107
Delegate< void(const Hue::Device &device, Hue::Device::Attributes attr)> StateChangeDelegate
A global callback may be provided to perform actions when device states change.
Definition: Bridge.h:101
void onConfigChange(ConfigDelegate delegate)
Definition: Bridge.h:130
Manage a set of bit values using enumeration.
Definition: BitSet.h:44
String name
Randomly generated key.
Definition: Bridge.h:81
void begin()
Delegate< void(const Config &config)> ConfigDelegate
Called when a new user key is created.
Definition: Bridge.h:90
Json json
bool onHttpRequest(HttpServerConnection &connection) override
Type
Definition: Bridge.h:74
Abstract class to manage a list of devices.
Definition: Libraries/HueEmulator/src/include/Hue/Device.h:125
String deviceType
How the user identifies themselves.
Definition: Bridge.h:57
void enablePairing(bool enable)
Enable creation of new users.
Definition: Bridge.h:125
@ LWB004
Dimmable white.
void enable(Handler &commandHandler, HardwareSerial &serial)
String getField(Field desc) const override
Definition: Bridge.h:29
Type type
Configuration action to perform.
Definition: Bridge.h:79
Definition: Libraries/HueEmulator/src/include/Hue/Device.h:92
@ LWB007
Colour.
Information about user.
Definition: Bridge.h:56
Definition: HttpServerConnection.h:33
Model
Definition: Bridge.h:48
void resetStats()
Clear the bridge statistics.
Definition: Bridge.h:154
bool authorized
Only authorized users may perform actions.
Definition: Bridge.h:59
bool formatMessage(SSDP::Message &msg, SSDP::MessageSpec &ms) override