-
Notifications
You must be signed in to change notification settings - Fork 106
/
utils.js
65 lines (58 loc) · 1.83 KB
/
utils.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
const debug = {
mqtt: require('debug')('ring-mqtt'),
attr: require('debug')('ring-attr'),
disc: require('debug')('ring-disc')
}
const colors = require('colors/safe')
const dns = require('dns')
const os = require('os')
const { promisify } = require('util')
class Utils
{
// Sleep function (seconds)
sleep(sec) {
return this.msleep(sec*1000)
}
// Sleep function (milliseconds)
msleep(msec) {
return new Promise(res => setTimeout(res, msec))
}
// Return ISO time from epoch without milliseconds
getISOTime(epoch) {
return new Date(epoch).toISOString().slice(0,-5)+"Z"
}
async getHostFqdn() {
const pLookupService = promisify(dns.lookupService)
try {
return (await pLookupService(await this.getHostIp(), 0)).hostname
} catch {
console.log('Failed to resolve FQDN, using os.hostname() instead')
return os.hostname()
}
}
async getHostIp() {
const pLookup = promisify(dns.lookup)
try {
return (await pLookup(os.hostname())).address
} catch {
console.log('Failed to resolve hostname IP address, returning localhost instead')
return 'localhost'
}
}
log(message, level, category) {
category = category ? category : 'mqtt'
switch (level) {
case 'info':
debug[category](colors.green(`[${this.deviceData.name}] `)+message)
break;
case 'warn':
debug[category](colors.brightYellow(`[${this.deviceData.name}] `)+message)
break;
case 'error':
debug[category](colors.brightRed(`[${this.deviceData.name}] `)+message)
default:
debug[category](message)
}
}
}
module.exports = new Utils()