Skip to content

Commit

Permalink
Merge pull request #15 from rlisle/state
Browse files Browse the repository at this point in the history
Implement device type and current value.
It should not break previous code, since all existing variables and functions are unaffected.
  • Loading branch information
rlisle committed Jan 20, 2018
2 parents f97439c + 04a6ddd commit 8863e34
Show file tree
Hide file tree
Showing 28 changed files with 176 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Photon/IoTlib/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
example=everything
if [ $# -gt 0 ]; then
example=$1
else
example="starter"
fi
echo "particle compile photon examples/$example"
particle compile photon examples/$example
2 changes: 1 addition & 1 deletion Photon/IoTlib/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=IoT
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Control your Photon projects using events from other Photons, Alexa and iOS devices
Expand Down
3 changes: 3 additions & 0 deletions Photon/IoTlib/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Publish previously uploaded IoT library
particle library publish IoT

104 changes: 104 additions & 0 deletions Photon/IoTlib/src/IoT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ BSD license, check LICENSE for more information.
All text above must be included in any redistribution.
Changelog:
2018-01-17: Add functions for device state and type
2017-10-22: Convert to scene-like behavior
2017-10-12: Add control using device names
2017-05-15: Make devices generic
Expand Down Expand Up @@ -126,7 +127,11 @@ void IoT::begin()
_devices = new Devices();
_deviceNames = new DeviceNames();

// Subscribe to events. There is a 1/second limit for events.
Particle.subscribe(publishNameVariable, globalSubscribeHandler, MY_DEVICES);

// Register cloud variables. Up to 20 may be registered. Name length max 12.
// There does not appear to be any time limit/throttle on variable reads.
if(!Particle.variable(kSupportedActivitiesVariableName, supportedActivitiesVariable))
{
log("Unable to expose "+String(kSupportedActivitiesVariableName)+" variable");
Expand All @@ -137,8 +142,25 @@ void IoT::begin()
log("Unable to expose publishName variable");
return;
}

// Register cloud functions. Up to 15 may be registered. Name length max 12.
// Allows 1 string argument up to 63 chars long.
// There does not appear to be any time limit/throttle on function calls.
if(!Particle.function("program", &IoT::programHandler, this))
{
log("Unable to register program handler");
}
if(!Particle.function("value", &IoT::valueHandler, this))
{
log("Unable to register value handler");
}
if(!Particle.function("type", &IoT::typeHandler, this))
{
log("Unable to register type handler");
}
}


/**
* Loop method must be called periodically,
* typically from the sketch loop() method.
Expand Down Expand Up @@ -231,3 +253,85 @@ void IoT::subscribeHandler(const char *eventName, const char *rawData)
log(" performing activity " + name + " = " + String(value));
_behaviors->performActivity(name, value);
}


/**
* Program Handler
* Called by particle.io to update behaviors.
* It will define a new behavior for an activity for the specified device,
* and return an int indicating if the activity is new or changed.
*
* @param command "device:activity:compare:value:level"
* @returns int response indicating if activity already existed (1) or error (-1)
*/
int IoT::programHandler(String command) {
log("programHandler called with command: " + command);
String components[5];

int lastColonPosition = -1;
for(int i = 0; i < 4; i++)
{
int colonPosition = command.indexOf(':', lastColonPosition+1);
if(colonPosition == -1)
{
return -1 - i;
}
components[i] = command.substring(lastColonPosition+1, colonPosition);
lastColonPosition = colonPosition;
}
components[4] = command.substring(lastColonPosition+1);

// Parse out each item into the correct type
Device *device = _devices->getDeviceWithName(components[0]);
String activity = components[1];
char compare = components[2].charAt(0);
int value = components[3].toInt();
int level = components[4].toInt();

//TODO: see if behavior already exists. If so, then change it.
// Is there already a behavior for the same device and activity?


//TODO: Otherwise just add a new behavior.
log("programHandler: new behavior("+components[0]+", "+components[1]+", "+components[2]+", "+components[3]+", "+components[4]+")");
addBehavior(new Behavior(device, activity, compare, value, level));
addBehavior(new Behavior(device, activity, '=', 0, 0)); // Add 'Off' state also
return 0;
}

/**
* Value Handler
* Called by particle.io to read device current value.
* It will return an int indicating the current value of the specified device.
*
* @param deviceName String name of device
* @returns int response indicating value (0-100) or -1 if invalid device or error.
*/
int IoT::valueHandler(String deviceName) {
log("valueHandler called with device name: " + deviceName);

Device *device = _devices->getDeviceWithName(deviceName);
if(device==NULL) {
return -1;
}
return device->getPercent();
}

/**
* Type Handler
* Called by particle.io to read device type (enum).
* It will return a string indicating the type of the specified device.
* A string is used to allow flexibility and simple future expansion.
*
* @param deviceName String name of device
* @returns int indicating DeviceType of device
*/
int IoT::typeHandler(String deviceName) {
log("typeHandler called with device name: " + deviceName);

Device *device = _devices->getDeviceWithName(deviceName);
if(device==NULL) {
return -1;
}
return static_cast<int>(device->type());
}
4 changes: 4 additions & 0 deletions Photon/IoTlib/src/IoT.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ BSD license, check LICENSE for more information.
All text above must be included in any redistribution.
Changelog:
2018-01-17: Add functions for device state and type
2017-10-22: Convert to scene-like behavior
2017-05-15: Make devices generic
2017-03-24: Rename Patriot
Expand Down Expand Up @@ -104,4 +105,7 @@ class IoT {
void buildSupportedActivitiesVariable();
void performActivities(); //TODO: To be deprecated

int programHandler(String command);
int valueHandler(String deviceName);
int typeHandler(String deviceName);
};
8 changes: 0 additions & 8 deletions Photon/IoTlib/src/behaviors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ A Behavior object describes the response of a device
to received "activity notifications".
Activities are received via Particle.io Pub/Sub.
TODO:
1. persist behaviors to EEPROM
- Photon has 2k EEPROM (actually, flash working like EEPROM)
- So can hold 128 x 16 byte activity structures per device
http://www.github.com/rlisle/Patriot
Written by Ron Lisle
Expand Down Expand Up @@ -48,16 +43,13 @@ int Behaviors::addBehavior(Behavior *behavior)
IoT::log("Max # behaviors exceeded");
return -1;
}
//TODO: Write out all behaviors to EEPROM

return _numBehaviors - 1;
}


void Behaviors::performActivity(String name, int value)
{
Serial.println("Behaviors performing activity "+name+" = "+String(value));

for (int i = 0; i < _numBehaviors; i++)
{
Behavior *behavior = _behaviors[i];
Expand Down
22 changes: 19 additions & 3 deletions Photon/IoTlib/src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ BSD license, check license.txt for more information.
All text above must be included in any redistribution.
Changelog:
2018-01-17: Add function for device type
2017-10-27: V2.0.0
2017-05-20: Provide default implementations for everything,
so this class is not abstract anymore.
Expand All @@ -23,17 +24,32 @@ All text above must be included in any redistribution.
******************************************************************/
#pragma once

enum class DeviceType {
Unknown,
Fan,
Light,
Motor,
NCD8Relay,
Presence,
Relay,
Switch,
TempHumidity,
Ultrasonic
};

class Device {
protected:
String _name;
int _percent;
String _name;
DeviceType _type;
int _percent;

public:
// Note: refer to http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/
// for an explanation of how derived constructor member initialization works.
Device(String name = "") : _name(name) { };
Device(String name = "", DeviceType type = DeviceType::Unknown) : _name(name), _type(type) { }

virtual String name() { return _name; };
virtual DeviceType type() { return _type; };

// This method can either read the device directly, or use a value
// set in the loop() if continuous or asynchronous polling is used.
Expand Down
3 changes: 3 additions & 0 deletions Photon/IoTlib/upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Upload IoT library
# Be sure to increment version number befor uploading
particle library upload
2 changes: 1 addition & 1 deletion Photon/Plugins/DHT/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotDHT
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support DHT11 / DHT22 sensors.
Expand Down
4 changes: 3 additions & 1 deletion Photon/Plugins/DHT/src/PatriotDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ BSD license, check license.txt for more information.
All text above must be included in any redistribution.
Changelog:
2018-01-18: Add name and type properties
2017-05-20: Convert to plugin
2017-03-24: Rename Patriot
2017-03-05: Convert to v2 particle library
Expand All @@ -40,7 +41,8 @@ extern String publishNameVariable;
/**
* Constructor.
*/
DHT::DHT(int pin, int type)
DHT::DHT(int pin, String name, int type)
: Device(name, DeviceType::TempHumidity)
{
lastLoopTime = 0;
interval = kDefaultInterval;
Expand Down
2 changes: 1 addition & 1 deletion Photon/Plugins/Fan/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotFan
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support fans.
Expand Down
3 changes: 2 additions & 1 deletion Photon/Plugins/Fan/src/PatriotFan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ BSD license, check license.txt for more information.
All text above must be included in any redistribution.
Changelog:
2018-01-18: Add type property
2017-10-28: Convert to v2.
2017-05-20: Convert to Patriot plugin library
2017-03-24: Rename Patriot
Expand All @@ -32,7 +33,7 @@ All text above must be included in any redistribution.
* @param name String name used to address the fan.
*/
Fan::Fan(int pinNum, String name)
: Device(name)
: Device(name, DeviceType::Fan)
{
_pinNum = pinNum;
_percent = 0;
Expand Down
2 changes: 1 addition & 1 deletion Photon/Plugins/Light/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotLight
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support lights.
Expand Down
3 changes: 1 addition & 2 deletions Photon/Plugins/Light/src/PatriotLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @param forceDigital True if output On/Off only (even if pin supports PWM)
*/
Light::Light(int pinNum, String name, bool isInverted, bool forceDigital)
: Device(name),
: Device(name, DeviceType::Light),
_pin(pinNum),
_isInverted(isInverted),
_forceDigital(forceDigital)
Expand All @@ -49,7 +49,6 @@ Light::Light(int pinNum, String name, bool isInverted, bool forceDigital)
outputPWM(); // Set initial off state
}


/**
* Set percent
* @param percent Int 0 to 100
Expand Down
2 changes: 1 addition & 1 deletion Photon/Plugins/Motorized/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotMotorized
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support motorized devices.
Expand Down
3 changes: 2 additions & 1 deletion Photon/Plugins/Motorized/src/PatriotMotorized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BSD license, check license.txt for more information.
All text above must be included in any redistribution.
Changelog:
2018-01-18: Add type property
2017-10-28: Convert to v2.
2017-10-08: Add pulse mode
2017-09-17: Initial version based on fan plugin.
Expand All @@ -38,7 +39,7 @@ All text above must be included in any redistribution.
* @param name String name used to address this device.
*/
Motorized::Motorized(int8_t openPinNum, int8_t closePinNum, int8_t duration, String name)
: Device(name)
: Device(name, DeviceType::Motor)
{
_openPinNum = openPinNum;
_closePinNum = closePinNum;
Expand Down
2 changes: 1 addition & 1 deletion Photon/Plugins/NCD8Relay/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotNCD8Relay
version=2.0.2
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support NCD 8 Relay board
Expand Down
3 changes: 2 additions & 1 deletion Photon/Plugins/NCD8Relay/src/PatriotNCD8Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Datasheets:
Changelog:
2018-01-18: Add type property
2017-12-03: Add retry.
2017-10-03: Initial creation
******************************************************************/
Expand All @@ -40,7 +41,7 @@ int8_t NCD8Relay::_addresses[8]; // Addresses of up to 8 boards
* @param duration Optional seconds value to automatically turn off relay. 0 = no automatic turn off.
*/
NCD8Relay::NCD8Relay(int8_t address, int8_t numRelays, int8_t relayNum, String name, int8_t duration)
: Device(name)
: Device(name, DeviceType::NCD8Relay)
{
_relayNum = relayNum;
_percent = 0;
Expand Down
2 changes: 1 addition & 1 deletion Photon/Plugins/Relay/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotRelay
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support a relay connected to a GPIO.
Expand Down
3 changes: 2 additions & 1 deletion Photon/Plugins/Relay/src/PatriotRelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
All text above must be included in any redistribution.
Changelog:
2018-01-18: Add type property
2017-10-31: Initial creation
******************************************************************/

Expand All @@ -27,7 +28,7 @@
* @param duration is an optional automatic turn off duration in seconds.
*/
Relay::Relay(int8_t pinNum, String name, int8_t duration)
: Device(name)
: Device(name, DeviceType::Relay)
{
_pinNum = pinNum;
_duration = duration;
Expand Down
2 changes: 1 addition & 1 deletion Photon/Plugins/Switch/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PatriotSwitch
version=2.0.0
version=2.1.0
author=Ron Lisle
license=BSD
sentence=Extend Patriot IoT to support switches.
Expand Down

0 comments on commit 8863e34

Please sign in to comment.