Skip to content

Commit 2de3659

Browse files
committed
added import rules script
1 parent ec34490 commit 2de3659

6 files changed

+63
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
params.php
2+
params_dest.php

README.md

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# OPENHAB 4: UI2FILES
22

3-
Command line tool to migrate UI config to files for **OpenHAB v. 4** (semantic model compatible)
3+
Command line tools to help migrating configuration from UI to files for **OpenHAB v. 4** (semantic model compatible)
44

5-
This tool is writter in **PHP 8** and can be run as a docker container in Windows, using runme.bat. Otherwise you could run by yourself using a local **PHP 8** installation
5+
These tools are written in **PHP 8** and can be run as a docker container in Windows, using the provided runme_XXXX.bat. Otherwise you could run by yourself using a local **PHP 8** installation
66

7-
It generate things and items files, partial services.cfg file and json rules source files to be imported using API, reading bridges, things, channels, links, location, equipments, groups, points, items, rules and addons from OpenHAB 4 configuration using API Rest.
7+
At this moment there are two distinct scripts
8+
9+
## convert.php
10+
11+
It generates things and items files, partial services.cfg file for addons and json rules(scenes source files to be imported using API (or the import_rules.php script), reading bridges, things, channels, links, location, equipments, groups, points, items, rules/scenes and addons from OpenHAB 4 configuration using API Rest.
812

913
- .things files (bridges, things and channels) will be generated in *output_folder*/things
1014
- .items files (locations, equipments, groups, points, items and links) will be generated in *output_folder*/items
@@ -15,8 +19,28 @@ In the *output_folder* there is 1 file
1519

1620
- XX_addonsconfig.txt: config variables for installed addons as simple reference
1721

18-
## Usage
22+
### Usage
1923

2024
- Copy *params.php.template* in *params.php* and populate config variables according to your setup
21-
- Run *runme.bat*
25+
- Run *runme_convert.bat*
2226
- Files will be generated in output folder
27+
28+
## import_rules.php
29+
30+
It create rules and scenes on the destination platform, reading the json payload files generated by the **convert.php** software (*see previous chapter*)
31+
32+
### Usage
33+
34+
- Copy *params_dest.php.template* in *params_dest.php* and populate config variables according to your setup
35+
- Run *runme_import_rules.bat*
36+
- You can follow the progress in the terminal output
37+
38+
# DISCLAIMER
39+
40+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
46+
SOFTWARE.

import_rules.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
ini_set("display_errors", 1);
4+
5+
include_once("params_dest.php");
6+
include_once("utils.php");
7+
8+
$rulesPayloadPath = RULES_PAYLOAD_PATH;
9+
10+
if(!file_exists($rulesPayloadPath))
11+
die("Invalid payload rules path " . $rulesPayloadPath);
12+
13+
if ($handle = opendir($rulesPayloadPath)){
14+
$urlRules = OH_API_BASEURL . "/rules";
15+
while (false !== ($entry = readdir($handle))){
16+
$tmp = explode("_", $entry);
17+
if(array_pop($tmp) == "rules.json"){
18+
echo "Found file: " . $entry . PHP_EOL;
19+
$payload = file_get_contents($rulesPayloadPath . "/" . $entry);
20+
$responsePlain = curlPOST($urlRules, $payload, OH_CLOUD_USERNAME, OH_CLOUD_PASSWORD, ["X-OPENHAB-TOKEN: " . OH_API_TOKEN]);
21+
echo "API Response" . PHP_EOL . "\tcode => " . $responsePlain["httpcode"] . PHP_EOL . "\tbody => " . $responsePlain["response"] . PHP_EOL . PHP_EOL;
22+
}
23+
}
24+
}

params_dest.php.template

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
define("RULES_PAYLOAD_PATH", "output/rules"); // input path where json payload files will be found
4+
define("OH_API_BASEURL", "https://home.myopenhab.org/rest"); // base url api openhab
5+
define("OH_CLOUD_USERNAME", "xxx"); // openhab cloud username if using cloud base url
6+
define("OH_CLOUD_PASSWORD", "xxx"); // openhab cloud password if using cloud base url
7+
define("OH_API_TOKEN", "xxx"); // openhab api token

runme.bat runme_convert.bat

File renamed without changes.

runme_import_rules.bat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
docker container run --rm --network=host -v .\:/app/ -w /app php:8.2-cli php /app/import_rules.php
2+
pause

0 commit comments

Comments
 (0)