summaryrefslogtreecommitdiffstats
path: root/src/sources/hddtempsrc.cpp
blob: b22a6c8101e9411369fb68ebf3a1b8a088829e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************
 *   Copyright (C) 2007 by Ken Werner                                      *
 *   [email protected]                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "hddtempsrc.h"

#include <tqsocketdevice.h>
#include <tqhostaddress.h>
#include <tqcstring.h> 

#include <klocale.h>

//#include <kdebug.h>

const TQ_ULONG HDDTempSrc::BUFFERSIZE = 128;
const TQ_UINT32 HDDTempSrc::ADDRESS = (127<<24)|1;
const TQ_UINT16 HDDTempSrc::PORT = 7634;

HDDTempSrc::HDDTempSrc(TQWidget* inParent, uint inIndex, const TQString& inDevice, const TQString& inModelName):
		LabelSource(inParent),
		mIndex(inIndex),
		mTrigger(this){
	mID = "HDDTemp" + TQString().setNum(inIndex);
	mName = mID;
	mDescription = i18n("This source is provided by hddtemp. (%1, %2)").tqarg(inDevice).tqarg(inModelName);
}

HDDTempSrc::~HDDTempSrc(){
}

std::list<Source*>HDDTempSrc::createInstances(TQWidget* inParent){
	std::list<Source*> list;
	TQSocketDevice sd;
	sd.setBlocking(true);
	if(sd.connect((ADDRESS), PORT)){
		//kdDebug() << "hddtemp is availalble" << endl;
		TQCString tmp(0);
		TQ_LONG numBytes = 0;
		TQ_LONG numTotalBytes = 0;
		do{
			tmp.resize(numTotalBytes+BUFFERSIZE);
			numBytes = sd.readBlock(tmp.data()+numTotalBytes, BUFFERSIZE);
            // numBytes could be -1 too in case of an error!
			numTotalBytes += TQMAX(numBytes, 0);
		}while(numBytes>0);
		sd.close();
		tmp.resize(numTotalBytes+1);	
		//kdDebug() << "following data was read: " << tmp << endl;
		// split the string
		TQStringList sl = TQStringList::split(tmp[0], tmp);
		// create the sources
		if(sl.size() > 0 && sl.size()%4 == 0){
			for(uint i = 0; i < sl.size(); i += 4)
				list.push_back(new HDDTempSrc(inParent, i/4, sl[i], sl[i+1]));
		}
	}//else
		//kdDebug() << "hddtemp is not availalble" << endl; // << sd.error() << endl;
	return list;
}

TQString HDDTempSrc::fetchValue(){
	TQString s = "n/a";
	TQSocketDevice sd;
	sd.setBlocking(true);
	if(sd.connect((ADDRESS), PORT)){
		TQCString tmp(0);
		TQ_LONG numBytes = 0;
		TQ_LONG numTotalBytes = 0;
		do{
			tmp.resize(numTotalBytes+BUFFERSIZE);
			numBytes = sd.readBlock(tmp.data()+numTotalBytes, BUFFERSIZE);
            // numBytes could be -1 too in case of an error!
			numTotalBytes += TQMAX(numBytes, 0);
		}while(numBytes>0);
		sd.close();
		tmp.resize(numTotalBytes+1);
		TQStringList sl = TQStringList::split(tmp[0], tmp);
		if(sl.size() > 0 && sl.size()%4 == 0){
			s = formatTemperature(sl[mIndex*4+2]);
		}
	}
	return s;
}