# HG changeset patch # User Michiel Broek # Date 1407436447 -7200 # Node ID 5d013b4a9138503352a37ed71929b023f3d39cf0 # Parent c74bbc24a1c873bfdf0d06b4eefe89fbaedb4ed3 Added PROFILE GET and PROFILE PUT commands. Adjusted the web interface diff -r c74bbc24a1c8 -r 5d013b4a9138 thermferm/server.c --- a/thermferm/server.c Thu Aug 07 19:38:30 2014 +0200 +++ b/thermferm/server.c Thu Aug 07 20:34:07 2014 +0200 @@ -509,9 +509,9 @@ */ int cmd_profile(char *buf) { - char ibuf[SS_BUFSIZE], *sstep, *rest, *targ, *param; + char ibuf[SS_BUFSIZE], *sstep, *rest, *targ, *param, *kwd, *val; int i, j, rlen, istep, irest; - float ftarg; + float ftarg, fval; socklen_t fromlen; char *opt; profiles_list *profile, *tmpp; @@ -584,6 +584,72 @@ return 1; } + } else if (strcmp(opt, (char *)"GET") == 0) { + for (profile = Config.profiles; profile; profile = profile->next) { + if (strcmp(profile->uuid, param) == 0) { + srv_send((char *)"213 Profile %s record follows:", profile->uuid); + srv_send((char *)"UUID,%s", profile->uuid); + srv_send((char *)"NAME,%s", profile->name); + srv_send((char *)"INITTEMP,%.1f", profile->inittemp); + srv_send((char *)"."); + return 1; + } + } + srv_send((char *)"440 No such profile"); + return 1; + + } else if (strcmp(opt, (char *)"PUT") == 0) { + for (profile = Config.profiles; profile; profile = profile->next) { + if (strcmp(profile->uuid, param) == 0) { + while (1) { + memset((char *)&ibuf, 0, SS_BUFSIZE); + fromlen = sizeof(peeraddr_in); + rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); + if (rlen == -1) { + syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); + srv_send((char *)"518 recfrom(): %s", strerror(errno)); + return 1; + } + for (i = 0; i < strlen(ibuf); i++) { + if (ibuf[i] == '\n') + ibuf[i] = '\0'; + if (ibuf[i] == '\r') + ibuf[i] = '\0'; + } + for (i = strlen(ibuf) -1; i > 0; i--) { + if (ibuf[i] == ' ') + ibuf[i] = '\0'; + else + break; + } + if (strlen(ibuf)) { + if (debug) { + syslog(LOG_NOTICE, "recv: \"%s\"", ibuf); + fprintf(stdout, "recv: \"%s\"\n", ibuf); + } + if (strcmp(ibuf, (char *)".") == 0) { + srv_send((char *)"219 Accepted Device record"); + return 0; + } + kwd = strtok(ibuf, ",\0"); + val = strtok(NULL, "\0"); + if (kwd && val) { + if (strcmp(kwd, (char *)"NAME") == 0) { + if (profile->name) + free(profile->name); + profile->name = xstrcpy(val); + } else if (strcmp(kwd, (char *)"INITTEMP") == 0) { + if (sscanf(val, "%f", &fval) == 1) + profile->inittemp = fval; + } + } + } + } + } + } + srv_send((char *)"440 No such profile"); + return 1; + } else if (strcmp(opt, (char *)"GETS") == 0) { for (profile = Config.profiles; profile; profile = profile->next) { @@ -1179,6 +1245,8 @@ srv_send((char *)"PROFILE ADD name Add new profile with name"); srv_send((char *)"PROFILE DEL uuid Delete profile by uuid"); srv_send((char *)"PROFILE LIST List available profiles"); + srv_send((char *)"PROFILE GET uuid Get Profile record by uuid"); + srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid"); srv_send((char *)"PROFILE GETS uuid Profile get steps list"); srv_send((char *)"PROFILE PUTS uuid Profile put steps list"); srv_send((char *)"UNIT uuid Select unit by uuid"); diff -r c74bbc24a1c8 -r 5d013b4a9138 www-thermferm/profiles.php --- a/www-thermferm/profiles.php Thu Aug 07 19:38:30 2014 +0200 +++ b/www-thermferm/profiles.php Thu Aug 07 20:34:07 2014 +0200 @@ -186,7 +186,7 @@ if ($_POST['key'] == 'Add') { - $cmd = "ADD PROFILE ".$_POST['Name']; + $cmd = "PROFILE ADD ".$_POST['Name']; $sock = open_socket(); if ($sock != false) { @@ -213,38 +213,57 @@ /* - * Profile update + * Profile update or delete * * @param string $_POST['UUID'] The profile UUID * @param string $_POST['Name'] The profile name + * @param string $_POST['Inittemp'] The profile initial temperature * @param string $_POST['key'] The button pressed. */ function profile_update() { /* * Build the update command */ - if ($_POST['key'] == 'Delete') - $update_cmd = "DEL PROFILE ".$_POST['UUID']; - else - $update_cmd = "PROFILE ".$_POST['UUID'].",".$_POST['Name']; + if ($_POST['key'] == 'Delete') { + $sock = open_socket(); + if ($sock != false) { + socket_write($sock, "PROFILE DEL ".$_POST['UUID'], 4096); + /* Absorb response */ + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + } + socket_close($sock); + } + } + - $sock = open_socket(); - if ($sock != false) { - /* - * Send command and absorb the result. - */ - socket_write($sock, $update_cmd, 4096); - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - } - socket_close($sock); + if ($_POST['key'] == 'Save') { + $sock = open_socket(); + if ($sock != false) { + /* + * Send command and absorb the result. + */ + socket_write($sock, "PROFILE PUT ".$_POST['UUID'], 4096); + usleep(20000); + socket_write($sock, "NAME,".$_POST['Name'], 4096); + usleep(20000); + socket_write($sock, "INITTEMP,".$_POST['Inittemp'], 4096); + usleep(20000); + socket_write($sock, ".", 4096); + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + } + socket_close($sock); + } } unset($_POST['UUID']); unset($_POST['Name']); - unset($_POST['Steps']); + unset($_POST['Inittemp']); unset($_POST['key']); unset($_POST['command']); load('profiles.php'); @@ -375,7 +394,7 @@ * * @param string $_POST['UUID'] Unique record UUID * @param string $_POST['Name'] Profile name - * @param int $_POST['Steps'] Profile steps + * @param float $_POST['Inittemp'] Profile initial temperature * @param string $_POST['key'] Key choice, Save or Cancel * @param string $_POST['command'] Command used, 'add' or 'update' * @@ -389,7 +408,7 @@ global $arr; - if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['key']) && isset($_POST['command'])) { + if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['Inittemp']) && isset($_POST['key']) && isset($_POST['command'])) { if ($_POST['key'] == 'Cancel') return 99; @@ -453,7 +472,7 @@ $heading = 'ThermFerm - Edit Profile'; } - edit_screen($_POST['UUID'], $_POST['Name'], $_POST['command'], $heading, $error); + edit_screen($_POST['UUID'], $_POST['command'], $heading, $error); } @@ -467,7 +486,27 @@ * @param string $heading Pagina heading title. * @Param string $error_message Blank or previous error. */ -function edit_screen($UUID, $Name, $command, $heading, $error_message) { +function edit_screen($UUID, $command, $heading, $error_message) { + + /* + * Get current profile data + */ + $sock = open_socket(); + if ($sock == false) { + load("profiles.php"); + return; + } + + socket_write($sock, "PROFILE GET ".$UUID, 4096); + $answer = ""; + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + $answer .= $line; + } + socket_close($sock); + $reply = explode("\r\n", $answer); $outstr = build_header($heading); $outstr .= '
'.PHP_EOL; @@ -476,18 +515,29 @@ $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; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; - $outstr .= ' '.PHP_EOL; + + if (startsWith($reply[0], "213")) { + $i = 1; + while (1) { + if (strcmp($reply[$i], ".") == 0) + break; + $f = explode(",", $reply[$i]); + + if ($f[0] == "NAME") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + if ($f[0] == "INITTEMP") { + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + $outstr .= ' '.PHP_EOL; + } + $i++; + } + } $outstr .= ' '.PHP_EOL; $outstr .= ' '.PHP_EOL; $outstr .= ' '.PHP_EOL; $outstr .= ''; $outstr .= ''; - $outstr .= ''; $outstr .= ''; + $outstr .= ''; $outstr .= ' '.PHP_EOL; $outstr .= '
UUID'.$UUID.'
Profile Name
Profile Steps'.$Steps.'
Profile Name
Initial temperature
'; @@ -512,27 +562,9 @@ * @param string $_GET['UUID'] The UUID of the Profile. */ function profile_edit() { - - global $arr; - if ($_GET['action'] == 'edit') { - - if (startsWith($arr[0], "212")) { - $j = 1; - while (1) { - if (strcmp($arr[$j], ".") == 0) - break; - $f = explode(",", $arr[$j]); - if (strcmp($f[0], $_GET['UUID']) == 0) { - edit_screen($f[0], $f[1], 'update', 'ThermFerm - Edit Profile', ''); - return; - } - $j++; - } - } else { - load('profiles.php'); - } - + edit_screen($_GET['UUID'], 'update', 'ThermFerm - Edit Profile', ''); + return; } else { load('profiles.php'); } @@ -595,8 +627,8 @@ $outstr .= '
'.PHP_EOL; $outstr .= '
'.PHP_EOL;