X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=src%2FW1TemperatureSensor.cc;h=5f954cfaa1403923fd58f639128510a2288ee33b;hb=9e569babf7e271015fedac3b7153d9035ff17ee0;hp=a2b53375a954a4ed7e542675180cdbe5db1e6330;hpb=f61cc31d6bd16c50ca5900eebc5760bcb61a8bcc;p=lib1wire.git diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index a2b5337..5f954cf 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -5,68 +5,96 @@ * Author: lamikr */ #include -#include #include +#include +#include + +#include #include "W1TemperatureSensor.hh" using namespace std; using namespace w1; -W1TemperatureSensor::W1TemperatureSensor(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; +#define CONST_UNIT_CELCIUS "C" + +template +bool string_to_number(NumberDataType& result, + const std::string& string_param, + std::ios_base& (*format)(std::ios_base&)) +{ + std::istringstream iss(string_param); + return !(iss >> format >> result).fail(); +} - text = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file; - cout << text << endl; - cout << "verify that you have w1_therm kernel module loaded" << endl; +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(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 { - ifs.close(); + log_error("Failed to convert temperature %s to celcius value.", raw_value.c_str()); + *err_flg = 1; } + return dbl_val; } -W1TemperatureSensor::~W1TemperatureSensor() { +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) { + 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(); + } } -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; +W1TemperatureSensor::~W1TemperatureSensor() { + log_debug("destructor\n"); } -string W1TemperatureSensor::get_value() { - vector text_file; - string temp; - string ret_val; - string last_line; - int pos; - int length; +vector *W1TemperatureSensor::get_raw_data() { + vector *ret_val; + string tmp_str; + string val_str; + double val_dbl; + int pos; + int b_cnt; + int err_flg; - ret_val = ""; + 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(); + ret_val->push_back(val_dbl); + } } } } @@ -74,12 +102,9 @@ string W1TemperatureSensor::get_value() { } string W1TemperatureSensor::get_unit() { - return "C"; + return CONST_UNIT_CELCIUS; } -void W1TemperatureSensor::printout() { - string text; - - text = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value(); - cout << text << endl; +unsigned int W1TemperatureSensor::get_data_decimal_precision() { + return 3; }