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

Commit 2346635

Browse files
committed
Implement ServerStatsAccess (part of #12)
1 parent 6a4f2a0 commit 2346635

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

core/sync/LocalStatsAccess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function listFiles() : array {
1616
);
1717
}
1818

19-
public function getFile( string $file, string $device ) : array {
19+
public function getFile( string $file, string $device ) : array {
2020
return Config::getStorageReader(substr($file, 0, -5))->getArray();
2121
}
2222

core/sync/ServerStatsAccess.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class ServerStatsAccess extends StatsAccess {
77
private string $token;
88
private string $thisClientName;
99

10+
private bool $requestError = false;
11+
1012
public function __construct(){
1113
$c = Config::getStorageReader('config');
1214
$this->uri = $c->getValue(['sync', 'server', 'uri']);
@@ -15,31 +17,64 @@ public function __construct(){
1517
$this->thisClientName = $c->getValue(['sync', 'server', 'thisname']);
1618
}
1719

20+
private function postToServer(string $endpoint, array $data = array() ) : array {
21+
$context = array(
22+
'http' => array(
23+
'method' => 'POST',
24+
'header' => 'Content-Type: application/x-www-form-urlencoded',
25+
'content' => http_build_query(array(
26+
'group' => $this->groupId,
27+
'token' => $this->token,
28+
'client' => $this->thisClientName,
29+
'data' => json_encode($data)
30+
))
31+
));
32+
$append = substr($this->uri, -1) === '/' ? '' : '/';
33+
34+
if( in_array($endpoint, ['add', 'list', 'get'])){
35+
$append .= 'api/' . $endpoint . '.php';
36+
$ret = file_get_contents( $this->uri . $append, false, stream_context_create($context));
37+
38+
if( $ret !== false ){
39+
$json = json_decode( $ret, true);
40+
if( !is_null($json) ){
41+
$this->requestError = false;
42+
return $json;
43+
}
44+
}
45+
}
46+
47+
$this->requestError = true;
48+
return array();
49+
}
50+
1851
public function listFiles() : array {
19-
/**
20-
* ToDo
21-
*/
22-
return array();
52+
return $this->postToServer('list');
2353
}
2454

2555
public function getFile( string $file, string $device ) : array {
26-
/**
27-
* ToDo
28-
*/
29-
return array();
56+
return $this->postToServer(
57+
'get',
58+
array(
59+
'file' => $file,
60+
'device' => $device
61+
));
3062
}
3163

3264
public function initialSync() : bool {
33-
/**
34-
* ToDo
35-
*/
36-
return false;
65+
$ok = true;
66+
foreach( $this->filesToSyncInitially() as $file ){
67+
$this->setDayTasks(json_decode(
68+
file_get_contents( Config::getStorageDir() . '/' . $file ),
69+
true
70+
));
71+
$ok &= !$this->requestError;
72+
}
73+
return $ok;
3774
}
3875

3976
public function setDayTasks(array $tasks) : void {
40-
/**
41-
* ToDo
42-
*/
77+
$this->postToServer('add', $tasks );
4378
}
4479

4580
}

0 commit comments

Comments
 (0)