thermferm/server.c

changeset 132
8bd209d1c020
parent 131
528dc0bb81ab
child 133
345307762220
equal deleted inserted replaced
131:528dc0bb81ab 132:8bd209d1c020
408 } 408 }
409 409
410 410
411 411
412 /* 412 /*
413 * PROFILE List profile status of current unit
414 * PROFILE uuid,name Rename profile name
415 */
416 int cmd_profile(char *buf)
417 {
418 char *opt, *uuid, *param;
419 profiles_list *profile;
420 units_list *unit;
421
422 opt = strtok(buf, " \0");
423 opt = strtok(NULL, "\0");
424
425 if (opt == NULL) {
426 /*
427 * Profile status of current unit
428 */
429 if (current_unit == NULL) {
430 srv_send((char *)"401 No fermenter unit selected");
431 return 1;
432 }
433 for (unit = Config.units; unit; unit = unit->next) {
434 if (strcmp(current_unit, unit->uuid) == 0)
435 break;
436 }
437
438 if (unit->profile == NULL) {
439 srv_send((char *)"441 Unit has no profile");
440 return 1;
441 }
442 if (unit->mode != UNITMODE_PROFILE) {
443 srv_send((char *)"442 Unit is not using a profile");
444 return 1;
445 }
446 srv_send((char *)"241 Profile status follows:");
447
448
449 srv_send((char *)".");
450 return 1;
451
452 } else {
453 /*
454 * uuid,name rename profile
455 */
456 uuid = strtok(opt, ",");
457 param = strtok(NULL, "\0");
458 fprintf(stdout, "uuid: '%s' param: '%s'\n", uuid, param);
459 for (profile = Config.profiles; profile; profile = profile->next) {
460 if (strcmp(profile->uuid, uuid) == 0) {
461 syslog(LOG_NOTICE, "Profile %s rename from '%s' to '%s'", uuid, profile->name, param);
462 if (profile->name)
463 free(profile->name);
464 profile->name = xstrcpy(param);
465 srv_send((char *)"240 Profile updated");
466 return 0;
467 }
468 }
469 srv_send((char *)"440 No such profile");
470 return 1;
471 }
472
473 srv_send((char *)"502 Unknown command option");
474 return 1;
475 }
476
477
478
479 /*
413 * SET BEER float 480 * SET BEER float
414 * SET FRIDGE float 481 * SET FRIDGE float
415 * SET VOLUME float 482 * SET VOLUME float
416 * SET IDLE_LOW float 483 * SET IDLE_LOW float
417 * SET IDLE_HIGH float 484 * SET IDLE_HIGH float
422 */ 489 */
423 int cmd_set(char *buf) 490 int cmd_set(char *buf)
424 { 491 {
425 char *opt, *param; 492 char *opt, *param;
426 units_list *unit; 493 units_list *unit;
494 profiles_list *profile;
427 int rc; 495 int rc;
428 float fval; 496 float fval;
429 497
430 opt = strtok(buf, " \0"); 498 opt = strtok(buf, " \0");
431 opt = strtok(NULL, " \0"); 499 opt = strtok(NULL, " \0");
540 unit->name = xstrcpy(param); 608 unit->name = xstrcpy(param);
541 srv_send((char *)"214 Unit %s NAME set to '%s'", current_unit, param); 609 srv_send((char *)"214 Unit %s NAME set to '%s'", current_unit, param);
542 // TODO: change logfile name 610 // TODO: change logfile name
543 return 0; 611 return 0;
544 } else if (strcmp(opt, (char *)"PROFILE") == 0) { 612 } else if (strcmp(opt, (char *)"PROFILE") == 0) {
545 // check profile file, excists, type, at least one profile item 613 /*
614 * Check for active profile, already selected etc.
615 */
616 if (unit->profile && (unit->mode == UNITMODE_PROFILE)) {
617 srv_send((char *)"541 Cannot change profile while a profile is active");
618 return 1;
619 }
620 if (unit->profile && (strcmp(unit->profile, param) == 0)) {
621 srv_send((char *)"542 Profile already set");
622 return 1;
623 }
624 for (profile = Config.profiles; profile; profile = profile->next) {
625 if (strcmp(profile->uuid, param) == 0) {
626 if (unit->profile)
627 free(unit->profile);
628 unit->profile = xstrcpy(param);
629 srv_send((char *)"242 Unit profile changed to %s", param);
630 return 0;
631 }
632 }
633 srv_send((char *)"543 Invalid profile");
634 return 1;
546 } 635 }
547 636
548 srv_send((char *)"502 Unknown command option"); 637 srv_send((char *)"502 Unknown command option");
549 return 1; 638 return 1;
550 } 639 }
590 679
591 680
592 681
593 void cmd_server(void) 682 void cmd_server(void)
594 { 683 {
595 char *inp, *p, *q, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; 684 char buf[SS_BUFSIZE];
596 int i, rc, rlen; 685 int i, rlen;
597 socklen_t fromlen; 686 socklen_t fromlen;
598 float newtemp;
599 #ifdef HAVE_WIRINGPI_H 687 #ifdef HAVE_WIRINGPI_H
600 int j; 688 int j;
601 #endif 689 #endif
602 690
603 memset((char *)&buf, 0, SS_BUFSIZE); 691 memset((char *)&buf, 0, SS_BUFSIZE);
632 wrconfig(); 720 wrconfig();
633 } else if (strncmp(buf, "HELP", 4) == 0) { 721 } else if (strncmp(buf, "HELP", 4) == 0) {
634 srv_send((char *)"100 Help text follows"); 722 srv_send((char *)"100 Help text follows");
635 srv_send((char *)"Recognized commands:"); 723 srv_send((char *)"Recognized commands:");
636 srv_send((char *)""); 724 srv_send((char *)"");
637 srv_send((char *)"ADD PROFILE name Add a new profile with \"name\""); 725 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
638 srv_send((char *)"ADD UNIT name Add a new unit with \"name\""); 726 srv_send((char *)"ADD PROFILE name Add a new profile with \"name\"");
639 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); 727 srv_send((char *)"ADD UNIT name Add a new unit with \"name\"");
640 srv_send((char *)"LIST List all fermenter units"); 728 // srv_send((char *)"DEL PROFILE uuid Delete profile with uuid");
641 srv_send((char *)"LIST BUS List 1-wire bus"); 729 // srv_send((char *)"DEL UNIT uuid Delete unit with uuid");
642 srv_send((char *)"LIST LOG List logfile data in 1 hour lines"); 730 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)");
643 srv_send((char *)"LIST PROFILES List available profiles"); 731 srv_send((char *)"LIST List all fermenter units");
644 srv_send((char *)"LIST UNIT List fermenter unit"); 732 srv_send((char *)"LIST BUS List 1-wire bus");
733 srv_send((char *)"LIST LOG List logfile data in 1 hour lines");
734 srv_send((char *)"LIST PROFILES List available profiles");
735 srv_send((char *)"LIST UNIT List fermenter unit");
645 srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE"); 736 srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE");
646 // srv_send((char *)"PROFILE Profile status"); 737 // srv_send((char *)"PROFILE Profile status of current unit");
647 // srv_send((char *)"PROFILE start|stop|pause Profile start, stop or pause"); 738 srv_send((char *)"PROFILE uuid,name Profile rename");
648 srv_send((char *)"SET BEER val Set beer temperature"); 739 srv_send((char *)"SET BEER val Set beer temperature");
649 srv_send((char *)"SET FRIDGE val Set fridge temperature"); 740 srv_send((char *)"SET FRIDGE val Set fridge temperature");
650 srv_send((char *)"SET IDLE_LOW val Set idle temperature low (-5.0 .. -0.1)"); 741 srv_send((char *)"SET IDLE_LOW val Set idle temperature low (-5.0 .. -0.1)");
651 srv_send((char *)"SET IDLE_HIGH val Set idle temperature high (0.1 .. 5.0)"); 742 srv_send((char *)"SET IDLE_HIGH val Set idle temperature high (0.1 .. 5.0)");
652 srv_send((char *)"SET NAME name Set name or beername for the unit"); 743 srv_send((char *)"SET NAME name Set name or beername for the unit");
653 // srv_send((char *)"SET PROFILE uuid,name Set named profile"); 744 srv_send((char *)"SET PROFILE uuid Set unit profile");
654 srv_send((char *)"SET TEMP_MIN val Set unit minimum temperature"); 745 // srv_send((char *)"SET PROFILE start|stop|pause Profile start, stop or pause");
655 srv_send((char *)"SET TEMP_MAX val Set unit maximum temperature"); 746 srv_send((char *)"SET TEMP_MIN val Set unit minimum temperature");
656 srv_send((char *)"SET VOLUME val Set unit volume"); 747 srv_send((char *)"SET TEMP_MAX val Set unit maximum temperature");
657 srv_send((char *)"UNIT uuid Select unit by uuid"); 748 srv_send((char *)"SET VOLUME val Set unit volume");
749 srv_send((char *)"UNIT uuid Select unit by uuid");
658 srv_send((char *)"."); 750 srv_send((char *)".");
659 } else if (strncmp(buf, "LCD", 3) == 0) { 751 } else if (strncmp(buf, "LCD", 3) == 0) {
660 #ifdef HAVE_WIRINGPI_H 752 #ifdef HAVE_WIRINGPI_H
661 srv_send((char *)"201 information follows"); 753 srv_send((char *)"201 information follows");
662 for (j = 0; j < 4; j++) { 754 for (j = 0; j < 4; j++) {
672 #endif 764 #endif
673 } else if (strncmp(buf, "LIST", 4) == 0) { 765 } else if (strncmp(buf, "LIST", 4) == 0) {
674 cmd_list(buf); 766 cmd_list(buf);
675 } else if (strncmp(buf, "MODE", 4) == 0) { 767 } else if (strncmp(buf, "MODE", 4) == 0) {
676 cmd_mode(buf); 768 cmd_mode(buf);
769 } else if (strncmp(buf, "PROFILE", 7) == 0) {
770 if (cmd_profile(buf) == 0)
771 wrconfig();
677 } else if (strncmp(buf, "SET", 3) == 0) { 772 } else if (strncmp(buf, "SET", 3) == 0) {
678 if (cmd_set(buf) == 0) 773 if (cmd_set(buf) == 0)
679 wrconfig(); 774 wrconfig();
680 } else if (strncmp(buf, "UNIT", 4) == 0) { 775 } else if (strncmp(buf, "UNIT", 4) == 0) {
681 cmd_unit(buf); 776 cmd_unit(buf);
682
683 /*
684 * Now the old style (brewPi) commands. These will go away.
685 */
686 } else if (strncmp(buf, "ack", 3) == 0) {
687 srv_send((char *)"ack");
688 } else if (strncmp(buf, "lcd", 3) == 0) {
689 sprintf(obuf, "[\" \", \" \", \" \", \" \"]");
690 #ifdef HAVE_WIRINGPI_H
691 for (i = 0; i < 20; i++) {
692 obuf[i+2] = lcdbuf[lcdHandle][i][0];
693 obuf[i+26] = lcdbuf[lcdHandle][i][1];
694 obuf[i+50] = lcdbuf[lcdHandle][i][2];
695 obuf[i+74] = lcdbuf[lcdHandle][i][3];
696 }
697 #endif
698 srv_send(obuf);
699 } else if (strncmp(buf, "getMode", 7) == 0) {
700 srv_send("%c", UNITmode[Config.units->mode]);
701 } else if (strncmp(buf, "getFridge", 9) == 0) {
702 srv_send("%.1f", Config.units->fridge_set);
703 } else if (strncmp(buf, "getBeer", 7) == 0) {
704 srv_send("%.1f", Config.units->beer_set);
705 } else if (strncmp(buf, "getControlConstants", 19) == 0) {
706 srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }",
707 Config.tempFormat, Config.units->temp_set_min, Config.units->temp_set_max, Config.units->idle_rangeH, Config.units->idle_rangeL);
708 } else if (strncmp(buf, "getControlSettings", 18) == 0) {
709 srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }",
710 UNITmode[Config.units->mode], Config.units->beer_set, Config.units->fridge_set, cs_heatEstimator, cs_coolEstimator);
711 } else if (strncmp(buf, "getControlVariables", 19) == 0) {
712 srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff);
713 } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) {
714 // Does nothing anymore
715 srv_send("ack");
716 } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) {
717 // Does nothing anymore
718 srv_send("ack");
719 } else if (strncmp(buf, "setBeer=", 8) == 0) {
720 inp = xstrcpy(buf+8);
721 rc = sscanf(inp, "%f", &newtemp);
722 if (debug)
723 fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc);
724 if (rc == 1) {
725 if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) {
726 syslog(LOG_NOTICE, "Beer temperature set to %.1f degrees in web interface", newtemp);
727 srv_send("ack");
728 Config.units->mode = UNITMODE_BEER;
729 Config.units->beer_set = newtemp;
730 } else {
731 syslog(LOG_NOTICE, "Beer temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.units->temp_set_min, Config.units->temp_set_max);
732 srv_send("err");
733 }
734 } else {
735 syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp);
736 srv_send("err");
737 }
738 free(inp);
739 } else if (strncmp(buf, "setFridge=", 10) == 0) {
740 inp = xstrcpy(buf+10);
741 rc = sscanf(inp, "%f", &newtemp);
742 if (debug)
743 fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc);
744 if (rc == 1) {
745 if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) {
746 syslog(LOG_NOTICE, "Fridge temperature set to %.1f degrees in web interface", newtemp);
747 srv_send("ack");
748 Config.units->mode = UNITMODE_FRIDGE;
749 Config.units->fridge_set = newtemp;
750 } else {
751 syslog(LOG_NOTICE, "Fridge temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.units->temp_set_min, Config.units->temp_set_max);
752 srv_send("err");
753 }
754 } else {
755 syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp);
756 srv_send("err");
757 }
758 free(inp);
759 } else if (strncmp(buf, "setOff", 6) == 0) {
760 if (debug)
761 fprintf(stdout, "temperature control disabled\n");
762 syslog(LOG_NOTICE, "Notification: Temperature control disabled");
763 Config.units->mode = UNITMODE_OFF;
764 srv_send("ack");
765 } else if (strncmp(buf, "setParameters=", 14) == 0) {
766 inp = xstrcpy(buf+14); /* {"tempSetMax":30.5} */
767 if (debug)
768 fprintf(stdout, "setParameters: %s\n", inp);
769 strtok(inp, (char *)"\"");
770 p = strtok(NULL, (char *)"\"");
771 q = strtok(NULL, (char *)":,}");
772 if (strcmp(p, (char *)"tempSetMin") == 0) {
773 rc = sscanf(q, "%f", &newtemp);
774 if (rc == 1) {
775 syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp);
776 Config.units->temp_set_min = newtemp;
777 srv_send("ack");
778 } else {
779 srv_send("ERR");
780 }
781 } else if (strcmp(p, (char *)"tempSetMax") == 0) {
782 rc = sscanf(q, "%f", &newtemp);
783 if (rc == 1) {
784 syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp);
785 Config.units->temp_set_max = newtemp;
786 srv_send("ack");
787 } else {
788 srv_send("ERR");
789 }
790 } else {
791 fprintf(stdout, "p='%s' q='%s' inp='%s'\n", p, q, inp);
792 srv_send("ERR");
793 }
794 free(inp);
795 // stopScript
796 // quit
797 // eraseLogs
798 // interval
799 // startNewBrew
800 // pauseLogging
801 // stopLogging
802 // resumeLogging
803 // dateTimeFormatDisplay
804 } else if (strncmp(buf, "setActiveProfile", 16) == 0) {
805 syslog(LOG_NOTICE, "Setting profile '%s' as active profile", "undefined");
806 // programArduino
807 // refreshDeviceList
808 // getDeviceList
809 // applyDevice
810 } else { 777 } else {
811 if (debug) 778 if (debug)
812 fprintf(stdout, "unknown command \"%s\"\n", buf); 779 fprintf(stdout, "unknown command \"%s\"\n", buf);
813 srv_send((char *)"ERR"); 780 srv_send((char *)"500 Unknown command");
814 } 781 }
815 } 782 }
816 } 783 }
817 784
818 close(s); 785 close(s);
819 } 786 }
787
820 788
821 789
822 #ifdef HAVE_WIRINGPI_H 790 #ifdef HAVE_WIRINGPI_H
823 PI_THREAD (my_server_loop) 791 PI_THREAD (my_server_loop)
824 #else 792 #else

mercurial