mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-10-18 14:50:36 +08:00
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
const fs = require('fs')
|
|
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'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new Utils()
|