Skip to content

Commit

Permalink
Updated the schedule implementation to support the changes in the API…
Browse files Browse the repository at this point in the history
… with regard to return values and time being replaced by localtime
  • Loading branch information
peter-murray committed Dec 30, 2015
1 parent ced0067 commit 2cc295f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 32 deletions.
8 changes: 3 additions & 5 deletions hue-api/commands/schedules-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//

var Trait = require("traits").Trait,
deepExtend = require("deep-extend"),
tApiMethod = require("./traits/tApiMethod"),
tDescription = require("./traits/tDescription"),
tScheduleBody = require("./traits/tScheduleBody"),
Expand All @@ -23,10 +24,7 @@ function processAllSchedules(result) {
var values = [];

Object.keys(result).forEach(function (value) {
values.push({
id: value,
name: result[value].name
});
values.push(deepExtend({id: value}, result[value]));
});

return values;
Expand Down Expand Up @@ -115,7 +113,7 @@ apiTraits.setScheduleAttributes = Trait.compose(
"1.0",
"Whitelist"
),
tDescription("Gets all attributes for a schedule."),
tDescription("Sets attributes for a schedule."),
tScheduleBody(true),
tPostProcessing(validateUpdateResults)
);
Expand Down
6 changes: 2 additions & 4 deletions hue-api/commands/traits/tScheduleBody.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

var Trait = require("traits").Trait,
tBodyArguments = require("./tBodyArguments");
var tBodyArguments = require("./tBodyArguments");

module.exports = function (allOptional) {
return tBodyArguments(
Expand All @@ -10,8 +9,7 @@ module.exports = function (allOptional) {
{name: "name", type: "string", maxLength: 32, optional: true},
{name: "description", type: "string", maxLength: 64, optional: true},
{name: "command", type: "string", maxLength: 90, optional: allOptional ? true : false},
{name: "time", type: "time", optional: allOptional ? true : false},
//{name: "localtime", type: "time", optional: allOptional ? true : false},
{name: "localtime", type: "time", optional: allOptional ? true : false},
{name: "status", type: "string", minLength: 5, maxlength: 16, optional: true},
{name: "autodelete", type: "boolean", optional: true}
]
Expand Down
41 changes: 37 additions & 4 deletions hue-api/scheduledEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ module.exports.create = function () {
schedule.withDescription(arg.description);
}

if (arg.time) {
schedule.at(arg.time);
if (arg.time || arg.localtime) {
schedule.at(arg.time || arg.localtime);
}

if (arg.command) {
schedule.withCommand(arg.command);
}

if (arg.status) {
scedule.withEnabledState(arg.status);
}
}
}

Expand All @@ -55,6 +59,21 @@ Schedule.prototype.when = function (time) {
return this;
};

//Schedule.prototype.atRandomizedTime = function(time, random) {
// //Randomized time [YYYY]:[MM]:[DD]T[hh]:[mm]:[ss]A[hh]:[mm]:[ss]
// //([date]T[time]A[time])
//};
//
//Schedule.prototype.atRecurringTime = function() {
// //Recurring times W[bbb]/T[hh]:[mm]:[ss]
// //Every day of the week given by bbb at given time
//};
//
//Schedule.prototype.atRecurringRandomizedTime = function() {
// //Recurring randomized times W[bbb]/T[hh]:[mm]:[ss]A[hh]:[mm]:[ss]
// //Every weekday given by bbb at given left side time, randomized by right side time. Right side time has to be smaller than 12 hours
//};

Schedule.prototype.withName = function(name) {
// The 1.0 API only accepts up to 32 characters for the name
var nameCharMax = 32;
Expand Down Expand Up @@ -87,6 +106,20 @@ Schedule.prototype.withCommand = function(command) {
return this;
};

Schedule.prototype.withEnabledState = function(enabled) {
var state;

if (enabled === "enabled") {
state = "enabled";
} else if (enabled === "disabled") {
state = "disabled";
} else {
state = enabled ? "enabled" : "disabled";
}

utils.combine(this, {status: state});
};

//TODO this time is now performed in localtime inside the bridge as of 1.1
/**
* Obtains the time as a string in UTC format that can be used to trigger a scheduled event.
Expand All @@ -111,12 +144,12 @@ function _getTime(time) {
}

if (timeValue !== null && !isNaN(timeValue)) {
timeValue = new Date(timeValue).toJSON();
timeValue = new Date(timeValue).toJSON();//TODO verify this is in local time...
} else {
throw new errors.ApiError("Invalid time value, '" + time + "'");
}

result.time = timeValue.substring(0, timeValue.lastIndexOf("."));
result.localtime = timeValue.substring(0, timeValue.lastIndexOf("."));
return result;
}

Expand Down
63 changes: 44 additions & 19 deletions test/schedule-tests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";

var expect = require("chai").expect
, assert = require("chai").assert
, hue = require("..")
, HueApi = hue.api
, scheduledEvent = hue.scheduledEvent
, ApiError = hue.ApiError
, testValues = require("./support/testValues.js")
;

Expand Down Expand Up @@ -34,29 +32,56 @@ describe("Hue API", function () {
}

function generateTestScheduleEvent() {
var now = Date.now(),
time = new Date(now);

// Take into account local time zones and daylight savings
time = now + (-1 * time.getTimezoneOffset() * 60 * 60 * 100) + 10000;

return scheduledEvent.create()
.withName("createTest")
.withCommand(validCommand)
.at(time);
.at(Date.now() + 10000);
}


describe("scheduleTests", function () {

//TODO need a test for get all schedules
describe("#getSchedules()", function() {

describe("should list all scheduled events", function() {

function validateschedules(cb) {
return function(schedules) {
expect(schedules).to.be.an.instanceof(Array);
expect(schedules).to.have.length.at.least(1);

expect(schedules[0]).to.have.property("id");
expect(schedules[0]).to.have.property("name");
expect(schedules[0]).to.have.property("time");
expect(schedules[0]).to.have.property("localtime");
expect(schedules[0]).to.have.property("command");
expect(schedules[0]).to.have.property("created");

cb();
}
}

it("using #promise", function(done) {
hue.getSchedules()
.then(validateschedules(done))
.done();
});

it("using #callback", function(done) {
hue.getSchedules(function(err, res) {
expect(err).to.be.null;
validateschedules(done)(res);
});
});
});
});


describe("#scheduleEvent", function () {

function checkResults(result) {
expect(result).to.exist;
expect(result).to.have.property("id").to.not.be.null;

return result.id;
}

Expand Down Expand Up @@ -186,9 +211,9 @@ describe("Hue API", function () {
.done();
});

it.skip("should update an existing schedule time", function (finished) {
hue.updateSchedule(scheduleId, {"time": "December 12, 2015, 12:01:33"})
.then(validateUpdate(["time"]))
it("should update an existing schedule time", function (finished) {
hue.updateSchedule(scheduleId, {"localtime": "December 12, 2020, 12:01:33"})
.then(validateUpdate(["localtime"]))
.then(testComplete(finished))
.done();
});
Expand All @@ -204,7 +229,7 @@ describe("Hue API", function () {
var updates = {
"name" : "New Name",
"description": "Does Something",
"time" : "February 18, 2016 00:00:31",
"localtime" : Date.now() + 100,
"command" : {
"address": "/api/0/lights/invalid",
"method" : "GET",
Expand Down Expand Up @@ -250,10 +275,10 @@ describe("Hue API", function () {
});
});

it.skip("should update an existing schedule time", function (finished) {
hue.updateSchedule(scheduleId, {"time": "December 12, 2015, 12:01:33"}, function(err, results) {
it("should update an existing schedule time", function (finished) {
hue.updateSchedule(scheduleId, {"localtime": "December 12, 2020, 12:01:33"}, function(err, results) {
expect(err).to.be.null;
validateUpdate(["time"])(results);
validateUpdate(["localtime"])(results);
finished();
});
});
Expand All @@ -270,7 +295,7 @@ describe("Hue API", function () {
var updates = {
"name" : "New Name",
"description": "Does Something",
"time" : "February 18, 2016 00:00:31",
"localtime" : "February 18, 2016 00:00:31",
"command" : {
"address": "/api/0/lights/invalid",
"method" : "GET",
Expand Down
14 changes: 14 additions & 0 deletions test/scheduledEvent-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ describe("ScheduleEvent", function () {
});


describe("withEnabledState()", function() {

it ("should set an enabled state", function () {
scheduledEvent.withEnabledState(true);
expect(scheduledEvent).to.have.property("status", "enabled");
});

it ("should set a disabled state", function () {
scheduledEvent.withEnabledState(false);
expect(scheduledEvent).to.have.property("status", "disabled");
});
});


describe("create() from object", function () {

it("should load name and description values", function () {
Expand Down

0 comments on commit 2cc295f

Please sign in to comment.