Skip to content

Commit

Permalink
Added bumper compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
WelterRocks committed Oct 21, 2020
1 parent 72baef1 commit 7d24fc7
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 52 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ linked to your account. EcoPhacs has been written in PHP and is an alternative
to the [Sucks](https://github.com/wpietri/sucks) project.

# News
- **2020-10-21** Added bumper compat.
Now, it is possible to use EcoPhacs with bumper. Added some compatibility code to make EcoPhacs work
with the bumper server project.
- **2020-10-17** EcoPhacs-MQTT release.
The first beta version of the EcoPhacs-MQTT daemon has been released. Now, it is possible to connect
your Ecovacs Deebot directly to a MQTT broker, which is e.g. useful to control and supervise your Ecovacs
Expand Down Expand Up @@ -113,6 +116,14 @@ But remember, to configure your /etc/ecophacs/ecophacs.conf with `EcoPhacs-Confi
to set up your MQTT credentials. Also, TLS is currently not supported by the
`EcoPhacs-MQTT.php` tool.

## Bumper usage
If you want to use EcoPhacs with bumper, add the following options to `EcoPhacs-Daemon.php`
or `EcoPhacs-MQTT.php`, to enable to bumper compatibility mode in EcoPhacs:

```
EcoPhacs-{Daemon|MQTT}.php --bump-api /path/to/bumper/certs/bumper.crt --dry-login --api-urls "https://[IP-OF-BUMPER-SERVER]/v1/private/%country%/%app_language%/%device_id%/%app_code%/%app_version%/%app_channel%/%device_type%" "https://[IP-OF-BUMPER-SERVER]/api/users/user.do" --is-bumper-server start|foreground
```

# Library usage
## General information
This library is in an early development stage. You can use this library
Expand Down
7 changes: 4 additions & 3 deletions override/welterrocks/xmpp-php/src/Xml/Stanzas/Iq.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ public function getFeatures(string $forJid)
$this->socket->send($xml);
}

public function command($from, $to, $todo, $command = null)
{
public function command($from, $to, $todo, $command = null, $no_ns0 = false)
{
$ctl = "<ctl id='{$this->uniqueId()}' td='{$todo}'".(($command) ? ">{$command}</ctl>" : "/>");
$query = "<query xmlns='com:ctl'>{$ctl}</query>";
$xml = "<iq xmlns='jabber:client' type='set' from='{$from}' to='{$to}' id='{$this->uniqueId()}'>{$query}</iq>";

$xml = "<iq ".(($no_ns0) ? "" : "xmlns='jabber:client' ")."type='set' from='{$from}' to='{$to}' id='{$this->uniqueId()}'>{$query}</iq>";

$this->socket->send($xml);
}
Expand Down
104 changes: 100 additions & 4 deletions sbin/EcoPhacs-Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@

$pid = null;

$bump_api = null;
$dry_login = null;

// Create CLI object
try
{
Expand Down Expand Up @@ -229,7 +232,7 @@ function worker_loop(Client $ecovacs, $devices)
{
global $ticks_state, $ticks_lifespans, $ticks_output;
global $cli, $fifo_in, $fifo_out, $public_functions;
global $worker_reload, $daemon_terminate;
global $worker_reload, $daemon_terminate, $dry_login, $bump_api;

// Dispatch signals in inner loop
$cli->signals_dispatch();
Expand Down Expand Up @@ -432,7 +435,7 @@ function mother()
function daemon()
{
global $cli, $fifo_in, $fifo_out, $public_functions;
global $daemon_terminate, $worker_reload;
global $daemon_terminate, $worker_reload, $bump_api, $dry_login;

// Register shutdown function
register_shutdown_function("shutdown_daemon");
Expand Down Expand Up @@ -472,16 +475,28 @@ function daemon()
// Create the client (main) object
$ecovacs = new Client(select_config_file());

// Check, whether we have to "bump" the API
if (is_object($bump_api))
{
$ecovacs->bump_api($bump_api->public_key, $bump_api->key, $bump_api->secret, $bump_api->url_main, $bump_api->url_user, $bump_api->realm);

if ($bump_api->is_bumper_server)
$ecovacs->set_as_bumper_server();
}

// Initialize error string store
$error = null;

// Dispatch signals in outer loop
$cli->signals_dispatch();

// Try login or "dry" login
$do_login = (($dry_login) ? "dry_login" : "try_login");

// Try to login, if not yet done, otherwise fetch device list
if (!$ecovacs->try_login($error))
if (!$ecovacs->$do_login($error))
{
$cli->log("Unable to login: ".$error, LOG_ALERT);
$cli->log("Unable to ".(($dry_login) ? "dry-" : "")."login: ".$error, LOG_ALERT);

sleep(10);

Expand Down Expand Up @@ -595,6 +610,87 @@ function shutdown_daemon()
remove_pid();
}

// Use alternative security informations ("bump" the API)
if ($cli->has_argument("--bump-api"))
{
$public_key_file = $cli->get_argument_childs("--bump-api", 1);

$bump_api = new \stdClass;

$bump_api->key = null;
$bump_api->secret = null;
$bump_api->url_main = null;
$bump_api->url_user = null;
$bump_api->realm = null;
$bump_api->public_key = null;
$bump_api->is_bumper_server = null;

if ($cli->has_argument("--is-bumper-server"))
$bump_api->is_bumper_server = true;
else
$bump_api->is_bumper_server = false;

if ($cli->has_argument("--api-credentials"))
{
$credentials = $cli->get_argument_childs("--api-credentials", 2);

if ((is_array($credentials)) && (count($credentials) == 2))
{
$bump_api->key = $credentials[0];
$bump_api->secret = $credentials[1];
}

unset($credentials);
}

if ($cli->has_argument("--api-urls"))
{
$urls = $cli->get_argument_childs("--api-urls", 2);

if ((is_array($urls)) && (count($urls) == 2))
{
$bump_api->url_main = $urls[0];
$bump_api->url_user = $urls[1];
}

unset($urls);
}

if ($cli->has_argument("--api-realm"))
{
$args = $cli->get_argument_childs("--api-realm", 1);

if ((is_array($args)) && (count($args) == 1))
$bump_api->realm = $args[0];

unset($args);
}

if (!$public_key_file)
$cli->exit_error(CLI::COLOR_LIGHT_RED."Missing certificate filename after argument.".CLI::COLOR_EOL, 2);
elseif (is_array($public_key_file))
$public_key_file = $public_key_file[0];

if (!file_exists($public_key_file))
$cli->exit_error(CLI::COLOR_LIGHT_RED."Certificate file not found.".CLI::COLOR_EOL, 2);

$cert = explode("-----", str_replace("\n", "", trim(@file_get_contents($public_key_file))));

unset($public_key_file);

if (count($cert) > 3)
$bump_api->public_key = $cert[2];

if (!$bump_api->public_key)
$cli->exit_error(CLI::COLOR_LIGHT_RED."Not a valid certificate file.".CLI::COLOR_EOL, 3);

unset($cert);
}

// Check for "dry" login
if ($cli->has_argument("--dry-login"))
$dry_login = true;

// Check usage
if ($cli->has_argument("start"))
{
Expand Down
106 changes: 101 additions & 5 deletions sbin/EcoPhacs-MQTT.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@

$pid = null;

$bump_api = null;
$try_login = null;

// Create CLI object
try
{
Expand Down Expand Up @@ -121,7 +124,7 @@ function select_config_file()
function worker_loop(MQTT $mqtt, Client $ecovacs, $devices)
{
global $ticks_state, $ticks_lifespans, $ticks_output;
global $cli, $public_functions;
global $cli, $public_functions, $dry_login, $bump_api;
global $worker_reload, $daemon_terminate;

// Dispatch signals in inner loop
Expand All @@ -140,7 +143,7 @@ function worker_loop(MQTT $mqtt, Client $ecovacs, $devices)

$dev->get_clean_state();
$dev->get_charge_state();
$dev->get_battery_info();
$dev->get_battery_info();
}

$ticks_state = 0;
Expand Down Expand Up @@ -395,7 +398,7 @@ function mother()
// Daemon callback does the hard part
function daemon()
{
global $cli, $public_functions;
global $cli, $public_functions, $bump_api, $dry_login;
global $daemon_terminate, $worker_reload;

// Register shutdown function
Expand Down Expand Up @@ -467,17 +470,29 @@ function daemon()

// Create the client object
$ecovacs = new Client($config_file);

// Check, whether we have to "bump" the API
if (is_object($bump_api))
{
$ecovacs->bump_api($bump_api->public_key, $bump_api->key, $bump_api->secret, $bump_api->url_main, $bump_api->url_user, $bump_api->realm);

if ($bump_api->is_bumper_server)
$ecovacs->set_as_bumper_server();
}

// Initialize error string store
$error = null;

// Dispatch signals in outer loop
$cli->signals_dispatch();

// Try login or "dry" login
$do_login = (($dry_login) ? "dry_login" : "try_login");

// Try to login, if not yet done, otherwise fetch device list
if (!$ecovacs->try_login($error))
if (!$ecovacs->$do_login($error))
{
$cli->log("Unable to login: ".$error, LOG_ALERT);
$cli->log("Unable to ".(($dry_login) ? "dry-" : "")."login: ".$error, LOG_ALERT);

sleep(10);

Expand Down Expand Up @@ -589,6 +604,87 @@ function shutdown_daemon()
remove_pid();
}

// Use alternative security informations ("bump" the API)
if ($cli->has_argument("--bump-api"))
{
$public_key_file = $cli->get_argument_childs("--bump-api", 1);

$bump_api = new \stdClass;

$bump_api->key = null;
$bump_api->secret = null;
$bump_api->url_main = null;
$bump_api->url_user = null;
$bump_api->realm = null;
$bump_api->public_key = null;
$bump_api->is_bumper_server = null;

if ($cli->has_argument("--is-bumper-server"))
$bump_api->is_bumper_server = true;
else
$bump_api->is_bumper_server = false;

if ($cli->has_argument("--api-credentials"))
{
$credentials = $cli->get_argument_childs("--api-credentials", 2);

if ((is_array($credentials)) && (count($credentials) == 2))
{
$bump_api->key = $credentials[0];
$bump_api->secret = $credentials[1];
}

unset($credentials);
}

if ($cli->has_argument("--api-urls"))
{
$urls = $cli->get_argument_childs("--api-urls", 2);

if ((is_array($urls)) && (count($urls) == 2))
{
$bump_api->url_main = $urls[0];
$bump_api->url_user = $urls[1];
}

unset($urls);
}

if ($cli->has_argument("--api-realm"))
{
$args = $cli->get_argument_childs("--api-realm", 1);

if ((is_array($args)) && (count($args) == 1))
$bump_api->realm = $args[0];

unset($args);
}

if (!$public_key_file)
$cli->exit_error(CLI::COLOR_LIGHT_RED."Missing certificate filename after argument.".CLI::COLOR_EOL, 2);
elseif (is_array($public_key_file))
$public_key_file = $public_key_file[0];

if (!file_exists($public_key_file))
$cli->exit_error(CLI::COLOR_LIGHT_RED."Certificate file not found.".CLI::COLOR_EOL, 2);

$cert = explode("-----", str_replace("\n", "", trim(@file_get_contents($public_key_file))));

unset($public_key_file);

if (count($cert) > 3)
$bump_api->public_key = $cert[2];

if (!$bump_api->public_key)
$cli->exit_error(CLI::COLOR_LIGHT_RED."Not a valid certificate file.".CLI::COLOR_EOL, 3);

unset($cert);
}

// Check for "dry" login
if ($cli->has_argument("--dry-login"))
$dry_login = true;

// Check usage
if ($cli->has_argument("start"))
{
Expand Down
Loading

0 comments on commit 7d24fc7

Please sign in to comment.