-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
190 lines (157 loc) · 4.81 KB
/
index.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const net = require('net');
const bsplit = require('buffer-split')
const jspack = require('jspack').jspack
const keypress = require('keypress')
const fs = require('fs');
keypress(process.stdin);
var tallies = {};
const file_date = new Date();
const month = file_date.getMonth();
const day = file_date.getDate();
const year = file_date.getFullYear();
const hours = file_date.getHours();
const minutes = file_date.getMinutes();
const path_pfx = `./${month}-${day}-${year}-${hours}-${minutes}`
class Index {
constructor(tally_obj) {
this.duration = 0;
this.timer1 = null;
this.updateControl(tally_obj);
this.counter = 0;
}
updateControl(obj) {
this.rh_tally = obj.control.rh_tally;
this.lh_tally = obj.control.lh_tally;
this.text_tally = obj.control.text_tally;
this.brightness = obj.control.brightness;
this.text = obj.TEXT;
this.index = obj.INDEX[0];
if (this.text_tally > 0 || this.rh_tally > 0 || this.lh_tally > 0) {
//two options, start timer if not running or do nothing
if (this.timer1 == null) { //timer stopped
console.log(this.text + ' => PGM')
this.counter ++;
this.timer1 = Date.now();
writeToDisk()
} else {
//pass
}
} else {
//two options, either stop timer if running, or do nothing
if (this.timer1 != null) {
console.log(this.text + " => Null")
this.duration += Date.now() - this.timer1;
this.timer1 = null;
writeToDisk()
} else {
//pass
}
}
}
}
var client = new net.Socket();
client.connect(6969, "10.8.60.61", () => {
console.log('Connected');
console.clear();
})
client.on('data', (data) => {
delim = new Buffer.from([0xfe, 0x02]);
var spl_data = bsplit(data, delim);
spl_data.forEach((data) => {
if (data.length > 0 ) {
parse(data);
}
})
})
client.on('close', () => {
console.log("Socket closed.")
})
var parse = function(data) {
if (data.length > 12) {
tallyobj = {};
var cursor = 0;
//Message Format
const _PBC = 2 //bytes
const _VAR = 1
const _FLAGS = 1
const _SCREEN = 2
const _INDEX = 2
const _CONTROL = 2
//Display Data
const _LENGTH = 2
tallyobj.PBC = jspack.Unpack( "<H", data, cursor);
cursor += _PBC;
tallyobj.VAR = jspack.Unpack( "<B", data, cursor);
cursor += _VAR;
tallyobj.FLAGS = jspack.Unpack( "<B", data, cursor);
cursor += _FLAGS;
tallyobj.SCREEN = jspack.Unpack( "<H", data, cursor);
cursor += _SCREEN;
tallyobj.INDEX = jspack.Unpack( "<H", data, cursor);
cursor += _INDEX;
tallyobj.CONTROL = jspack.Unpack( "<H", data, cursor);
cursor += _CONTROL;
//console.log(CONTROL[0] >> 0&2b1)
tallyobj.control = {};
tallyobj.control.rh_tally = (tallyobj.CONTROL >> 0 & 0b11);
tallyobj.control.text_tally = (tallyobj.CONTROL >> 2 & 0b11);
tallyobj.control.lh_tally = (tallyobj.CONTROL >> 4 & 0b11);
tallyobj.control.brightness = (tallyobj.CONTROL >> 6 & 0b11);
tallyobj.control.reserved = (tallyobj.CONTROL >> 8 & 0b1111111);
tallyobj.control.control_data = (tallyobj.CONTROL >> 15 & 0b1);
var LENGTH = jspack.Unpack( "<H", data, cursor)
cursor += _LENGTH;
tallyobj.TEXT = jspack.Unpack( "s".repeat(LENGTH), data, cursor)
if (tallyobj.TEXT != undefined) {
tallyobj.TEXT = tallyobj.TEXT.join("")
//console.log(tallyobj.INDEX + " " + tallyobj.TEXT + " " + tallyobj.CONTROL);
if (tallyobj.INDEX.toString() in tallies) {
tallies[tallyobj.INDEX.toString()].updateControl(tallyobj);
} else {
tallies[tallyobj.INDEX.toString()] = new Index(tallyobj);
}
};
}
}
var writeToDisk = function() {
let path = `${path_pfx}.json`
fs.writeFileSync(path, JSON.stringify(tallies, null, 2))
}
var writeCSVToDisk = function(data) {
let write_data = data.join("\n")
let path = `${path_pfx}.csv`
fs.writeFileSync(path, write_data)
}
function msToTime (ms) {
var seconds = (ms/1000);
var minutes = parseInt(seconds/60, 10);
seconds = seconds%60;
var hours = parseInt(minutes/60, 10);
minutes = minutes%60;
return hours + ':' + minutes + ':' + seconds;
}
process.stdin.on('keypress', function (ch, key) {
//console.log('got "keypress"', key);
if (key && key.ctrl && key.name == 'c') {
var csv_data = ["index, text, duration, counter"];
console.clear();
console.log()
console.log("Printing report:")
console.log("Start time: " + file_date)
console.log("End time: " + new Date())
Object.values(tallies).forEach((item) => {
console.log(`${item.text} has been on ${item.counter} times for ${msToTime(item.duration)}`)
csv_data.push(`${item.index},${item.text},${item.duration},${item.counter}`);
})
//console.log('csv_data', csv_data);
writeCSVToDisk(csv_data);
process.stdin.pause();
process.exit();
}
if (key && key.ctrl && key.name == "p") {
console.log('Writing to disk...')
writeToDisk();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();