Skip to content

Commit 9fa4792

Browse files
authored
Merge pull request #5 from Kattjakt/broadcast-to-everyone
feat: broadcast to everyone, let OS decide client UDP port, add icon
2 parents 018aec1 + d8cb6b1 commit 9fa4792

7 files changed

Lines changed: 33 additions & 6 deletions

File tree

build/icon.icns

117 KB
Binary file not shown.

build/icon.ico

-42.8 KB
Binary file not shown.

build/icon.png

43.3 KB
Loading

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pa2ui",
3-
"version": "1.0.0",
4-
"description": "An Electron application with React and TypeScript",
3+
"version": "1.1.0",
4+
"description": "A highly opinionated UI for the DBX DriveRack PA2",
55
"main": "./out/main/index.js",
66
"author": "Leo Sandström <leo@sandstrom.party>",
77
"homepage": "https://electron-vite.org",

resources/icon.png

43.3 KB
Loading

src/main/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function createWindow(): void {
1515
height: 670,
1616
show: false,
1717
autoHideMenuBar: true,
18-
...(process.platform === 'linux' ? { icon } : {}),
18+
icon,
1919
webPreferences: {
2020
preload: join(__dirname, '../preload/index.js'),
2121
sandbox: false

src/main/pa2/discovery.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import udp from 'dgram'
2+
import os from 'os'
23

34
export type Device = {
45
ip: string
@@ -9,9 +10,32 @@ export type Device = {
910
type Callback = (devices: Device[]) => void
1011

1112
const DBX_UDP_PORT = 19272
12-
const CLIENT_UDP_PORT = 52990
13+
const CLIENT_UDP_PORT = 0 // Leave it to OS
14+
15+
const getBroadcastAddresses = (): string[] => {
16+
const interfaces = os.networkInterfaces()
17+
const addrs: string[] = []
18+
19+
for (const name in interfaces) {
20+
const iface = interfaces[name]
21+
if (!iface) continue
22+
23+
for (const addr of iface) {
24+
if (addr.family === 'IPv4' && !addr.internal && !addr.address.startsWith('169.254.')) {
25+
const ip = addr.address.split('.').map(Number)
26+
const netmask = addr.netmask.split('.').map(Number)
27+
const broadcast = ip.map((byte, i) => byte | (~netmask[i] & 255)).join('.')
28+
addrs.push(broadcast)
29+
}
30+
}
31+
}
32+
33+
if (addrs.length === 0) {
34+
return ['255.255.255.255']
35+
}
1336

14-
const BROADCAST_IP = '192.168.1.255'
37+
return addrs
38+
}
1539
const BROADCAST_PACKET = `
1640
delay 100
1741
get \\\\Node\\AT\\Class_Name
@@ -42,7 +66,10 @@ export class Discovery {
4266
server.setBroadcast(true)
4367

4468
const timer = setInterval(() => {
45-
server.send(Buffer.from(BROADCAST_PACKET), DBX_UDP_PORT, BROADCAST_IP)
69+
70+
for (const ip of getBroadcastAddresses()) {
71+
server.send(Buffer.from(BROADCAST_PACKET), DBX_UDP_PORT, ip)
72+
}
4673

4774
this.devices = this.devices.filter((device) => {
4875
if (Date.now() - device.lastSeen > 5000) {

0 commit comments

Comments
 (0)