-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hexuustc/ui-ws-handle
UI ws handle
- Loading branch information
Showing
11 changed files
with
12,861 additions
and
41,723 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import emitter from "tiny-emitter/instance"; | ||
|
||
export default emitter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import emitter from "./event_bus.js"; | ||
|
||
let DEBUG_MODE = true; | ||
let DEBUG_WS_SERVER = "202.38.79.96:12148"; | ||
|
||
let notifySocket; | ||
let PI_SERVER_ADDRESS = window.location.host; | ||
|
||
export default function initWebsocket() { | ||
if (DEBUG_MODE) { | ||
notifySocket = new WebSocket("ws://" + DEBUG_WS_SERVER + "/ws/"); | ||
} else { | ||
notifySocket = new WebSocket("ws://" + PI_SERVER_ADDRESS + "/ws/"); | ||
} | ||
notifySocket.onmessage = function (e) { | ||
const data = JSON.parse(e.data); | ||
//console.log("receive message:", data); | ||
const type = data["type"]; | ||
const idx = data["idx"]; | ||
let payload = data["payload"]; | ||
if (type == "LED") { | ||
emitter.emit("led-recv", idx, payload); | ||
} else if (type == "UART") { | ||
emitter.emit("term-recv", payload); | ||
} else if (type == "HEXPLAY") { | ||
payload = parseInt(payload); | ||
emitter.emit("hexplay-recv", idx, payload); | ||
} else if (type == "WF") { | ||
// document.getElementById("download").innerHTML = "Download"; | ||
// $("#download").attr("href", "./waveform.vcd"); | ||
} else if (type == "XDC") { | ||
emitter.emit("dispatch-xdc", payload); | ||
} | ||
}; | ||
} | ||
|
||
function sendJson(json) { | ||
console.log(json); | ||
notifySocket.send(json); | ||
} | ||
|
||
function sendStartNotify(json) { | ||
var data = JSON.parse(json); | ||
var id = data["id"]; | ||
if (id == -2) { | ||
notifySocket.send(json); | ||
} else { | ||
console.log("sendStartNotify: wrong id!"); | ||
} | ||
} | ||
|
||
function sendStopNotify() { | ||
notifySocket.send( | ||
JSON.stringify({ | ||
id: -1, | ||
}) | ||
); | ||
} | ||
|
||
emitter.on("stop-notify", function () { | ||
sendStopNotify(); | ||
}); | ||
|
||
emitter.on("submit-json", function (init_json) { | ||
setTimeout(function () { | ||
sendStartNotify(init_json); | ||
}, 1000); | ||
}); | ||
|
||
emitter.on("term-send", function (val) { | ||
sendJson( | ||
JSON.stringify({ | ||
id: -3, | ||
type: "UART", | ||
idx: 0, | ||
payload: val, | ||
}) | ||
); | ||
}); | ||
|
||
emitter.on("sw-change", function (idx, val) { | ||
console.log("SW", idx, val); | ||
sendJson( | ||
JSON.stringify({ | ||
id: -3, | ||
type: "BTN", | ||
idx: idx, | ||
payload: val, | ||
}) | ||
); | ||
}); |
Oops, something went wrong.