Hi,
I am running a pimatic server and an openhab server simultaneously.
I like pimatic for it’s ability to run Homeduino and the flexibility of handling variables and I like openhab because it’s easy to provision things to Amazon Echo etc.
To make it easier to bring both worlds together I made a php script which exports pimatic switches as items for openhab2.
So I thought I release the script here in the forum, maybe someone of you has use for it.
Please understand that I cannot give full support for this script, I expect knowledge of how to run php on a server before using the script.
Please make sure, that your open_basedir config enables the script to write into it’s own directory.
Feel free to change or/and improve the script, have fun.
<?php
$htmlHeader = "<html>
<head>
<style>
body { background-color: darkgray; }
body { background-image: linear-gradient(135deg, lightgray 30%, lightgray 45%, lightgray 65%, darkgray 69%); font-family: \"Open Sans\" }
</style>
</head>
<body>
<script>
function check_all(name, el) {
var box = el.form.elements[name];
if (!box.length) box.checked = el.checked; else
for (var i = 0; i < box.length; i++)
box[i].checked = el.checked;
}
</script>
";
$htmlFooter="</body>
</html>";
// pimatic server config, standards are pre configured, change if necessary
$pimaticServer = ""; // Pimatic Server IP
$pimaticPort = "8081"; // Pimatic Server Port
$pimaticProto = "http"; // Pimatic Server http or https Protocol
$pimaticUser = "admin"; // Pimatic User Name
$pimaticPass = ""; // Pimatic User Password
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$makeZip = false;
if ($result = file_get_contents(dirname(__FILE__) . "/devicestmp.json"))
{
if ($devices = json_decode($result, true))
{
if (isset($_POST['zip']) && $_POST['zip'] == "on")
{
$makeZip = true;
$zip = new ZipArchive();
$filename = dirname(__FILE__) . "/items.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE)
{
exit("Cannot create zip file <$filename>\n");
}
}
else
{
echo $htmlHeader;
}
$basicAuthString = base64_encode($pimaticUser . ':' . $pimaticPass);
$template = "Switch #ID# \"#NAME#\" [ \"Switchable\" ]" .
" { http=\">[ON:GET:$pimaticProto://$pimaticServer:$pimaticPort/api/device/#ID#/turnOn{Authorization=Basic $basicAuthString}]" .
" >[OFF:GET:$pimaticProto://$pimaticServer:$pimaticPort/api/device/rfswitch/turnOff{Authorization=Basic $basicAuthString}]\"}";
foreach ($devices['devices'] as $device)
{
if(in_array($device['config']['id'], $_POST['myitems']))
{
$itemsString = str_replace(array('#ID#', '#NAME#'), array($device['config']['id'], $device['config']['name']), $template);
if ($makeZip)
{
$zip->addFromString($device['config']['id'] . ".items", $itemsString);
}
else
{
$tmpFilename = dirname(__FILE__) . "/" . $device['config']['id'] . ".items";
if (file_put_contents($tmpFilename, $itemsString) )
{
echo "Created: " . basename($tmpFilename) . "<p />\n";
}
else
{
echo "Failed: " . basename($tmpFilename) . "<p />\n";
}
}
}
}
if ($makeZip) {
$zip->close();
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($filename) . "\"");
readfile($filename);
}
else
{
echo "The item files where stored in " . dirname(__FILE__);
echo $htmlFooter;
}
}
}
}
else
{
echo $htmlHeader;
$url = $pimaticProto . "://" . $pimaticServer . ":" . $pimaticPort . "/api/devices";
$cu = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cu, CURLOPT_VERBOSE, 1);
curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($cu, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cu, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cu, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($cu, CURLOPT_URL, $url);
curl_setopt($cu, CURLOPT_USERPWD, "$pimaticUser:$pimaticPass");
$result = curl_exec($cu);
$cu_error = curl_error($cu);
curl_close($cu);
if ($cu_error)
{
echo "cURL Error: $cu_error";
}
else
{
if ($devices = json_decode($result, true))
{
file_put_contents(dirname(__FILE__) . "/devicestmp.json", $result);
?>
<form action="" method="post">
<h3>Select switches to export to items:</h3>
<fieldset>
<label><input type="checkbox" onclick="check_all('myitems[]', this)"> Check all </label>
<ul>
<?php
foreach ($devices['devices'] as $device)
{
if(stristr($device['config']['class'], "switch"))
{
echo " <li>\n".
" <label>\n".
" <input type=\"checkbox\" name=\"myitems[]\" value=\"" . $device['config']['id'] . "\">\n".
" " . $device['config']['id'] . " - " . $device['config']['name'] . " - " . $device['config']['class'] . "\n" .
" </label>\n".
" </li>\n";
}
}
?>
</ul>
<label><input type="checkbox" name="zip">Export as zip</label>
</fieldset>
<button type="submit">Make my items</button>
</form>
<?php
}
}
echo $htmlFooter;
}
?>