#include <sstream>
#include <iomanip>
-#include "W1Util.hh"
-#include "W1TemperatureSensor.hh"
-
#include <plp/log.h>
+#include "W1TemperatureSensor.hh"
+
using namespace std;
using namespace w1;
+#define CONST_UNIT_CELCIUS "C"
+
template <class NumberDataType>
bool string_to_number(NumberDataType& result,
const std::string& string_param,
return !(iss >> format >> result).fail();
}
-string convert_celcius_value_to_three_digits(string raw_value) {
- string ret_val;
- int int_val;
+double convert_w1_temperature_to_celcius(string raw_value,
+ int *err_flg) {
bool suc_flg;
double dbl_val;
+ dbl_val = 0;
suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
if (suc_flg == true) {
- dbl_val = dbl_val / 1000;
+ 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 celcius 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) {
- log_debug("trying to open file: %s\n", slave_file.c_str());
+W1TemperatureSensor::W1TemperatureSensor(string device_id_param,
+ string device_type_param,
+ dirent *direntry_param): W1Device(device_id_param, device_type_param, direntry_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 + "\n";
- log_debug(text.c_str());
- log_debug("verify that you have w1_therm kernel module loaded.\n");
+ log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_type().c_str(), slave_file.c_str());
+ log_error("Verify that you have w1_therm kernel module loaded.\n");
ifs.close();
}
}
W1TemperatureSensor::~W1TemperatureSensor() {
+ log_debug("destructor\n");
}
-bool W1TemperatureSensor::is_supported_family_code(int family_code) {
- bool ret_val;
-
- ret_val = false;
- switch(family_code) {
- case 0x10:
- case 0x28:
- ret_val = true;
- break;
- }
- return ret_val;
-}
-
-string W1TemperatureSensor::get_raw_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;
+ 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_celcius_value_to_three_digits(ret_val);
return ret_val;
}
string W1TemperatureSensor::get_unit() {
- return "C";
+ return CONST_UNIT_CELCIUS;
}
-string W1TemperatureSensor::get_devicetype_name() {
- return "Temperature Sensor";
+unsigned int W1TemperatureSensor::get_data_decimal_precision() {
+ return 3;
}