-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.ts
131 lines (122 loc) · 3.97 KB
/
console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Solenoid16 } from './boards';
import { MPU } from './mpu';
import { SwitchEvent, matrix } from './switch-matrix';
import * as fs from 'fs';
import * as readline from 'readline';
require('./machine');
import { safeSetTimeout, wait } from './timer';
import { machine, MomentarySolenoid, OnOffSolenoid } from './machine';
import { num } from './util';
import { initMachine } from './init';
import { Log } from './log';
const argv = require('yargs').argv;
initMachine().then(async () => {
if (argv.s || argv.source) {
await source(argv.s || argv.source);
}
console.log('ready:');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let lastCmd = '';
while (true) {
let cmd: string = '';
try {
cmd = await new Promise(r => rl.question('>', r));
await parseCommand(cmd || lastCmd);
} catch (e) {
Log.error(['console'], '', e);
}
if (cmd)
lastCmd = cmd;
}
function getCoil(str: string): OnOffSolenoid|MomentarySolenoid {
const name = str.toLowerCase().replace(/ /g, '');
const b = str[0] === 'a'? machine.solenoidBank1 : machine.solenoidBank2;
const n = num(str.slice(1), -1);
for (const key of Object.keys(machine)) {
if (!key.startsWith('c')) continue;
const coil = (machine as any)[key] as OnOffSolenoid|MomentarySolenoid;
if ((key.toLowerCase().slice(1) === name) ||
(coil.board === b && coil.num === n)
) {
return coil;
}
}
throw `coil '${str}' not recognized`;
}
// eslint-disable-next-line complexity
async function parseCommand(input: string) {
let result: Promise<any>;
if (input.startsWith('$')) {
result = MPU.sendCommand(input.slice(1));
} else {
const cmd = input.split(' ');
switch (cmd[0]) {
// case 'fakesw':
// // Events.fire(new StateEvent());
// Events.fire(new SwitchEvent(matrix[0][0]));
// result = Promise.resolve('fired');
// break;
case 'sw-state':
result = Promise.resolve(matrix.flatMap(row => row.flatMap(cell => cell?.state? 'X':'.').join('')).join('\n'));
break;
case 'wait':
result = wait(num(cmd[1]), 'user wait').then(() => 'done');
break;
case 's':
case 'source':
result = source(cmd[1]);
break;
case 'f': {
const ms = cmd.length > 2? num(cmd[2]):undefined;
const coil = getCoil(cmd[1]);
if (coil instanceof MomentarySolenoid)
result = coil.fire(ms);
else
throw 'coil is not momentary';
break;
}
case 'on': {
const coil = getCoil(cmd[1]);
if (coil instanceof OnOffSolenoid)
result = coil.set(true);
else
throw 'coil is not on-off';
break;
}
case 'off': {
const coil = getCoil(cmd[1]);
if (coil instanceof OnOffSolenoid)
result = coil.set(false);
else
throw 'coil is not on-off';
break;
}
case 't':
case 'toggle': {
const coil = getCoil(cmd[1]);
if (coil instanceof OnOffSolenoid)
result = coil.toggle();
else
throw 'coil is not on-off';
break;
}
default:
throw `unknown command ${cmd[0]}`;
}
}
console.log(await result);
}
async function source(path: string) {
const lines = fs.readFileSync(path).toString().split('\n');
for (const line of lines) {
await parseCommand(line);
}
}
})
.catch(err => {
Log.error(['console'], 'fatal error ', err);
process.exit(1);
});