diff --git a/backend/bin.mjs b/backend/bin.mjs index 51a35ca..2e654e8 100755 --- a/backend/bin.mjs +++ b/backend/bin.mjs @@ -1,9 +1,10 @@ import minimist from 'minimist' import { run } from './server.mjs' +import p from 'path' import createDebug from 'debug' import Dotenv from 'dotenv' -Dotenv.config('..') +Dotenv.config({ path: p.resolve(p.join(process.cwd(), '..', '.env')) }) Dotenv.config() const debug = createDebug('streamer') @@ -13,10 +14,10 @@ main().catch(onerror) async function main () { const args = minimist(process.argv.slice(2), { default: { - port: 3030, + port: process.env.PORT || 3030, host: '0.0.0.0', - input: 'alsa:default', - output: 'alsa:default', + input: process.env.STUDIOX_INPUT || 'alsa:hifiberry', + output: process.env.STUDIOX_OUTPUT || 'alsa:hifiberry', dev: false }, alias: { diff --git a/backend/lib/rut950.mjs b/backend/lib/rut950.mjs index 4fb16d4..d92edc2 100644 --- a/backend/lib/rut950.mjs +++ b/backend/lib/rut950.mjs @@ -1,22 +1,43 @@ import JsonRpc from 'node-jsonrpc-client' import esMain from 'es-main' +import Debug from 'debug' +const debug = Debug('rut950') -const DEFAULT_OPTS = { +const defaultOpts = () => ({ url: process.env.RUT950_UBUS_URL || 'http://192.168.1.1/ubus', username: process.env.RUT950_USERNAME || 'admin', password: process.env.RUT950_PASSWORD || 'admin01' -} +}) const EMPTY_SESSID = '00000000000000000000000000000000' +const UBUS_STATUS_CODE = [ + 'UBUS_STATUS_OK', + 'UBUS_STATUS_INVALID_COMMAND', + 'UBUS_STATUS_INVALID_ARGUMENT', + 'UBUS_STATUS_METHOD_NOT_FOUND', + 'UBUS_STATUS_NOT_FOUND', + 'UBUS_STATUS_NO_DATA', + 'UBUS_STATUS_PERMISSION_DENIED', + 'UBUS_STATUS_TIMEOUT', + 'UBUS_STATUS_NOT_SUPPORTED', + 'UBUS_STATUS_UNKNOWN_ERROR', + 'UBUS_STATUS_CONNECTION_FAILED', + 'UBUS_STATUS_NO_MEMORY', + 'UBUS_STATUS_PARSE_ERROR', + 'UBUS_STATUS_SYSTEM_ERROR', +] + + export class Rut950 { constructor (opts = {}) { - opts = { ...DEFAULT_OPTS, ...opts } + opts = { ...defaultOpts(), ...opts } this.client = new JsonRpc(opts.url) this.opts = opts } async init () { + debug('call', 'session.login') const res = await this.client.call('call', [ EMPTY_SESSID, 'session', @@ -29,35 +50,43 @@ export class Rut950 { if (!res.result || !res.result.length === 2) { throw new Error('received invalid response') } + debug('res', res) const [code, data] = res.result + if (code !== 0) { + throw new Error('Error: ' + UBUS_STATUS_CODE[code]) + } this.sessid = data.ubus_rpc_session } async call (...params) { if (!this.sessid) await this.init() + debug('call', this.sessid, params) const res = await this.client.call('call', [ this.sessid, ...params ]) - console.log(res) + debug('res', res) if (res.error) { const err = new Error('RUT950 error: ' + res.error.message) err.code = res.error.code throw err } const result = res.result - if (!result || !result.length === 2 || result[0] !== 0) { - throw new Error('received invalid response') + if (!result) throw new Error('Received invalid response') + const [code, data] = result + if (code !== 0 || !data) { + throw new Error('received error: ' + UBUS_STATUS_CODE[code]) } return res.result[1] } } -if (esMain(import.meta)) { +export async function demo() { const rut = new Rut950() let iwinfo = await rut.call('iwinfo', 'info', { device: 'wlan0' }) console.log('IWINFO', iwinfo) //let uci = await rut.call('uci', 'show') + console.log('call uci show') let uci = await rut.call('file', 'exec', { command: 'uci', params: ['show']