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 {
59  RevokeUser,
60  };
61 
65  };
66 
73  using ConfigDelegate = Delegate<void(const Config& config)>;
74 
85 
90  Bridge(Hue::Device::Enumerator& devices) : Basic1Template(), devices(devices)
91  {
92  }
93 
98  void configure(const Config& config);
99 
109  {
110  pairingEnabled = enable;
111  }
112 
114  {
115  configDelegate = delegate;
116  }
117 
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 
162  {
163  if(stateChangeDelegate) {
164  stateChangeDelegate(device, changed);
165  }
166  }
167 
168  /* UPnP::Device */
169 
170  String getField(Field desc) const override;
171 
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
Manage a set of bit values using enumeration.
Definition: BitSet.h:45
Definition: HttpServerConnection.h:34
Definition: Bridge.h:51
void onStateChanged(StateChangeDelegate delegate)
Definition: Bridge.h:118
bool formatMessage(SSDP::Message &msg, SSDP::MessageSpec &ms) override
String getField(Field desc) const override
Bridge(Hue::Device::Enumerator &devices)
Constructor.
Definition: Bridge.h:90
void configure(const Config &config)
Perform a configuration action.
void resetStats()
Clear the bridge statistics.
Definition: Bridge.h:137
const UserMap & getUsers() const
Access the list of users.
Definition: Bridge.h:146
const Stats & getStats()
Get bridge statistics.
Definition: Bridge.h:129
void onConfigChange(ConfigDelegate delegate)
Definition: Bridge.h:113
void enablePairing(bool enable)
Enable creation of new users.
Definition: Bridge.h:108
void begin()
void getStatusInfo(JsonObject json)
Get bridge status information in JSON format.
void deviceStateChanged(const Hue::Device &device, Hue::Device::Attributes changed)
Devices call this method when their state has been updated.
Definition: Bridge.h:161
bool onHttpRequest(HttpServerConnection &connection) override
Abstract class to manage a list of devices.
Definition: Libraries/HueEmulator/src/include/Hue/Device.h:126
Definition: Libraries/HueEmulator/src/include/Hue/Device.h:93
Defines the information used to create an outgoing message.
Definition: MessageSpec.h:75
Message using regular HTTP header management class.
Definition: SSDP/src/include/Network/SSDP/Message.h:72
The String class.
Definition: WString.h:137
void enable(Handler &commandHandler, HardwareSerial &serial)
Json json
Definition: Bridge.h:30
Model
Definition: Bridge.h:31
@ LWB007
Colour.
@ LWB004
Dimmable white.
Definition: Bridge.h:56
String name
Randomly generated key.
Definition: Bridge.h:64
Type
Definition: Bridge.h:57
String deviceType
How device identifies itself.
Definition: Bridge.h:63
Type type
Configuration action to perform.
Definition: Bridge.h:62
Definition: Stats.h:27
Information about user.
Definition: Bridge.h:39
String deviceType
How the user identifies themselves.
Definition: Bridge.h:40
uint16_t count
Number of requests received from this user.
Definition: Bridge.h:41
bool authorized
Only authorized users may perform actions.
Definition: Bridge.h:42