diff --git a/API.md b/API.md index 05a4320..2e2a9ab 100644 --- a/API.md +++ b/API.md @@ -207,6 +207,8 @@ enabled: whether or not this trigger is enabled Changes the time trigger settings. Request body format is the same as is returned in the GET request. +If the "triggers" array is omitted entirely, the items will not be cleared or overwritten. + ## GET /api/triggers/motion @@ -257,6 +259,8 @@ enabled: whether or not this trigger is enabled Changes the motion trigger settings. Request body format is the same as is returned in the GET request. +If the "triggers" array is omitted entirely, the items will not be cleared or overwritten. + ## POST /api/firmware Uploads new firmware. The bin file should be posted as a multipart/form-data file attachment. Name is not relevant. \ No newline at end of file diff --git a/src/settings/triggers/motion.cpp b/src/settings/triggers/motion.cpp index 0fa9abe..0fc04b3 100644 --- a/src/settings/triggers/motion.cpp +++ b/src/settings/triggers/motion.cpp @@ -55,23 +55,26 @@ bool MotionTriggerSettings::fromJson(char* data, bool* changed) transitionTime(root["transitionTime"]); delay(root["delay"]); - JsonArray& jsonTriggers = root["triggers"]; - if (mTriggers != nullptr) - delete [] mTriggers; - - mTriggerCount = jsonTriggers.size(); - mTriggers = new MotionTrigger[mTriggerCount]; - - - for (uint8_t i = 0; i < mTriggerCount; i++) + if (root.containsKey("triggers")) { - JsonObject& jsonTrigger = jsonTriggers[i]; - MotionTrigger* trigger = &mTriggers[i]; + JsonArray& jsonTriggers = root["triggers"]; + if (mTriggers != nullptr) + delete [] mTriggers; - trigger->pin = jsonTrigger["pin"]; - trigger->brightness = jsonTrigger["brightness"]; - trigger->direction = (MotionDirection)(uint8_t)jsonTrigger["direction"]; - trigger->enabled = jsonTrigger["enabled"]; + mTriggerCount = jsonTriggers.size(); + mTriggers = new MotionTrigger[mTriggerCount]; + + + for (uint8_t i = 0; i < mTriggerCount; i++) + { + JsonObject& jsonTrigger = jsonTriggers[i]; + MotionTrigger* trigger = &mTriggers[i]; + + trigger->pin = jsonTrigger["pin"]; + trigger->brightness = jsonTrigger["brightness"]; + trigger->direction = (MotionDirection)(uint8_t)jsonTrigger["direction"]; + trigger->enabled = jsonTrigger["enabled"]; + } } if (changed != nullptr) diff --git a/src/settings/triggers/time.cpp b/src/settings/triggers/time.cpp index 83969a8..3d8d186 100644 --- a/src/settings/triggers/time.cpp +++ b/src/settings/triggers/time.cpp @@ -89,24 +89,27 @@ bool TimeTriggerSettings::fromJson(char* data, bool* changed) enabled(root["enabled"]); transitionTime(root["transitionTime"]); - JsonArray& jsonTriggers = root["triggers"]; - if (mTriggers != nullptr) - delete [] mTriggers; - - mTriggerCount = jsonTriggers.size(); - mTriggers = new TimeTrigger[mTriggerCount]; - - - for (uint8_t i = 0; i < mTriggerCount; i++) + if (root.containsKey("triggers")) { - JsonObject& jsonTrigger = jsonTriggers[i]; - TimeTrigger* trigger = &mTriggers[i]; + JsonArray& jsonTriggers = root["triggers"]; + if (mTriggers != nullptr) + delete [] mTriggers; - trigger->time = jsonTrigger["time"]; - trigger->daysOfWeek = jsonTrigger["daysOfWeek"]; - trigger->brightness = jsonTrigger["brightness"]; - trigger->triggerType = (TimeTriggerType)(uint8_t)jsonTrigger["triggerType"]; - trigger->enabled = jsonTrigger["enabled"]; + mTriggerCount = jsonTriggers.size(); + mTriggers = new TimeTrigger[mTriggerCount]; + + + for (uint8_t i = 0; i < mTriggerCount; i++) + { + JsonObject& jsonTrigger = jsonTriggers[i]; + TimeTrigger* trigger = &mTriggers[i]; + + trigger->time = jsonTrigger["time"]; + trigger->daysOfWeek = jsonTrigger["daysOfWeek"]; + trigger->brightness = jsonTrigger["brightness"]; + trigger->triggerType = (TimeTriggerType)(uint8_t)jsonTrigger["triggerType"]; + trigger->enabled = jsonTrigger["enabled"]; + } } if (changed != nullptr)