-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (106 loc) · 3.44 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
123
const SuperPixelApps = require('./superpixelapps');
const SuperPixelApp = require('./superpixelapp');
const SuperPixel = require('./superpixel');
function connectOptions() {
if (process.argv.length < 4) {
return { path: "/dev/ttyUSB0" };
} else if (process.argv[3].indexOf(".") != -1) {
return { ip: process.argv[3] };
} else {
return { path: process.argv[3] };
}
}
if (process.argv.length <= 2) {
console.log("Wrong number of arguments. Use the source luke.");
} else if (process.argv[2] == "list-devices") {
SuperPixel.listConnectedDevices()
.then((devices) => {
console.log(`Found ${devices.length} connected devices`);
devices.forEach(function(device) {
console.log(`> ${device.path}`);
device.disconnect();
});
});
} else if (process.argv[2] == "device-info") {
let kit = new SuperPixel(connectOptions());
kit.connect()
.then((device) => {
console.log(`Connected to ${kit.location()}`);
return kit.getDeviceInfo();
})
.then((data) => {
console.log(`Version: ${data.major}.${data.minor}.${data.patch}`);
return kit.getBatteryStatus();
})
.then((data) => {
console.log(`Battery: ${data.percent} ${data.status}`);
return kit.getWifiStatus();
})
.then((data) => {
console.log(`Wifi: ${data.ip} ${data.ssid}`);
return kit.scanWifi()
})
.then((data) => {
let networks = [];
data.forEach(network => networks.push(network.ssid));
console.log("Networks: " + JSON.stringify(networks));
kit.disconnect();
return Promise.resolve();
})
.catch(error => {
console.log("failed with error: " + error);
kit.disconnect();
});
} else if (process.argv[2] == "device-wifi") {
let kit = new SuperPixel(connectOptions());
kit.connect()
.then((device) => {
console.log(`Connected to ${kit.location()}`);
return kit.connectToWifi(process.argv[4], process.argv[5]);
})
.then((data) => {
console.log(`Wifi ${data.ip} ${data.ssid}`);
kit.disconnect();
return Promise.resolve();
})
.catch(error => {
console.log("failed with error: " + error);
kit.disconnect();
});
} else if (process.argv[2] == "device-name") {
let name = 'My Pixel Kit ' + parseInt(Math.random()*100);
if (process.argv.length >= 5) {
name = process.argv[4];
}
let kit = new SuperPixel(connectOptions());
kit.connect()
.then((device) => {
console.log(`Connected to ${kit.location()}`);
return kit.setName(name);
})
.then((data) => {
return kit.getName();
})
.then((data) => {
console.log('Pixel kit name is set to', data.name);
kit.disconnect();
})
.catch(error => {
console.log("failed with error: " + error);
kit.disconnect();
});
} else if (process.argv[2] == "device-app") {
let kit = new SuperPixel(connectOptions());
kit.connect()
.then((device) => {
console.log(`Connected to ${kit.location()}`);
console.log("Apps: " + JSON.stringify(SuperPixelApp.list()));
SuperPixelApp.activate(kit, process.argv[4])
})
.catch(error => {
console.log("failed with error: " + error);
kit.disconnect();
});
} else {
console.log("Unknown command. Use the source luke.");
}