* Author: lamikr
*/
#include <iostream>
+#include <fstream>
+
#include <time.h>
#include "W1Device.hh"
void W1Device::printout() {
string text;
- text = get_time() + ": device type = <unknown>, id = " + id + ", value = " + get_value();
+ text = get_formatted_data();
cout << text << endl;
}
+
+string W1Device::get_formatted_data() {
+ string ret_val;
+
+ ret_val = get_formatted_data(get_value());
+ 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();
+ return ret_val;
+}
+
+void W1Device::add_to_memory_cache(std::string formatted_data) {
+ // TODO: add mutex for memory_cache
+ memory_cache.push_back(formatted_data);
+}
+
+void W1Device::store() {
+ string file_path = "/tmp/" + id + ".txt";
+ string text_line;
+ ofstream data_file(file_path.c_str(), ios::app);
+
+ cout << "storing to " << file_path << "data size " << memory_cache.size() << endl;
+ // TODO: add mutex to protect memory_cache while it's read and emptied
+ if (data_file.is_open()) {
+ while(memory_cache.size() > 0) {
+ text_line = memory_cache.front();
+ memory_cache.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;
+ }
+}
#ifndef W1DEVICE_HH_
#define W1DEVICE_HH_
+#include <string>
+#include <list>
+
#include <dirent.h>
#include <stdbool.h>
-#include <string>
#ifndef W1_SCAN_ROOTDIR
#define W1_SCAN_ROOTDIR "/sys/bus/w1/devices"
int get_family_code();
std::string get_id();
std::string get_name();
- virtual void printout();
void set_name(std::string name_param);
virtual std::string get_value() = 0;
virtual std::string get_unit() = 0;
+ virtual std::string get_devicetype_name() = 0;
std::string get_time();
+ virtual void printout();
+ virtual void store();
protected:
+ void add_to_memory_cache(std::string formatted_data);
+ 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;
std::string id;
std::string name;
std::string dir_path;
std::string slave_file;
+ std::list<std::string> memory_cache;
private:
};
}
string last_line;
int pos;
int length;
+ string formatted_data;
ret_val = "<could not read>";
ifstream ifs(slave_file.c_str());
}
}
}
+ formatted_data = get_formatted_data(ret_val);
+ add_to_memory_cache(formatted_data);
return ret_val;
}
return "C";
}
+string W1TemperatureSensor::get_devicetype_name() {
+ return "Temperature Sensor";
+}
+/*
void W1TemperatureSensor::printout() {
string text;
- text = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value();
+ text = get_formatted_data();
cout << text << endl;
}
+
+string W1TemperatureSensor::get_formatted_data() {
+ string ret_val;
+
+ ret_val = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value() + " " + get_unit();
+ return ret_val;
+}
+*/
virtual ~W1TemperatureSensor();
std::string get_value();
std::string get_unit();
- void printout();
+ std::string get_devicetype_name();
+ //void printout();
protected:
+ //std::string get_formatted_data();
bool is_supported_family_code(int family_code);
};
}
{
W1Scanner *scanner;
list<W1Device *> device_list;
+ int round;
+ int interval_seconds;
+ int store_interval;
- scanner = new W1Scanner();
- device_list = scanner->get_device_list();
+ 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;
cout << name << ": " << value << " " << unit << endl;
*/
device->printout();
+ if (round >= store_interval) {
+ device->store();
+ }
+ }
+ sleep(interval_seconds);
+ if (round >= store_interval) {
+ round = 0;
}
- sleep(60);
}
return 0;
}