ini_set("display_errors", "on");
}
$time = time();
$timeout = $time - 170;
if (xcache_get('Read_Homewizard') < $time - 58) {
    $homewizard = file_get_contents($hwurl . 'get-sensors');
    $homewizard = json_decode($homewizard, true);
    if (!$homewizard) {
        if (xcache_get('homewizardconnection') != 'down') {
            xcache_set('homewizardconnection', 'down');
            telegram('Geen verbinding met Homewizard');
        }
    } else {
        if (xcache_get('homewizardconnection') != 'up') {
            xcache_set('homewizardconnection', 'up');
            telegram('Verbinding met Homewizard hersteld');
        }
        foreach ($homewizard['response']['thermometers'] as $temp) {
            if (isset($temp['te'])) {
                $TEMP = str_replace(',', '.', $temp['te']);
                $DUNIT = $temp['id'];
                if ($DUNIT == 1) {
                    $IDX = 36;
                } else {
                    if ($DUNIT == 4) {
                        $IDX = 37;
                    } else {
                        if ($DUNIT == 5) {
                            $IDX = 38;
                        } else {
                            if ($DUNIT == 6) {
function Zwavecancelaction()
{
    global $domoticzurl;
    $zwaveurl = $domoticzurl . 'ozwcp/admpost.html';
    $zwavedata = array('fun' => 'cancel');
    $zwaveoptions = array('http' => array('header' => 'Content-Type: application/x-www-form-urlencoded\\r\\n', 'method' => 'POST', 'content' => http_build_query($zwavedata)));
    $zwavecontext = stream_context_create($zwaveoptions);
    $result = file_get_contents($zwaveurl, false, $zwavecontext);
}
if (!empty($argv[1]) && !empty($argv[2])) {
    RefreshZwave2($argv[1], $argv[2]);
    //$mc->set('RefreshZwave'.$argv[1], $time);
} else {
    logwrite("RefreshZwave: no idx or name defined");
}
sleep(3);
if ($mc->get('deadnodes') < $eenmin) {
    foreach ($devices as $node => $data) {
        if ($node == "result") {
            foreach ($data as $index => $eltsNode) {
                if ($eltsNode["State"] == "Dead") {
                    telegram('Node ' . $eltsNode['NodeID'] . ' ' . $eltsNode['Description'] . ' (' . $eltsNode['Name'] . ') marked as dead, reviving ' . ZwaveHasnodefailed($eltsNode['NodeID']));
                    sleep(2);
                    Zwavecancelaction();
                    //shell_exec('/var/www/secure/reboot.sh');
                }
            }
        }
    }
    $mc->set('deadnodes', $time);
}
            $irc_user = getIrcUser($return[0]);
            $command = $return[1];
            if ($command == "JOIN") {
                telegram("[IRC] " . $irc_user . " joined #thebugcast");
            } elseif ($command == "PART") {
                telegram("[IRC] " . $irc_user . " left #thebugcast");
            } elseif ($command == "PRIVMSG") {
                $channel = $return[2];
                $payload = $text[2];
                if ($channel == "#thebugcast") {
                    if (ord($payload) == 1 && substr($payload, 1, 6) == "ACTION") {
                        $message = "[IRC] " . $irc_user . " " . trim(substr($payload, 7));
                    } else {
                        $message = "[IRC] " . $irc_user . ": " . rtrim($payload);
                    }
                    telegram($message);
                }
            }
        }
    }
    usleep(100);
}
function getIrcUser($tag)
{
    $arr = explode("!", $tag);
    $arr = explode(":", $arr[0]);
    return rtrim($arr[1]);
}
function telegram($message)
{
    $chat_id = "-19118825";
function RefreshZwave($node, $name = 'auto', $device = '')
{
    cset('timerefresh-' . $device, time);
    $devices = json_decode(file_get_contents(api . 'json.htm?type=openzwavenodes&idx=4'), true);
    for ($k = 1; $k <= 5; $k++) {
        ControllerBusy(20);
        $result = file_get_contents(api . 'ozwcp/refreshpost.html', false, stream_context_create(array('http' => array('header' => 'Content-Type: application/x-www-form-urlencoded\\r\\n', 'method' => 'POST', 'content' => http_build_query(array('fun' => 'racp', 'node' => $node))))));
        if ($result === 'OK') {
            cset('timerefresh-' . $device, time);
            break;
        }
        sleep(1);
    }
    if (cget('timedeadnodes') < vijfmin) {
        cset('timedeadnodes', time);
        foreach ($devices as $node => $data) {
            if ($node == "result") {
                foreach ($data as $index => $eltsNode) {
                    if ($eltsNode["State"] == "Dead" && !in_array($eltsNode['NodeID'], array(31, 50, 53, 55, 60))) {
                        telegram('Node ' . $eltsNode['NodeID'] . ' ' . $eltsNode['Description'] . ' (' . $eltsNode['Name'] . ') marked as dead, reviving ' . ZwaveCommand($eltsNode['NodeID'], 'HasNodeFailed'));
                        ControllerBusy(5);
                        ZwaveCommand(1, 'Cancel');
                    }
                }
            }
        }
    }
}
    //End Acties
} else {
    if ($all) {
        $domoticzconnection = $mc->get('domoticzconnection');
        $domoticzconnection = $domoticzconnection + 1;
        $mc->set('domoticzconnection', $domoticzconnection);
        if ($domoticzconnection == 2) {
            telegram('Geen verbinding met Domoticz');
        }
        if ($domoticzconnection == 4) {
            telegram('Domoticzonnection = ' . $domoticzconnection . ', restarting domoticz');
            telegram(shell_exec('/var/www/secure/restart_domoticz > /dev/null 2>&1 &'));
        } else {
            if ($domoticzconnection > 5) {
                telegram('Domoticzonnection = ' . $domoticzconnection . ', rebooting domoticz');
                telegram(shell_exec('/var/www/secure/reboot > /dev/null 2>&1 &'));
            }
        }
    }
}
if ($authenticated) {
    echo '<hr>Number of vars: ' . count(get_defined_vars()) . '<br/><pre>';
}
print_r(get_defined_vars());
echo '</pre>';
if ($actions >= 1) {
    if (isset($argv[1]) && $arg[1] == "All") {
        $msg = 'D' . number_format($domotime, 2) . '|P' . number_format($phptime, 2) . '|T' . number_format($execution, 2) . '|M' . $rpimem . '|C' . $rpicpu . '|E' . $errors . '|TE' . $totalerrors . '|' . $argv[1] . ' -> ' . $actions . ' actions';
    } else {
        if (isset($argv[1])) {
            $msg = 'D' . number_format($domotime, 2) . '|P' . number_format($phptime, 2) . '|T' . number_format($execution, 2) . '|E' . $errors . '|TE' . $totalerrors . '|' . $argv[1] . ' -> ' . $actions . ' actions';
        $result = file_get_contents($zwaveurl, false, $zwavecontext);
        logwrite('RefreshZwave node ' . $node . ' ' . $name . ' ' . $result);
        if ($result == 'OK') {
            break;
        }
        sleep(1);
    }
}
if (!empty($argv[1]) && !empty($argv[2])) {
    RefreshZwave2($argv[1], $argv[2]);
    $mc->set('RefreshZwave' . $argv[1], $time);
} else {
    logwrite("RefreshZwave: no idx or name defined");
}
sleep(3);
if ($mc->get('deadnodes') < $tienmin) {
    foreach ($devices as $node => $data) {
        if ($node == "result") {
            foreach ($data as $index => $eltsNode) {
                if ($eltsNode["State"] == "Dead") {
                    $reply = json_decode(file_get_contents($domoticzurl . 'json.htm?type=command&param=zwavenodeheal&idx=' . $zwaveidx . '&node=' . $eltsNode['NodeID']), true);
                    logwrite('Node ' . $eltsNode['NodeID'] . ' ' . $eltsNode['Description'] . ' (' . $eltsNode['Name'] . ') marked as dead, healing command = ' . $reply['status']);
                    telegram('Node ' . $eltsNode['NodeID'] . ' ' . $eltsNode['Description'] . ' (' . $eltsNode['Name'] . ') marked as dead, healing command = ' . $reply['status'] . '.Errors=' . $preverrors);
                    //			  $errors=$errors+1;
                }
            }
        }
    }
    $mc->set('deadnodes', $time);
}
//$totalerrors=$preverrors+$errors;if($totalerrors!=$preverrors) $mc->set('errors',$totalerrors);
function Udevice($idx, $nvalue, $svalue, $name = NULL)
{
    global $domoticzurl, $user, $actions, $Usleep, $errors;
    $reply = json_decode(file_get_contents($domoticzurl . 'json.htm?type=command&param=udevice&idx=' . $idx . '&nvalue=' . $nvalue . '&svalue=' . $svalue), true);
    if ($reply['status'] == 'OK') {
        $reply = 'OK';
    } else {
        $reply = 'ERROR';
        $errors = $errors + 1;
    }
    logwrite('Udevice ' . $name . ' N=' . $nvalue . ' S=' . $svalue . ' by ' . $user . ' = ' . $reply);
    if ($user == "Tobi") {
        telegram('Tobi Udevice ' . $idx . ' ' . $nvalue . ' ' . $snvalue);
    }
    $actions = $actions + 1;
    usleep($Usleep);
    return $reply;
}
                 }
             } else {
                 if (isset($_POST['Scene'])) {
                     if (Udevice($_POST['Scene'], 1, 'On', $_POST['Naam']) == 'ERROR') {
                         echo '<div id="message" class="balloon">Scene ' . $_POST['Naam'] . ' activeren' . '<br/>ERROR</div>';
                     }
                     if ($_POST['Scene'] == 5) {
                         $mc->set('dimtimeEettafel', $time);
                     }
                 } else {
                     if (isset($_POST['restart'])) {
                         telegram('Restart Domoticz executed by ' . $user);
                         telegram(shell_exec('/var/www/secure/restart_domoticz'));
                     } else {
                         if (isset($_POST['reboot'])) {
                             telegram('Reboot Domoticz Rpi executed by ' . $user);
                             shell_exec('sudo /var/www/secure/reboot');
                         } else {
                             if (isset($_POST['healnetwork'])) {
                                 file_get_contents($domoticzurl . 'json.htm?type=command&param=zwavenetworkheal&idx=' . $zwaveidx);
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 if (isset($_POST['denon'])) {
     $denon_address = 'http://192.168.0.15';
     $ctx = stream_context_create(array('http' => array('timeout' => 2)));
function RefreshZwave($node, $name = 'auto', $device = '')
{
    global $domoticzurl, $time, $zwaveidx, $vijfmin, $actions;
    $devices = json_decode(curl($domoticzurl . 'json.htm?type=openzwavenodes&idx=' . $zwaveidx), true);
    //logwrite('Refreshing node '.$node.' '.$device.' '.$name);
    $zwaveurl = $domoticzurl . 'ozwcp/refreshpost.html';
    $zwavedata = array('fun' => 'racp', 'node' => $node);
    $zwaveoptions = array('http' => array('header' => 'Content-Type: application/x-www-form-urlencoded\\r\\n', 'method' => 'POST', 'content' => http_build_query($zwavedata)));
    $zwavecontext = stream_context_create($zwaveoptions);
    for ($k = 1; $k <= 5; $k++) {
        $result = file_get_contents($zwaveurl, false, $zwavecontext);
        $actions = $actions + 1;
        if ($result === 'OK') {
            cset('timerefresh-' . $device, $time);
            break;
        }
        sleep(1);
    }
    if (cget('timedeadnodes') < $vijfmin) {
        cset('timedeadnodes', $time);
        foreach ($devices as $node => $data) {
            if ($node == "result") {
                foreach ($data as $index => $eltsNode) {
                    if ($eltsNode["State"] == "Dead" && !in_array($eltsNode['NodeID'], array(50))) {
                        $actions = $actions + 1;
                        telegram('Node ' . $eltsNode['NodeID'] . ' ' . $eltsNode['Description'] . ' (' . $eltsNode['Name'] . ') marked as dead, reviving ' . ZwaveHasnodefailed($eltsNode['NodeID']));
                        sleep(2);
                        Zwavecancelaction();
                    }
                }
            }
        }
    }
    return $result;
}