2021-06-04 19:28:39 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import Logger, { levels } from './logger.js';
|
2021-06-05 01:36:40 +00:00
|
|
|
import { functions } from './lights.js';
|
2021-06-04 19:28:39 +00:00
|
|
|
|
|
|
|
const cfg = JSON.parse(fs.readFileSync('./config.json'));
|
|
|
|
const log = new Logger("server", cfg.log_level ? levels[cfg.log_level] : levels.INFO);
|
|
|
|
|
|
|
|
|
2021-06-04 19:22:48 +00:00
|
|
|
export default function parse(data) {
|
2021-06-05 01:36:40 +00:00
|
|
|
let parsed = []
|
|
|
|
let errors = []
|
|
|
|
log.info(data);
|
|
|
|
// errors.push("not yet implemented");
|
|
|
|
data = JSON.parse(data);
|
|
|
|
let line = data.split("\n");
|
|
|
|
for (let lineNumber in line) {
|
|
|
|
let currentLine = line[lineNumber];
|
|
|
|
let split = currentLine.split(" ");
|
|
|
|
let command = split[0];
|
|
|
|
let arg1 = split[1];
|
|
|
|
let arg2 = split[2];
|
|
|
|
|
|
|
|
let match = functions[command];
|
|
|
|
if (match != undefined) {
|
|
|
|
if (
|
2021-06-05 01:46:00 +00:00
|
|
|
(match.options["requires_arg1"] && arg1 == undefined) ||
|
|
|
|
(match.options["requires_arg2"] && arg2 == undefined)
|
2021-06-05 01:36:40 +00:00
|
|
|
) {
|
2021-06-05 01:46:00 +00:00
|
|
|
errors.push(`error parsing line ${lineNumber}, invalid number of args`);
|
|
|
|
}
|
2021-06-05 02:12:08 +00:00
|
|
|
else if (!match.options["convert_args"] && parseInt(arg1)) {
|
|
|
|
errors.push(`error parsing line ${lineNumber}, argument ${arg1} cannot be a number`);
|
|
|
|
}
|
2021-06-05 01:46:00 +00:00
|
|
|
else {
|
2021-06-05 01:36:40 +00:00
|
|
|
parsed.push(
|
|
|
|
{
|
|
|
|
command: command,
|
|
|
|
arg1: arg1 || undefined,
|
|
|
|
arg2: arg2 || undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 19:22:48 +00:00
|
|
|
return {
|
2021-06-05 01:38:28 +00:00
|
|
|
parsed: parsed,
|
|
|
|
data: data,
|
|
|
|
errors: errors
|
2021-06-04 19:22:48 +00:00
|
|
|
}
|
|
|
|
}
|