/
serial_websocket.js
108 lines (101 loc) · 3.17 KB
/
serial_websocket.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
/*
Gordon Williams (gw@pur3.co.uk)
*/
(function() {
if (typeof window == "undefined" || typeof WebSocket == undefined) return;
console.log("WebSockets support enabled - running in web browser");
var ws;
var dataWrittenCallbacks = [];
var str2ab=function(str) {
var buf=new ArrayBuffer(str.length);
var bufView=new Uint8Array(buf);
for (var i=0; i<str.length; i++) {
var ch = str.charCodeAt(i);
if (ch>=256) {
console.warn("Attempted to send non-8 bit character - code "+ch);
ch = "?".charCodeAt(0);
}
bufView[i] = ch;
}
return buf;
};
var getPorts=function(callback) {
if (Espruino.Config.RELAY_KEY) {
callback([{path:'Web IDE Relay', description:'BLE connection via a phone', type : "bluetooth"}]);
} else {
Espruino.Core.Utils.getJSONURL("/serial/ports", function(ports) {
if (!Array.isArray(ports)) callback([]);
else callback(ports);
});
}
};
var openSerial=function(serialPort, openCallback, receiveCallback, disconnectCallback) {
if (Espruino.Config.RELAY_KEY) {
ws = new WebSocket("wss://" + window.location.host + ":8443/", "espruino");
} else {
ws = new WebSocket("ws://" + window.location.host + "/" + serialPort, "serial");
}
dataWrittenCallbacks = [];
ws.onerror = function(event) {
connectCallback(undefined);
ws = undefined;
};
ws.onopen = function() {
if (Espruino.Config.RELAY_KEY) {
ws.send("\x10"+Espruino.Config.RELAY_KEY);
}
Espruino.callProcessor("connected", undefined, function() {
openCallback("Hello");
});
ws.onerror = undefined;
ws.onmessage = function (event) {
//console.log("MSG:"+event.data);
if (event.data[0]=="\x00") {
receiveCallback(str2ab(event.data.substr(1)));
} else if (event.data[0]=="\x02") {
// if it's a data written callback, execute it
var c = dataWrittenCallbacks.shift();
if (c) c();
}
};
ws.onclose = function(event) {
currentDevice = undefined;
Espruino.callProcessor("disconnected", undefined, function() {
disconnectCallback();
});
ws = undefined;
};
}
};
var closeSerial=function(callback) {
if (ws) {
ws.close();
ws = undefined;
}
};
var writeSerial = function(data, callback) {
dataWrittenCallbacks.push(callback);
if (ws) ws.send("\x01"+data);
};
// ----------------------------------------------------------
Espruino.Core.Serial.devices.push({
"name" : "Websocket",
"getPorts": getPorts,
"open": openSerial,
"write": writeSerial,
"close": closeSerial,
});
if (window.location.host.substr(-16) == "www.espruino.com") {
Espruino.Core.SerialWebSocket = {
"init": function() {
Espruino.Core.Config.add("RELAY_KEY", {
section : "Communications",
name : "Relay Key",
description : "The key displayed when https://www.espruino.com/ide/relay is viewed on a phone. You'll then be able to use the Web IDE on your PC",
type : "string",
defaultValue : ""
});
}
};
}
})();