-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
122 lines (108 loc) · 3.21 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var IBMIoT = require('./iot.js'),
Droid = require('./droid.js'),
Brain = require('./brain.js');
function withEar(droid, bb8, handler) {
var config = {
// place your configurations here
"org" : "",
"id" : "",
"domain": "internetofthings.ibmcloud.com",
"auth-key" : "",
"auth-token" : ""
};
var iot = new IBMIoT.AppService(config);
iot.connect({
onConnected: function() {
console.log('### Subscriber.Connected ###');
bb8.color('white', handler);
iot.subscribe('Robot', 'Robot', 'Run');
},
onDeviceEvent: function(deviceType, deviceId, eventType, format, payload) {
console.log('### appService.DeviceEvent ###');
var command = JSON.parse(payload);
console.log(command);
switch(command.action) {
case '#spin':
bb8.setHeading(45);
break;
case '#turn':
bb8.setHeading(90);
break;
case '#color':
bb8.randomColor(handler);
break;
case '#run':
bb8.roll(150, 0);
setTimeout(function(){
bb8.stop(handler);
}, 3000);
break;
case '#back':
bb8.roll(150, 180);
setTimeout(function(){
bb8.stop();
}, 3000);
break;
}
}
});
}
function initDroid(callback) {
// Michael's bb8 by default
var droid = new Droid('4bffadbe51124d2c832f46f4ade1ff86');
droid.connect({
onConnected: function(bb8, handler) {
bb8.startCalibration();
callback(droid, bb8, handler);
}
});
}
initDroid(withEar);
function withMouth(think) {
var config = {
// place your configurations here
"org" : "",
"id" : "",
"domain": "",
"auth-key" : "",
"auth-token" : ""
};
var iot = new IBMIoT.AppService(config);
iot.connect({
onConnected: function() {
console.log('### Publisher.Connected ###');
iot.publish({
type: 'Controller',
id: 'Mind',
event: 'Run',
data: { connect: true }
});
think.on('blink', function (data) {
console.log('### blink ###');
data.blink = true;
iot.publish({
type: 'Controller',
id: 'Mind',
event: 'Run',
data: data
});
});
think.on('data', function (data) {
console.log('### data ###');
data.data = true;
iot.publish({
type: 'Controller',
id: 'Mind',
event: 'Run',
data: data
});
});
}
});
}
function initBrain(callback) {
var brain = new Brain();
var think = brain.think();
callback(think);
}
initBrain(withMouth);