Thu, 31 Jul 2014 23:39:11 +0200
Threads partly working via new devices interface
93 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with EC-65K; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include "thermferm.h" | |
106 | 24 | #include "units.h" |
25 | #include "xutil.h" | |
93 | 26 | |
27 | ||
28 | extern int debug; | |
29 | extern sys_config Config; | |
30 | extern int my_shutdown; | |
31 | ||
32 | extern const char UNITMODE[5][8]; | |
33 | ||
34 | ||
35 | ||
36 | int read_w1_28(char *address, int *val) | |
37 | { | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
38 | devices_list *device; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
39 | int tmp; |
93 | 40 | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
41 | for (device = Config.devices; device; device = device->next) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
42 | if (strcmp(address, device->uuid) == 0) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
43 | tmp = device->value; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
44 | *val = tmp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
45 | return device->present; |
93 | 46 | } |
47 | } | |
48 | ||
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
49 | return 1; |
93 | 50 | } |
51 | ||
52 | ||
53 | ||
54 | #ifdef HAVE_WIRINGPI_H | |
55 | PI_THREAD (my_units_loop) | |
56 | #else | |
57 | void *my_units_loop(void *threadid) | |
58 | #endif | |
59 | { | |
60 | units_list *unit; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
61 | int rc, temp, deviation; |
93 | 62 | |
63 | /* | |
64 | * Initialize units for processing | |
65 | */ | |
66 | for (unit = Config.units; unit; unit = unit->next) { | |
67 | /* | |
68 | * Safety, turn everything off | |
69 | */ | |
70 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
71 | if (unit->profile && (int)unit->prof_started && (unit->mode == UNITMODE_PROFILE)) { | |
72 | syslog(LOG_NOTICE, "Starting unit %s profile %s", unit->name, unit->profile); | |
73 | } else if (unit->mode == UNITMODE_BEER) { | |
74 | syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); | |
75 | } else if (unit->mode == UNITMODE_FRIDGE) { | |
76 | syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); | |
77 | } else if (unit->mode == UNITMODE_NONE) { | |
78 | syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); | |
79 | } else { | |
80 | syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); | |
81 | } | |
82 | } | |
83 | ||
84 | syslog(LOG_NOTICE, "Thread my_units_loop started"); | |
85 | if (debug) | |
86 | fprintf(stdout, "Thread my_units_loop started\n"); | |
87 | ||
88 | /* | |
89 | * Loop forever until the external shutdown variable is set. | |
90 | */ | |
91 | for (;;) { | |
92 | ||
93 | if (my_shutdown) | |
94 | break; | |
95 | ||
96 | /* | |
97 | * Update the state of all fermenter units | |
98 | */ | |
99 | for (unit = Config.units; unit; unit = unit->next) { | |
100 | ||
101 | if (my_shutdown) | |
102 | break; | |
103 | ||
104 | if (unit->air_address) { | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
105 | rc = read_w1_28(unit->air_address, &temp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
106 | if (rc == 2) { |
93 | 107 | /* |
108 | * It is possible to have read errors or extreme values. | |
109 | * This can happen with bad connections so we compare the | |
110 | * value with the previous one. If the difference is too | |
111 | * much, we don't send that value. That also means that if | |
112 | * the next value is ok again, it will be marked invalid too. | |
113 | * Maximum error is 20 degrees for now. | |
114 | */ | |
115 | deviation = 20000; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
116 | if ((unit->air_temperature == 0) || |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
117 | (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
118 | unit->air_temperature = temp; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
119 | unit->air_state = 0; |
93 | 120 | } else { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
121 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); |
93 | 122 | if (debug) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
123 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); |
93 | 124 | } |
125 | } | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
126 | } else if (rc == 3) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
127 | unit->air_state = 1; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
128 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
129 | unit->air_state = 2; |
93 | 130 | } |
131 | } | |
132 | if (my_shutdown) | |
133 | break; | |
134 | ||
135 | if (unit->beer_address) { | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
136 | rc = read_w1_28(unit->beer_address, &temp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
137 | if (rc == 2) { |
93 | 138 | deviation = 20000; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
139 | if ((unit->beer_temperature == 0) || |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
140 | (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
141 | unit->beer_temperature = temp; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
142 | unit->beer_state = 0; |
93 | 143 | } else { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
144 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); |
93 | 145 | if (debug) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
146 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); |
93 | 147 | } |
148 | } | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
149 | } else if (rc == 3) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
150 | unit->beer_state = 1; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
151 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
152 | unit->beer_state = 2; |
93 | 153 | } |
154 | } | |
155 | if (my_shutdown) | |
156 | break; | |
157 | ||
158 | ||
159 | } | |
160 | usleep(10000); | |
161 | } | |
162 | ||
163 | /* | |
164 | * Stop units processing in a neat way | |
165 | */ | |
166 | for (unit = Config.units; unit; unit = unit->next) { | |
167 | /* | |
168 | * Turn everything off | |
169 | */ | |
170 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
171 | syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); | |
172 | } | |
173 | ||
174 | syslog(LOG_NOTICE, "Thread my_units_loop stopped"); | |
175 | if (debug) | |
176 | fprintf(stdout, "Thread my_units_loop stopped\n"); | |
177 | return 0; | |
178 | } | |
179 | ||
180 |