• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 26710666338

31 May 2026 10:55AM UTC coverage: 44.862%. Remained the same
26710666338

Pull #1738

github

web-flow
Merge fa5bf2601 into 185b10f22
Pull Request #1738: Support Nanoleaf v2 protocol, closes #1730

8557 of 19863 branches covered (43.08%)

20 of 37 new or added lines in 3 files covered. (54.05%)

1 existing line in 1 file now uncovered.

22116 of 49298 relevant lines covered (44.86%)

47.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/plugins/nanoleaf/NanoleafDevice.cpp
1
/*
2
 * This program is free software; you can redistribute it and/or modify
3
 * it under the terms of the GNU General Public License as published by
4
 * the Free Software Foundation; either version 2 of the License, or
5
 * (at your option) any later version.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU Library General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15
 *
16
 * NanoleafDevice.cpp
17
 * A Nanoleaf Device.
18
 * Copyright (C) 2017 Peter Newman
19
 */
20

21
#include <memory>
22
#include <limits>
23
#include <set>
24
#include <string>
25
#include <vector>
26

27
#include "ola/Callback.h"
28
#include "ola/Logging.h"
29
#include "ola/StringUtils.h"
30
#include "ola/network/IPV4Address.h"
31
#include "ola/network/InterfacePicker.h"
32
#include "ola/network/NetworkUtils.h"
33
#include "ola/network/SocketAddress.h"
34
#include "olad/PluginAdaptor.h"
35
#include "olad/Port.h"
36
#include "olad/Preferences.h"
37
#include "plugins/nanoleaf/NanoleafDevice.h"
38
#include "plugins/nanoleaf/NanoleafPort.h"
39

40
namespace ola {
41
namespace plugin {
42
namespace nanoleaf {
43

44
using ola::network::IPV4Address;
45
using ola::network::IPV4SocketAddress;
46
using std::auto_ptr;
47
using std::set;
48
using std::string;
49
using std::vector;
50

51
const char NanoleafDevice::VERSION_V1_TEXT[] = "v1";
52
const char NanoleafDevice::VERSION_V2_TEXT[] = "v2";
53

54
/*
55
 * Create a new Nanoleaf Device
56
 */
57
NanoleafDevice::NanoleafDevice(
×
58
    AbstractPlugin *owner,
59
    Preferences *preferences,
60
    PluginAdaptor *plugin_adaptor,
61
    const ola::network::IPV4Address &controller)
×
62
    : Device(owner, "Nanoleaf Device"),
63
      m_node(NULL),
×
64
      m_preferences(preferences),
×
65
      m_plugin_adaptor(plugin_adaptor),
×
66
      m_controller(controller) {
×
67
  SetDefaults();
×
68
}
×
69

70

71
/*
72
 * Start this device
73
 * @return true on success, false on failure
74
 */
75
bool NanoleafDevice::StartHook() {
×
NEW
76
  string text_version = m_preferences->GetValue(VersionKey());
×
77

NEW
78
  NanoleafNode::NanoleafVersion version = NanoleafNode::VERSION_V1;
×
NEW
79
  if (text_version == VERSION_V1_TEXT) {
×
80
    version = NanoleafNode::VERSION_V1;
NEW
81
  } else if (text_version == VERSION_V2_TEXT) {
×
82
    version = NanoleafNode::VERSION_V2;
83
  } else {
NEW
84
    OLA_WARN << "Unknown Nanoleaf protocol version " << text_version
×
NEW
85
             << ", defaulting to v1";
×
86
  }
87

NEW
88
  vector<uint16_t> panels;
×
89
  vector<string> panel_list;
×
90
  StringSplit(m_preferences->GetValue(PanelsKey()), &panel_list, ",");
×
91
  vector<string>::const_iterator iter = panel_list.begin();
×
92
  for (; iter != panel_list.end(); ++iter) {
×
93
    if (iter->empty()) {
×
94
      continue;
×
95
    }
96

97
    // TODO(Peter): Check < 255 if version 1
NEW
98
    uint16_t panel;
×
99
    if (!StringToInt(*iter, &panel)) {
×
100
      OLA_WARN << "Invalid value for panel: " << *iter;
×
101
      return false;
×
102
    }
103
    panels.push_back(panel);
×
104
  }
105

106
  if (panels.empty()) {
×
107
    OLA_WARN << "No panels found";
×
108
    m_node = NULL;
×
109
    return false;
×
110
  }
111

112
  // TODO(Peter): Check and warn if we have more than a universe of panels,
113
  // possibly truncate the extra panels too
114

115
  // Don't bother passing in a source socket, let the node generate it's own
NEW
116
  m_node = new NanoleafNode(m_plugin_adaptor, panels, NULL, version);
×
117

118
  if (!m_node->Start()) {
×
119
    delete m_node;
×
120
    m_node = NULL;
×
121
    return false;
×
122
  }
123

124
  uint16_t ip_port;
×
125
  if (!StringToInt(m_preferences->GetValue(IPPortKey()), &ip_port)) {
×
126
    ip_port = DEFAULT_STREAMING_PORT;
×
127
  }
128
  IPV4SocketAddress socket_address = IPV4SocketAddress(m_controller, ip_port);
×
129
  AddPort(new NanoleafOutputPort(this, socket_address, m_node, 0));
×
130
  return true;
131
}
×
132

133

134
string NanoleafDevice::DeviceId() const {
×
135
  return m_controller.ToString();
×
136
}
137

138

139
string NanoleafDevice::IPPortKey() const {
×
140
  return m_controller.ToString() + "-port";
×
141
}
142

143

144
string NanoleafDevice::PanelsKey() const {
×
145
  return m_controller.ToString() + "-panels";
×
146
}
147

148

NEW
149
string NanoleafDevice::VersionKey() const {
×
NEW
150
  return m_controller.ToString() + "-version";
×
151
}
152

153

UNCOV
154
void NanoleafDevice::SetDefaults() {
×
155
  // Set device options
156
  m_preferences->SetDefaultValue(PanelsKey(), StringValidator(), "");
×
157

NEW
158
  set<string> valid_versions;
×
NEW
159
  valid_versions.insert(VERSION_V1_TEXT);
×
NEW
160
  valid_versions.insert(VERSION_V2_TEXT);
×
161

NEW
162
  m_preferences->SetDefaultValue(VersionKey(),
×
NEW
163
                                 SetValidator<string>(valid_versions),
×
164
                                 VERSION_V1_TEXT);
165

166
  m_preferences->SetDefaultValue(
×
167
      IPPortKey(),
×
168
      UIntValidator(1, std::numeric_limits<uint16_t>::max()),
×
169
      DEFAULT_STREAMING_PORT);
170
  m_preferences->Save();
×
171
}
×
172

173

174
/**
175
 * Stop this device. This is called before the ports are deleted
176
 */
177
void NanoleafDevice::PrePortStop() {
×
178
  m_node->Stop();
×
179
}
×
180

181

182
/*
183
 * Stop this device
184
 */
185
void NanoleafDevice::PostPortStop() {
×
186
  delete m_node;
×
187
  m_node = NULL;
×
188
}
×
189
}  // namespace nanoleaf
190
}  // namespace plugin
191
}  // namespace ola
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc