Skip to content

Commit 27a5077

Browse files
committed
refactor into publish funcn
1 parent f490a4a commit 27a5077

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

src/Wippersnapper.cpp

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ bool Wippersnapper::buildWSTopics() {
750750
// Topic to signal pin configuration complete from device to broker
751751
WS._topic_device_pin_config_complete = (char *)malloc(
752752
sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) +
753-
strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) + strlen("device/pinConfigComplete") + 1);
753+
strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) +
754+
strlen("device/pinConfigComplete") + 1);
754755

755756
// Topic for signals from device to broker
756757
WS._topic_signal_device = (char *)malloc(
@@ -786,7 +787,7 @@ bool Wippersnapper::buildWSTopics() {
786787
is_success = false;
787788
}
788789

789-
// Create registration status complete topic
790+
// Create registration status complete topic
790791
if (WS._topic_description_status_complete) {
791792
strcpy(WS._topic_description_status_complete, WS._username);
792793
strcat(WS._topic_description_status_complete, "/wprsnpr/");
@@ -1084,50 +1085,60 @@ void Wippersnapper::connect() {
10841085
setStatusLEDColor(LED_CONNECTED);
10851086

10861087
// Register hardware with Wippersnapper
1087-
WS_DEBUG_PRINTLN("Registering Board...")
1088+
WS_DEBUG_PRINTLN("Registering hardware with WipperSnapper...")
10881089
setStatusLEDColor(LED_IO_REGISTER_HW);
10891090

10901091
if (!registerBoard()) {
1091-
haltError("Unable to register board with Wippersnapper.");
1092+
haltError("Unable to register with WipperSnapper.");
10921093
}
10931094
feedWDT(); // Hardware registered with IO, feed WDT */
1095+
WS_DEBUG_PRINTLN("Registered with WipperSnapper.");
10941096

1095-
WS_DEBUG_PRINTLN("Registered board with Wippersnapper.");
1096-
statusLEDBlink(WS_LED_STATUS_CONNECTED);
1097-
1098-
WS_DEBUG_PRINTLN("POLLING CONFIG PACKETS FOR 2SEC.");
1097+
WS_DEBUG_PRINTLN("Polling for configuration message...");
10991098
WS._mqtt->processPackets(2000);
1099+
11001100
// did we get and process the registration message?
11011101
if (!WS.pinCfgCompleted)
11021102
haltError("Did not get configuration message from broker, resetting...");
11031103

1104-
// Publish that we've set up the pins and are ready to run
1105-
// Publish RegistrationComplete message to broker
1106-
wippersnapper_signal_v1_SignalResponse msg = wippersnapper_signal_v1_SignalResponse_init_zero;
1107-
msg.which_payload = wippersnapper_signal_v1_SignalResponse_configuration_complete_tag;
1108-
msg.payload.configuration_complete = true;
1109-
1110-
// encode registration request message
1111-
uint8_t _message_buffer[128];
1112-
pb_ostream_t _msg_stream =
1113-
pb_ostream_from_buffer(_message_buffer, sizeof(_message_buffer));
1114-
1115-
bool _status = pb_encode(
1116-
&_msg_stream,
1117-
wippersnapper_description_v1_RegistrationComplete_fields, &msg);
1118-
size_t _message_len = _msg_stream.bytes_written;
1119-
1120-
// verify message encoded correctly
1121-
if (!_status)
1122-
haltError("Could not encode, resetting...");
1123-
1124-
// Publish message
1125-
WS_DEBUG_PRINTLN("Publishing to pin config complete...");
1126-
WS.publish(WS._topic_device_pin_config_complete, _message_buffer, _message_len, 1);
1127-
WS_DEBUG_PRINTLN("Completed registration process, configuration next!");
1104+
// Publish that we have completed the configuration workflow
1105+
feedWDT();
1106+
runNetFSM();
1107+
publishPinConfigComplete();
11281108

1109+
// Run application
1110+
statusLEDBlink(WS_LED_STATUS_CONNECTED);
1111+
WS_DEBUG_PRINTLN(
1112+
"Registration and configuration complete!\nRunning application...");
1113+
}
11291114

1130-
WS_DEBUG_PRINTLN("Configuration complete!\nRunning application.");
1115+
void Wippersnapper::publishPinConfigComplete() {
1116+
// Publish that we've set up the pins and are ready to run
1117+
wippersnapper_signal_v1_SignalResponse msg =
1118+
wippersnapper_signal_v1_SignalResponse_init_zero;
1119+
msg.which_payload =
1120+
wippersnapper_signal_v1_SignalResponse_configuration_complete_tag;
1121+
msg.payload.configuration_complete = true;
1122+
1123+
// encode registration request message
1124+
uint8_t _message_buffer[128];
1125+
pb_ostream_t _msg_stream =
1126+
pb_ostream_from_buffer(_message_buffer, sizeof(_message_buffer));
1127+
1128+
bool _status =
1129+
pb_encode(&_msg_stream,
1130+
wippersnapper_description_v1_RegistrationComplete_fields, &msg);
1131+
size_t _message_len = _msg_stream.bytes_written;
1132+
1133+
// verify message encoded correctly
1134+
if (!_status)
1135+
haltError("Could not encode, resetting...");
1136+
1137+
// Publish message
1138+
WS_DEBUG_PRINTLN("Publishing to pin config complete...");
1139+
WS.publish(WS._topic_device_pin_config_complete, _message_buffer,
1140+
_message_len, 1);
1141+
WS_DEBUG_PRINTLN("Completed registration process, configuration next!");
11311142
}
11321143

11331144
/**************************************************************************/
@@ -1138,12 +1149,8 @@ void Wippersnapper::connect() {
11381149
/**************************************************************************/
11391150
ws_status_t Wippersnapper::run() {
11401151
// Check networking
1141-
// TODO: Handle MQTT errors within the new netcode, or bring them outwards to
1142-
// another fsm
11431152
runNetFSM();
11441153
feedWDT();
1145-
1146-
// keepalive
11471154
pingBroker();
11481155

11491156
// Process all incoming packets from Wippersnapper MQTT Broker

src/Wippersnapper.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ class Wippersnapper {
211211
bool encodePubRegistrationReq();
212212
void decodeRegistrationResp(char *data, uint16_t len);
213213
void pollRegistrationResp();
214+
// Configuration API
215+
void publishPinConfigComplete();
214216

215217
// run() loop
216218
ws_status_t run();
@@ -281,6 +283,7 @@ class Wippersnapper {
281283
_incomingSignalMsg; /*!< Incoming signal message from broker */
282284

283285
bool pinCfgCompleted = false;
286+
284287
private:
285288
void _init();
286289

@@ -302,7 +305,7 @@ class Wippersnapper {
302305
status resp. from the broker */
303306
char *_topic_description_status_complete;
304307
char *_topic_device_pin_config_complete;
305-
char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */
308+
char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */
306309

307310
Adafruit_MQTT_Subscribe
308311
*_topic_description_sub; /*!< Subscription for registration topic. */

src/components/register/Wippersnapper_Register.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void Wippersnapper::pollRegistrationResp() {
8181
WS_DEBUG_PRINT("Polling for registration message response...");
8282
WS_DEBUG_PRINTLN(WS._boardStatus);
8383
WS._mqtt->processPackets(10); // poll
84-
//WS._mqtt->ping(); // keepalive
84+
// WS._mqtt->ping(); // keepalive
8585
}
8686
}
8787

@@ -131,7 +131,8 @@ void Wippersnapper::decodeRegistrationResp(char *data, uint16_t len) {
131131
WS._boardStatus = WS_BOARD_DEF_OK;
132132

133133
// Publish RegistrationComplete message to broker
134-
wippersnapper_description_v1_RegistrationComplete msg = wippersnapper_description_v1_RegistrationComplete_init_zero;
134+
wippersnapper_description_v1_RegistrationComplete msg =
135+
wippersnapper_description_v1_RegistrationComplete_init_zero;
135136
msg.is_complete = true;
136137

137138
// encode registration request message
@@ -140,23 +141,22 @@ void Wippersnapper::decodeRegistrationResp(char *data, uint16_t len) {
140141
pb_ostream_from_buffer(_message_buffer, sizeof(_message_buffer));
141142

142143
bool _status = pb_encode(
143-
&_msg_stream,
144-
wippersnapper_description_v1_RegistrationComplete_fields, &msg);
144+
&_msg_stream, wippersnapper_description_v1_RegistrationComplete_fields,
145+
&msg);
145146
size_t _message_len = _msg_stream.bytes_written;
146147

147148
// verify message encoded correctly
148-
if (!_status) {
149-
WS._boardStatus = WS_BOARD_DEF_INVALID;
150-
return;
151-
}
149+
if (!_status) {
150+
WS._boardStatus = WS_BOARD_DEF_INVALID;
151+
return;
152+
}
152153

153154
// Publish message
154-
WS.publish(_topic_description_status_complete, _message_buffer, _message_len, 1);
155+
WS.publish(_topic_description_status_complete, _message_buffer,
156+
_message_len, 1);
155157
WS_DEBUG_PRINTLN("Completed registration process, configuration next!");
156158

157159
} else {
158160
WS._boardStatus = WS_BOARD_DEF_INVALID;
159161
}
160-
161-
162162
}

0 commit comments

Comments
 (0)