-
Notifications
You must be signed in to change notification settings - Fork 3
/
artik_cloud_switch_rest.js
98 lines (81 loc) · 2.73 KB
/
artik_cloud_switch_rest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2016 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var sami = "https://api.samsungsami.io/v1.1/messages";
var cloud_switch_id = "08027d8460e34a0db81c3268285da1e9";
var token = "33b5e6833b88476890f410ee02dafb4b";
/**
* USER TOKEN:The token(/bearer) information obtained from calling the self API is the user token
* The user token is valid as long as the user is logged into the artik cloud account by default
*
* DEVICE TOKEN: The token(/bearer) information obtained from attaching the device to the user account
* is the Device token. It is valid as long as the device is attached to the user account by default
*
* APPLICATION TOKEN:
* The session token is valid for 15 min since it is created by default
**/
// var ddid = "c15fae7f2e584eeebb3b0ef6fa917315";
/*
var serialport = require("serialport")
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
*/
var Client = require("node-rest-client").Client;
var c = new Client();
var Gpio = require('onoff').Gpio,
button = new Gpio(121, 'in', 'both');
gnd = new Gpio(123,'out');
var myButtonCount=0;
var myButtonState=false;
function exit() {
button.unexport();
process.exit();
}
function build_mesg (newState, ts) {
var args = {
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer "+token
},
data: {
"sdid": cloud_switch_id,
"ts": ts,
"type": "message",
"data": {
"state": newState
}
}
};
return args;
}
function toggleButton(err, state){
if( (myButtonCount++) %2 ) //the button, we have do not maintain the state memory, hence doing it in SW
{
myButtonState = myButtonState ? false : true ; // toggle my button state;
console.log('my current Button State '+myButtonState);
var args = build_mesg(myButtonState, new Date().valueOf());
c.post(sami, args, function(data, response) {
console.log(data);
});
}
//console.log('Button event '+state);
}
/* End of console ****/
console.log('starting cloud switch');
button.watch(toggleButton);
process.on('SIGINT', exit);