2 * W1TemperatureSensor.cc
4 * Created on: Oct 20, 2010
13 #include "W1TemperatureSensor.hh"
19 bool string_to_number(T& result,
20 const std::string& string_param,
21 std::ios_base& (*format)(std::ios_base&))
23 std::istringstream iss(string_param);
24 return !(iss >> format >> result).fail();
27 string convert_for_3_digits_value(string raw_value) {
33 sucFlg = string_to_number<double>(dbl_val, raw_value, dec);
35 dbl_val = dbl_val / 1000;
36 std::ostringstream out;
37 out << fixed << setprecision(3) << dbl_val;
46 W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
47 ifstream ifs(slave_file.c_str());
48 if (ifs.is_open() == false) {
51 text = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file;
53 cout << "verify that you have w1_therm kernel module loaded" << endl;
60 W1TemperatureSensor::~W1TemperatureSensor() {
63 bool W1TemperatureSensor::is_supported_family_code(int family_code) {
76 string W1TemperatureSensor::get_value() {
77 vector<string> text_file;
83 string formatted_data;
86 ret_val = "<could not read>";
87 ifstream ifs(slave_file.c_str());
88 if (ifs.is_open() == true) {
89 while(getline(ifs, temp)) {
90 if (temp.length() > 0) {
92 //cout << ret_val << endl;
96 length = last_line.length();
98 pos = last_line.find("t=");
100 (pos + 2 < length)) {
101 ret_val = last_line.substr(pos + 2);
105 ret_val = convert_for_3_digits_value(ret_val);
106 formatted_data = get_formatted_data(ret_val);
107 add_to_memory_cache(formatted_data);
111 string W1TemperatureSensor::get_unit() {
115 string W1TemperatureSensor::get_devicetype_name() {
116 return "Temperature Sensor";
119 void W1TemperatureSensor::printout() {
122 text = get_formatted_data();
123 cout << text << endl;
126 string W1TemperatureSensor::get_formatted_data() {
129 ret_val = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value() + " " + get_unit();