-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathohm-mqtt-service.js
127 lines (110 loc) · 2.99 KB
/
ohm-mqtt-service.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
/* eslint-env node */
const commandLineArgs = require('command-line-args')
const commandLineUsage = require('command-line-usage')
const filename = require('path').join(__dirname, '\\', 'index.js')
const serviceName = 'ohm-mqtt'
const cliDefinition = [
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Display this usage guide.'
},
{
name: 'install',
alias: 'i',
type: Boolean,
description: 'Install ohm-mqtt as Windows Service'
},
{
name: 'uninstall',
alias: 'u',
type: Boolean,
description: 'Uninstall ohm-mqtt Windows Service'
},
{
name: 'options',
type: String,
description:
"Commandline options to pass to script\nKnown bug: first option can not start with '--'. To work around use '-c' or '-f' as first parameter"
}
]
const cliParameter = commandLineArgs(cliDefinition)
if (cliParameter.help) {
showHelp()
}
if (cliParameter.install && cliParameter.uninstall) {
throw new Error('Conflicting parameter specified')
} else if (cliParameter.install) {
cliParameter.parameter = cliParameter.options || ''
installService()
} else if (cliParameter.uninstall) {
uninstallService()
} else {
showHelp()
}
function installService () {
var Service = require('node-windows').Service
// Create a new service object
var svc = new Service({
name: serviceName,
description:
'Reads Open Hardware Monitor measurements from JSON export and publishes to MQTT broker',
script: filename,
scriptOptions: cliParameter.options,
env: {
name: 'NODE_ENV',
value: 'production'
}
})
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function () {
svc.start()
})
// Just in case this file is run twice.
svc.on('alreadyinstalled', function () {
console.log('This service is already installed.')
})
// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start', function () {
console.log(svc.name + ' has been installed and is running')
})
// Install the script as a service.
svc.install()
}
function uninstallService () {
var Service = require('node-windows').Service
// Create a new service object
var svc = new Service({
name: serviceName,
script: filename
})
// Listen for the "uninstall" event so we know when it's done.
svc.on('uninstall', function () {
console.log('Service uninstall complete.')
console.log('The service exists: ', svc.exists)
})
// Uninstall the service.
svc.uninstall()
}
function showHelp () {
const usage = commandLineUsage([
{
header: 'ohm-mqtt-service',
content:
'Install ohm-mqtt as a Windows Service, for start-up at boot time '
},
{
header: 'Options',
optionList: cliDefinition
},
{
content:
'Project home: {underline https://github.com/jacques42/ohm-mqtt}'
}
])
console.log(usage)
process.exit()
}