Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 6f96e9f

Browse files
committed
Im- & Export CLI Promts
1 parent 6f965d6 commit 6f96e9f

4 files changed

Lines changed: 76 additions & 47 deletions

File tree

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,21 @@ to the server.
5252

5353
### Import to Server
5454
Create the the device for which the data should be imported and
55-
remember its token. (The name of the device can only be set via the Webinterface).
55+
remember its token. (The name of the device can only be set once via the Webinterface).
5656

57-
1. Open `./imexport/import.php`
58-
2. Fill in `Server URL, Account Name, Device Name, Device Token` between `APIClient( ... );` as stated in the file
59-
3. Change the `$sourcePath`, it has to be the directory containing the devices data (e.g. `2020-04-01.json`, `2020-04-03.json`, ...)
60-
4. Set the timezone `date_default_timezone_set( 'Europe/Berlin' );` (see https://www.php.net/manual/en/timezones.php for list of supported ones)
61-
4. Run `php ./imexport/import.php`
57+
1. Run `php ./imexport/import.php`
58+
2. Type credentials of device when prompted
59+
3. Fill in path to the directory containing the devices data (e.g. `2020-04-01.json`, `2020-04-03.json`, ...)
60+
4. Change the timezone, if you want (see https://www.php.net/manual/en/timezones.php for list of supported zones)
6261
5. The data for the device will be available in the Webinterface (and through the API).
6362

6463
### Export from Server
6564
One may use any device of an account.
6665

67-
1. Open `./imexport/export.php`
68-
2. Fill in `Server URL, Account Name, Device Name, Device Token` between `APIClient( ... );` as stated in the file
69-
3. One may change the `$destPath`, it has to be any empty directory (or it will be created).
70-
4. Run `php ./imexport/export.php`
71-
5. The data for all devices will show up in `$destPath`
66+
1. Run `php ./imexport/export.php`
67+
2. Type credentials of device when prompted
68+
3. Change the export directory if you want (it has to be any empty directory and it will be created if not exists).
69+
4. The data for all devices will show up the directory
7270

7371
## Custom Graphs
7472
It is possible to create custom graph-views for the stats view.

imexport/api.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@
33
die('Commandline only!');
44
}
55

6+
function createAPIReadline() : APIClient {
7+
$check = array(
8+
'URI' => fn(string $u) => @filter_var($u, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_SCHEME_REQUIRED),
9+
'Group/ Username' => fn(string $g) => preg_match('/^[A-Za-z0-9]+$/', $g) === 1,
10+
'Device' => fn(string $n) => preg_match( '/^[A-Za-z0-9\-]+$/', $n) === 1,
11+
'Device Token' => fn(string $t) => preg_match('/^[A-Za-z0-9]+$/', $t) === 1
12+
);
13+
$tabs = array(
14+
'URI' => 3,
15+
'Group/ Username' => 1,
16+
'Device' => 3,
17+
'Device Token' => 2
18+
);
19+
$data = array();
20+
21+
echo "Please create a Device in the Webinterface, then fill in details:" . PHP_EOL;
22+
foreach($check as $name => $checker ){
23+
do {
24+
$input = trim(readline($name. ':' . str_repeat("\t", $tabs[$name])));
25+
if(empty($input) || !$checker($input) ){
26+
echo "\tError invalid format!" . PHP_EOL;
27+
}
28+
else{
29+
$data[$name] = $input;
30+
}
31+
} while( empty( $data[$name] ) );
32+
}
33+
34+
return new APIClient(
35+
$data['URI'],
36+
$data['Group/ Username'],
37+
$data['Device'],
38+
$data['Device Token']
39+
);
40+
}
41+
642
class APIClient {
743

844
private string $uri;
@@ -22,7 +58,7 @@ private function postToServer(string $endpoint, array $data = array()) : array {
2258
'http' => array(
2359
'method' => 'POST',
2460
'header' => 'Content-Type: application/x-www-form-urlencoded',
25-
//'ignore_errors' => true,
61+
'ignore_errors' => true,
2662
'content' => http_build_query(array(
2763
'group' => $this->groupId,
2864
'token' => $this->token,
@@ -51,8 +87,13 @@ private function postToServer(string $endpoint, array $data = array()) : array {
5187
return array();
5288
}
5389

54-
public function listFiles() : array {
55-
return $this->postToServer('list');
90+
public function listFiles(int $timeMin, int $timeMax) : array {
91+
return $this->postToServer(
92+
'list',
93+
array(
94+
'timeMin' => $timeMin,
95+
'timeMax' => $timeMax
96+
));
5697
}
5798

5899
public function getFile( string $file, string $device ) : array {

imexport/export.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,20 @@
44
}
55
require_once(__DIR__ . '/api.php');
66

7-
/**
8-
* LOGIN
9-
*/
10-
$client = new APIClient(
11-
"http://localhost:8080/", // Server URL
12-
"admin", // Account/ Group Name
13-
"test", // Device Name
14-
"YvCSpXLZxpT0RPLI4yg6JIhSBKwILJ1vFqNILvf6luji7JrtUS" // Device Token
15-
);
16-
/**
17-
* SETTINGS
18-
*/
7+
$client = createAPIReadline();
198
$destPath = __DIR__ . '/exported/';
9+
echo PHP_EOL . 'Export will be written to "'. $destPath .'"' . PHP_EOL;
10+
$newPath = readline('Type other path to change or leave empty to use path above: ');
11+
if(!empty($newPath)){
12+
$destPath = $newPath;
13+
}
2014

21-
/**
22-
* TOOL
23-
*/
2415
if(!is_dir($destPath)){
2516
if(!mkdir($destPath, 0740, true)){
2617
die('Error creating export path!');
2718
}
2819
}
29-
foreach( $client->listFiles() as $f ){
20+
foreach( $client->listFiles(0, time()) as $f ){
3021
$devicePath = $destPath . '/' . $f['device'] . '/';
3122
if(!is_dir($devicePath)){
3223
if(!mkdir($devicePath, 0740, true)){

imexport/import.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
}
55
require_once(__DIR__ . '/api.php');
66

7-
/**
8-
* LOGIN
9-
*/
10-
$client = new APIClient(
11-
"http://localhost:8080/", // Server URL
12-
"admin", // API Username/ Group
13-
"test", // API Device name
14-
"9iTOiAj0nMnN0yxZvYf2bnt9vSlCPKKBb7U0f8chkcLnVCQPe3" // Token for device
15-
);
16-
/**
17-
* SETTINGS
18-
*/
19-
$sourcePath = '/Users/me/Documents/TTTData/laptop/'; // data folder to import from
20-
date_default_timezone_set( 'Europe/Berlin' ); // timezone
7+
echo "Please make sure to use the right Device name, it can't be changed later!" . PHP_EOL;
8+
$client = createAPIReadline();
9+
10+
echo PHP_EOL . 'Please give a directory where the JSON to import of the device are stored.' . PHP_EOL;
11+
do{
12+
$sourcePath = readline('Type directory: ');
13+
} while( empty($sourcePath) || !is_dir($sourcePath));
14+
15+
$timezone = 'Europe/Berlin';
16+
echo PHP_EOL . 'Timezone will be "'. $timezone .'"' . PHP_EOL;
17+
$newZone = readline('Type other timezone to change or leave empty to use timezone above: ');
18+
if(!empty($newZone)){
19+
$timezone = $newZone;
20+
}
21+
date_default_timezone_set( $timezone );
22+
2123

22-
/**
23-
* TOOL
24-
*/
2524
foreach( scandir($sourcePath) as $f ){
2625
if(preg_match('/^\d{4}-(0|1)\d-[0-3]\d\.json$/', $f) === 1){
2726
$client->setDayTasks(

0 commit comments

Comments
 (0)