-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
50 lines (40 loc) · 1.74 KB
/
import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
* use: php import.php path reading
*
* path: file path to the FHEM log file you would like to convert
* reading: optional parameter which will overwrite the name of the reading.
* Useful when you have a simple log file containing just one value
*/
require(__DIR__ . '/../vendor/autoload.php');
if (!isset($argv[1])) {
die('please pass a path via the first command line argument');
}
$pathToLogFile = $argv[1];
if (isset($argv[2])) {
$overwriteReading = $argv[2];
}
$pdoMysql = new PDO('mysql:dbname=fhem;host=localhost;port=3306', '<Your Username>', '<Your Password>');
$pdoMysql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$mysqlSource = new \FhemMigrateLogfiles\Repository\Source\Mysql($pdoMysql);
$fileName = basename($pathToLogFile);
$fileSource = new \FhemMigrateLogfiles\Repository\Source\File(new SplFileObject($pathToLogFile)) or die('Could not open file: ' . $pathToLogFile);
try {
$devices = $mysqlSource->readDevices();
$events = [];
do {
try {
if (($event = $fileSource->readNextEvent($devices)) === null) {
break;
}
$events[] = $event;
} catch (\FhemMigrateLogfiles\Repository\Source\ReadingFailedException $readingFailedException) {
die($readingFailedException->getMessage());
}
} while (!empty($event));
$mysqlSource->saveEvent($events);
} catch (\FhemMigrateLogfiles\Repository\Source\ReadingFailedException $readingFailedException) {
die($readingFailedException->getMessage());
} catch (\FhemMigrateLogfiles\Repository\Source\SavingFailedException $savingFailedException) {
die($savingFailedException->getMessage());
}