$st = checkStatus($mac, $s20Table); if (!$timer) { echo "Timer is off, status = " . actionToTxt($st) . "\n"; break; } else { echo sprintf("%02d:%02d:%02d to => %s, current is %s\n", $h, $m, $s, actionToTxt($action), actionToTxt($st)); } ob_flush(); sleep(2); } } // // Test automatic switch off after on first device. // echo "\n\n\nTesting automatic switch off after on using switch " . $name . "\n"; $initValue = getSwitchOffTimer($mac, $s20Table); $setOff = 1800; $res = setSwitchOffTimer($mac, $setOff, $s20Table); if ($res == $setOff) { echo "\nSetting automatic switch off timer to " . $setOff . "s OK\n"; } else { echo "\nSet automatic switch off timer failed\n"; } $setOff = 0; $res = setSwitchOffTimer($mac, $setOff, $s20Table); if ($res == $setOff) { echo "\nResetting automatic switch off timer OK\n"; } else { echo "\nReset automatic switch off timer " . $name . " failed\n"; } // Program again the initial value
function setSwitchOffTimer($mac, $sec, &$s20Table) { // // Sets the automatic switch off timer in table 4 to $sec // subscribe($mac, $s20Table); $table = 4; $vflag = "17"; $recTable = getTable($mac, $table, $vflag, $s20Table); $switchOffData = substr($recTable, 164 * 2, 8); // This substring defines the format for // "automatic switch off after switch on" according to the following // // XXYYZZZZ // // XX, Enabled satus: XX=00 Disabled,XX=01 - Enabled // YY, Action to be performed after switch on: 00-turn off, 01-turn on // ZZZZ Initial countdown in seconds, little endian // // Not sure what is the purpose of YY=01 (switch on after switch on), // but we have seen this configuration ocasionaly, possibly as // a side effect of some other operation. // $switchOffData = ($sec == 0 ? "00" : "01") . "00"; $switchOffData = $switchOffData . secToHexLE($sec); // Set timer $newTableAux = substr_replace($recTable, $switchOffData, 2 * 164, 8); // replace receive code with send code $newTableAux = substr_replace($newTableAux, WRITE_SOCKET_CODE, 2 * 4, 4); // // Delete byte 18 (??) // Wireshark shows... // $newTable = substr_replace($newTableAux, "", 18 * 2, 2); // // Delete byte 25 and 26 of resulting (??) // Wireshark shows... // $newTable = substr_replace($newTable, "", 25 * 2, 4); // Update msg size, just in case it has changed $newTable = adjustMsgSize($newTable); $reply = createSocketSendHexMsgWaitReply($mac, $newTable, $s20Table); $newSec = getSwitchOffTimer($mac, $s20Table); return $newSec; }