2 * W1TemperatureSensor.cc
4 * Created on: Oct 20, 2010
12 #include "W1TemperatureSensor.hh"
18 bool string_to_number(T& result,
19 const std::string& string_param,
20 std::ios_base& (*format)(std::ios_base&))
22 std::istringstream iss(string_param);
23 return !(iss >> format >> result).fail();
26 string convert_for_3_digits_value(string raw_value) {
32 sucFlg = string_to_number<double>(dbl_val, raw_value, dec);
34 dbl_val = dbl_val / 1000;
35 std::ostringstream out;
36 out << fixed << setprecision(3) << dbl_val;
45 W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
46 ifstream ifs(slave_file.c_str());
47 if (ifs.is_open() == false) {
50 text = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
52 cout << "verify that you have w1_therm kernel module loaded." << endl;
59 W1TemperatureSensor::~W1TemperatureSensor() {
62 bool W1TemperatureSensor::is_supported_family_code(int family_code) {
75 string W1TemperatureSensor::get_value() {
81 string formatted_data;
84 ret_val = "<could not read>";
85 ifstream ifs(slave_file.c_str());
86 if (ifs.is_open() == true) {
87 while(getline(ifs, temp)) {
88 if (temp.length() > 0) {
90 //cout << ret_val << endl;
94 length = last_line.length();
96 pos = last_line.find("t=");
99 ret_val = last_line.substr(pos + 2);
103 ret_val = convert_for_3_digits_value(ret_val);
107 string W1TemperatureSensor::get_unit() {
111 string W1TemperatureSensor::get_devicetype_name() {
112 return "Temperature Sensor";
115 void W1TemperatureSensor::printout() {
118 text = get_formatted_data();
119 cout << text << endl;
122 string W1TemperatureSensor::get_formatted_data() {
125 ret_val = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value() + " " + get_unit();