-
Notifications
You must be signed in to change notification settings - Fork 0
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
014b769
commit f622023
Showing
10 changed files
with
143 additions
and
74 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
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
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,29 @@ | ||
import { app } from 'electron' | ||
import { sendOSCMessage } from './sendOSCMessage' | ||
import { Command } from 'commander' | ||
|
||
export const runCLI = (): void => { | ||
const program = new Command() | ||
|
||
program | ||
.name('KMOSC') | ||
.description('KMOSC CLI tool for sending OSC messages.') | ||
.version(app.getVersion()) | ||
.command('send') | ||
.argument('<host>') | ||
.argument('<port>') | ||
.argument('<address>') | ||
.argument('<type>') | ||
.argument('<message>') | ||
.action((host, port, address, type, message) => { | ||
if (!host || !port || !address || !type || !message) { | ||
program.help() | ||
process.exit(1) | ||
} else { | ||
sendOSCMessage(host, port, address, type, message) | ||
app.quit() | ||
} | ||
}) | ||
|
||
program.parse() | ||
} |
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,74 @@ | ||
import { Client, Message } from 'node-osc' | ||
import { z } from 'zod' | ||
|
||
const schema = z.object({ | ||
host: z | ||
.string() | ||
.ip({ message: 'Error: <host> must be a valid email address or localhost' }) | ||
.or(z.literal('localhost')), | ||
port: z.coerce | ||
.number() | ||
.gt(1, { message: 'Error: <port> must be a number greater than 1' }) | ||
.refine((value) => !isNaN(value), { | ||
message: 'Error: <port> must be a number greater than 1' | ||
}), | ||
address: z.string().startsWith('/', { message: 'Error: <address> must start with a /' }), | ||
type: z.string().refine((value) => ['int', 'float', 'string', 'boolean'].includes(value), { | ||
message: 'Error: <message> must be one of "int", "float", "string", or "boolean"' | ||
}), | ||
message: z.string().min(1, { message: 'Error: <message> must be a non-empty string' }) | ||
}) | ||
|
||
export const sendOSCMessage = ( | ||
host: string, | ||
port: string, | ||
address: string, | ||
valueType: 'int' | 'boolean' | 'float' | 'string', | ||
value: string | ||
): void => { | ||
const parsed = schema.safeParse({ | ||
host, | ||
port, | ||
address, | ||
type: valueType, | ||
message: value | ||
}) | ||
|
||
if (!parsed.success) { | ||
console.error(parsed.error.errors.forEach((error) => console.error(error.message))) | ||
return | ||
} | ||
|
||
const message: Message = new Message(parsed.data.address) | ||
|
||
switch (parsed.data.type) { | ||
case 'int': | ||
message.append(parseInt(parsed.data.message)) | ||
break | ||
case 'float': | ||
message.append(parseFloat(parsed.data.message)) | ||
break | ||
case 'boolean': | ||
if (parsed.data.message === 'false' || parsed.data.message === '0') { | ||
message.append(false) | ||
break | ||
} else if (parsed.data.message === 'true' || parsed.data.message === '1') { | ||
message.append(true) | ||
break | ||
} | ||
break | ||
case 'string': | ||
message.append(parsed.data.message) | ||
break | ||
default: | ||
return | ||
} | ||
|
||
const oscClient = new Client(parsed.data.host, Number(parsed.data.port)) | ||
oscClient.send(message, (error) => { | ||
if (error) { | ||
console.error('Error sending OSC message:', error) | ||
} | ||
oscClient.close() | ||
}) | ||
} |
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