8b9c14c63c616d247d641e459ae1ee015b5ff8b6
1 /**
2 * collectd - src/ted.c
3 * Copyright (C) 2005,2006 Peter Holik
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Eric Reed <ericr at reedhome.net>
21 *
22 * This is a collectd module for The Energy Detective: A low-cost whole
23 * house energy monitoring system. For more information on TED, see
24 * http://theenergydetective.com
25 *
26 * This module was not created by Energy, Inc. nor is it supported by
27 * them in any way. It was created using information from two sources:
28 * David Satterfield's TED module for Misterhouse, and Micah Dowty's TED
29 * Python Module.
30 *
31 * This has only tested with the model 1001 RDU, with
32 * firmware version 9.01U. The USB port is uses the very common FTDI
33 * USB-to-serial chip, so the RDU will show up as a serial device on
34 * Windows, Mac OS, or Linux.
35 **/
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40 #include "configfile.h"
42 #if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
43 # include <termios.h>
44 # include <sys/ioctl.h>
45 # include <math.h>
46 #else
47 # error "No applicable input method."
48 #endif
52 #define LINE_LENGTH 282
53 #define PKT_REQUEST "\xAA"
54 #define ESCAPE 0x10
55 #define PKT_BEGIN 0x04
56 #define PKT_END 0x03
58 #define DEFAULT_DEVICE "/dev/ttyUSB"
59 #define CLIENT_LIST_PREFIX "CLIENT_LIST,"
61 static char *device = NULL;
62 static int fd = -1;
64 static const char *config_keys[] = { "Device" };
65 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70 static int ted_read_value(double *kv, double *voltage)
71 {
72 int retry = 3; /* sometimes we receive garbadge */
74 do
75 {
76 struct timeval time_end;
78 tcflush(fd, TCIFLUSH);
80 if (gettimeofday (&time_end, NULL) < 0)
81 {
82 char errbuf[1024];
83 ERROR ("ted plugin: gettimeofday failed: %s",
84 sstrerror (errno, errbuf,
85 sizeof (errbuf)));
86 return (-1);
87 }
88 time_end.tv_sec++;
90 while (1)
91 {
92 unsigned char buf[4096];
93 unsigned char package_buffer[4096];
94 char sResultByte;
95 char sCmd[1];
96 int status;
97 int byte;
98 int package_length=-1;
99 int start_flag=0;
100 int escape_flag=0;
101 struct timeval timeout;
102 struct timeval time_now;
103 sCmd[0] = 0xAA;
105 status = write (fd, sCmd, 1);
106 INFO ("status of write %d",status);
107 if (status < 0)
108 {
109 ERROR ("ted plugin: swrite failed.");
110 return (-1);
111 }
114 if (gettimeofday (&time_now, NULL) < 0)
115 {
116 char errbuf[1024];
117 ERROR ("ted plugin: "
118 "gettimeofday failed: %s",
119 sstrerror (errno, errbuf,
120 sizeof (errbuf)));
121 return (-1);
122 }
123 /*if (timeval_cmp (time_end, time_now, &timeout) < 0)
124 break; */
126 usleep(700000);
127 status = select(fd+1, NULL, NULL, NULL, &timeout);
128 INFO ("status 1 %d",status);
129 status = 1;
133 if (status > 0) /* usually we succeed */
134 {
135 status = read(fd, buf, 4096);
136 INFO ("status of read %d",status);
138 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
139 continue;
141 for (byte=0; byte< status; byte++) {
142 sResultByte = buf[byte];
143 if (escape_flag) {
144 escape_flag = 0;
145 if ((sResultByte==ESCAPE) & (package_length > 0)){
146 package_buffer[package_length] = ESCAPE;
147 package_length++;
148 }
149 else if (sResultByte==PKT_BEGIN){
150 start_flag = 1;
151 package_length=0;
152 }
153 else if (sResultByte==PKT_END){
154 package_buffer[package_length] = '\0';
155 package_length++;
156 }
157 }
158 else if (sResultByte == ESCAPE)
159 escape_flag = 1;
160 else if (package_length >= 0){
161 package_buffer[package_length] = sResultByte;
162 package_length++;
163 }
165 }
167 INFO ("read package_length %d",package_length);
169 if (package_length == 279)
170 {
171 *kv = ((package_buffer[248] * 256) + package_buffer[247])*10.0;
172 INFO ("kv %f",*kv);
173 *voltage = ((package_buffer[252] * 256) + package_buffer[251])/10.0;
174 INFO ("voltage %f",*voltage);
175 return (0); /* value received */
176 }
177 else
178 INFO ("Not the correct package");
179 usleep(700000);
180 continue;
181 //return (-1); /* Not pro package */
182 }
183 else if (!status) /* Timeout */
184 {
185 break;
186 }
187 else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
188 {
189 usleep(700000);
190 continue;
191 }
192 else /* status == -1 */
193 {
194 char errbuf[1024];
195 ERROR ("ted plugin: "
196 "select failed: %s",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
198 break;
199 }
200 }
201 } while (--retry);
203 return (-2); /* no value received */
204 } /* int ted_read_value */
206 static int ted_init (void)
207 {
208 int i;
209 int status;
210 //char device[] = "/dev/ttyUSB ";
211 char sCmd[1];
213 char buf[4096];
214 sCmd[0] = 0xAA;
216 if (device == NULL)
217 device = DEFAULT_DEVICE;
219 for (i = 0; i < 10; i++)
220 {
221 device[strlen(device)-1] = i + '0';
223 if ((fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) > 0)
224 {
225 struct termios options;
226 // Get the current options for the port...
227 tcgetattr(fd, &options);
228 options.c_cflag = B19200 | CS8 | CSTOPB | CREAD | CLOCAL;
229 options.c_iflag = IGNBRK | IGNPAR;
230 options.c_oflag = 0;
231 options.c_lflag = 0;
232 options.c_cc[VTIME] = 3;
233 options.c_cc[VMIN] = 50;
235 // Set the new options for the port...
236 tcflush(fd, TCIFLUSH);
237 tcsetattr(fd, TCSANOW, &options);
239 status = swrite (fd, sCmd, 1);
240 if (status < 0)
241 continue;
242 usleep(900000);
243 status = read(fd, buf, 4096);
244 if (status < 0)
245 continue;
246 INFO ("status of read %d",status);
247 INFO ("length of read %d", strlen(buf));
249 INFO ("ted plugin: Device "
250 "found at %s", device);
251 return (0);
253 }
254 }
256 ERROR ("ted plugin: No device found");
257 return (-1);
258 }
259 #undef LINE_LENGTH
261 static void ted_submit (char *type_instance, double value)
262 {
263 value_t values[1];
264 value_list_t vl = VALUE_LIST_INIT;
266 values[0].gauge = value;
268 vl.values = values;
269 vl.values_len = 1;
270 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
271 sstrncpy (vl.plugin, "ted", sizeof (vl.plugin));
272 sstrncpy (vl.type, "ted", sizeof (vl.type));
273 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
275 plugin_dispatch_values (&vl);
276 }
278 static int ted_config (const char *key, const char *value)
279 {
280 if (strcasecmp ("Device", key) == 0)
281 {
282 sfree (device);
283 device = sstrdup (value);
284 }
285 else
286 {
287 return (-1);
288 }
289 return (0);
290 } /* int openvpn_config */
293 static int ted_read (void)
294 {
295 double kv;
296 double voltage;
298 if (fd < 0)
299 return (-1);
301 if (ted_read_value (&kv,&voltage) != 0)
302 return (-1);
304 ted_submit ("kv", kv);
305 ted_submit ("voltage", voltage);
306 return (0);
307 } /* int ted_read */
309 static int ted_shutdown (void)
310 {
311 if (fd >= 0)
312 {
313 close (fd);
314 fd = -1;
315 }
317 return (0);
318 }
320 void module_register (void)
321 {
322 plugin_register_config ("ted", ted_config,
323 config_keys, config_keys_num);
324 plugin_register_init ("ted", ted_init);
325 plugin_register_read ("ted", ted_read);
326 plugin_register_shutdown ("ted", ted_shutdown);
327 } /* void module_register */