Commit 6aab6688 authored by PhiBo's avatar PhiBo 🚀

Use template for DataString

parent 423ef53a
#include "sensor_node.h"
DataString::DataString(uint16_t max_length)
{
this->max_length = max_length;
this->data = (uint8_t *)malloc(sizeof(uint8_t) * max_length);
this->length = 0;
}
void DataString::reset()
{
this->length = 0;
}
size_t DataString::write(uint8_t c)
{
this->data[this->length] = c;
this->length++;
}
ArduRPC_SensorNode::ArduRPC_SensorNode(ArduRPC &rpc, char *name) : ArduRPCHandler()
{
this->type = 0x9999;
this->registerSelf(rpc, name, (void *)this);
this->status = 0;
this->cache = new DataString(1024);
}
uint8_t ArduRPC_SensorNode::call(uint8_t cmd_id)
......@@ -44,13 +25,13 @@ uint8_t ArduRPC_SensorNode::call(uint8_t cmd_id)
this->sensor_uuid[SENSOR_NODE_KEY_MAX_LENGTH] = '\0';
this->status = 2;
this->cache->reset();
this->cache->print("[");
this->cache.reset();
this->cache.print("[");
return RPC_RETURN_SUCCESS;
} else if (cmd_id == 0x11) {
/* finish() */
if(this->status == 2 or this->status == 3) {
this->cache->print("]");
this->cache.print("]");
}
this->status = 4;
this->submitData();
......@@ -76,52 +57,52 @@ uint8_t ArduRPC_SensorNode::call(uint8_t cmd_id)
if(this->status == 2) {
this->status = 3;
} else {
this->cache->print(",");
this->cache.print(",");
}
this->cache->print("[");
this->cache.print("[");
// sensor id
u8 = this->_rpc->getParam_uint8();
this->cache->print(u8);
this->cache->print(",");
this->cache.print(u8);
this->cache.print(",");
// sensor type
u16 = this->_rpc->getParam_uint16();
this->cache->print(u16);
this->cache->print(",");
this->cache.print(u16);
this->cache.print(",");
// value type
u8 = this->_rpc->getParam_uint8();
this->cache->print(u8);
this->cache->print(",");
this->cache.print(u8);
this->cache.print(",");
// value
// - read value type
u8 = this->_rpc->getParam_uint8();
// - read value
if(u8 == RPC_INT8) {
i8 = this->_rpc->getParam_int8();
this->cache->print(i8);
this->cache.print(i8);
} else if(u8 == RPC_UINT8) {
u8 = this->_rpc->getParam_uint8();
this->cache->print(u8);
this->cache.print(u8);
} else if(u8 == RPC_INT16) {
i16 = this->_rpc->getParam_int16();
this->cache->print(i16);
this->cache.print(i16);
} else if(u8 == RPC_UINT16) {
u16 = this->_rpc->getParam_uint16();
this->cache->print(u16);
this->cache.print(u16);
} else if(u8 == RPC_INT32) {
i32 = this->_rpc->getParam_int32();
this->cache->print(i32);
this->cache.print(i32);
} else if(u8 == RPC_UINT32) {
u32 = this->_rpc->getParam_uint32();
this->cache->print(u32);
this->cache.print(u32);
} else if(u8 == RPC_FLOAT) {
f = this->_rpc->getParam_float();
this->cache->print(f);
this->cache.print(f);
} else {
// value type not supported
this->cache->print("\"n/a\"");
this->cache.print("\"n/a\"");
}
this->cache->print("]");
this->cache.print("]");
return RPC_RETURN_SUCCESS;
}
return RPC_RETURN_COMMAND_NOT_FOUND;
......@@ -153,10 +134,10 @@ void ArduRPC_SensorNode::submitData()
client->println(this->sensor_key);
client->print("X-Sensor-Version: 1\r\n");
client->print("Content-Length: ");
client->println(this->cache->length);
client->println(this->cache.length);
client->println();
for(i = 0; i < this->cache->length; i++) {
Serial.print((char)this->cache->data[i]);
for(i = 0; i < this->cache.length; i++) {
Serial.print((char)this->cache.data[i]);
}
this->status = 0;
client->stop();
......
......@@ -34,16 +34,20 @@
#define SENSOR_NODE_UUID_MAX_LENGTH 64
#define SENSOR_NODE_KEY_MAX_LENGTH 64
template <uint16_t SIZE>
class DataString : public Print
{
public:
DataString(uint16_t);
void reset();
virtual size_t write(uint8_t);
DataString() { this->length = 0; };
void reset() { this->length = 0; };
virtual size_t write(uint8_t c) {
this->data[this->length] = c;
this->length++;
};
uint16_t length;
uint8_t *data;
uint8_t data[SIZE];
private:
uint16_t max_length;
uint16_t max_length = SIZE;
};
class ArduRPC_SensorNode : public ArduRPCHandler
......@@ -55,7 +59,7 @@ class ArduRPC_SensorNode : public ArduRPCHandler
void ICACHE_FLASH_ATTR submitData();
private:
uint8_t status;
DataString *cache;
DataString<1024> cache;
char sensor_uuid[SENSOR_NODE_UUID_MAX_LENGTH + 1];
char sensor_key[SENSOR_NODE_UUID_MAX_LENGTH + 1];
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment