-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
158 lines (119 loc) · 5.11 KB
/
index.ts
File metadata and controls
158 lines (119 loc) · 5.11 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Layout, SectionPosition } from '@packtrack/layout';
import { Message, findMessageType, MonitorRouteMessage, TypedMessage, MonitorTrainPositionMessage, MonitorTrainSpeedPermitMessage, ThrottleButtonLightMessage, ThrottleTachometerMessage, ThrottleButtonPressMessage, ThrottleSpeedMessage, ThrottleLockMessage } from '@packtrack/protocol';
import { MeasuredPosition, Train, TrainIndex } from '@packtrack/train'
import { readdirSync, readFileSync } from 'fs';
import { DOMParser } from "xmldom";
import { Renderer } from './renderer';
import { Socket } from 'net';
import { Discovery } from './discovery';
import { Display } from './display';
import { SerialPort } from './serial';
import { Button, nextTrainButton, pilotButton, previousTrainButton } from './button';
export const throttleSerialPath = process.env.THROTTLE_SERIAL_PATH;
export const panelSerialPath = process.env.PANEL_SERIAL_PATH;
export const enableFullscreen = 'FULLSCREEN' in process.env;
console.log(`throttle device path: ${throttleSerialPath}`);
console.log(`panel device path: ${throttleSerialPath}`);
console.log(`fullscreen: ${throttleSerialPath}`);
const layoutFileLocation = process.env.LAYOUT_FILE_LOCATION;
const layout = Layout.from(new DOMParser().parseFromString(readFileSync(layoutFileLocation).toString(), "text/xml"));
const trainIndexFileLocation = process.env.TRAIN_INDEX_FILE_LOCATION;
const trainIndex = TrainIndex.from(new DOMParser().parseFromString(readFileSync(trainIndexFileLocation).toString(), "text/xml"), layout);
const connection = new Socket();
const start = () => {
const discovery = new Discovery('throttle-master');
discovery.find().then(director => {
connection.connect(141, director, () => {
panelSerial.attach().then(() => {
renderer.start();
});
throttleSerial.attach();
});
const router = new Map<TypedMessage, (message: Message) => void>();
router.set(MonitorRouteMessage, message => {
for (let district of layout.allDistricts) {
for (let router of district.routers) {
if (router.domainName == message.headers.router) {
router.activeRoute = router.routes.find(route => route.name == message.headers['active-route']);
console.log('-- router', router.name, 'routes', router.activeRoute?.name);
}
}
}
});
router.set(MonitorTrainSpeedPermitMessage, message => {
const train = trainIndex.trains.find(train => train.name == message.headers.train);
train.permit(+message.headers['speed'], new Date(message.headers['issued'] as string));
panelSerial.send(new ThrottleTachometerMessage(Math.floor(train.currentSpeedPermit.speed * 3.6)));
});
router.set(MonitorTrainPositionMessage, message => {
const train = trainIndex.trains.find(train => train.name == message.headers.train);
let position;
for (let district of layout.allDistricts) {
for (let section of district.sections) {
if (section.domainName == message.headers['position-section']) {
position = new SectionPosition(
section,
+message.headers['position-offset'],
false
)
}
}
}
train.lastPositioner = new MeasuredPosition(
new Date(+message.headers['position-time']),
position,
train.reversed,
0
);
console.log(train.name, train.lastPositioner.location.toString());
});
const setTrain = (train: Train) => {
activeTrain = train;
connection.write(new ThrottleLockMessage(train.name).toBuffer());
console.log(`ACTIVE: ${train.name}`);
};
setTrain(trainIndex.trains[0]);
previousTrainButton.press = () => setTrain(trainIndex.trains[trainIndex.trains.indexOf(activeTrain) - 1] ?? trainIndex.trains.at(-1));
nextTrainButton.press = () => setTrain(trainIndex.trains[trainIndex.trains.indexOf(activeTrain) + 1] ?? trainIndex.trains[0]);
let buffer = Buffer.from([]);
connection.on('data', data => buffer = Message.dispatch(buffer, data, message => {
const type = findMessageType(message);
router.get(type)(message);
}));
connection.on('close', () => {
renderer.stop();
setTimeout(() => start(), 1000);
});
});
};
let activeTrain: Train;
const display = new Display(layout, trainIndex);
const renderer = new Renderer(1366, 768, layout, (width, height, context) => {
display.render(width, height, context, activeTrain);
});
Button.update = message => panelSerial.send(message);
const panelSerial = new SerialPort(panelSerialPath, message => {
console.log(message);
pilotButton.light();
const router = new Map<TypedMessage, (message: Message) => void>();
router.set(ThrottleButtonPressMessage, message => {
const button = Button.buttons.find(button => button.channel == message.headers.channel);
console.log('**', button.channel);
if (button?.press) {
button.press();
}
});
const type = findMessageType(message);
router.get(type)(message);
});
const throttleSerial = new SerialPort(throttleSerialPath, message => {
const router = new Map<TypedMessage, (message: Message) => void>();
router.set(ThrottleSpeedMessage, message => {
connection.write(new ThrottleSpeedMessage(
+message.headers.speed * 0xff
).toBuffer());
});
const type = findMessageType(message);
router.get(type)(message);
});
start();