-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (57 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var spark = require("spark"),
TogglClient = require("toggl-api"),
toggl = new TogglClient({apiToken: "YOURAPITOKEN"}),
_ = require("underscore"),
currentParticle;
initParticle();
function initParticle() {
spark.on("login", function(err, body) {
console.log("Particle device login successful: ", body);
var deviceList = spark.listDevices();
deviceList.then(function(devices) {
currentParticle = _.find(devices, function(device) {
return device.name == "Timon";
});
console.log("Timon was found: ", currentParticle);
currentParticle.onEvent("buttonPressed", function() {
console.log("Button was pressed!");
toggl.getCurrentTimeEntry(function(err, currentEntry) {
if (currentEntry) {
console.log(currentEntry.description + " is running");
toggl.stopTimeEntry(currentEntry.id, function(err, stoppedEntry) {
console.log(stoppedEntry.description + " was stopped");
currentParticle.callFunction("ledTrigger", "OFF", function(result) {
console.log("LED should be off");
});
});
} else {
var currentDate = new Date(),
yesterday = new Date();
yesterday.setDate(currentDate.getDate() - 1);
toggl.getTimeEntries(yesterday.toISOString(), currentDate.toISOString(), function(err, data) {
if (!err) {
var lastEntry = data[data.length - 1];
console.log(lastEntry);
toggl.startTimeEntry({
description: lastEntry.description,
pid: lastEntry.pid,
wid: lastEntry.wid
}, function(err, timeEntry) {
console.log("Entry started");
currentParticle.callFunction("ledTrigger", "ON", function(result) {
console.log("LED should be on");
});
});
}
});
}
});
});
});
});
spark.login({
accessToken: "YOURSPARKTOKEN"
}, function(err, body) {
if (!err) console.log("API login complete!");
});
}