#include <sstream>
#include <iomanip>
+#include "W1Util.hh"
#include "W1TemperatureSensor.hh"
+#include <plp/log.h>
+
using namespace std;
using namespace w1;
-template <class T>
-bool string_to_number(T& result,
+#define CONST_UNIT_CELCIUS "C"
+
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
const std::string& string_param,
std::ios_base& (*format)(std::ios_base&))
{
return !(iss >> format >> result).fail();
}
-string convert_for_3_digits_value(string raw_value) {
- string ret_val;
- int int_val;
- bool sucFlg;
+double convert_w1_temperature_to_celcius(string raw_value, int *err_flg) {
+ bool suc_flg;
double dbl_val;
- sucFlg = string_to_number<double>(dbl_val, raw_value, dec);
- if (sucFlg == true) {
- dbl_val = dbl_val / 1000;
+ dbl_val = 0;
+ suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
+ if (suc_flg == true) {
+ dbl_val = dbl_val / 1000;
+ *err_flg = 0;
+/*
std::ostringstream out;
out << fixed << setprecision(3) << dbl_val;
ret_val = out.str();
+*/
}
else {
- ret_val = raw_value;
+ log_error("Failed to convert temperature %s to celsius value", raw_value.c_str());
+ *err_flg = 1;
}
- return ret_val;
+ return dbl_val;
}
-W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
+W1TemperatureSensor::W1TemperatureSensor(int family_code_param,
+ string device_id_param,
+ dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
+ log_debug("trying to open file: %s\n", slave_file.c_str());
+
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_therm kernel module loaded." << endl;
- }
- else {
+ log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_device_type().c_str(), slave_file.c_str());
+ log_error("Verify that you have w1_therm kernel module loaded.\n");
ifs.close();
}
}
W1TemperatureSensor::~W1TemperatureSensor() {
}
-bool W1TemperatureSensor::is_supported_family_code(int family_code) {
+bool W1TemperatureSensor::is_supported_w1_family_code(int family_code) {
bool ret_val;
ret_val = false;
return ret_val;
}
-string W1TemperatureSensor::get_value() {
- string temp;
- string ret_val;
- string last_line;
+vector<double> *W1TemperatureSensor::get_raw_data() {
+ vector<double> *ret_val;
+ string tmp_str;
+ string val_str;
+ double val_dbl;
int pos;
- int length;
- string formatted_data;
- int int_value;
+ int b_cnt;
+ int err_flg;
- ret_val = "<could not read>";
+ ret_val = NULL;
+ err_flg = 0;
ifstream ifs(slave_file.c_str());
if (ifs.is_open() == true) {
- while(getline(ifs, temp)) {
- if (temp.length() > 0) {
- last_line = temp;
- //cout << ret_val << endl;
+ while(getline(ifs, tmp_str)) {
+ if (tmp_str.empty() == false) {
+ val_str = tmp_str;
}
}
ifs.close();
- length = last_line.length();
- if (length > 0) {
- pos = last_line.find("t=");
+ b_cnt = val_str.length();
+ if (b_cnt > 0) {
+ pos = val_str.find("t=");
if ((pos >= 0) &&
- (pos + 2 < length)) {
- ret_val = last_line.substr(pos + 2);
+ ((pos + 2) < b_cnt)) {
+ val_str = val_str.substr(pos + 2);
+ val_dbl = convert_w1_temperature_to_celcius(val_str, &err_flg);
+ if (err_flg == 0) {
+ ret_val = new vector<double>();
+ ret_val->push_back(val_dbl);
+ }
}
}
}
- ret_val = convert_for_3_digits_value(ret_val);
return ret_val;
}
string W1TemperatureSensor::get_unit() {
- return "C";
+ return CONST_UNIT_CELCIUS;
}
-string W1TemperatureSensor::get_devicetype_name() {
+string W1TemperatureSensor::get_device_type() {
return "Temperature Sensor";
}
-/*
-void W1TemperatureSensor::printout() {
- string text;
- text = get_formatted_data();
- cout << text << endl;
+unsigned int W1TemperatureSensor::get_data_decimal_precision() {
+ return 3;
}
-
-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;
-}
-*/