Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Added hybrid search method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Avdeev committed Sep 11, 2018
1 parent 5f58233 commit 1d6fe27
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 62 deletions.
158 changes: 98 additions & 60 deletions modules/bluetoothdevices/bluetoothdevices.class.php
Expand Up @@ -282,80 +282,112 @@ function processCycle() {
$address = strtolower($obj->getProperty('address'));
// Search device
$is_found = false;
if(!IsWindowsOS()) {
$scanMethod = strtolower($this->config['scanMethod']);
if(IsWindowsOS()) {
// Windows
switch($scanMethod) {
case 'hybrid': // Hybrid method
case 'ping': // Ping
// FIXME: BluetoothView (v1.66) does not support ping
if($scanMethod != 'hybrid') {
die(date('Y/m/d H:i:s').' Method is not supported for Windows OS: '.$this->config['scanMethod'].PHP_EOL);
break;
}
case 'discovery': // Discovery
// FIXME: does not find BLE devices
// FIXME: finds an offline device if it is paired
if(!$is_found) {
$devices_file = SERVER_ROOT.'/apps/bluetoothview/devices.txt';
unlink($devices_file);
exec(SERVER_ROOT.'/apps/bluetoothview/bluetoothview.exe /stab '.$devices_file);
if(file_exists($devices_file)) {
if($data = LoadFile($devices_file)) {
$data = str_replace(chr(0), '', $data);
$data = str_replace("\r", '', $data);
$lines = explode("\n", $data);
$total = count($lines);
for($i=0; $i<$total; $i++) {
$fields = explode("\t", $lines[$i]);
if(strtolower(trim($fields[2])) == $address) {
$is_found = true;
break;
}
}
}
}
}
if($scanMethod != 'hybrid') {
break;
}
case 'connect': // Connect
if(!$is_found) {
exec(SERVER_ROOT.'/apps/bluetoothview/bluetoothview.exe /try_to_connect '.$address, $data, $code);
if($code == 0) {
$is_found = true;
}
}
if($scanMethod != 'hybrid') {
break;
}
default: // Unknown
if($scanMethod != 'hybrid') {
die(date('Y/m/d H:i:s').' Unknown method: '.$this->config['scanMethod'].PHP_EOL);
}
}
} else {
// Linux
if(time()-intval(getGlobal('bluetoothdevices_resetTime') > intval($this->config['resetInterval']))) {
if((intval($this->config['resetInterval']) >= 0) && (time()-intval(getGlobal('bluetoothdevices_resetTime') > intval($this->config['resetInterval'])))) {
// Reset bluetooth
echo date('Y/m/d H:i:s').' Reset bluetooth'.PHP_EOL;
exec('sudo hciconfig hci0 down; sudo hciconfig hci0 up');
setGlobal('bluetoothdevices_resetTime', time());
}
if(strtolower($this->config['scanMethod']) == 'ping') {
// Ping
$data = exec(str_replace('%ADDRESS%', $address, 'sudo l2ping %ADDRESS% -c1 -f | awk \'/loss/ {print $3}\''));
if(intval($data) > 0) {
$is_found = true;
}
} elseif(strtolower($this->config['scanMethod']) == 'discovery') {
// Discovery
$data = array();
exec('sudo hcitool scan | grep ":"', $data);
exec('sudo timeout -s INT 30s hcitool lescan | grep ":"', $data);
$total = count($data);
for($i=0; $i<$total; $i++) {
$data[$i] = trim($data[$i]);
if(!$data[$i]) {
continue;
switch($scanMethod) {
case 'hybrid': // Hybrid method
case 'ping': // Ping
if(!$is_found) {
$data = exec(str_replace('%ADDRESS%', $address, 'sudo l2ping %ADDRESS% -c1 -f | awk \'/loss/ {print $3}\''));
if(intval($data) > 0) {
$is_found = true;
}
}
if(strtolower(substr($data[$i], 0, 17)) == $address) {
$is_found = true;
if($scanMethod != 'hybrid') {
break;
}
}
} elseif(strtolower($this->config['scanMethod']) == 'connect') {
// Connect
$data = exec('sudo hcitool cc '.$address.' 2>&1');
if(empty($data)) {
$is_found = true;
}
} else {
// Unknown
die(date('Y/m/d H:i:s').' Unknown method: '.$this->config['scanMethod'].PHP_EOL);
}
} else {
// Windows
if(strtolower($this->config['scanMethod']) == 'ping') { // FIXME
// Ping
die(date('Y/m/d H:i:s').' Method is not supported for Windows OS: '.$this->config['scanMethod'].PHP_EOL);
} elseif(strtolower($this->config['scanMethod']) == 'discovery') {
// Discovery
$devices_file = SERVER_ROOT.'/apps/bluetoothview/devices.txt';
unlink($devices_file);
exec(SERVER_ROOT.'/apps/bluetoothview/bluetoothview.exe /stab '.$devices_file);
if(file_exists($devices_file)) {
if($data = LoadFile($devices_file)) {
$data = str_replace(chr(0), '', $data);
$data = str_replace("\r", '', $data);
$lines = explode("\n", $data);
$total = count($lines);
case 'discovery': // Discovery
if(!$is_found) {
$data = array();
exec('sudo hcitool scan | grep ":"', $data);
exec('sudo timeout -s INT 30s hcitool lescan | grep ":"', $data);
$total = count($data);
for($i=0; $i<$total; $i++) {
$fields = explode("\t", $lines[$i]);
if(strtolower(trim($fields[2])) == $address) {
$data[$i] = trim($data[$i]);
if(!$data[$i]) {
continue;
}
if(strtolower(substr($data[$i], 0, 17)) == $address) {
$is_found = true;
break;
}
}
}
}
} elseif(strtolower($this->config['scanMethod']) == 'connect') {
// Connect
exec(SERVER_ROOT.'/apps/bluetoothview/bluetoothview.exe /try_to_connect '.$address, $data, $code);
if($code == 0) {
$is_found = true;
}
} else {
// Unknown
die(date('Y/m/d H:i:s').' Unknown method: '.$this->config['scanMethod'].PHP_EOL);
if($scanMethod != 'hybrid') {
break;
}
case 'connect': // Connect
if(!$is_found) {
$data = exec('sudo hcitool cc '.$address.' 2>&1');
if(empty($data)) {
$is_found = true;
}
}
if($scanMethod != 'hybrid') {
break;
}
default: // Unknown
if($scanMethod != 'hybrid') {
die(date('Y/m/d H:i:s').' Unknown method: '.$this->config['scanMethod'].PHP_EOL);
}
}
}
// Update object
Expand Down Expand Up @@ -452,7 +484,13 @@ function dbInstall($data) {

// Config
$this->getConfig();
$this->config['scanMethod'] = 'discovery';
if(IsWindowsOS()) {
// Windows
$this->config['scanMethod'] = 'connect';
} else {
// Linux
$this->config['scanMethod'] = 'hybrid';
}
$this->config['scanInterval'] = 60; // 1 min
$this->config['scanTimeout'] = 5*60; // 5 min
$this->config['resetInterval'] = 2*60*60; // 2 hrs
Expand Down
6 changes: 4 additions & 2 deletions templates/bluetoothdevices/bluetoothdevices.html
Expand Up @@ -129,9 +129,11 @@
<div class="form-group">
<label class="col-lg-3 control-label">Метод поиска устройств:</label>
<div class="col-lg-8">
<label class="radio"><input type="radio" name="scanMethod" value="hybrid" [#if SCAN_METHOD="hybrid"#] checked[#endif#]>Гибридный</label>
<label class="radio"><input type="radio" name="scanMethod" value="ping" [#if SCAN_METHOD="ping"#] checked[#endif#]>PING запрос (только для Linux)</label>
<label class="radio"><input type="radio" name="scanMethod" value="discovery" [#if SCAN_METHOD="discovery"#] checked[#endif#]>Сканирование радиоэфира</label>
<label class="radio"><input type="radio" name="scanMethod" value="connect" [#if SCAN_METHOD="connect"#] checked[#endif#]>Прямое подключение</label>
<label class="radio"><input type="radio" name="scanMethod" value="ping" [#if SCAN_METHOD="ping"#] checked[#endif#]>PING запрос (только для Linux)</label>
<p class="help-block">Обратите внимание, что на данный момент отслеживание BLE устройств доступно только для платформы Linux.</p>
</div>
</div>
<div class="form-group">
Expand All @@ -152,7 +154,7 @@
<label class="col-lg-3 control-label">Интервал перезагрузки:</label>
<div class="col-lg-8">
<input type="text" class="form-control" name="resetInterval" value="[#RESET_INTERVAL#]" id="resetInterval">
<p class="help-block">Время в секундах, по прошествию которого будет производится перезагрузка Bluetooth модуля (только для Linux систем).</p>
<p class="help-block">Время в секундах, по прошествию которого будет производится перезагрузка Bluetooth модуля (только для Linux систем). Чтобы отключить данную функцию введите -1 (минус один).</p>
</div>
</div>
<div class="form-group">
Expand Down

0 comments on commit 1d6fe27

Please sign in to comment.