Commit ec51852d authored by Peter Müller's avatar Peter Müller
Browse files

Use l2ping instead of hcitool to detect bluetooth devices

parent 8c3c7738
Pipeline #165 passed with stage
in 11 seconds
......@@ -29,8 +29,12 @@ module.exports = {
description: "Das verwendete Hardware-Gerät für Bluetooth-Prüfungen."
type: "string"
default: "hci0"
bluetoothUseSudo:
description: "Verwende 'sudo' für Bluetooth-Prüfungen? Wird benötigt, wenn Pimatic nicht als root gestartet wird."
type: "boolean"
default: false
pingTimeout:
description: "Timeout in Sekunden für einen Netzwerk-Ping."
description: "Timeout in Sekunden für einen Ping."
type: "number"
default: 2
}
......
......@@ -29,8 +29,12 @@ module.exports = {
description: "The used hardware device for the bluetooth checks."
type: "string"
default: "hci0"
bluetoothUseSudo:
description: "Use 'sudo' for bluetooth checking? Needed if pimatic isn't started as root."
type: "boolean"
default: false
pingTimeout:
description: "Timeout in seconds for a network ping."
description: "Timeout in seconds for a ping."
type: "number"
default: 2
}
......
......@@ -51,6 +51,8 @@ module.exports = (env) ->
@bluetoothDevice = @config.bluetoothDevice or 'hci0'
@bluetoothSudo = if @config.bluetoothUseSudo then 'sudo' else ''
@pingTimeout = @config.pingTimeout or 2
assert @config.networkIpAddresses.length > 0 or @config.bluetoothMacAddresses.length > 0, "You need to define at least one IP or MAC address"
......@@ -120,26 +122,24 @@ module.exports = (env) ->
env.logger.debug "#{@id} checking bluetooth #{mac}"
# new promise for running hcitool
return new Promise( (resolve, reject) =>
# get the device name using hcitool...
# got the name -> device present
# got no name -> device absent
exec "hcitool -i #{@bluetoothDevice} name #{mac}", (error, stdout, stderr) =>
exec "#{@bluetoothSudo}l2ping -i #{@bluetoothDevice} -c 1 -t #{@pingTimeout} #{mac}", (error, stdout, stderr) =>
if error
env.logger.warn "error running 'hcitool':", error
resolve()
return
stdout = stdout.trim()
if stdout.length > 0
# present
env.logger.debug "#{@id} bluetooth #{mac} present (#{stdout})"
okCount++
if error.code is 1
# device absent
env.logger.debug "#{@id} bluetooth #{mac} absent"
errorCount++
else
# other error
env.logger.warn "#{@id} error running 'l2ping':", error
resolve()
return
else
# absent
env.logger.debug "#{@id} bluetooth #{mac} absent"
errorCount++
# device present
# even if no ping response...
env.logger.debug "#{@id} bluetooth #{mac} present"
okCount++
# resolve the hcitool-promise
# resolve the l2ping-promise
resolve()
)
)
......
......@@ -20,7 +20,7 @@
"README.md",
"README-de.md"
],
"version": "1.0.2",
"version": "1.1.0",
"license": "GPL-2.0",
"configSchema": "netcheck-config-schema.coffee",
"homepage": "https://crycode.de/pimatic-netcheck",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment