-
-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move listing ports into serial host binding
- Loading branch information
Showing
10 changed files
with
174 additions
and
80 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
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,60 @@ | ||
import fs from "node:fs/promises"; | ||
import os from "node:os"; | ||
import path from "node:path"; | ||
import { SerialPort } from "serialport"; | ||
import { type EnumeratedPort, type Serial } from "../serialport/Bindings.js"; | ||
import { createNodeSerialPortFactory } from "../serialport/NodeSerialPort.js"; | ||
import { createNodeSocketFactory } from "../serialport/NodeSocket.js"; | ||
|
||
/** An implementation of the Serial bindings for Node.js */ | ||
export const serial: Serial = { | ||
createFactoryByPath(path) { | ||
if (path.startsWith("tcp://")) { | ||
const url = new URL(path); | ||
return Promise.resolve(createNodeSocketFactory({ | ||
host: url.hostname, | ||
port: parseInt(url.port), | ||
})); | ||
} else { | ||
return Promise.resolve(createNodeSerialPortFactory( | ||
path, | ||
)); | ||
} | ||
}, | ||
|
||
async list() { | ||
// Put symlinks to the serial ports first if possible | ||
const ret: EnumeratedPort[] = []; | ||
if (os.platform() === "linux") { | ||
const dir = "/dev/serial/by-id"; | ||
const symlinks = await fs.readdir(dir).catch(() => []); | ||
|
||
for (const l of symlinks) { | ||
try { | ||
const fullPath = path.join(dir, l); | ||
const target = path.join( | ||
dir, | ||
await fs.readlink(fullPath), | ||
); | ||
if (!target.startsWith("/dev/tty")) continue; | ||
|
||
ret.push({ | ||
type: "link", | ||
path: fullPath, | ||
}); | ||
} catch { | ||
// Ignore. The target might not exist or we might not have access. | ||
} | ||
} | ||
} | ||
|
||
// Then the actual serial ports | ||
const ports = await SerialPort.list(); | ||
ret.push(...ports.map((port) => ({ | ||
type: "tty" as const, | ||
path: port.path, | ||
}))); | ||
|
||
return ret; | ||
}, | ||
}; |
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
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,24 @@ | ||
import { type ZWaveSerialBindingFactory } from "./ZWaveSerialStream.js"; | ||
|
||
export type EnumeratedPort = { | ||
type: "link"; | ||
path: string; | ||
} | { | ||
type: "tty"; | ||
path: string; | ||
} | { | ||
type: "socket"; | ||
path: string; | ||
} | { | ||
type: "custom"; | ||
factory: ZWaveSerialBindingFactory; | ||
}; | ||
|
||
/** Abstractions to interact with serial ports on different platforms */ | ||
export interface Serial { | ||
/** Create a binding factory from the given path, if supported by the platform */ | ||
createFactoryByPath?: (path: string) => Promise<ZWaveSerialBindingFactory>; | ||
|
||
/** List the available serial ports, if supported by the platform */ | ||
list?: () => Promise<EnumeratedPort[]>; | ||
} |
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
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,11 @@ | ||
// FIXME: This should eventually live in @zwave-js/host | ||
|
||
import { type Serial } from "@zwave-js/serial"; | ||
import { type FileSystem, type Platform } from "@zwave-js/shared/bindings"; | ||
|
||
/** Abstractions for a host system Z-Wave JS is running on */ | ||
export interface Host { | ||
fs: FileSystem; | ||
platform: Platform; | ||
serial: Serial; | ||
} |
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