Skip to content

Commit 57fbe80

Browse files
committed
🚧 WIP, dsx, handle event/remove
1 parent 36226cc commit 57fbe80

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,13 @@ bool cbDecodeBrokerToDevice(pb_istream_t *stream, const pb_field_t *field,
379379
return false;
380380
}
381381
break;
382+
case wippersnapper_signal_BrokerToDevice_ds18x20_remove_tag:
383+
WS_DEBUG_PRINTLN("-> DS18X20 Remove Message Type");
384+
if (!WsV2._ds18x20_controller->Handle_Ds18x20Remove(stream)) {
385+
WS_DEBUG_PRINTLN("ERROR: Unable to remove DS18X20 sensor!");
386+
return false;
387+
}
388+
break;
382389
default:
383390
WS_DEBUG_PRINTLN("ERROR: BrokerToDevice message type not found!");
384391
return false;
@@ -895,6 +902,12 @@ bool Wippersnapper_V2::PublishSignal(pb_size_t which_payload, void *payload) {
895902
wippersnapper_signal_DeviceToBroker_ds18x20_added_tag;
896903
MsgSignal.payload.ds18x20_added = *(wippersnapper_ds18x20_Ds18x20Added *)payload;
897904
break;
905+
case wippersnapper_signal_DeviceToBroker_ds18x20_event_tag:
906+
WS_DEBUG_PRINTLN("DS18X20Event");
907+
MsgSignal.which_payload =
908+
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag;
909+
MsgSignal.payload.ds18x20_event = *(wippersnapper_ds18x20_Ds18x20Event *)payload;
910+
break;
898911
default:
899912
WS_DEBUG_PRINTLN("ERROR: Invalid signal payload type, bailing out!");
900913
return false;

src/components/ds18x20/controller.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
118118
*/
119119
/***********************************************************************/
120120
bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
121+
WS_DEBUG_PRINT("Removing DS18X20 sensor...");
121122
// Attempt to decode the stream
122123
if (!_DS18X20_model->DecodeDS18x20Remove(stream)) {
123124
WS_DEBUG_PRINTLN("ERROR: Unable to decode Ds18x20Remove message");
@@ -135,8 +136,7 @@ bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
135136
return true;
136137
}
137138
}
138-
WS_DEBUG_PRINT("Removed OneWire Pin: ");
139-
WS_DEBUG_PRINTLN(pin_name);
139+
WS_DEBUG_PRINT("Removed!");
140140
return true;
141141
}
142142

@@ -186,25 +186,34 @@ void DS18X20Controller::update() {
186186
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT,
187187
temp_f);
188188
}
189-
// Print out the SensorEvent message's contents for debugging
190-
// TODO: After debugging, maybe remove this
189+
// Get and print out the SensorEvent message's contents
191190
wippersnapper_ds18x20_Ds18x20Event event_msg =
192191
*_DS18X20_model->GetDS18x20EventMsg();
193-
WS_DEBUG_PRINTLN("SensorEvent message:");
194-
WS_DEBUG_PRINT("Sensor OneWire bus pin: ");
195-
WS_DEBUG_PRINTLN(event_msg.onewire_pin);
196-
WS_DEBUG_PRINT("Sensor events count: ");
197-
WS_DEBUG_PRINTLN(event_msg.sensor_events_count);
192+
pb_size_t event_count = event_msg.sensor_events_count;
198193

199-
for (int i = 0;
200-
i < _DS18X20_model->GetDS18x20EventMsg()->sensor_events_count; i++) {
201-
WS_DEBUG_PRINT("Sensor type: ");
202-
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].type);
194+
WS_DEBUG_PRINT("Sensor OneWire bus pin: ");
195+
WS_DEBUG_PRINT(temp_dsx_driver.GetOneWirePin());
196+
WS_DEBUG_PRINT(" got ");
197+
WS_DEBUG_PRINT(event_count);
198+
WS_DEBUG_PRINTLN(" sensor events");
199+
for (int i = 0; i < event_count; i++) {
203200
WS_DEBUG_PRINT("Sensor value: ");
204201
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].value.float_value);
205-
WS_DEBUG_PRINT("Sensor value type: ");
206-
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].which_value);
207202
}
203+
204+
// Encode the Ds18x20Event message
205+
if (!_DS18X20_model->EncodeDs18x20Event()) {
206+
WS_DEBUG_PRINTLN("ERROR: Failed to encode Ds18x20Event message");
207+
continue;
208+
}
209+
// Publish the Ds18x20Event message to the broker
210+
WS_DEBUG_PRINT("Publishing Ds18x20Event message to broker...");
211+
if (!WsV2.PublishSignal(
212+
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
213+
_DS18X20_model->GetDS18x20EventMsg())) {
214+
WS_DEBUG_PRINTLN("ERROR: Failed to publish Ds18x20Event message");
215+
continue;
216+
}
217+
WS_DEBUG_PRINTLN("Published!");
208218
}
209-
// TODO: Encode and publish the Ds18x20Event message to the broker
210219
}

src/components/ds18x20/model.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,13 @@ wippersnapper_ds18x20_Ds18x20Event *DS18X20Model::GetDS18x20EventMsg() {
144144
@return True if the message was successfully encoded, False otherwise.
145145
*/
146146
/*************************************************************************/
147-
bool DS18X20Model::EncodeDs18x20Event(
148-
char *onewire_pin, pb_size_t sensor_events_count,
149-
const wippersnapper_sensor_SensorEvent sensor_events[2]) {
150-
// Fill the Ds18x20Event message
151-
_msg_DS18x20Event = wippersnapper_ds18x20_Ds18x20Event_init_zero;
152-
strcpy(_msg_DS18x20Event.onewire_pin, onewire_pin);
153-
_msg_DS18x20Event.sensor_events_count = sensor_events_count;
154-
// Fill with sensor_events
155-
for (int i = 0; i < _msg_DS18x20Event.sensor_events_count; i++) {
156-
_msg_DS18x20Event.sensor_events[i] = sensor_events[i];
157-
}
158-
// Encode and return the Ds18x20Event message
147+
bool DS18X20Model::EncodeDs18x20Event() {
148+
// take the filled _msg_DS18x20Event we built in the controller and encode it
159149
size_t sz_msg;
160150
if (!pb_get_encoded_size(&sz_msg, wippersnapper_ds18x20_Ds18x20Event_fields,
161151
&_msg_DS18x20Event))
162152
return false;
153+
163154
uint8_t buf[sz_msg];
164155
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
165156
return pb_encode(&msg_stream, wippersnapper_ds18x20_Ds18x20Event_fields,

src/components/ds18x20/model.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class DS18X20Model {
3636
bool DecodeDS18x20Remove(pb_istream_t *stream);
3737
wippersnapper_ds18x20_Ds18x20Remove *GetDS18x20RemoveMsg();
3838
// Ds18x20Event Message
39-
bool
40-
EncodeDs18x20Event(char *onewire_pin, pb_size_t sensor_events_count,
41-
const wippersnapper_sensor_SensorEvent sensor_events[2]);
39+
bool EncodeDs18x20Event();
4240
wippersnapper_ds18x20_Ds18x20Event *GetDS18x20EventMsg();
4341
// TODO: move the below to private if we arent using it in controller?
4442
wippersnapper_ds18x20_Ds18x20Event

0 commit comments

Comments
 (0)