-
-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2631c90
commit a885768
Showing
5 changed files
with
194 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,48 @@ | ||
<template> | ||
<div | ||
v-if="isManual" | ||
id="port-override-option" | ||
style="display: none" | ||
:style="{ display: isManual ? 'flex' : 'none' }" | ||
> | ||
<label | ||
for="port-override" | ||
><span>{{ $t("portOverrideText") }}</span> | ||
<input | ||
id="port-override" | ||
type="text" | ||
value="/dev/rfcomm0" | ||
:value="value" | ||
@change="inputValueChanged($event)" | ||
></label> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { set as setConfig } from '../../js/ConfigStorage'; | ||
import { set as setConfig } from "../../js/ConfigStorage"; | ||
export default { | ||
props: { | ||
isManual: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
props: { | ||
value: { | ||
type: String, | ||
default: '/dev/rfcomm0', | ||
}, | ||
isManual: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
}, | ||
methods: { | ||
inputValueChanged(event) { | ||
setConfig({'portOverride': event.target.value}); | ||
this.$emit('input', event.target.value); | ||
}, | ||
}, | ||
}; | ||
</script> | ||
|
||
<style lang="less" scoped> | ||
#port-override-option { | ||
label { | ||
display: flex; | ||
align-items: center; | ||
gap: 0.5rem; | ||
} | ||
label { | ||
display: flex; | ||
align-items: center; | ||
gap: 0.5rem; | ||
} | ||
} | ||
</style> |
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,145 @@ | ||
class WebsocketSerial extends EventTarget { | ||
constructor() { | ||
super(); | ||
|
||
this.connected = false; | ||
this.connectionInfo = null; | ||
|
||
this.bitrate = 0; | ||
this.bytesSent = 0; | ||
this.bytesReceived = 0; | ||
this.failed = 0; | ||
|
||
this.logHead = "[WEBSOCKET] "; | ||
|
||
this.address = "ws://localhost:5761"; | ||
|
||
this.ws = null; | ||
|
||
this.connect = this.connect.bind(this); | ||
} | ||
|
||
handleReceiveBytes(info) { | ||
this.bytesReceived += info.detail.byteLength; | ||
} | ||
|
||
handleDisconnect() { | ||
this.disconnect(); | ||
} | ||
|
||
createPort(url) { | ||
this.address = url; | ||
return { | ||
path: url, | ||
displayName: `Betaflight SITL`, | ||
vendorId: 0, | ||
productId: 0, | ||
port: 0, | ||
}; | ||
} | ||
|
||
getConnectedPort() { | ||
return { | ||
path: this.address, | ||
displayName: `Betaflight SITL`, | ||
vendorId: 0, | ||
productId: 0, | ||
port: 0, | ||
}; | ||
} | ||
|
||
async getDevices() { | ||
return []; | ||
} | ||
|
||
async blob2uint(blob) { | ||
const buffer = await new Response(blob).arrayBuffer(); | ||
return new Uint8Array(buffer); | ||
} | ||
|
||
waitForConnection(socket) { | ||
return new Promise((resolve) => { | ||
const interval = setInterval(() => { | ||
if (socket.connected) { | ||
clearInterval(interval); // Stop checking | ||
resolve(); // Resolve the promise | ||
} | ||
}, 100); // Check every 100ms, adjust as needed | ||
}); | ||
} | ||
|
||
async connect(path, options) { | ||
this.address = path; | ||
console.log(`${this.logHead} Connecting to ${this.address}`); | ||
|
||
this.ws = new WebSocket(this.address, "wsSerial"); | ||
let socket = this; | ||
|
||
this.ws.onopen = function(e) { | ||
console.log(`${socket.logHead} Connected: `, e); | ||
socket.connected = true; | ||
socket.dispatchEvent( | ||
new CustomEvent("connect", { detail: { | ||
socketId: socket.address, | ||
}}), | ||
); | ||
}; | ||
|
||
await this.waitForConnection(socket); | ||
|
||
this.ws.onclose = function(e) { | ||
console.log(`${socket.logHead} Connection closed: `, e); | ||
|
||
socket.disconnect(() => { | ||
socket.dispatchEvent(new CustomEvent("disconnect", this.disconnect.bind(this))); | ||
}); | ||
}; | ||
|
||
this.ws.onerror = function(e) { | ||
console.error(`${socket.logHead} Connection error: `, e); | ||
|
||
socket.disconnect(() => { | ||
socket.dispatchEvent(new CustomEvent("disconnect", this.disconnect.bind(this))); | ||
}); | ||
}; | ||
|
||
this.ws.onmessage = async function(msg) { | ||
let uint8Chunk = await socket.blob2uint(msg.data); | ||
socket.dispatchEvent( | ||
new CustomEvent("receive", { detail: uint8Chunk }), | ||
); | ||
}; | ||
} | ||
|
||
async disconnect() { | ||
this.connected = false; | ||
this.bytesReceived = 0; | ||
this.bytesSent = 0; | ||
|
||
if (this.ws) { | ||
try { | ||
this.ws.close(); | ||
} catch (e) { | ||
console.error(`${this.logHead}Failed to close socket: ${e}`); | ||
} | ||
} | ||
} | ||
|
||
async send(data, cb) { | ||
if (this.ws) { | ||
try { | ||
this.ws.send(data); | ||
this.bytesSent += data.byteLength; | ||
} | ||
catch(e) { | ||
console.error(`${this.logHead}Failed to send data e: ${e}`); | ||
} | ||
} | ||
|
||
return { | ||
bytesSent: data.byteLength, | ||
}; | ||
} | ||
} | ||
|
||
export default new WebsocketSerial(); |
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 |
---|---|---|
@@ -1,6 +1,18 @@ | ||
import CONFIGURATOR from "./data_storage"; | ||
import serialWeb from "./webSerial.js"; | ||
import BT from "./protocols/bluetooth.js"; | ||
import websocketSerial from "./protocols/websocket.js"; | ||
import virtualSerial from "./virtualSerial.js"; | ||
|
||
export let serialShim = () => CONFIGURATOR.virtualMode ? virtualSerial: CONFIGURATOR.bluetoothMode ? BT : serialWeb; | ||
export const serialShim = () => { | ||
if (CONFIGURATOR.virtualMode) { | ||
return virtualSerial; | ||
} | ||
if (CONFIGURATOR.manualMode) { | ||
return websocketSerial; | ||
} | ||
if (CONFIGURATOR.bluetoothMode) { | ||
return BT; | ||
} | ||
return serialWeb; | ||
}; |