-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
134 lines (106 loc) · 2.89 KB
/
model.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
const { exec } = require('child_process')
const find = require('find-process')
const code = require('./codes.json')
const fkill = require('fkill')
////////////////////////////////SETTINGS/////////////////////////////////
let airfader = 'C:/ProgramData/Microsoft/Windows/"Start Menu"/Programs/AirFader/airfader.lnk'
let DME = 'C:/Windows/SysWOW64/"DME-N Network Driver.exe"'
let startCmd = `start ${airfader}`
let cmd = "AirFader.exe"
//////////////////////////////////MODEL//////////////////////////////////
class AirFader {
async start() {
//If not started
if (!(await running('AirFader.exe'))) {
//Restarting the dme driver
await restartDme()
//start it
if (await execSync(startCmd)) {
// Check its started
if ( await running(cmd) ) {
return code.started
} else {
return code.error
}
}
// Handle unable to start error
return code.error
} else {
//Handle already running
return code.running
}
}
async stop() {
//if running
if (await running(cmd)) {
//Killing the process
await fkill("AirFader.exe", {
tree: true,
force: true
})
//Checking that it's killed
if (!(await running(cmd))) {
return code.stopped
} else {
return code.error
}
} else {
//If not running already
return code.notRunning
}
}
async restart() {
await this.off()
await this.on()
return code.restarted
}
async status() {
if (await running(cmd)) {
return code.running
} else {
return code.notRunning
}
}
}
////////////////////////////FUNCTIONS//////////////////////
function delay(time) {
setTimeout(() => {
return true
}, time);
}
async function running(name) {
if ((await find('name', name)).length) {
return true
} else {
return false
}
}
async function restartDme() {
if (await running('DME-N Network Driver.exe')) {
//Kill DME Driver
await fkill('DME-N Network Driver.exe', {
tree: true,
force: true
})
//Start DME Driver
exec(`start ${DME}`)
return true
} else {
await execSync(`start ${DME}`)
return true
}
}
function execSync(cmd) {
//returns the error or true if no error
return new Promise((resolve, reject) => {
exec(cmd, (err) => {
if (err) {
console.log(err)
reject(err)
} else {
resolve(true)
}
})
})
}
module.exports = AirFader