This commit is contained in:
rdl technix 2023-03-01 17:45:27 +01:00
parent 1c683d9f9a
commit 457a91da1d
2 changed files with 41 additions and 11 deletions

View file

@ -1,9 +1,10 @@
import minimist from 'minimist' import minimist from 'minimist'
import { run } from './server.mjs' import { run } from './server.mjs'
import p from 'path'
import createDebug from 'debug' import createDebug from 'debug'
import Dotenv from 'dotenv' import Dotenv from 'dotenv'
Dotenv.config('..') Dotenv.config({ path: p.resolve(p.join(process.cwd(), '..', '.env')) })
Dotenv.config() Dotenv.config()
const debug = createDebug('streamer') const debug = createDebug('streamer')
@ -13,10 +14,10 @@ main().catch(onerror)
async function main () { async function main () {
const args = minimist(process.argv.slice(2), { const args = minimist(process.argv.slice(2), {
default: { default: {
port: 3030, port: process.env.PORT || 3030,
host: '0.0.0.0', host: '0.0.0.0',
input: 'alsa:default', input: process.env.STUDIOX_INPUT || 'alsa:hifiberry',
output: 'alsa:default', output: process.env.STUDIOX_OUTPUT || 'alsa:hifiberry',
dev: false dev: false
}, },
alias: { alias: {

View file

@ -1,22 +1,43 @@
import JsonRpc from 'node-jsonrpc-client' import JsonRpc from 'node-jsonrpc-client'
import esMain from 'es-main' 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', url: process.env.RUT950_UBUS_URL || 'http://192.168.1.1/ubus',
username: process.env.RUT950_USERNAME || 'admin', username: process.env.RUT950_USERNAME || 'admin',
password: process.env.RUT950_PASSWORD || 'admin01' password: process.env.RUT950_PASSWORD || 'admin01'
} })
const EMPTY_SESSID = '00000000000000000000000000000000' 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 { export class Rut950 {
constructor (opts = {}) { constructor (opts = {}) {
opts = { ...DEFAULT_OPTS, ...opts } opts = { ...defaultOpts(), ...opts }
this.client = new JsonRpc(opts.url) this.client = new JsonRpc(opts.url)
this.opts = opts this.opts = opts
} }
async init () { async init () {
debug('call', 'session.login')
const res = await this.client.call('call', [ const res = await this.client.call('call', [
EMPTY_SESSID, EMPTY_SESSID,
'session', 'session',
@ -29,35 +50,43 @@ export class Rut950 {
if (!res.result || !res.result.length === 2) { if (!res.result || !res.result.length === 2) {
throw new Error('received invalid response') throw new Error('received invalid response')
} }
debug('res', res)
const [code, data] = res.result const [code, data] = res.result
if (code !== 0) {
throw new Error('Error: ' + UBUS_STATUS_CODE[code])
}
this.sessid = data.ubus_rpc_session this.sessid = data.ubus_rpc_session
} }
async call (...params) { async call (...params) {
if (!this.sessid) await this.init() if (!this.sessid) await this.init()
debug('call', this.sessid, params)
const res = await this.client.call('call', [ const res = await this.client.call('call', [
this.sessid, this.sessid,
...params ...params
]) ])
console.log(res) debug('res', res)
if (res.error) { if (res.error) {
const err = new Error('RUT950 error: ' + res.error.message) const err = new Error('RUT950 error: ' + res.error.message)
err.code = res.error.code err.code = res.error.code
throw err throw err
} }
const result = res.result const result = res.result
if (!result || !result.length === 2 || result[0] !== 0) { if (!result) throw new Error('Received invalid response')
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] return res.result[1]
} }
} }
if (esMain(import.meta)) { export async function demo() {
const rut = new Rut950() const rut = new Rut950()
let iwinfo = await rut.call('iwinfo', 'info', { device: 'wlan0' }) let iwinfo = await rut.call('iwinfo', 'info', { device: 'wlan0' })
console.log('IWINFO', iwinfo) console.log('IWINFO', iwinfo)
//let uci = await rut.call('uci', 'show') //let uci = await rut.call('uci', 'show')
console.log('call uci show')
let uci = await rut.call('file', 'exec', { let uci = await rut.call('file', 'exec', {
command: 'uci', command: 'uci',
params: ['show'] params: ['show']