1
0
mirror of https://github.com/4yn/slidershim.git synced 2024-09-24 03:08:23 +02:00

add option to change brokenithm port number

This commit is contained in:
4yn 2022-03-25 15:56:29 +08:00
parent 1fe10cf615
commit 3ce8f47905
9 changed files with 79 additions and 29 deletions

View File

@ -39,22 +39,19 @@
<ul class="links"></ul> <ul class="links"></ul>
<script> <script>
(function () { (function () {
const search = window.location.search; var params = new URLSearchParams(window.location.search);
if (search.slice(0, 3) !== "?d=" || search.length <= 3) { var d = params.get("d") || "";
return; var p = params.get("p") || "1606";
} var ul = document.querySelector(".links");
const ul = document.querySelector(".links"); d.split(";").forEach(function (aip) {
search var ip = atob(aip);
.slice(3) console.log(aip, ip);
.split(";") var li = document.createElement("li");
.map((x) => atob(x)) var a = document.createElement("a");
.forEach((ip) => { a.innerText = "".concat(ip, ":").concat(p);
const li = document.createElement("li");
const a = document.createElement("a");
a.innerText = ip;
a.setAttribute("target", "_blank"); a.setAttribute("target", "_blank");
a.setAttribute("rel", "noopener"); a.setAttribute("rel", "noopener");
a.href = `http://${ip}:1606/`; a.href = "http://".concat(ip, ":").concat(p, "/");
li.appendChild(a); li.appendChild(a);
ul.appendChild(li); ul.appendChild(li);
}); });

View File

@ -18,7 +18,7 @@ async fn main() {
let _worker = AsyncHaltableWorker::new( let _worker = AsyncHaltableWorker::new(
"brokenithm", "brokenithm",
BrokenithmJob::new(&state, &BrokenithmSpec::Nostalgia, &false), BrokenithmJob::new(&state, &BrokenithmSpec::Nostalgia, &false, &1606),
); );
let mut input = String::new(); let mut input = String::new();
io::stdin().read_line(&mut input).unwrap(); io::stdin().read_line(&mut input).unwrap();

View File

@ -35,6 +35,7 @@ impl Config {
"disableAirStrings": false, "disableAirStrings": false,
"divaSerialPort": "COM1", "divaSerialPort": "COM1",
"divaBrightness": 63, "divaBrightness": 63,
"brokenithmPort": 1606,
"keyboardSensitivity": 20, "keyboardSensitivity": 20,
"outputPolling": "100", "outputPolling": "100",
"outputWebsocketUrl": "localhost:3000", "outputWebsocketUrl": "localhost:3000",

View File

@ -42,12 +42,13 @@ impl Context {
DeviceMode::Brokenithm { DeviceMode::Brokenithm {
spec, spec,
lights_enabled, lights_enabled,
port,
} => ( } => (
None, None,
None, None,
Some(AsyncHaltableWorker::new( Some(AsyncHaltableWorker::new(
"brokenithm", "brokenithm",
BrokenithmJob::new(&state, spec, lights_enabled), BrokenithmJob::new(&state, spec, lights_enabled, port),
)), )),
), ),
DeviceMode::Hardware { spec, disable_air } => ( DeviceMode::Hardware { spec, disable_air } => (

View File

@ -268,14 +268,21 @@ pub struct BrokenithmJob {
state: SliderState, state: SliderState,
spec: BrokenithmSpec, spec: BrokenithmSpec,
lights_enabled: bool, lights_enabled: bool,
port: u16,
} }
impl BrokenithmJob { impl BrokenithmJob {
pub fn new(state: &SliderState, spec: &BrokenithmSpec, lights_enabled: &bool) -> Self { pub fn new(
state: &SliderState,
spec: &BrokenithmSpec,
lights_enabled: &bool,
port: &u16,
) -> Self {
Self { Self {
state: state.clone(), state: state.clone(),
spec: spec.clone(), spec: spec.clone(),
lights_enabled: *lights_enabled, lights_enabled: *lights_enabled,
port: *port,
} }
} }
} }
@ -299,7 +306,7 @@ impl AsyncHaltableJob for BrokenithmJob {
} }
}); });
let addr = SocketAddr::from(([0, 0, 0, 0], 1606)); let addr = SocketAddr::from(([0, 0, 0, 0], self.port));
info!("Brokenithm server listening on {}", addr); info!("Brokenithm server listening on {}", addr);
let server = Server::bind(&addr) let server = Server::bind(&addr)

View File

@ -25,6 +25,7 @@ pub enum DeviceMode {
Brokenithm { Brokenithm {
spec: BrokenithmSpec, spec: BrokenithmSpec,
lights_enabled: bool, lights_enabled: bool,
port: u16,
}, },
DivaSlider { DivaSlider {
port: String, port: String,
@ -62,6 +63,9 @@ impl DeviceMode {
true => BrokenithmSpec::GroundOnly, true => BrokenithmSpec::GroundOnly,
}, },
lights_enabled: false, lights_enabled: false,
port: u16::try_from(v["brokenithmPort"].as_i64()?)
.ok()
.or(Some(1606))?,
}, },
"brokenithm-led" => DeviceMode::Brokenithm { "brokenithm-led" => DeviceMode::Brokenithm {
spec: match v["disableAirStrings"].as_bool()? { spec: match v["disableAirStrings"].as_bool()? {
@ -69,12 +73,25 @@ impl DeviceMode {
true => BrokenithmSpec::GroundOnly, true => BrokenithmSpec::GroundOnly,
}, },
lights_enabled: true, lights_enabled: true,
port: u16::try_from(v["brokenithmPort"].as_i64()?)
.ok()
.or(Some(1606))?,
}, },
"brokenithm-nostalgia" => DeviceMode::Brokenithm { "brokenithm-nostalgia" => DeviceMode::Brokenithm {
spec: BrokenithmSpec::Nostalgia, spec: BrokenithmSpec::Nostalgia,
lights_enabled: false, lights_enabled: false,
port: u16::try_from(v["brokenithmPort"].as_i64()?)
.ok()
.or(Some(1606))?,
}, },
_ => return None, _ => return None,
}) })
} }
pub fn get_port(&self) -> Option<u16> {
match self {
DeviceMode::Brokenithm { port, .. } => Some(*port),
_ => None,
}
}
} }

View File

@ -1,5 +1,6 @@
use directories::ProjectDirs; use directories::ProjectDirs;
use image::Luma; use image::Luma;
use log::info;
use qrcode::QrCode; use qrcode::QrCode;
use std::{error::Error, fs, path::PathBuf}; use std::{error::Error, fs, path::PathBuf};
@ -25,7 +26,7 @@ fn get_config_dir() -> Option<Box<PathBuf>> {
} }
/// Generates a helper QR for connecting with brokenithm /// Generates a helper QR for connecting with brokenithm
pub fn get_brokenithm_qr_path() -> Option<Box<PathBuf>> { pub fn get_brokenithm_qr_path(port: Option<u16>) -> Option<Box<PathBuf>> {
let config_dir = get_config_dir()?; let config_dir = get_config_dir()?;
let brokenithm_qr_path = config_dir.join("brokenithm.png"); let brokenithm_qr_path = config_dir.join("brokenithm.png");
@ -36,7 +37,10 @@ pub fn get_brokenithm_qr_path() -> Option<Box<PathBuf>> {
.filter(|s| s.as_str().chars().filter(|x| *x == '.').count() == 3) .filter(|s| s.as_str().chars().filter(|x| *x == '.').count() == 3)
.map(|s| base64::encode_config(s, base64::URL_SAFE_NO_PAD)) .map(|s| base64::encode_config(s, base64::URL_SAFE_NO_PAD))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(";"); .join(";")
+ "&p="
+ port.or(Some(1606)).unwrap().to_string().as_str();
info!("Url generated {}", link);
let qr = QrCode::new(link).ok()?; let qr = QrCode::new(link).ok()?;
let image = qr.render::<Luma<u8>>().build(); let image = qr.render::<Luma<u8>>().build();
image.save(brokenithm_qr_path.as_path()).ok()?; image.save(brokenithm_qr_path.as_path()).ok()?;

View File

@ -110,8 +110,15 @@ fn main() {
}); });
// Show brokenithm qr // Show brokenithm qr
app.listen_global("openBrokenithmQr", |_| { let config_clone = Arc::clone(&config);
let brokenithm_qr_path = slider_io::get_brokenithm_qr_path(); app.listen_global("openBrokenithmQr", move |_| {
let config_handle = config_clone.lock();
let brokenithm_qr_path = slider_io::get_brokenithm_qr_path(
config_handle
.as_ref()
.map(|c| c.device_mode.get_port())
.unwrap_or(None),
);
if let Some(brokenithm_qr_path) = brokenithm_qr_path { if let Some(brokenithm_qr_path) = brokenithm_qr_path {
open::that(brokenithm_qr_path.as_path()).ok(); open::that(brokenithm_qr_path.as_path()).ok();
} }

View File

@ -13,6 +13,7 @@
let disableAirStrings = false; let disableAirStrings = false;
let divaSerialPort = "COM1"; let divaSerialPort = "COM1";
let divaBrightness = 63; let divaBrightness = 63;
let brokenithmPort = 1606;
let keyboardSensitivity = 20; let keyboardSensitivity = 20;
let outputPolling = "100"; let outputPolling = "100";
let outputWebsocketUrl = "http://localhost:3000"; let outputWebsocketUrl = "http://localhost:3000";
@ -65,6 +66,7 @@
disableAirStrings = payload.disableAirStrings || false; disableAirStrings = payload.disableAirStrings || false;
divaSerialPort = payload.divaSerialPort || "COM1"; divaSerialPort = payload.divaSerialPort || "COM1";
divaBrightness = payload.divaBrightness || 63; divaBrightness = payload.divaBrightness || 63;
brokenithmPort = payload.brokenithmPort || 1606;
keyboardSensitivity = payload.keyboardSensitivity || 20; keyboardSensitivity = payload.keyboardSensitivity || 20;
outputPolling = payload.outputPolling || "100"; outputPolling = payload.outputPolling || "100";
outputWebsocketUrl = outputWebsocketUrl =
@ -119,6 +121,7 @@
disableAirStrings, disableAirStrings,
divaSerialPort, divaSerialPort,
divaBrightness, divaBrightness,
brokenithmPort,
keyboardSensitivity, keyboardSensitivity,
outputPolling, outputPolling,
outputWebsocketUrl, outputWebsocketUrl,
@ -207,14 +210,27 @@
</div> </div>
{/if} {/if}
{#if deviceMode.slice(0, 10) === "brokenithm"} {#if deviceMode.slice(0, 10) === "brokenithm"}
<div class="row">
<div class="label">Brokenithm Port</div>
<div class="input">
<input
type="number"
min="1024"
max="65535"
step="1"
bind:value={brokenithmPort}
on:change={markDirty}
/>
</div>
</div>
<div class="row"> <div class="row">
<div class="label" /> <div class="label" />
<div class="input"> <div class="input">
<div class="serverlist"> <div class="serverlist">
Brokenithm server running, access at one of: Brokenithm will be running at one of:
<div class="iplist"> <div class="iplist">
{ips {ips
.map((x) => `http://${x}:1606/`) .map((x) => `http://${x}:${brokenithmPort || 1606}/`)
.join("\n") .join("\n")
.trim()} .trim()}
</div> </div>