lib1wire_la_SOURCES = \
W1Device.cc W1Device.hh \
W1Scanner.cc W1Scanner.hh \
+ W1Store.cc W1Store.hh \
W1TemperatureSensor.cc W1TemperatureSensor.hh \
W1CounterDevice.cc W1CounterDevice.hh
lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined
lib1wireinclude_HEADERS = \
W1Device.hh \
W1Scanner.hh \
+ W1Store.hh \
W1TemperatureSensor.hh \
W1CounterDevice.hh
\ No newline at end of file
}
ifs.close();
}
- add_to_memory_cache(ret_val);
return ret_val;
}
#include <time.h>
+#include "W1Store.hh"
#include "W1Device.hh"
using namespace w1;
string W1Device::get_formatted_data() {
string ret_val;
+ string val;
- ret_val = get_formatted_data(get_value());
+ val = get_value();
+ ret_val = get_formatted_data(val);
return ret_val;
}
string W1Device::get_formatted_data(string value) {
string ret_val;
- ret_val = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", value = " + value + " " + get_unit();
+ ret_val = get_time() + "|" + get_devicetype_name() + "|" + id + "|" + value + " " + get_unit();
+ add_to_memory_cache(ret_val);
return ret_val;
}
}
void W1Device::store() {
+ W1Store::store(id, memory_cache);
+/*
string file_path = "/tmp/" + id + ".txt";
string text_line;
ofstream data_file(file_path.c_str(), ios::app);
else {
cout << "could not open file " << file_path << " for writing data." << endl;
}
+*/
}
std::string get_formatted_data();
std::string get_formatted_data(std::string value);
virtual bool is_supported_family_code(int family_code) = 0;
- int family_code;
+ int family_code;
std::string id;
std::string name;
std::string dir_path;
--- /dev/null
+/*
+ * W1Store.cc
+ *
+ * Created on: Oct 31, 2010
+ * Author: lamikr
+ */
+
+#include <list>
+#include <string>
+#include <iostream>
+#include <fstream>
+
+#include <time.h>
+
+#include "W1Store.hh"
+
+using namespace std;
+using namespace w1;
+
+std::string W1Store::location = "/tmp/";
+
+W1Store::W1Store() {
+ // TODO Auto-generated constructor stub
+}
+
+W1Store::~W1Store() {
+ // TODO Auto-generated destructor stub
+}
+
+void W1Store::set_location(string location_param) {
+ location = location_param;
+}
+
+void W1Store::store(std::string device_id, std::list<std::string> string_list) {
+
+ string file_path = location + device_id + ".txt";
+ string text_line;
+ ofstream data_file(file_path.c_str(), ios::app);
+
+ cout << "storing to " << file_path << ", data size " << string_list.size() << endl;
+ // TODO: add mutex to protect string_list while it's read and emptied
+ if (data_file.is_open()) {
+ while(string_list.size() > 0) {
+ text_line = string_list.front();
+ string_list.pop_front();
+ if (text_line.length() > 0) {
+ cout << "storing line: " << text_line << endl;
+ data_file << text_line << endl;
+ }
+ }
+ data_file.close();
+ }
+ else {
+ cout << "could not open file " << file_path << " for writing data." << endl;
+ }
+}
--- /dev/null
+/*
+ * W1Store.hh
+ *
+ * Created on: Oct 31, 2010
+ * Author: lamikr
+ */
+
+#ifndef W1STORE_HH_
+#define W1STORE_HH_
+
+#include <string>
+#include <list>
+
+namespace w1 {
+ class W1Store {
+ public:
+ W1Store();
+ virtual ~W1Store();
+ static std::string location;
+ static void set_location(std::string location_param);
+ static void store(std::string device_id, std::list<std::string> string_list);
+ };
+}
+
+#endif /* W1STORE_HH_ */
}
}
}
- ret_val = convert_for_3_digits_value(ret_val);
- formatted_data = get_formatted_data(ret_val);
- add_to_memory_cache(formatted_data);
+ ret_val = convert_for_3_digits_value(ret_val);
return ret_val;
}
#include <plp/log.h>
+#include "W1Store.hh"
#include "W1Scanner.hh"
using namespace w1;
using namespace std;
-int main(int argc, char** argv)
-{
+int main(int argc, char** argv) {
W1Scanner *scanner;
list<W1Device *> device_list;
int round;
int interval_seconds;
int store_interval;
+ string location;
- interval_seconds = 60;
- store_interval = 10;
+ location = "/tmp/";
+ if (argc > 1) {
+ location = argv[1];
+ log_info("storage location: %s\n", location.c_str());
+ }
+ W1Store::set_location(location.c_str());
+ interval_seconds = 3;
+ store_interval = 3;
scanner = new W1Scanner();
device_list = scanner->get_device_list();
round = 0;
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);
if (round >= store_interval) {