thermferm/server.c

changeset 175
b73490398368
parent 174
244de612c572
child 176
8c7d87a2c094
equal deleted inserted replaced
174:244de612c572 175:b73490398368
39 extern const char UNITmode[5]; 39 extern const char UNITmode[5];
40 extern const char TEMPSTATE[3][8]; 40 extern const char TEMPSTATE[3][8];
41 extern const char DEVTYPE[7][6]; 41 extern const char DEVTYPE[7][6];
42 extern const char DEVPRESENT[4][6]; 42 extern const char DEVPRESENT[4][6];
43 extern const char DEVDIR[7][11]; 43 extern const char DEVDIR[7][11];
44 extern const char PROFSTATE[4][6];
44 45
45 46
46 int s; /* connected socket */ 47 int s; /* connected socket */
47 int ls; /* listen socket */ 48 int ls; /* listen socket */
48 49
96 97
97 98
98 99
99 /* 100 /*
100 * ADD PROFILE name 101 * ADD PROFILE name
101 * ADD UNIT name
102 */ 102 */
103 int unit_add(char *buf) 103 int unit_add(char *buf)
104 { 104 {
105 units_list *unit, *tmpu;
106 profiles_list *profile, *tmpp; 105 profiles_list *profile, *tmpp;
107 uuid_t uu; 106 uuid_t uu;
108 char *opt, *param; 107 char *opt, *param;
109 108
110 opt = strtok(buf, " \0"); 109 opt = strtok(buf, " \0");
146 } 145 }
147 146
148 syslog(LOG_NOTICE, "Profile \"%s\" with uuid %s added", param, profile->uuid); 147 syslog(LOG_NOTICE, "Profile \"%s\" with uuid %s added", param, profile->uuid);
149 srv_send((char *)"211 Profile \"%s\" with uuid %s added", param, profile->uuid); 148 srv_send((char *)"211 Profile \"%s\" with uuid %s added", param, profile->uuid);
150 return 0; 149 return 0;
151
152 } else if (strcmp(opt, (char *)"UNIT") == 0) {
153 unit = (units_list *)malloc(sizeof(units_list));
154 unit->next = NULL;
155 unit->version = 1;
156 unit->uuid = malloc(37);
157 uuid_generate(uu);
158 uuid_unparse(uu, unit->uuid);
159 if (current_unit)
160 free(current_unit);
161 current_unit = xstrcpy(unit->uuid);
162 unit->name = xstrcpy(param);
163 unit->air_address = unit->beer_address = unit->io1_address = unit->io2_address = unit->profile = NULL;
164 unit->volume = 0.0;
165 unit->air_state = unit->beer_state = 1;
166 unit->heater_available = unit->cooler_available = unit->fan_available = FALSE;
167 unit->air_temperature = unit->beer_temperature = 20000;
168 unit->beer_set = unit->fridge_set = 20.0;
169 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = unit->prof_state = 0;
170 unit->temp_set_min = 1.0;
171 unit->temp_set_max = 30.0;
172 unit->idle_rangeH = 1.0;
173 unit->idle_rangeL = -1.0;
174 unit->prof_started = (time_t)0;
175
176 if (Config.units == NULL) {
177 Config.units = unit;
178 } else {
179 for (tmpu = Config.units; tmpu; tmpu = tmpu->next) {
180 if (tmpu->next == NULL) {
181 tmpu->next = unit;
182 break;
183 }
184 }
185 }
186
187 syslog(LOG_NOTICE, "Unit with uuid %s added", unit->uuid);
188 srv_send((char *)"211 Unit with uuid %s added", unit->uuid);
189 return 0;
190 } 150 }
191 151
192 srv_send((char *)"502 Unknown command option"); 152 srv_send((char *)"502 Unknown command option");
193 return 1; 153 return 1;
194 } 154 }
199 void delete_Profile(char *uuid) 159 void delete_Profile(char *uuid)
200 { 160 {
201 profiles_list *current = Config.profiles; 161 profiles_list *current = Config.profiles;
202 profiles_list *previous = NULL; 162 profiles_list *previous = NULL;
203 prof_step *step, *olds; 163 prof_step *step, *olds;
204
205 164
206 while (current) { 165 while (current) {
207 if (strcmp(current->uuid, uuid) == 0) { 166 if (strcmp(current->uuid, uuid) == 0) {
208 if (previous == NULL) { 167 if (previous == NULL) {
209 Config.profiles = current->next; 168 Config.profiles = current->next;
349 param = strtok(NULL, "\0"); 308 param = strtok(NULL, "\0");
350 309
351 if (strcmp(opt, (char *)"LIST") == 0) { 310 if (strcmp(opt, (char *)"LIST") == 0) {
352 srv_send((char *)"212 bus devices:"); 311 srv_send((char *)"212 bus devices:");
353 for (device = Config.devices; device; device = device->next) { 312 for (device = Config.devices; device; device = device->next) {
354 srv_send((char *)"%s,%s,%d,%d,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment); 313 srv_send((char *)"%s,%s,%d,%d,%s,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment, DEVDIR[device->direction]);
355 } 314 }
356 srv_send((char *)"."); 315 srv_send((char *)".");
357 return 0; 316 return 0;
358 } 317 }
359 318
545 504
546 /* 505 /*
547 * LIST 506 * LIST
548 * LIST LOG 507 * LIST LOG
549 * LIST PROFILES 508 * LIST PROFILES
550 * LIST UNIT
551 */ 509 */
552 int cmd_list(char *buf) 510 int cmd_list(char *buf)
553 { 511 {
554 char *opt, *filename, *p, q[2], buffer[256]; 512 char *opt, *filename, *p, q[2], buffer[256];
555 units_list *unit; 513 units_list *unit;
622 for (profile = Config.profiles; profile; profile = profile->next) { 580 for (profile = Config.profiles; profile; profile = profile->next) {
623 j = 0; 581 j = 0;
624 for (step = profile->steps; step; step = step->next) 582 for (step = profile->steps; step; step = step->next)
625 j++; 583 j++;
626 srv_send((char *)"%s,%s,%d,%d", profile->uuid, profile->name, j, profile->busy); 584 srv_send((char *)"%s,%s,%d,%d", profile->uuid, profile->name, j, profile->busy);
627 }
628 srv_send((char *)".");
629 return 0;
630
631 } else if (strcmp(opt, (char *)"UNIT") == 0) {
632 /*
633 * List configured and selected fermenter unit
634 */
635 if (current_unit == NULL) {
636 srv_send((char *)"401 No fermenter unit selected");
637 return 1;
638 }
639
640 srv_send((char *)"213 Unit %s listing follows:", current_unit);
641 for (unit = Config.units; unit; unit = unit->next) {
642 if (strcmp(current_unit, unit->uuid) == 0) {
643 srv_send((char *)"NAME,%s", unit->name);
644 srv_send((char *)"UUID,%s", unit->uuid);
645 if (unit->air_address) {
646 srv_send((char *)"AIR_ADDRESS,%s", unit->air_address);
647 srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
648 srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0);
649 }
650 if (unit->beer_address) {
651 srv_send((char *)"BEER_ADDRESS,%s", unit->beer_address);
652 srv_send((char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]);
653 srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0);
654 }
655 if (unit->io1_address) {
656 srv_send((char *)"IO1_ADDRESS,%s", unit->io1_address);
657 }
658 if (unit->io2_address) {
659 srv_send((char *)"IO2_ADDRESS,%s", unit->io2_address);
660 }
661 if (unit->heater_available) {
662 srv_send((char *)"HEATER_AVAILABLE,%s", unit->heater_available);
663 srv_send((char *)"HEATER_STATE,%d", unit->heater_state);
664 }
665 if (unit->cooler_available) {
666 srv_send((char *)"COOLER_AVAILABLE,%s", unit->cooler_available);
667 srv_send((char *)"COOLER_STATE,%d", unit->cooler_state);
668 }
669 if (unit->fan_available) {
670 srv_send((char *)"FAN_AVAILABLE,%s", unit->fan_available);
671 srv_send((char *)"FAN_STATE,%d", unit->fan_state);
672 }
673 srv_send((char *)"MODE,%s", UNITMODE[unit->mode]);
674 srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set);
675 srv_send((char *)"BEER_SET,%.1f", unit->beer_set);
676 if (unit->profile) {
677 srv_send((char *)"PROFILE,%s", unit->profile);
678 }
679 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min);
680 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max);
681 srv_send((char *)"IDLE_RANGE_L,%.1f", unit->idle_rangeL);
682 srv_send((char *)"IDLE_RANGE_H,%.1f", unit->idle_rangeH);
683 }
684 } 585 }
685 srv_send((char *)"."); 586 srv_send((char *)".");
686 return 0; 587 return 0;
687 } 588 }
688 589
933 } 834 }
934 835
935 836
936 837
937 /* 838 /*
938 * SET BEER float
939 * SET FRIDGE float
940 * SET VOLUME float
941 * SET IDLE_LOW float
942 * SET IDLE_HIGH float
943 * SET TEMP_MIN float
944 * SET TEMP_MAX float
945 * SET NAME string
946 * SET PROFILE string 839 * SET PROFILE string
947 */ 840 */
948 int cmd_set(char *buf) 841 int cmd_set(char *buf)
949 { 842 {
950 char *opt, *param; 843 char *opt, *param;
982 for (unit = Config.units; unit; unit = unit->next) { 875 for (unit = Config.units; unit; unit = unit->next) {
983 if (strcmp(current_unit, unit->uuid) == 0) 876 if (strcmp(current_unit, unit->uuid) == 0)
984 break; 877 break;
985 } 878 }
986 879
987 /* 880 if (strcmp(opt, (char *)"PROFILE") == 0) {
988 * If a valid parameter float value
989 */
990 if (rc == 1) {
991 if (strcmp(opt, (char *)"BEER") == 0) {
992 if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) {
993 unit->beer_set = fval;
994 srv_send((char *)"214 Unit %s BEER set to %.1f", current_unit, fval);
995 return 0;
996 } else {
997 srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max);
998 return 1;
999 }
1000 } else if (strcmp(opt, (char *)"FRIDGE") == 0) {
1001 if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) {
1002 unit->fridge_set = fval;
1003 srv_send((char *)"214 Unit %s BEER set to %.1f", current_unit, fval);
1004 return 0;
1005 } else {
1006 srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max);
1007 return 1;
1008 }
1009 } else if (strcmp(opt, (char *)"VOLUME") == 0) {
1010 /*
1011 * Must fit in a 2 TEU container
1012 */
1013 if ((fval >= 0.0) && (fval <= 77020.0)) {
1014 unit->volume = fval;
1015 srv_send((char *)"214 Unit %s VOLUME set to %.1f", current_unit, fval);
1016 return 0;
1017 } else {
1018 srv_send((char *)"510 New volume not between 0 and 77020");
1019 return 1;
1020 }
1021 } else if (strcmp(opt, (char *)"IDLE_LOW") == 0) {
1022 if ((fval >= -5.0) && (fval <= -0.1)) {
1023 unit->idle_rangeL = fval;
1024 srv_send((char *)"214 Unit %s IDLE_LOW set to %.1f", current_unit, fval);
1025 return 0;
1026 } else {
1027 srv_send((char *)"510 New value not between -5.0 and -0.1");
1028 return 1;
1029 }
1030 } else if (strcmp(opt, (char *)"IDLE_HIGH") == 0) {
1031 if ((fval >= 0.1) && (fval <= 5.0)) {
1032 unit->idle_rangeH = fval;
1033 srv_send((char *)"214 Unit %s IDLE_HIGH set to %.1f", current_unit, fval);
1034 return 0;
1035 } else {
1036 srv_send((char *)"510 New value not between -5.0 and -0.1");
1037 return 1;
1038 }
1039 } else if (strcmp(opt, (char *)"TEMP_MIN") == 0) {
1040 if ((fval >= -2.0) && (fval <= 35.0) && (fval < unit->temp_set_max)) {
1041 unit->temp_set_min = fval;
1042 srv_send((char *)"214 Unit %s TEMP_MIN set to %.1f", current_unit, fval);
1043 return 0;
1044 } else {
1045 srv_send((char *)"510 New value not between -2.0 and 35.0 and lower then TEMP_MAX");
1046 return 1;
1047 }
1048 } else if (strcmp(opt, (char *)"TEMP_MAX") == 0) {
1049 if ((fval >= -2.0) && (fval <= 35.0) && (fval > unit->temp_set_min)) {
1050 unit->temp_set_max = fval;
1051 srv_send((char *)"214 Unit %s TEMP_MAX set to %.1f", current_unit, fval);
1052 return 0;
1053 } else {
1054 srv_send((char *)"510 New value not between -2.0 and 35.0 and higher then TEMP_MIN");
1055 return 1;
1056 }
1057 }
1058 }
1059
1060 /*
1061 * Set new unit or beer name
1062 */
1063 if (strcmp(opt, (char *)"NAME") == 0) {
1064 if (unit->name)
1065 free(unit->name);
1066 unit->name = xstrcpy(param);
1067 srv_send((char *)"214 Unit %s NAME set to '%s'", current_unit, param);
1068 // TODO: change logfile name
1069 return 0;
1070 } else if (strcmp(opt, (char *)"PROFILE") == 0) {
1071 /* 881 /*
1072 * Check for active profile, already selected etc. 882 * Check for active profile, already selected etc.
1073 */ 883 */
1074 if (unit->profile && (unit->mode == UNITMODE_PROFILE)) { 884 if (unit->profile && (unit->mode == UNITMODE_PROFILE)) {
1075 srv_send((char *)"541 Cannot change profile while a profile is active"); 885 srv_send((char *)"541 Cannot change profile while a profile is active");
1096 return 1; 906 return 1;
1097 } 907 }
1098 908
1099 909
1100 910
911 int delete_Unit(char *uuid)
912 {
913 units_list *current = Config.units;
914 units_list *previous = NULL;
915
916 while (current) {
917 if (strcmp(current->uuid, uuid) == 0) {
918 if (previous == NULL) {
919 Config.units = current->next;
920 free(current->uuid);
921 current->uuid = NULL;
922 free(current->name);
923 current->name = NULL;
924 if (current->air_address)
925 free(current->air_address);
926 current->air_address = NULL;
927 if (current->beer_address)
928 free(current->beer_address);
929 current->beer_address = NULL;
930 if (current->heater_address)
931 free(current->heater_address);
932 current->heater_address = NULL;
933 if (current->cooler_address)
934 free(current->cooler_address);
935 current->cooler_address = NULL;
936 if (current->fan_address)
937 free(current->fan_address);
938 current->fan_address = NULL;
939 if (current->door_address)
940 free(current->door_address);
941 current->door_address = NULL;
942 if (current->profile)
943 free(current->profile);
944 current->profile = NULL;
945 free(current);
946 return 1;
947 } else {
948 free(current->uuid);
949 current->uuid = NULL;
950 free(current->name);
951 current->name = NULL;
952 if (current->air_address)
953 free(current->air_address);
954 current->air_address = NULL;
955 if (current->beer_address)
956 free(current->beer_address);
957 current->beer_address = NULL;
958 if (current->heater_address)
959 free(current->heater_address);
960 current->heater_address = NULL;
961 if (current->cooler_address)
962 free(current->cooler_address);
963 current->cooler_address = NULL;
964 if (current->fan_address)
965 free(current->fan_address);
966 current->fan_address = NULL;
967 if (current->door_address)
968 free(current->door_address);
969 current->door_address = NULL;
970 if (current->profile)
971 free(current->profile);
972 current->profile = NULL;
973 free(current);
974 current = previous->next;
975 return 1;
976 }
977 } else {
978 previous = current;
979 current = current->next;
980 }
981 }
982 return 0;
983 }
984
985
986
1101 /* 987 /*
1102 * UNIT uuid 988 * UNIT uuid
989 * UNIT ADD name
990 * UNIT DEL uuid
991 * UNIT LIST
992 * UNIT GET uuid
993 * UNIT PUT uuid
1103 */ 994 */
1104 int cmd_unit(char *buf) 995 int cmd_unit(char *buf)
1105 { 996 {
1106 char *opt; 997 char *opt, *param = NULL, *kwd, *val, ibuf[SS_BUFSIZE];
1107 units_list *tmp; 998 units_list *unit, *tmpu;
999 uuid_t uu;
1000 socklen_t fromlen;
1001 int i, rlen;
1002 float fval;
1108 1003
1109 opt = strtok(buf, " \0"); 1004 opt = strtok(buf, " \0");
1110 opt = strtok(NULL, " \0"); 1005 opt = strtok(NULL, " \0");
1111 1006
1112 if (opt == NULL) { 1007 if (opt == NULL) {
1113 srv_send((char *)"501 Parameter missing"); 1008 srv_send((char *)"501 Parameter missing");
1114 return 1; 1009 return 1;
1115 } 1010 }
1116 1011 param = strtok(NULL, "\0");
1117 if (strlen(opt) == 36) { 1012
1013 /*
1014 * UNIT uuid
1015 */
1016 if ((strlen(opt) == 36) && (param == NULL)) {
1118 /* 1017 /*
1119 * Search using uuid 1018 * Search using uuid
1120 */ 1019 */
1121 for (tmp = Config.units; tmp; tmp = tmp->next) { 1020 for (unit = Config.units; unit; unit = unit->next) {
1122 if (strcmp(opt, tmp->uuid) == 0) { 1021 if (strcmp(opt, unit->uuid) == 0) {
1123 srv_send((char *)"210 Unit %s selected", tmp->uuid); 1022 srv_send((char *)"210 Unit %s selected", unit->uuid);
1124 if (current_unit) 1023 if (current_unit)
1125 free(current_unit); 1024 free(current_unit);
1126 current_unit = xstrcpy(tmp->uuid);; 1025 current_unit = xstrcpy(unit->uuid);;
1127 return 0; 1026 return 1;
1128 } 1027 }
1129 } 1028 }
1130 srv_send((char *)"410 No such unit"); 1029 srv_send((char *)"410 No such unit");
1131 return 1; 1030 return 1;
1132 } 1031 }
1032
1033 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) {
1034 srv_send((char *)"212 Fermenter Units list follows:");
1035 for (unit = Config.units; unit; unit = unit->next) {
1036 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
1037 }
1038 srv_send((char *)".");
1039 return 1;
1040 }
1041
1042 if (param == NULL) {
1043 srv_send((char *)"501 Parameter missing");
1044 return 1;
1045 }
1046
1047 if (strcmp(opt, (char *)"ADD") == 0) {
1048 unit = (units_list *)malloc(sizeof(units_list));
1049 unit->next = NULL;
1050 unit->version = 1;
1051 unit->uuid = malloc(37);
1052 uuid_generate(uu);
1053 uuid_unparse(uu, unit->uuid);
1054 if (current_unit)
1055 free(current_unit);
1056 current_unit = xstrcpy(unit->uuid);
1057 unit->name = xstrcpy(param);
1058 unit->air_address = unit->beer_address = unit->heater_address = unit->cooler_address = \
1059 unit->fan_address = unit->door_address = unit->profile = NULL;
1060 unit->volume = 0.0;
1061 unit->air_state = unit->beer_state = 1;
1062 unit->air_temperature = unit->beer_temperature = 20000;
1063 unit->beer_set = unit->fridge_set = 20.0;
1064 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = unit->prof_state = 0;
1065 unit->temp_set_min = 1.0;
1066 unit->temp_set_max = 30.0;
1067 unit->idle_rangeH = 1.0;
1068 unit->idle_rangeL = -1.0;
1069 unit->prof_started = (time_t)0;
1070
1071 if (Config.units == NULL) {
1072 Config.units = unit;
1073 } else {
1074 for (tmpu = Config.units; tmpu; tmpu = tmpu->next) {
1075 if (tmpu->next == NULL) {
1076 tmpu->next = unit;
1077 break;
1078 }
1079 }
1080 }
1081
1082 syslog(LOG_NOTICE, "Unit with uuid %s added", unit->uuid);
1083 srv_send((char *)"211 Unit with uuid %s added", unit->uuid);
1084 return 0;
1085 }
1086
1087 if (strcmp(opt, (char *)"DEL") == 0) {
1088 if (delete_Unit(param)) {
1089 syslog(LOG_NOTICE, "Deleted Unit with %s", param);
1090 srv_send((char *)"211 Unit %s deleted", param);
1091 return 0;
1092 } else {
1093 srv_send((char *)"440 Delete Unit: No such unit %s", param);
1094 return 1;
1095 }
1096 }
1097
1098 if (strcmp(opt, (char *)"GET") == 0) {
1099 for (unit = Config.units; unit; unit = unit->next) {
1100 if (strcmp(param, unit->uuid) == 0) {
1101 srv_send((char *)"213 Unit %s listing follows:", unit->uuid);
1102 srv_send((char *)"NAME,%s", unit->name);
1103 srv_send((char *)"UUID,%s", unit->uuid);
1104 srv_send((char *)"VOLUME,%2f", unit->volume);
1105 srv_send((char *)"AIR_ADDRESS,%s", unit->air_address);
1106 srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
1107 srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0);
1108 srv_send((char *)"BEER_ADDRESS,%s", MBSE_SS(unit->beer_address));
1109 srv_send((char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]);
1110 srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0);
1111 srv_send((char *)"HEATER_ADDRESS,%s", unit->heater_address);
1112 srv_send((char *)"HEATER_STATE,%d", unit->heater_state);
1113 srv_send((char *)"COOLER_ADDRESS,%s", unit->cooler_address);
1114 srv_send((char *)"COOLER_STATE,%d", unit->cooler_state);
1115 srv_send((char *)"FAN_ADDRESS,%s", unit->fan_address);
1116 srv_send((char *)"FAN_STATE,%d", unit->fan_state);
1117 srv_send((char *)"DOOR_ADDRESS,%s", unit->door_address);
1118 srv_send((char *)"DOOR_STATE,%d", unit->door_state);
1119 srv_send((char *)"MODE,%s", UNITMODE[unit->mode]);
1120 srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set);
1121 srv_send((char *)"BEER_SET,%.1f", unit->beer_set);
1122 srv_send((char *)"PROFILE,%s", unit->profile);
1123 if (unit->profile) {
1124 srv_send((char *)"PROF_STARTED,%d", (int)unit->prof_started);
1125 srv_send((char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]);
1126 srv_send((char *)"PROF_TARGET,%.3f", unit->prof_target);
1127 }
1128 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min);
1129 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max);
1130 srv_send((char *)"IDLE_RANGE_L,%.1f", unit->idle_rangeL);
1131 srv_send((char *)"IDLE_RANGE_H,%.1f", unit->idle_rangeH);
1132 srv_send((char *)".");
1133 return 1;
1134 }
1135 }
1136 srv_send((char *)"410 No such unit");
1137 return 1;
1138 }
1139
1140 if (strcmp(opt, (char *)"PUT") == 0) {
1141 for (unit = Config.units ; unit; unit = unit->next) {
1142 if (strcmp(unit->uuid, param) == 0) {
1143 while (1) {
1144 memset((char *)&ibuf, 0, SS_BUFSIZE);
1145 fromlen = sizeof(peeraddr_in);
1146 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
1147 if (rlen == -1) {
1148 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
1149 srv_send((char *)"518 recfrom(): %s", strerror(errno));
1150 return 1;
1151 }
1152 for (i = 0; i < strlen(ibuf); i++) {
1153 if (ibuf[i] == '\n')
1154 ibuf[i] = '\0';
1155 if (ibuf[i] == '\r')
1156 ibuf[i] = '\0';
1157 }
1158 for (i = strlen(ibuf) -1; i > 0; i--) {
1159 if (ibuf[i] == ' ')
1160 ibuf[i] = '\0';
1161 else
1162 break;
1163 }
1164 if (strlen(ibuf)) {
1165 if (debug) {
1166 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
1167 fprintf(stdout, "recv: \"%s\"\n", ibuf);
1168 }
1169 if (strcmp(ibuf, (char *)".") == 0) {
1170 srv_send((char *)"219 Accepted Unit record");
1171 return 0;
1172 }
1173 kwd = strtok(ibuf, ",\0");
1174 val = strtok(NULL, "\0");
1175 if (kwd && val) {
1176 /*
1177 * Accept writable data. The client can sent just one line,
1178 * but may also sent everything. Simply ignore things we
1179 * don't understand.
1180 */
1181 if (strcmp(kwd, (char *)"NAME") == 0) {
1182 if (unit->name)
1183 free(unit->name);
1184 unit->name = xstrcpy(val);
1185
1186 } else if (strcmp(kwd, (char *)"VOLUME") == 0) {
1187 if (sscanf(val, "%f", &fval) == 1)
1188 unit->volume = fval;
1189
1190 } else if (strcmp(kwd, (char *)"AIR_ADDRESS") == 0) {
1191 if (unit->air_address)
1192 free(unit->air_address);
1193 unit->air_address = xstrcpy(val);
1194
1195 } else if (strcmp(kwd, (char *)"BEER_ADDRESS") == 0) {
1196 if (unit->beer_address)
1197 free(unit->beer_address);
1198 unit->beer_address = xstrcpy(val);
1199
1200 } else if (strcmp(kwd, (char *)"HEATER_ADDRESS") == 0) {
1201 if (unit->heater_address)
1202 free(unit->heater_address);
1203 unit->heater_address = xstrcpy(val);
1204
1205 } else if (strcmp(kwd, (char *)"COOLER_ADDRESS") == 0) {
1206 if (unit->cooler_address)
1207 free(unit->cooler_address);
1208 unit->cooler_address = xstrcpy(val);
1209
1210 } else if (strcmp(kwd, (char *)"FAN_ADDRESS") == 0) {
1211 if (unit->fan_address)
1212 free(unit->fan_address);
1213 unit->fan_address = xstrcpy(val);
1214
1215 } else if (strcmp(kwd, (char *)"DOOR_ADDRESS") == 0) {
1216 if (unit->door_address)
1217 free(unit->door_address);
1218 unit->door_address = xstrcpy(val);
1219
1220 } else if (strcmp(kwd, (char *)"MODE") == 0) {
1221 for (i = 0; i < 5; i++) {
1222 if (strcmp(val, UNITMODE[i]) == 0) {
1223 unit->mode = i;
1224 break;
1225 }
1226 }
1227
1228 } else if (strcmp(kwd, (char *)"FRIDGE_SET") == 0) {
1229 if (sscanf(val, "%f", &fval) == 1)
1230 unit->fridge_set = fval;
1231
1232 } else if (strcmp(kwd, (char *)"BEER_SET") == 0) {
1233 if (sscanf(val, "%f", &fval) == 1)
1234 unit->beer_set = fval;
1235
1236 } else if (strcmp(kwd, (char *)"PROFILE") == 0) {
1237 if (unit->profile)
1238 free(unit->profile);
1239 unit->profile = xstrcpy(val);
1240
1241 } else if (strcmp(kwd, (char *)"PROF_STATE") == 0) {
1242 for (i = 0; i < 4; i++) {
1243 if (strcmp(val, PROFSTATE[i]) == 0) {
1244 unit->prof_state = i;
1245 break;
1246 }
1247 }
1248
1249 } else if (strcmp(kwd, (char *)"TEMP_SET_MIN") == 0) {
1250 if (sscanf(val, "%f", &fval) == 1)
1251 unit->temp_set_min = fval;
1252
1253 } else if (strcmp(kwd, (char *)"TEMP_SET_MAX") == 0) {
1254 if (sscanf(val, "%f", &fval) == 1)
1255 unit->temp_set_max = fval;
1256
1257 } else if (strcmp(kwd, (char *)"IDLE_RANGE_L") == 0) {
1258 if (sscanf(val, "%f", &fval) == 1)
1259 unit->idle_rangeL = fval;
1260
1261 } else if (strcmp(kwd, (char *)"IDLE_RANGE_H") == 0) {
1262 if (sscanf(val, "%f", &fval) == 1)
1263 unit->idle_rangeH = fval;
1264
1265 }
1266 }
1267 }
1268 }
1269 }
1270 }
1271 srv_send((char *)"440 No such device");
1272 return 1;
1273 }
1274
1133 1275
1134 srv_send((char *)"502 Unknown command option"); 1276 srv_send((char *)"502 Unknown command option");
1135 return 1; 1277 return 1;
1136 } 1278 }
1137 1279
1187 srv_send((char *)"100 Help text follows"); 1329 srv_send((char *)"100 Help text follows");
1188 srv_send((char *)"Recognized commands:"); 1330 srv_send((char *)"Recognized commands:");
1189 srv_send((char *)""); 1331 srv_send((char *)"");
1190 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 1332 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
1191 srv_send((char *)"ADD PROFILE name Add a new profile with \"name\""); 1333 srv_send((char *)"ADD PROFILE name Add a new profile with \"name\"");
1192 srv_send((char *)"ADD UNIT name Add a new unit with \"name\"");
1193 srv_send((char *)"DEL PROFILE uuid Delete profile with uuid"); 1334 srv_send((char *)"DEL PROFILE uuid Delete profile with uuid");
1194 // srv_send((char *)"DEL UNIT uuid Delete unit with uuid");
1195 srv_send((char *)"DEVICE ADD type Add new Device type"); 1335 srv_send((char *)"DEVICE ADD type Add new Device type");
1196 // srv_send((char *)"DEVICE DEL uuid Delete Device by uuid"); 1336 srv_send((char *)"DEVICE DEL uuid Delete Device by uuid");
1197 srv_send((char *)"DEVICE LIST List Devices"); 1337 srv_send((char *)"DEVICE LIST List Devices");
1198 srv_send((char *)"DEVICE GET uuid Get Device record by uuid"); 1338 srv_send((char *)"DEVICE GET uuid Get Device record by uuid");
1199 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid"); 1339 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid");
1200 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); 1340 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)");
1201 srv_send((char *)"LIST List all fermenter units"); 1341 srv_send((char *)"LIST List all fermenter units");
1202 srv_send((char *)"LIST LOG List logfile data in 1 hour lines"); 1342 srv_send((char *)"LIST LOG List logfile data in 1 hour lines");
1203 srv_send((char *)"LIST PROFILES List available profiles"); 1343 srv_send((char *)"LIST PROFILES List available profiles");
1204 srv_send((char *)"LIST UNIT List fermenter unit");
1205 srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE"); 1344 srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE");
1206 // srv_send((char *)"PROFILE Profile status of current unit"); 1345 // srv_send((char *)"PROFILE Profile status of current unit");
1207 srv_send((char *)"PROFILE uuid,name Profile rename"); 1346 srv_send((char *)"PROFILE uuid,name Profile rename");
1208 srv_send((char *)"PROFILE GETS uuid Profile get steps list"); 1347 srv_send((char *)"PROFILE GETS uuid Profile get steps list");
1209 srv_send((char *)"PROFILE PUTS uuid Profile put steps list"); 1348 srv_send((char *)"PROFILE PUTS uuid Profile put steps list");
1210 srv_send((char *)"SET BEER val Set beer temperature");
1211 srv_send((char *)"SET FRIDGE val Set fridge temperature");
1212 srv_send((char *)"SET IDLE_LOW val Set idle temperature low (-5.0 .. -0.1)");
1213 srv_send((char *)"SET IDLE_HIGH val Set idle temperature high (0.1 .. 5.0)");
1214 srv_send((char *)"SET NAME name Set name or beername for the unit");
1215 srv_send((char *)"SET PROFILE uuid Set unit profile"); 1349 srv_send((char *)"SET PROFILE uuid Set unit profile");
1216 // srv_send((char *)"SET PROFILE start|stop|pause Profile start, stop or pause"); 1350 // srv_send((char *)"SET PROFILE start|stop|pause Profile start, stop or pause");
1217 srv_send((char *)"SET TEMP_MIN val Set unit minimum temperature");
1218 srv_send((char *)"SET TEMP_MAX val Set unit maximum temperature");
1219 srv_send((char *)"SET VOLUME val Set unit volume"); 1351 srv_send((char *)"SET VOLUME val Set unit volume");
1220 srv_send((char *)"UNIT uuid Select unit by uuid"); 1352 srv_send((char *)"UNIT uuid Select unit by uuid");
1353 srv_send((char *)"UNIT ADD name Add a new unit with name");
1354 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid");
1355 srv_send((char *)"UNIT LIST List Units");
1356 srv_send((char *)"UNIT GET uuid Get Unit record by uuid");
1357 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid");
1221 srv_send((char *)"."); 1358 srv_send((char *)".");
1222 } else if (strncmp(buf, "LCD", 3) == 0) { 1359 } else if (strncmp(buf, "LCD", 3) == 0) {
1223 #ifdef HAVE_WIRINGPI_H 1360 #ifdef HAVE_WIRINGPI_H
1224 srv_send((char *)"201 information follows"); 1361 srv_send((char *)"201 information follows");
1225 for (j = 0; j < 4; j++) { 1362 for (j = 0; j < 4; j++) {
1243 wrconfig(); 1380 wrconfig();
1244 } else if (strncmp(buf, "SET", 3) == 0) { 1381 } else if (strncmp(buf, "SET", 3) == 0) {
1245 if (cmd_set(buf) == 0) 1382 if (cmd_set(buf) == 0)
1246 wrconfig(); 1383 wrconfig();
1247 } else if (strncmp(buf, "UNIT", 4) == 0) { 1384 } else if (strncmp(buf, "UNIT", 4) == 0) {
1248 cmd_unit(buf); 1385 if (cmd_unit(buf) == 0)
1386 wrconfig();
1249 } else { 1387 } else {
1250 if (debug) 1388 if (debug)
1251 fprintf(stdout, "unknown command \"%s\"\n", buf); 1389 fprintf(stdout, "unknown command \"%s\"\n", buf);
1252 srv_send((char *)"500 Unknown command"); 1390 srv_send((char *)"500 Unknown command");
1253 } 1391 }

mercurial