-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
151 lines (130 loc) · 4.55 KB
/
controller.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
const app = require('electron').app;
const path = require('path');
const nedb = require('nedb-revived');
const SerialPort = require('serialport');
const { StringStream } = require('scramjet');
function log(data) {
console.log('[controller]', data);
}
var db = { // load the databases
buttons: new nedb({
filename: path.join(app.getPath('userData'), 'buttons.db'),
autoload: true
}),
settings: new nedb({
filename: path.join(app.getPath('userData'), 'settings.db'),
autoload: true
})
};
module.exports = controller = {};
controller.getSerialPort = callback => {
db.settings.loadDatabase(); // update the database
db.settings.findOne({ setting: 'serialPort' }, (err, setting) => { // get the serial port from the settings database
if (err) throw err;
if (setting == null) { // if the setting isn't set in the database
SerialPort.list().then( // we fetch the list of connected serial devices
ports => {
ports.forEach(port => {
if (['2341', '2886'].includes(port.vendorId)) { // if the vendorId is arduino
callback(port.path); // we return the value
return;
} else { // no corresponding device has been found
return;
}
});
},
err => {
throw err;
}
)
} else {
callback(setting.value); // we are going to connect to the setting's value
return;
}
});
}
var serialport;
controller.connect = (callback = () => {}) => {
controller.getSerialPort(port => { // fetch the controller
log('Connecting to serial port ' + port)
serialport = new SerialPort(port, { // initialize the serial port connection
baudRate: 9600,
autoOpen: false,
parser: new SerialPort.parsers.Readline('\r\n')
});
serialport.open(() => { // when the serial port is opened
log('Serial connected');
callback();
controller.updateConfig(); // update the controller config
controller.handleData(); // create the data handler
serialport.on('error', err => {
throw err;
});
});
});
}
controller.disconnect = (callback = () => {}) => {
log('Disconnecting serial...');
if (typeof serialport == 'undefined') {
callback();
} else {
serialport.close(callback);
}
}
controller.reconnect = () => {
log('Reconnecting serial...');
controller.disconnect(() => {
controller.connect();
});
}
controller.handleData = () => {
serialport.pipe(new StringStream).lines('\r\n').each(data => { // for each line (split the data with \r\n)
data = data.split('/'); // data format: dataType/data
db.buttons.loadDatabase(); // update the database
db.buttons.findOne({ id: parseInt(data[1]) }, (err, button) => { // find the button in the database
if (err) throw err;
switch (data[0]) { // depending on the type of action
case 'buttonPush': // if the button is released after a short press
controller.pushHandler(button);
break;
case 'buttonHold': // if the button is pressed for a long time (1 sec by default)
controller.holdHandler(button);
break;
}
});
});
}
controller.updateConfig = () => {
log('Updating config');
db.settings.loadDatabase(); // update the database
db.settings.findOne({ setting: 'buttonHoldTimer' }, (err, setting) => { // get the config from the settings database
if (err) throw err;
if (typeof(setting.value) == 'undefined') { // if the setting isn't set in the database
buttonHoldTimer = 1; // set it to 1s by default
} else {
buttonHoldTimer = setting.value; // otherwise set it to the value found in the database
}
log('config: buttonHoldTimer: ' + buttonHoldTimer)
serialport.write('buttonHoldTimer/' + buttonHoldTimer*1000 + '\n'); // send the config to the controller
});
}
controller.getStatus = callback => { // get the current controller status, connected or not
if (typeof serialport == 'undefined') { // if the serial is not initialized yet
callback(false); // return "false" (using callback because the rest of the function is asynchronous)
return;
}
if (serialport.isOpen) { // we could just use this variable, but sometimes it returns false, while the connection is opened, and data can be transmitted. so we only trust it if returns true
callback(true); // return true
return;
}
var writeResult = false;
serialport.write('\n', () => { // we try to write something to the serial, the callback only triggers if the write was successful
callback(true); // return true
writeResult = true;
});
setTimeout(() => { // after 1 sec, we check if the connection was successful (serialport doesn't have a error callback)
if (!writeResult) { // if the write was not successful
callback(false); // return false
}
}, 1000);
}