Skip to content

Commit

Permalink
v1.5.11
Browse files Browse the repository at this point in the history
 - improved support for heyOOCSI (function parameters, signatures, )
 - added setState / setValue for heyOOCSI
 - added JSON manipulation functions
 - added getName for OOCSI clients
 - added data structure to keep heyOOCSI channel information
  • Loading branch information
matsfunk committed Apr 6, 2022
1 parent 7000f4c commit c49da77
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 73 deletions.
Binary file modified dist/oocsi.zip
Binary file not shown.
17 changes: 16 additions & 1 deletion dist/oocsi/OOCSI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,21 @@ JsonObject OOCSI::addJsonObject(const char* key) {
return object;
}

void OOCSI::setJsonObject(const char* key, JsonObject obj) {
jsonMessage[key] = obj;
}

// function for adding a nested Json array to the message,
// returns the new array which can then be filled with data
JsonArray OOCSI::addJsonArray(const char* key) {
JsonArray array = jsonMessage.createNestedArray(key);
return array;
}

void OOCSI::setJsonArray(const char* key, JsonArray arr) {
jsonMessage[key] = arr;
}

// close and send out the message
void OOCSI::sendMessage() {
client.print("sendraw ");
Expand All @@ -617,8 +625,10 @@ void OOCSI::sendMessage() {

// log the outgoing message
void OOCSI::printSendMessage() {
if(logging)
if(logging) {
serializeJson(jsonMessage, Serial);
Serial.println();
}
}

// return client list
Expand Down Expand Up @@ -670,6 +680,11 @@ bool OOCSI::containsClient(const char* clientName) {
return true;
}

// return the client name
String OOCSI::getName() {
return OOCSIName;
}

// print message if logging is activated
void OOCSI::print(const String &message) {
if(logging)
Expand Down
8 changes: 6 additions & 2 deletions dist/oocsi/OOCSI.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class OOCSI {
void keepAlive();
void subscribe(const char* chan);
void unsubscribe(const char* chan);
OOCSIDevice heyOOCSI();
OOCSIDevice heyOOCSI(const char* name);

// sending data
Expand All @@ -70,7 +71,9 @@ class OOCSI {
OOCSI addFloatArray(const char* key, float* value, int len);
OOCSI addStringArray(const char* key, String value[], int len);
JsonObject addJsonObject(const char* key);
void setJsonObject(const char* key, JsonObject obj);
JsonArray addJsonArray(const char* key);
void setJsonArray(const char* key, JsonArray arr);
void sendMessage();
void printSendMessage();

Expand All @@ -91,13 +94,14 @@ class OOCSI {
bool has(const char* key);
String keys();
void printMessage();
void setActivityLEDPin(int ledPin);
void setLogging(bool log);

// misc functions
void setActivityLEDPin(int ledPin);
void setLogging(bool log);
String getClients();
String getChannels();
bool containsClient(const char* clientName);
String getName();


private:
Expand Down
113 changes: 89 additions & 24 deletions dist/oocsi/OOCSIDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,46 @@

#include "OOCSIDevice.h"

// create a prototype:
OOCSIDevice::OOCSIDevice(const char* prototypeName, OOCSI &client, const char* OOCSIName) {
// create a device for a given OOCSI client
OOCSIDevice::OOCSIDevice(const char* name, OOCSI &client) {
oocsi = &client;
oocsi->newMessage("heyOOCSI!");
device = oocsi->addJsonObject(prototypeName);
prototypeName = name;

StaticJsonDocument<1000> docDevices;
device = docDevices.to<JsonObject>();
properties = device.createNestedObject("properties");
properties["device_id"] = OOCSIName;
components = device.createNestedObject("components");
location = device.createNestedObject("location");

StaticJsonDocument<1000> docChannels;
channels = docChannels.to<JsonObject>();
}

// Add device properties
// add device properties
void OOCSIDevice::addProperty(const char* property, const char* propertyValue) {
properties[property] = propertyValue;
}

// Location initialisation
void OOCSIDevice::addLocation(const char* locationName, const char* latitude,const char* longitude) {
// location initialisation
void OOCSIDevice::addLocation(const char* locationName, int latitude, int longitude) {
location[locationName][0] = latitude;
location[locationName][1] = longitude;
}

// Sensor configuration
void OOCSIDevice::addSensor(const char* sensorName, const char* sensorChannel, const char* sensorType, const char* sensorDefaultValue, const char* sensorUnit, const char* icon) {
// sensor configuration
void OOCSIDevice::addSensor(const char* sensorName, const char* sensorChannel, const char* sensorType, int sensorDefaultValue, const char* sensorUnit, const char* icon) {
JsonObject sensor = components.createNestedObject(sensorName);
sensor["channel_name"] = sensorChannel;
sensor["type"] = "sensor";
sensor["value"] = sensorDefaultValue;
sensor["sensor_type"] = sensorType;
sensor["unit"] = sensorUnit;
sensor["icon"] = icon;
sensor["icon"] = icon;
channels[sensorName] = sensorChannel;
}

// Number input configuration
void OOCSIDevice::addNumber(const char* numberName, const char* numberChannel, const char* numberMin, const char* numberMax, const char* numberDefaultValue, const char* numberUnit, const char* icon) {
// number input configuration
void OOCSIDevice::addNumber(const char* numberName, const char* numberChannel, float numberMin, float numberMax, float numberDefaultValue, const char* numberUnit, const char* icon) {
JsonObject number = components.createNestedObject(numberName);
number["channel_name"] = numberChannel;
number["type"] = "number";
Expand All @@ -53,30 +58,45 @@ void OOCSIDevice::addNumber(const char* numberName, const char* numberChannel, c
number["value"] = numberDefaultValue;
number["sensor_type"] = "number";
number["unit"]=numberUnit;
number["icon"] = icon;
number["icon"] = icon;
channels[numberName] = numberChannel;
}

// number input configuration
void OOCSIDevice::addNumber(const char* numberName, const char* numberChannel, float numberDefaultValue, const char* numberUnit, const char* icon) {
JsonObject number = components.createNestedObject(numberName);
number["channel_name"] = numberChannel;
number["type"] = "number";
number["value"] = numberDefaultValue;
number["sensor_type"] = "number";
number["unit"]=numberUnit;
number["icon"] = icon;
channels[numberName] = numberChannel;
}

// Binary sensor configuration
void OOCSIDevice::addBinarySensor(const char* sensorName, const char* sensorChannel, const char* sensorType, const char* sensorDefaultState, const char* icon) {
// binary sensor configuration
void OOCSIDevice::addBinarySensor(const char* sensorName, const char* sensorChannel, const char* sensorType, bool sensorDefaultState, const char* icon) {
JsonObject sensor = components.createNestedObject(sensorName);
sensor["channel_name"] = sensorChannel;
sensor["type"] = "binary_sensor";
sensor["state"] = sensorDefaultState;
sensor["sensor_type"] = sensorType;
sensor["icon"] = icon;
sensor["icon"] = icon;
channels[sensorName] = sensorChannel;
}

// Switch configuration
void OOCSIDevice::addSwitch(const char* switchName, const char* switchChannel, const char* switchDefaultState, const char* icon) {
// switch configuration
void OOCSIDevice::addSwitch(const char* switchName, const char* switchChannel, bool switchDefaultState, const char* icon) {
JsonObject switchConfig = components.createNestedObject(switchName);
switchConfig["channel_name"] = switchChannel;
switchConfig["type"] = "switch";
switchConfig["state"] = switchDefaultState;
switchConfig["icon"] = icon;
switchConfig["icon"] = icon;
channels[switchName] = switchChannel;
}

// light configuration
void OOCSIDevice::addLight(const char* lightName, const char* lightChannel, const char* ledType, int spectrum, const char* lightDefaultState, int defaultBrightness, const char* icon) {
void OOCSIDevice::addLight(const char* lightName, const char* lightChannel, const char* ledType, int spectrum, bool lightDefaultState, int defaultBrightness, const char* icon) {
JsonObject light = components.createNestedObject(lightName);
light["channel_name"] = lightChannel;
light["type"] = "light";
Expand Down Expand Up @@ -109,10 +129,11 @@ void OOCSIDevice::addLight(const char* lightName, const char* lightChannel, cons
spectrums.add("WHITE");
break;
}
channels[lightName] = lightChannel;
}

//light configuration incase of CCT lights
void OOCSIDevice::addLight(const char* lightName, const char* lightChannel, const char* ledType, int spectrum, const char* lightDefaultState, int defaultBrightness, int miredMax, int miredMin, const char* icon) {
// light configuration incase of CCT lights
void OOCSIDevice::addLight(const char* lightName, const char* lightChannel, const char* ledType, int spectrum, bool lightDefaultState, int defaultBrightness, int miredMax, int miredMin, const char* icon) {
JsonObject light = components.createNestedObject(lightName);
light["channel_name"] = lightChannel;
light["type"] = "light";
Expand Down Expand Up @@ -147,14 +168,58 @@ void OOCSIDevice::addLight(const char* lightName, const char* lightChannel, cons
}
light["max"] = miredMax;
light["min"] = miredMin;
channels[lightName] = lightChannel;
}

// send configured interview
void OOCSIDevice::sayHi() {
oocsi->newMessage("heyOOCSI!");
String name = oocsi->getName();
properties["device_id"] = name;
if(strlen(prototypeName) == 0) {
oocsi->setJsonObject(name.c_str(), device);
} else {
oocsi->setJsonObject(prototypeName, device);
}
oocsi->sendMessage();
}

// --------------------------------------------------------------------------------------------------------------------

void OOCSIDevice::setValue(const char* componentName, float value) {
setValue(componentName, "value", value);
}

void OOCSIDevice::setValue(const char* componentName, const char* key, float value) {
if(channels.containsKey(componentName)) {
oocsi->newMessage(channels[componentName]);
oocsi->addFloat(key, value);
oocsi->addString("component", componentName);
oocsi->sendMessage();
}
}

void OOCSIDevice::setState(const char* componentName, bool state) {
setState(componentName, "value", state);
}

void OOCSIDevice::setState(const char* componentName, const char* key, bool state) {
if(channels.containsKey(componentName)) {
oocsi->newMessage(channels[componentName]);
oocsi->addBool(key, state);
oocsi->addString("component", componentName);
oocsi->sendMessage();
}
}

// --------------------------------------------------------------------------------------------------------------------

// return OOCSIDevice with default name
OOCSIDevice OOCSI::heyOOCSI() {
return OOCSIDevice("", *this);
}

// return OOCSIDevice with given name
OOCSIDevice OOCSI::heyOOCSI(const char* name) {
return OOCSIDevice(name, *this, OOCSIName);
return OOCSIDevice(name, *this);
}
39 changes: 30 additions & 9 deletions dist/oocsi/OOCSIDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,47 @@
class OOCSI;
class OOCSIDevice {
public:
// setup, connection, subscription
OOCSIDevice(const char* prototypeName, OOCSI &client, const char* OOCSIName);
// setup
OOCSIDevice(const char* prototypeName, OOCSI &client);

// configure the device
void addProperty(const char* property, const char* propertyValue);
void addLocation(const char* locationName, const char* latitude,const char* longitude);
void addSensor(const char* sensorName , const char* sensorChannel, const char* sensorType, const char* sensorDefaultValue, const char* sensorUnit, const char* icon);
void addNumber(const char* numberName , const char* numberChannel, const char* numberMin, const char* numberMax, const char* numberDefaultValue, const char* numberUnit, const char* icon);
void addBinarySensor(const char* sensorName , const char* sensorChannel, const char* sensorType, const char* sensorDefaultState, const char* icon);
void addSwitch(const char* switchName , const char* switchChannel, const char* switchDefaultState, const char* icon);
void addLight(const char* lightName , const char* lightChannel, const char* ledType, int spectrum, const char* lightDefaultState, int defaultBrightness, int miredMax, int miredMin, const char* icon);
void addLight(const char* lightName , const char* lightChannel, const char* ledType, int spectrum, const char* lightDefaultState, int defaultBrightness, const char* icon);
void addLocation(const char* locationName, int latitude, int longitude);
void addSensor(const char* sensorName , const char* sensorChannel, const char* sensorType, int sensorDefaultValue, const char* sensorUnit, const char* icon);
void addNumber(const char* numberName , const char* numberChannel, float numberDefaultValue, const char* numberUnit, const char* icon);
void addNumber(const char* numberName , const char* numberChannel, float numberMin, float numberMax, float numberDefaultValue, const char* numberUnit, const char* icon);
void addBinarySensor(const char* sensorName , const char* sensorChannel, const char* sensorType, bool sensorDefaultState, const char* icon);
void addSwitch(const char* switchName , const char* switchChannel, bool switchDefaultState, const char* icon);
void addLight(const char* lightName , const char* lightChannel, const char* ledType, int spectrum, bool lightDefaultState, int defaultBrightness, int miredMax, int miredMin, const char* icon);
void addLight(const char* lightName , const char* lightChannel, const char* ledType, int spectrum, bool lightDefaultState, int defaultBrightness, const char* icon);

// finalize setup with a single message to OOCSI server
void sayHi();

// get value or state of device components after setup
float getValue(const char* componentName, float defaultValue);
float getValue(const char* componentName, const char* key, float defaultValue);
bool getState(const char* componentName, bool defaultState);
bool getState(const char* componentName, const char* key, bool defaultState);

// set value or state of device components after setup
void setValue(const char* componentName, float value);
void setValue(const char* componentName, const char* key, float value);
void setState(const char* componentName, bool state);
void setState(const char* componentName, const char* key, bool state);

private:
OOCSI* oocsi;
const char* prototypeName;

// just for the sayHi message
JsonObject device;
JsonObject properties;
JsonObject components;
JsonObject location;

// for later when setting values and states
JsonObject channels;
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/******************************************************************************
Example of the OOCSI-ESP library connecting to OOCSI over WiFi and creating
an OOCSI device with a location and a binary sensor component that is then
registered. Loop() shows how to set the state the binary sensor component.
******************************************************************************/

#include "OOCSI.h"
#include "OOCSIDevice.h"

// use this if you want the OOCSI-ESP library to manage the connection to the Wifi
// SSID of your Wifi network, the library currently does not support WPA2 Enterprise networks
const char* ssid = "yourssid";
// Password of your Wifi network.
const char* password = "yourpassword";

// name for connecting with OOCSI (unique handle)
const char* OOCSIName = "ESP_sayHi_test_binary_sensor";
// put the adress of your OOCSI server here, can be URL or IP address string
const char* hostserver = "your OOCSI server address";

// OOCSI reference for the entire sketch
OOCSI oocsi = OOCSI();
OOCSIDevice binarySensor = oocsi.heyOOCSI();

// put your setup code here, to run once:
void setup() {
Serial.begin(115200);

// output OOCSI activity on the built-in LED
pinMode(LED_BUILTIN, OUTPUT);
oocsi.setActivityLEDPin(LED_BUILTIN);

// use this to switch off logging to Serial
// oocsi.setLogging(false);

// setting up OOCSI. processOOCSI is the name of the function to call when receiving messages, can be a random function name
// connect wifi and OOCSI to the server
oocsi.connect(OOCSIName, hostserver, ssid, password);

// configure the binary sensor with a textual location label and numerical coordinates
binarySensor.addLocation("hallway", 380, 430);
// add a binary sensor, for example, a sensor that senses if a door is open or not
// parameters: name, channel for updates, type, default is off (false), and a "door" icon
binarySensor.addBinarySensor("front_door_open", "hallway", "door", false, "door");
// register binary sensor on server
binarySensor.sayHi();
}

// put your main code here, to run repeatedly:
void loop() {

// switch the binary sensor state randomly
binarySensor.setState("front_door_open", random(0, 2) < 1);

// let OOCSI keep the connection to the OOCSI server fresh and crunchy
// use this keepAlive() function if you do NOT need to receive data from OOCSI
// use the check() function if you also need to process incoming messages
oocsi.keepAlive();

// wait 2.5 secs for the next run
delay(2500);
}
Loading

0 comments on commit c49da77

Please sign in to comment.