IOControl/include/IO/Request.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "Error.h"
23 #include "DevNode.h"
24 #include "Event.h"
25 #include <debug_progmem.h>
26 
27 namespace IO
28 {
29 /*
30  * IO Commands
31  */
32 #define IOCOMMAND_MAP(XX) \
33  XX(undefined, "Undefined or invalid") \
34  XX(query, "Query node states") \
35  XX(off, "Turn node off or set to minimum") \
36  XX(on, "Turn node on or set to maximum") \
37  XX(toggle, "Toggle node(s) between on and off") \
38  XX(latch, "Relay nodes") \
39  XX(momentary, "Relay nodes") \
40  XX(delay, "Relay nodes") \
41  XX(set, "Set value") \
42  XX(adjust, "Adjust value") \
43  XX(update, "Perform update cycle (e.g. DMX512)")
44 
45 enum class Command {
46 #define XX(tag, comment) tag,
48 #undef XX
49 };
50 
52 bool fromString(Command& cmd, const char* str);
53 
54 class Device;
55 class Request;
56 
75 class Request : public LinkedObjectTemplate<Request>
76 {
77 public:
79 
94 
95  Request(Device& device) : device(device)
96  {
97  debug_d("Request %p created", this);
98  }
99 
100  // Prevent copying; if required, add `virtual clone()`
101  Request(const Request&) = delete;
102 
103  virtual ~Request()
104  {
105  debug_d("Request %p (%s) destroyed", this, requestId.c_str());
106  }
107 
111  ErrorCode error() const
112  {
113  return errorCode;
114  }
115 
116  bool isPending() const
117  {
118  return errorCode == Error::pending;
119  }
120 
124  String caption() const;
125 
129  virtual ErrorCode parseJson(JsonObjectConst json);
130 
138  virtual void submit();
139 
140  /*
141  * Usually called by device or controller, but can also be used to
142  * pass a request to its callback first, for example on a configuration
143  * error.
144  */
145  void complete(ErrorCode err);
146 
150  virtual void getJson(JsonObject json) const;
151 
155  void setID(const String& value)
156  {
157  requestId = value;
158  }
159 
163  void setCommand(Command cmd)
164  {
165  debug_d("setCommand(0x%08x: %s)", cmd, toString(cmd).c_str());
166  command = cmd;
167  }
168 
172  void onComplete(Callback callback)
173  {
174  this->callback = callback;
175  }
176 
177  bool nodeQuery(DevNode node)
178  {
179  command = Command::query;
180  return setNode(node);
181  }
182 
183  bool nodeOff(DevNode node)
184  {
185  setCommand(Command::off);
186  return setNode(node);
187  }
188 
189  bool nodeOn(DevNode node)
190  {
191  setCommand(Command::on);
192  return setNode(node);
193  }
194 
195  bool nodeToggle(DevNode node)
196  {
197  setCommand(Command::toggle);
198  return setNode(node);
199  }
200 
204  virtual bool nodeAdjust(DevNode node, int value)
205  {
206  return false;
207  }
208 
212  virtual bool setNode(DevNode node)
213  {
214  return false;
215  }
216 
221  {
223  }
224 
225  /*
226  * Generic set state command. 0 is off, otherwise on.
227  * Dimmable nodes use percentage level 0 - 100.
228  */
229  virtual bool setNodeState(DevNode node, DevNode::State state)
230  {
231  if(state == DevNode::State::on) {
232  setCommand(Command::on);
233  } else if(state == DevNode::State::off) {
234  setCommand(Command::off);
235  } else {
236  return false;
237  }
238  return setNode(node);
239  }
240 
244  const CString& id() const
245  {
246  return requestId;
247  }
248 
250  {
251  return command;
252  }
253 
257  virtual void handleEvent(Event event);
258 
260 
261 private:
262  Callback callback;
263  Command command{Command::undefined};
264  ErrorCode errorCode{Error::pending};
265  CString requestId;
266 };
267 
268 } // namespace IO
virtual ~Request()
Definition: IOControl/include/IO/Request.h:103
#define str(s)
Definition: testrunner.h:124
void setID(const String &value)
Request identifiers are optional, useful for tracking remote requests.
Definition: IOControl/include/IO/Request.h:155
XX(tag, comment)
Identifies a device node.
Definition: DevNode.h:30
void onComplete(Callback callback)
Set the request completion callback.
Definition: IOControl/include/IO/Request.h:172
Command getCommand() const
Definition: IOControl/include/IO/Request.h:249
Definition: IOControl/include/IO/Controller.h:25
The String class.
Definition: WString.h:136
bool isPending() const
Definition: IOControl/include/IO/Request.h:116
Definition: Libraries/IOControl/include/IO/Error.h:71
virtual DevNode::States getNodeStates(DevNode node)
Query node status from response.
Definition: IOControl/include/IO/Request.h:220
Handles requests for a specific device; the requests are executed by the relevant controller...
Definition: Libraries/IOControl/include/IO/Device.h:35
String toString(Event event)
#define debug_d
Definition: debug_progmem.h:100
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:26
Json json
Request represents a single user request/response over a bus.
Definition: IOControl/include/IO/Request.h:75
bool fromString(Command &cmd, const char *str)
bool nodeOff(DevNode node)
Definition: IOControl/include/IO/Request.h:183
bool nodeQuery(DevNode node)
Definition: IOControl/include/IO/Request.h:177
Base class template for linked items with type casting.
Definition: LinkedObject.h:61
Command
Definition: IOControl/include/IO/Request.h:45
State
Definition: DevNode.h:36
Manage a set of bit values using enumeration.
Definition: BitSet.h:44
bool nodeToggle(DevNode node)
Definition: IOControl/include/IO/Request.h:195
int16_t ErrorCode
Definition: Libraries/IOControl/include/IO/Error.h:27
#define IOCOMMAND_MAP(XX)
Definition: IOControl/include/IO/Request.h:32
const CString & id() const
Get the request ID, if there is one.
Definition: IOControl/include/IO/Request.h:244
Event
Definition: Event.h:36
virtual bool setNode(DevNode node)
If nodes are supported, implemented this method.
Definition: IOControl/include/IO/Request.h:212
void setCommand(Command cmd)
Set the command code.
Definition: IOControl/include/IO/Request.h:163
Device & device
Definition: IOControl/include/IO/Request.h:259
Request(Device &device)
Definition: IOControl/include/IO/Request.h:95
bool nodeOn(DevNode node)
Definition: IOControl/include/IO/Request.h:189
virtual bool nodeAdjust(DevNode node, int value)
If nodes support analogue state (e.g. brightness) the implement this method.
Definition: IOControl/include/IO/Request.h:204
ErrorCode error() const
Request error code defaults to &#39;pending&#39; and is set on completion.
Definition: IOControl/include/IO/Request.h:111
virtual bool setNodeState(DevNode node, DevNode::State state)
Definition: IOControl/include/IO/Request.h:229