restructure parsing
This commit is contained in:
parent
e13e57a66b
commit
7a03bc121b
2 changed files with 90 additions and 33 deletions
84
lights.js
84
lights.js
|
@ -37,24 +37,60 @@ export function set_pattern(pat) {
|
|||
log.debug("pattern set");
|
||||
}
|
||||
|
||||
export const functions = {
|
||||
random: (index, args, prev) => { return Math.floor(Math.random() * 256) },
|
||||
constant: (index, args, prev) => { return args; },
|
||||
modulo: (index, args, prev) => { return prev % args; },
|
||||
move: (index, args, prev) => { targets[args] = prev; return prev; },
|
||||
swap: (index, args, prev) => { let temp = targets[args]; targets[args] = prev; return temp; },
|
||||
add: (index, args, prev) => { return prev + args; },
|
||||
subtract: (index, args, prev) => { return prev - args; }
|
||||
export class Function {
|
||||
func;
|
||||
options;
|
||||
|
||||
constructor(func, options) {
|
||||
this.func = func;
|
||||
this.options = options;
|
||||
}
|
||||
}
|
||||
|
||||
export const reqs = {
|
||||
random: false,
|
||||
constant: true,
|
||||
modulo: true,
|
||||
move: true,
|
||||
swap: true,
|
||||
add: true,
|
||||
subtract: true
|
||||
export const functions = {
|
||||
random: new Function(
|
||||
(index, arg1, arg2) => { return Math.floor(Math.random() * 256) },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: false
|
||||
}
|
||||
),
|
||||
constant: new Function(
|
||||
(index, arg1, arg2) => { return args; },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: true
|
||||
}),
|
||||
modulo: new Function(
|
||||
(index, arg1, arg2) => { return arg1 % args; },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: true
|
||||
}),
|
||||
move: new Function(
|
||||
(index, arg1, arg2) => { targets[args] = arg1; return arg1; },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: true
|
||||
}),
|
||||
swap: new Function(
|
||||
(index, arg1, arg2) => { let temp = targets[args]; targets[args] = arg1; return temp; },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: true
|
||||
}),
|
||||
add: new Function(
|
||||
(index, arg1, arg2) => { return arg1 + args; },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: true
|
||||
}),
|
||||
subtract: new Function(
|
||||
(index, arg1, arg2) => { return arg1 - args; },
|
||||
{
|
||||
requires_arg1: true,
|
||||
requires_arg2: true
|
||||
})
|
||||
}
|
||||
|
||||
function tick_pattern() {
|
||||
|
@ -64,23 +100,11 @@ function tick_pattern() {
|
|||
if (Object.keys(pattern).length > 0) {
|
||||
for (let i = 0; i < cfg.leds; i++) {
|
||||
for (let command of pattern) {
|
||||
if (command.valid) {
|
||||
let instruction = command.instruction;
|
||||
let name = instruction.name.toLowerCase();
|
||||
let name = command.command;
|
||||
if (functions[name]) {
|
||||
for (let target of instruction.args) {
|
||||
let channel = target.channel;
|
||||
if (!targets[channel]) {
|
||||
targets[channel] = 0;
|
||||
}
|
||||
log.debug("targets " + name + " " + channel + " " + target.arg);
|
||||
targets[channel] =
|
||||
functions[name](i, target.arg, targets[channel]);
|
||||
targets[command.arg1] = functions[name].func(i, targets[command.arg1], target.arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.debug(`next: ${targets["r"]}, ${targets["g"]}, ${targets["b"]}`);
|
||||
next_pattern[i] = rgb_to_int(targets["r"] || 0, targets["g"] || 0, targets["b"] || 0);
|
||||
targets["r"] = 0; targets["g"] = 0; targets["b"] = 0;
|
||||
|
|
37
parse.js
37
parse.js
|
@ -1,14 +1,47 @@
|
|||
import * as fs from 'fs';
|
||||
import Logger, { levels } from './logger.js';
|
||||
import { functions } from './lights.js';
|
||||
|
||||
const cfg = JSON.parse(fs.readFileSync('./config.json'));
|
||||
const log = new Logger("server", cfg.log_level ? levels[cfg.log_level] : levels.INFO);
|
||||
|
||||
|
||||
export default function parse(data) {
|
||||
log.info(data)
|
||||
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 (
|
||||
!(match.options["requires_arg1"] && arg1 != undefined) &&
|
||||
!(match.options["requires_arg2"] && arg2 != undefined)
|
||||
) {
|
||||
parsed.push(
|
||||
{
|
||||
command: command,
|
||||
arg1: arg1 || undefined,
|
||||
arg2: arg2 || undefined
|
||||
}
|
||||
);
|
||||
}
|
||||
else {
|
||||
errors.push(`error parsing line ${lineNumber}, invalid number of args`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
parsed: parsed
|
||||
data: data,
|
||||
errors: ["not yet implemented"]
|
||||
errors: errors
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue