thermferm/server.c

changeset 95
2c28afc329a5
parent 92
116226a8c70a
child 100
012576d7386d
equal deleted inserted replaced
94:38ee038f4cec 95:2c28afc329a5
30 extern int lcdHandle; 30 extern int lcdHandle;
31 extern unsigned char lcdbuf[MAX_LCDS][20][4]; 31 extern unsigned char lcdbuf[MAX_LCDS][20][4];
32 #endif 32 #endif
33 extern sys_config Config; 33 extern sys_config Config;
34 extern const char UNITMODE[5][8]; 34 extern const char UNITMODE[5][8];
35 extern const char UNITmode[5];
35 36
36 int s; /* connected socket */ 37 int s; /* connected socket */
37 int ls; /* listen socket */ 38 int ls; /* listen socket */
38 39
39 struct sockaddr_in myaddr_in; /* for local socket address */ 40 struct sockaddr_in myaddr_in; /* for local socket address */
43 44
44 #define SS_BUFSIZE 1024 45 #define SS_BUFSIZE 1024
45 #define SS_TIMEOUT 300 46 #define SS_TIMEOUT 300
46 47
47 48
49 float cs_heatEstimator = 0.2;
50 float cs_coolEstimator = 5;
48 float cv_beerDiff = 0.0; 51 float cv_beerDiff = 0.0;
49
50
51
52 void defaultControlSettings(void)
53 {
54 Config.cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */
55 Config.cs_beerSet = 20.0;
56 Config.cs_fridgeSet = 20.0;
57 Config.cs_heatEstimator = 0.2;
58 Config.cs_coolEstimator = 5;
59 }
60
61
62
63 void defaultControlConstants(void)
64 {
65 Config.tempFormat = 'C';
66 Config.cc_tempSetMin = 1.0;
67 Config.cc_tempSetMax = 30.0;
68 Config.cc_idleRangeH = 1.000;
69 Config.cc_idleRangeL = -1.000;
70 }
71 52
72 53
73 54
74 /* 55 /*
75 * Send message to client 56 * Send message to client
634 } else if (strncmp(buf, "SET", 3) == 0) { 615 } else if (strncmp(buf, "SET", 3) == 0) {
635 if (cmd_set(buf) == 0) 616 if (cmd_set(buf) == 0)
636 wrconfig(); 617 wrconfig();
637 } else if (strncmp(buf, "UNIT", 4) == 0) { 618 } else if (strncmp(buf, "UNIT", 4) == 0) {
638 cmd_unit(buf); 619 cmd_unit(buf);
620
621 /*
622 * Now the old style (brewPi) commands. These will go away.
623 */
639 } else if (strncmp(buf, "ack", 3) == 0) { 624 } else if (strncmp(buf, "ack", 3) == 0) {
640 srv_send((char *)"ack"); 625 srv_send((char *)"ack");
641 } else if (strncmp(buf, "lcd", 3) == 0) { 626 } else if (strncmp(buf, "lcd", 3) == 0) {
642 sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); 627 sprintf(obuf, "[\" \", \" \", \" \", \" \"]");
643 #ifdef HAVE_WIRINGPI_H 628 #ifdef HAVE_WIRINGPI_H
648 obuf[i+74] = lcdbuf[lcdHandle][i][3]; 633 obuf[i+74] = lcdbuf[lcdHandle][i][3];
649 } 634 }
650 #endif 635 #endif
651 srv_send(obuf); 636 srv_send(obuf);
652 } else if (strncmp(buf, "getMode", 7) == 0) { 637 } else if (strncmp(buf, "getMode", 7) == 0) {
653 srv_send("%c", Config.cs_mode); 638 srv_send("%c", UNITmode[Config.units->mode]);
654 } else if (strncmp(buf, "getFridge", 9) == 0) { 639 } else if (strncmp(buf, "getFridge", 9) == 0) {
655 srv_send("%.1f", Config.cs_fridgeSet); 640 srv_send("%.1f", Config.units->fridge_set);
656 } else if (strncmp(buf, "getBeer", 7) == 0) { 641 } else if (strncmp(buf, "getBeer", 7) == 0) {
657 srv_send("%.1f", Config.cs_beerSet); 642 srv_send("%.1f", Config.units->beer_set);
658 } else if (strncmp(buf, "getControlConstants", 19) == 0) { 643 } else if (strncmp(buf, "getControlConstants", 19) == 0) {
659 srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", 644 srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }",
660 Config.tempFormat, Config.cc_tempSetMin, Config.cc_tempSetMax, Config.cc_idleRangeH, Config.cc_idleRangeL ); 645 Config.tempFormat, Config.units->temp_set_min, Config.units->temp_set_max, Config.units->idle_rangeH, Config.units->idle_rangeL);
661 } else if (strncmp(buf, "getControlSettings", 18) == 0) { 646 } else if (strncmp(buf, "getControlSettings", 18) == 0) {
662 srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", 647 srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }",
663 Config.cs_mode, Config.cs_beerSet, Config.cs_fridgeSet, Config.cs_heatEstimator, Config.cs_coolEstimator); 648 UNITmode[Config.units->mode], Config.units->beer_set, Config.units->fridge_set, cs_heatEstimator, cs_coolEstimator);
664 } else if (strncmp(buf, "getControlVariables", 19) == 0) { 649 } else if (strncmp(buf, "getControlVariables", 19) == 0) {
665 srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); 650 srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff);
666 } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) { 651 } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) {
667 defaultControlSettings(); 652 // Does nothing anymore
668 srv_send("ack"); 653 srv_send("ack");
669 } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) { 654 } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) {
670 defaultControlConstants(); 655 // Does nothing anymore
671 srv_send("ack"); 656 srv_send("ack");
672 } else if (strncmp(buf, "setBeer=", 8) == 0) { 657 } else if (strncmp(buf, "setBeer=", 8) == 0) {
673 inp = xstrcpy(buf+8); 658 inp = xstrcpy(buf+8);
674 rc = sscanf(inp, "%f", &newtemp); 659 rc = sscanf(inp, "%f", &newtemp);
675 if (debug) 660 if (debug)
676 fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); 661 fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc);
677 if (rc == 1) { 662 if (rc == 1) {
678 if ((Config.cc_tempSetMin <= newtemp) && (newtemp <= Config.cc_tempSetMax)) { 663 if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) {
679 syslog(LOG_NOTICE, "Beer temperature set to %.1f degrees in web interface", newtemp); 664 syslog(LOG_NOTICE, "Beer temperature set to %.1f degrees in web interface", newtemp);
680 srv_send("ack"); 665 srv_send("ack");
681 Config.cs_mode = 'b'; 666 Config.units->mode = UNITMODE_BEER;
682 Config.cs_beerSet = newtemp; 667 Config.units->beer_set = newtemp;
683 } else { 668 } else {
684 syslog(LOG_NOTICE, "Beer temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.cc_tempSetMin, Config.cc_tempSetMax); 669 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);
685 srv_send("err"); 670 srv_send("err");
686 } 671 }
687 } else { 672 } else {
688 syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); 673 syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp);
689 srv_send("err"); 674 srv_send("err");
693 inp = xstrcpy(buf+10); 678 inp = xstrcpy(buf+10);
694 rc = sscanf(inp, "%f", &newtemp); 679 rc = sscanf(inp, "%f", &newtemp);
695 if (debug) 680 if (debug)
696 fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); 681 fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc);
697 if (rc == 1) { 682 if (rc == 1) {
698 if ((Config.cc_tempSetMin <= newtemp) && (newtemp <= Config.cc_tempSetMax)) { 683 if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) {
699 syslog(LOG_NOTICE, "Fridge temperature set to %.1f degrees in web interface", newtemp); 684 syslog(LOG_NOTICE, "Fridge temperature set to %.1f degrees in web interface", newtemp);
700 srv_send("ack"); 685 srv_send("ack");
701 Config.cs_mode = 'f'; 686 Config.units->mode = UNITMODE_FRIDGE;
702 Config.cs_fridgeSet = newtemp; 687 Config.units->fridge_set = newtemp;
703 } else { 688 } else {
704 syslog(LOG_NOTICE, "Fridge temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.cc_tempSetMin, Config.cc_tempSetMax); 689 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);
705 srv_send("err"); 690 srv_send("err");
706 } 691 }
707 } else { 692 } else {
708 syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); 693 syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp);
709 srv_send("err"); 694 srv_send("err");
711 free(inp); 696 free(inp);
712 } else if (strncmp(buf, "setOff", 6) == 0) { 697 } else if (strncmp(buf, "setOff", 6) == 0) {
713 if (debug) 698 if (debug)
714 fprintf(stdout, "temperature control disabled\n"); 699 fprintf(stdout, "temperature control disabled\n");
715 syslog(LOG_NOTICE, "Notification: Temperature control disabled"); 700 syslog(LOG_NOTICE, "Notification: Temperature control disabled");
716 Config.cs_mode = 'o'; 701 Config.units->mode = UNITMODE_OFF;
717 srv_send("ack"); 702 srv_send("ack");
718 } else if (strncmp(buf, "setParameters=", 14) == 0) { 703 } else if (strncmp(buf, "setParameters=", 14) == 0) {
719 inp = xstrcpy(buf+14); /* {"tempSetMax":30.5} */ 704 inp = xstrcpy(buf+14); /* {"tempSetMax":30.5} */
720 if (debug) 705 if (debug)
721 fprintf(stdout, "setParameters: %s\n", inp); 706 fprintf(stdout, "setParameters: %s\n", inp);
724 q = strtok(NULL, (char *)":,}"); 709 q = strtok(NULL, (char *)":,}");
725 if (strcmp(p, (char *)"tempSetMin") == 0) { 710 if (strcmp(p, (char *)"tempSetMin") == 0) {
726 rc = sscanf(q, "%f", &newtemp); 711 rc = sscanf(q, "%f", &newtemp);
727 if (rc == 1) { 712 if (rc == 1) {
728 syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp); 713 syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp);
729 Config.cc_tempSetMin = newtemp; 714 Config.units->temp_set_min = newtemp;
730 srv_send("ack"); 715 srv_send("ack");
731 } else { 716 } else {
732 srv_send("ERR"); 717 srv_send("ERR");
733 } 718 }
734 } else if (strcmp(p, (char *)"tempSetMax") == 0) { 719 } else if (strcmp(p, (char *)"tempSetMax") == 0) {
735 rc = sscanf(q, "%f", &newtemp); 720 rc = sscanf(q, "%f", &newtemp);
736 if (rc == 1) { 721 if (rc == 1) {
737 syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp); 722 syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp);
738 Config.cc_tempSetMax = newtemp; 723 Config.units->temp_set_max = newtemp;
739 srv_send("ack"); 724 srv_send("ack");
740 } else { 725 } else {
741 srv_send("ERR"); 726 srv_send("ERR");
742 } 727 }
743 } else { 728 } else {
766 srv_send((char *)"ERR"); 751 srv_send((char *)"ERR");
767 } 752 }
768 } 753 }
769 } 754 }
770 755
771 // if (debug) {
772 // syslog(LOG_NOTICE, "End connection from %s port %u", hostname, ntohs(peeraddr_in.sin_port));
773 // fprintf(stdout, "End connection from %s port %u\n", hostname, ntohs(peeraddr_in.sin_port));
774 // }
775
776 close(s); 756 close(s);
777 } 757 }
778 758
779 759
780 #ifdef HAVE_WIRINGPI_H 760 #ifdef HAVE_WIRINGPI_H

mercurial