diff -r 7259ee8778e9 -r 244de612c572 www-thermferm/devices.php --- a/www-thermferm/devices.php Fri Aug 01 22:53:58 2014 +0200 +++ b/www-thermferm/devices.php Sat Aug 02 22:33:15 2014 +0200 @@ -30,7 +30,7 @@ return; } -socket_write($sock, "LIST BUS", 4096); +socket_write($sock, "DEVICE LIST", 4096); $answer = ""; while (1) { $line = socket_read($sock, 4096); @@ -39,44 +39,544 @@ $answer .= $line; } socket_close($sock); + +/* + * $arr contains the complete reply of the LIST BUS command + */ $arr = explode("\r\n", $answer); -$outstr = build_header("ThermFerm - Devices Setup"); +if (isset($_GET['action'])) { + switch ($_GET['action']) { + case 'edit': device_edit(); + break; + default: break; + } +} elseif (isset($_POST['action'])) { + switch ($_POST['action']) { + case 'testdata': testdata(); + break; + default: break; + } +} else { + device_list(); +} + +exit; + +/**************************************************************************** + * + */ + + +/* + * Device add + * + * @param string $_POST['Name'] The rpofile name + */ +function device_add() { + + if ($_POST['key'] == 'Add') { + + $cmd = "DEVICE ADD ".$_POST['Type']; -$outstr .= '
'.PHP_EOL; -$outstr .= '
'.PHP_EOL; -$outstr .= '
'.PHP_EOL; + $sock = open_socket(); + if ($sock != false) { + /* + * Send command and absorb the result. + */ + socket_write($sock, $cmd, 4096); + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + } + socket_close($sock); + } + } + + unset($_POST['UUID']); + unset($_POST['Name']); + unset($_POST['key']); + unset($_POST['command']); + load('devices.php'); +} + + + +/* + * Device update + * + * @param string $_POST['UUID'] The device UUID + * @param string $_POST['Type'] The device Type + * @param string $_POST['Direction'] The device IO Direction + * @param string $_POST['Value'] The device value + * @param string $_POST['Present'] The device Present state + * @param string $_POST['Address'] The device Address + * @param string $_POST['Subdevice'] The device Subaddress + * @param string $_POST['Gpiopin'] The device GPIO pin + * @param string $_POST['Description'] The device Description + * @param string $_POST['Comment'] The device Comment + * @param string $_POST['key'] The button pressed. + */ +function device_update() { + /* + * Build the update command + */ + if ($_POST['key'] == 'Delete') { + $sock = open_socket(); + if ($sock != false) { + socket_write($sock, "DEVICE DEL ".$_POST['UUID'], 4096); + /* Absorb response */ + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + } + socket_close($sock); + } + } -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; + if ($_POST['key'] == 'Save') { + $sock = open_socket(); + if ($sock != false) { + /* + * Send command and absorb the result. + */ + socket_write($sock, "DEVICE PUT ".$_POST['UUID'], 4096); + usleep(20000); + socket_write($sock, "TYPE,".$_POST['Type'], 4096); + usleep(20000); + socket_write($sock, "DIRECTION,".$_POST['Direction'], 4096); + usleep(20000); + socket_write($sock, "VALUE,".$_POST['Value'], 4096); + usleep(20000); + socket_write($sock, "PRESENT,".$_POST['Present'], 4096); + usleep(20000); + socket_write($sock, "ADDRESS,".$_POST['Address'], 4096); + usleep(20000); + socket_write($sock, "SUBDEVICE,".$_POST['Subdevice'], 4096); + usleep(20000); + socket_write($sock, "GPIOPIN,".$_POST['Gpiopin'], 4096); + usleep(20000); + socket_write($sock, "DESCRIPTION,".$_POST['Description'], 4096); + usleep(20000); + socket_write($sock, "COMMENT,".$_POST['Comment'], 4096); + usleep(20000); + socket_write($sock, '.', 4096); + /* Absorb response */ + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + } + socket_close($sock); + } + } + + unset($_POST['UUID']); + unset($_POST['Type']); + unset($_POST['Direction']); + unset($_POST['Value']); + unset($_POST['Present']); + unset($_POST['Address']); + unset($_POST['Subdevice']); + unset($_POST['Gpiopin']); + unset($_POST['Description']); + unset($_POST['Comment']); + unset($_POST['key']); + unset($_POST['command']); + load('devices.php'); +} + + + +/* + * Test input of a modified or new device. + * + * @param string $_POST['UUID'] Unique record UUID + * @param string $_POST['Type'] Device Type + * @param string $_POST['Direction'] + * @param string $_POST['Value'] + * @param string $_POST['Present'] + * @param string $_POST['Address'] + * @param string $_POST['Subdevice'] + * @param string $_POST['Gpiopin'] + * @param string $_POST['Description'] + * @param string $_POST['Comment'] + * @param string $_POST['key'] Key choice, Save or Cancel + * @param string $_POST['command'] Command used, 'add' or 'update' + * + * Return: 0 = Ok + * 1 = Missing data + * 2 = Address field too short + * 3 = Address/Subdevice already in use + * 4 = Description field too short + * 5 = Comment field too short + * 99 = Cancel key + */ +function test_thedata() { + + global $arr; + + if (isset($_POST['UUID']) && isset($_POST['Type']) && isset($_POST['Direction']) && isset($_POST['Value']) && + isset($_POST['Present']) && isset($_POST['Address']) && isset($_POST['Subdevice']) && isset($_POST['Gpiopin']) && + isset($_POST['Description']) && isset($_POST['Comment']) && isset($_POST['key']) && isset($_POST['command'])) { + + if ($_POST['key'] == 'Cancel') + return 99; + + if (strlen($_POST['Address']) < 2) + return 2; + + if (startsWith($arr[0], "212")) { + $j = 1; + while (1) { + if (strcmp($arr[$j], ".") == 0) + break; + $f = explode(",", $arr[$j]); + if (strcmp($f[0], $_POST['UUID']) && ($f[1] == $_POST['Address']) && ($f[2] == $_POST['Subdevice'])) { + return 3; + } + $j++; + } + } + + if (strlen($_POST['Description']) < 2) + return 4; + + if (strlen($_POST['Comment']) < 2) + return 5; + + } else { + return 1; + } + + return 0; +} + + + +/* + * Test result from edit screen and do next action + */ +function testdata() { + + $result = test_thedata(); + $error = ''; -if (startsWith($arr[0], "212")) { - $j = 1; + switch ($result) { + case 0: if ($_POST['command'] == 'add') { + device_add(); + return; + } else if ($_POST['command'] == 'update') { + device_update(); + return; + } + break; + case 1: $error = 'Missing data'; + break; + case 2: $error = 'The Address is too short'; + break; + case 3: $error = 'The Address + Subdevice is already in use, choose another one'; + break; + case 4: $error = 'The Description is too short'; + break; + case 5: $error = 'The Comment is too short'; + break; + case 99: + load('devices.php'); + break; + } + + if ($_POST['command'] == 'add') { + $heading = 'ThermFerm - Add Device'; + } else { + $heading = 'ThermFerm - Edit Device'; + } + + edit_screen($_POST['UUID'], $_POST['command'], $heading, $error); +} + + + +/* + * Unit edit screen. Used by unit_edit(), unit_add() and testdata() + * + * @param string $UUID The record UUID (fixed). + * @param string $command 'add' or 'update' + * @param string $heading Page heading title. + * @Param string $error_message Blank or previous error. + */ +function edit_screen($UUID, $command, $heading, $error_message) { + + $sock = open_socket(); + if ($sock == false) { + load('devices.php'); + } + + socket_write($sock, "DEVICE GET ".$UUID, 4096); + $answer = ""; while (1) { - if (strcmp($arr[$j], ".") == 0) + $line = socket_read($sock, 4096); + if ($line === '') break; - $f = explode(",", $arr[$j]); - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $j++; + $answer .= $line; + } + socket_close($sock); + $reply = explode("\r\n", $answer); + + $outstr = build_header($heading); + $outstr .= '
'.PHP_EOL; + $outstr .= ' '.$error_message.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
UUIDAddressSubdevRefcntComment
'.$f[0].''.$f[1].''.$f[2].''.$f[3].''.$f[4].'
'.PHP_EOL; + + if (startsWith($reply[0], "213")) { + $j = 1; + while (1) { + if (strcmp($reply[$j], ".") == 0) + break; + $f = explode(",", $reply[$j]); + if ($f[0] == "TYPE") { + $type = $f[1]; + /* + * Only allow certain types to edit. Auto detected hardware cannot be changed. + */ + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if (($type == "W1") || ($type == "GPIO")) + $outstr .= ' '.PHP_EOL; + else + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "DIRECTION") { + $direction = $f[1]; + /* + * Only devices that cannot auto detect can be changed. + */ + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if ($type == "W1") + $outstr .= ' '.PHP_EOL; + else { + $outstr .= ' '.PHP_EOL; + } + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "VALUE") { + /* + * Only output can be changed + */ + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if (($direction == "OUT_BIN") || ($direction == "OUT_ANALOG") || ($direction == "OUT_PWM")) + $outstr .= ' '.PHP_EOL; + else + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "PRESENT") { + /* + * Only devices that cannot auto detect can be changed. + */ + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if (($type == "W1") || ($type == "GPIO")) + $outstr .= ' '.PHP_EOL; + else { + $outstr .= ' '.PHP_EOL; + } + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "ADDRESS") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if (($type == "W1") || ($type == "GPIO")) + $outstr .= ' '.PHP_EOL; + else + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "SUBDEVICE") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if (($type == "W1") || ($type == "GPIO")) + $outstr .= ' '.PHP_EOL; + else + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "GPIOPIN") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "DESCRIPTION") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "COMMENT") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + $j++; + } + } + + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= '
Device Type'.$f[1].'
IO Direction and mode'.$f[1].'
Value'.$f[1].'
Device Present'.$f[1].'
Address'.$f[1].'
Subdevice'.$f[1].'
GPIO pin
Description
Comment
'; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= '
'.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= build_footer(); + echo $outstr; +} + + + +/* + * Edit a Device. Fetches the record data and shows the edit screen. + * + * @param string $_GET['action'] Must be 'edit'. + * @param string $_GET['UUID'] The UUID of the Unit. + */ +function device_edit() { + if ($_GET['action'] == 'edit') { + edit_screen($_GET['UUID'], 'update', 'ThermFerm - Edit Device', ''); + return; + } else { + load('devices.php'); } } -$outstr .= ' '.PHP_EOL; -$outstr .= ' '.PHP_EOL; -$outstr .= build_footer(); + + +/* + * @link edit device + * @link add device + */ +function device_list() { + global $arr; + + $outstr = build_header("ThermFerm - Devices Setup"); + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; -echo $outstr; + if (startsWith($arr[0], "212")) { + $j = 1; + while (1) { + if (strcmp($arr[$j], ".") == 0) + break; + $f = explode(",", $arr[$j]); + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + if ($f[3] == 0) + $outstr .= ' '.PHP_EOL; + else + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $j++; + } + } + + $outstr .= '
UUIDAddressSubRefCmtEdit
'.$f[0].''.$f[1].''.$f[2].''.$f[3].''.$f[4].'EditBusy
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ''; + $outstr .= ' '.PHP_EOL; + $outstr .= '
Add new Device
Select Device type'.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + $outstr .= '
'.PHP_EOL; + + $outstr .= build_footer(); + + echo $outstr; +} +