HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
node_app.cpp
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 #include <iostream>
18 #include <iomanip>
19 #include <sstream>
20 #include <fstream>
21 
22 #include <unistd.h>
23 
24 #include <forward_list>
25 #include <algorithm>
26 
27 #include "node.h"
28 
29 #include "common.h"
30 
31 #include "application.h"
32 
38 // =============================================================================
39 // Global Variables.
40 // =============================================================================
41 
43 static Node node;
44 
46 static SimpleLight *simple_light = nullptr;
47 
49 static SimpleSwitch *simple_switch = nullptr;
50 
51 // =============================================================================
52 // Commands
53 // =============================================================================
54 
58 COMMAND(Register, "r", "r:register device")
59 {
60  UNUSED(args);
61  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
62 
63  node.unit0()->device_management()->register_device();
64 }
65 
69 COMMAND(Address, "a", "a:device address")
70 {
71  UNUSED(args);
72  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
73 
74  LOG(APP) << "Device Address : " << node.address() << NL;
75 }
76 
80 COMMAND(SimpleLight, "sl", "sl:set device as a simple light")
81 {
82  UNUSED(args);
83  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
84 
85  if (simple_switch != nullptr)
86  {
87  delete simple_switch;
88  }
89 
90  if (simple_light == nullptr)
91  {
92  simple_light = new SimpleLight(1, node);
93  }
94 
95  LOG(APP) << "Device is now a Simple Light !" << NL;
96 }
97 
101 COMMAND(SimpleSwitch, "ss", "ss:set device as a simple switch")
102 {
103  UNUSED(args);
104  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
105 
106  if (simple_light != nullptr)
107  {
108  delete simple_light;
109  }
110 
111  if (simple_switch == nullptr)
112  {
113  simple_switch = new SimpleSwitch(1, node);
114  }
115 
116  LOG(APP) << "Device is now a Simple Switch !" << NL;
117 }
118 
122 COMMAND(On, "on", "on:On Command")
123 {
124  UNUSED(args);
125  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
126 
127  if (simple_switch != nullptr)
128  {
129  simple_switch->on();
130  }
131  else
132  {
133  LOG(ERROR) << "Simple Switch not configured !" << NL;
134  }
135 }
136 
140 COMMAND(Off, "off", "off:Off Command")
141 {
142  UNUSED(args);
143  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
144 
145  if (simple_switch != nullptr)
146  {
147  simple_switch->off();
148  }
149  else
150  {
151  LOG(ERROR) << "Simple Switch not configured !" << NL;
152  }
153 }
154 
158 COMMAND(Toggle, "toggle", "toggle:Toggle Command")
159 {
160  UNUSED(args);
161  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
162 
163  if (simple_switch != nullptr)
164  {
165  simple_switch->toggle();
166  }
167  else
168  {
169  LOG(ERROR) << "Simple Switch not configured !" << NL;
170  }
171 }
172 
173 // =============================================================================
174 // API
175 // =============================================================================
176 
177 // =============================================================================
178 // HF::Application::Initialize
179 // =============================================================================
183 // =============================================================================
185 {
186  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
187 
188  transport.initialize();
189 
190  transport.add(&node);
191 
192  COMMAND_ADD(Register);
193  COMMAND_ADD(Address);
194  COMMAND_ADD(SimpleLight);
195  COMMAND_ADD(SimpleSwitch);
196  COMMAND_ADD(On);
197  COMMAND_ADD(Off);
198  COMMAND_ADD(Toggle);
199 
201 }
202 
203 // =============================================================================
204 // HF::Application::Save
205 // =============================================================================
209 // =============================================================================
211 {
212  Saved();
213 }
214 
215 // =============================================================================
216 // HF::Application::Restore
217 // =============================================================================
221 // =============================================================================
223 {
224  Restored();
225 }
226 
This file contains the definition of the Node class that represents the HAN-FUN Node on the applicati...
This file contains the definitions for the HAN-FUN example applications.
void Saved()
Callback indicating that the application configuration has been saved.
Definition: main.cpp:95
This class defines the API for the transport layer.
void Restored()
Callback indicating that the application configuration has been restored.
Definition: main.cpp:107
#define COMMAND_ADD(_name)
Helper macro to add a new command to the registry.
Definition: apps/common.h:166
virtual void initialize()=0
Initialize the associated transport layer.
virtual void add(Endpoint *ep)=0
Register the given Endpoint to receive events.
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
COMMAND(Register, "r", "r:register device")
Register node command.
Definition: node_app.cpp:58
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void Restore()
Restore application configuration.
Definition: base_app.cpp:498
This file contains the definitions for the common functionality in the HAN-FUN example applications...
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
void Save()
Save application configuration.
Definition: base_app.cpp:473
void Initialize(HF::Transport::Layer &transport)
Initialize the application.
Definition: base_app.cpp:441