lib1wire_la_SOURCES = \
W1Device.cc W1Device.hh \
W1Scanner.cc W1Scanner.hh \
- W1TemperatureSensor.cc W1TemperatureSensor.hh
+ W1TemperatureSensor.cc W1TemperatureSensor.hh \
+ W1CounterDevice.cc W1CounterDevice.hh
lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined
AM_CPPFLAGS = $(SRC_CFLAGS) -Iidl
lib1wireinclude_HEADERS = \
W1Device.hh \
W1Scanner.hh \
- W1TemperatureSensor.hh
-
+ W1TemperatureSensor.hh \
+ W1CounterDevice.hh
\ No newline at end of file
--- /dev/null
+/*
+ * W1CounterDevice.cc
+ *
+ * Created on: Oct 30, 2010
+ * Author: lamikr
+ */
+
+#include <iostream>
+#include <fstream>
+
+#include "W1CounterDevice.hh"
+
+using namespace std;
+using namespace w1;
+
+W1CounterDevice::W1CounterDevice(dirent *direntry,
+ int family_code_param,
+ string id_param): W1Device(direntry, family_code_param, id_param) {
+ ifstream ifs(slave_file.c_str());
+ if (ifs.is_open() == false) {
+ string text;
+
+ text = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
+ cout << text << endl;
+ cout << "verify that you have w1_ds2423 kernel module loaded." << endl;
+ }
+ else {
+ ifs.close();
+ }
+}
+
+W1CounterDevice::~W1CounterDevice() {
+ // TODO Auto-generated destructor stub
+}
+
+bool W1CounterDevice::is_supported_family_code(int family_code) {
+ bool ret_val;
+
+ ret_val = false;
+ switch(family_code) {
+ case 0x1d:
+ ret_val = true;
+ break;
+ }
+ return ret_val;
+}
+
+string W1CounterDevice::get_value() {
+ string ret_val;
+ string value_line;
+ int pos;
+ int length;
+ int ii;
+
+ ret_val = "<could not read>";
+ ifstream ifs(slave_file.c_str());
+ if (ifs.is_open() == true) {
+ ret_val = "";
+ while(getline(ifs, value_line)) {
+ length = value_line.length();
+ if (length > 0) {
+ pos = value_line.find("c=");
+ if ((pos >= 0) &&
+ (pos + 2 < length)) {
+ value_line = value_line.substr(pos + 2);
+ }
+ else {
+ value_line = "";
+ }
+ }
+ else {
+ value_line = "";
+ }
+ if (ret_val.length() == 0) {
+ ret_val = value_line;
+ }
+ else {
+ ret_val = ret_val + "|" + value_line;
+ }
+ }
+ ifs.close();
+ }
+ add_to_memory_cache(ret_val);
+ return ret_val;
+}
+
+string W1CounterDevice::get_unit() {
+ return "";
+}
+
+string W1CounterDevice::get_devicetype_name() {
+ return "Counter Device";
+}
--- /dev/null
+/*
+ * W1CounterDevice.hh
+ *
+ * Created on: Oct 30, 2010
+ * Author: lamikr
+ */
+
+#ifndef W1COUNTERDEVICE_HH_
+#define W1COUNTERDEVICE_HH_
+
+#include "W1Device.hh"
+
+namespace w1 {
+ class W1CounterDevice: public w1::W1Device {
+ public:
+ W1CounterDevice(dirent *direntry, int family_code_param, std::string id_param);
+ virtual ~W1CounterDevice();
+ std::string get_value();
+ std::string get_unit();
+ std::string get_devicetype_name();
+ protected:
+ bool is_supported_family_code(int family_code);
+ };
+}
+
+#endif /* W1COUNTERDEVICE_HH_ */
#include <plp/log.h>
#include "W1Scanner.hh"
#include "W1TemperatureSensor.hh"
+#include "W1CounterDevice.hh"
using namespace w1;
using namespace std;
W1Scanner::W1Scanner() {
- log_debug("created");
+ log_debug("created\n");
}
W1Scanner::~W1Scanner() {
- log_debug("destroyed");
+ log_debug("destroyed\n");
}
bool W1Scanner::is_subdirectory(dirent *direntry) {
cout << "temperature sensor: " << family_code << endl;
ret_val = new W1TemperatureSensor(direntry, family_code, device_name);
break;
+ case 0x1d:
+ cout << "counter device: " << family_code << endl;
+ ret_val = new W1CounterDevice(direntry, family_code, device_name);
+ break;
}
}
}
return ret_val;
-
}
list<W1Device *> W1Scanner::get_device_list() {
* Author: lamikr
*/
#include <fstream>
-#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
if (ifs.is_open() == false) {
string text;
- text = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file;
+ text = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
cout << text << endl;
- cout << "verify that you have w1_therm kernel module loaded" << endl;
+ cout << "verify that you have w1_therm kernel module loaded." << endl;
}
else {
ifs.close();
}
string W1TemperatureSensor::get_value() {
- vector<string> text_file;
string temp;
string ret_val;
string last_line;
std::string get_value();
std::string get_unit();
std::string get_devicetype_name();
- //void printout();
protected:
- //std::string get_formatted_data();
bool is_supported_family_code(int family_code);
};
}
#include <string>
#include <iostream>
-#include <unistd.h>
+#include <plp/log.h>
#include "W1Scanner.hh"
int main(int argc, char** argv)
{
- W1Scanner *scanner;
+ W1Scanner *scanner;
list<W1Device *> device_list;
- int round;
- int interval_seconds;
- int store_interval;
+ int round;
+ int interval_seconds;
+ int store_interval;
interval_seconds = 60;
store_interval = 10;
- scanner = new W1Scanner();
- device_list = scanner->get_device_list();
- round = 0;
- while(1) {
- round++;
- for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++)
- {
- W1Device *device = (W1Device *)*list_iter;
+ scanner = new W1Scanner();
+ device_list = scanner->get_device_list();
+ round = 0;
+ if (device_list.size() > 0) {
+ while(1) {
+ round++;
+ for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++)
+ {
+ W1Device *device = (W1Device *)*list_iter;
/*
string name = device->get_name();
string value = device->get_value();
string unit = device->get_unit();
cout << name << ": " << value << " " << unit << endl;
*/
- device->printout();
- sleep(1);
+ device->printout();
+ sleep(1);
+ if (round >= store_interval) {
+ device->store();
+ }
+ }
+ sleep(interval_seconds);
if (round >= store_interval) {
- device->store();
+ round = 0;
}
}
- sleep(interval_seconds);
- if (round >= store_interval) {
- round = 0;
- }
+ }
+ else {
+ log_debug("could not find 1-wire devices.\n");
}
return 0;
}