-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·118 lines (99 loc) · 3.21 KB
/
main.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
/* Do some fun stuff with Javascript via UDP
Eventually we will implement the SecretAPI here. Eventually. */
// Constructor method for the holiday using SecretAPI
// Requires a string 'address' (i.e. IP address 192.168.0.20) or resolvable name (i.e. 'light.local')
//
function Holiday(address) {
this.address = address;
console.log("Address set to ", this.address)
this.NUM_GLOBES = 50;
this.FRAME_SIZE = 160; // Secret API rame size
this.FRAME_IGNORE = 10; // Ignore the first 10 bytes of frame
socketId = null; // No socket number just yet
this.closeSocket = closeSocket;
this.setglobe = setglobe;
this.getglobe = getglobe;
this.render = render;
var globes = new Uint8Array(160);
this.globes = globes;
console.log('Array created');
// Fill the header of the array with zeroes
for (i=0; i < this.FRAME_IGNORE; i++) {
this.globes[i] = 0x00;
}
// Create the socket we'll use to communicate with the Holiday
chrome.socket.create('udp', {},
function(socketInfo) { // Callback when creation is complete
// The socket is created, now we want to connect to the service
socketId = socketInfo.socketId;
console.log('socket created ', socketInfo.socketId);
}
);
function closeSocket() {
chrome.socket.destroy(socketId);
console.log("Socket destroyed");
}
function setglobe(globenum, r, g, b) {
// Sets a globe's color
if ((globenum < 0) || (globenum >= this.NUM_GLOBES)) {
return;
}
baseptr = this.FRAME_IGNORE + 3*globenum;
globes[baseptr] = r;
globes[baseptr+1] = g;
globes[baseptr+2] = b;
return;
}
function getglobe() {
// Sets a globe's color
if ((globenum < 0) || (globenum >= this.NUM_GLOBES)) {
return;
}
baseptr = this.FRAME_IGNORE + 3*globenum;
r = globes[baseptr];
g = globes[baseptr+1];
b = globes[baseptr+2];
return [r,g,b];
}
function render() {
console.log("Holiday.render");
//var locaddr = this.address;
var glbs = this.globes;
var sid = socketId;
if (sid == null) {
console.log("No socket abort render");
return;
}
// Connect via the socket
chrome.socket.connect(socketId, this.address, 9988, function(result) {
// We are now connected to the socket so send it some data
chrome.socket.write(socketId, glbs.buffer,
function(sendInfo) {
console.log("wrote " + sendInfo.bytesWritten);
});
});
return;
}
}
// Start Demo
function demoStart() {
console.log("demoStart");
//var addr = 'http://' + $('#selector').val() + '/';
var iotasURL = 'http://' + $('#selector').val() + '/iotas/0.1/device/moorescloud.holiday/localhost/hostname'
//window.open(addr/*, /*"popupWindow", "width=600,height=600,scrollbars=yes"*/);
//copyToClipboard($('#selector').val());
$.getJSON(iotasURL, function( data ) {
var aName = data.hostname;
console.log("Hostname is ", aName);
});
return;
}
// Lordy, this is one of the reasons I hate Javascript
// And it's not Javascript's fault. It's the DOM.
$( document ).ready( function() {
console.log("Doing the ready");
// And here's the stuff we do.
$("#thebutton").click(function () {
refresher();
});
});