summaryrefslogtreecommitdiff
authortille <tille>2002-06-24 11:16:30 (UTC)
committer tille <tille>2002-06-24 11:16:30 (UTC)
commit5bbfde2c34b3bbccdae89171fa0528ca8ae22b32 (patch) (side-by-side diff)
tree69546c118ae88be28c29196db39205620de702f1
parent191d88cc062f4c50b28984e17b93139c685efee1 (diff)
downloadopie-5bbfde2c34b3bbccdae89171fa0528ca8ae22b32.zip
opie-5bbfde2c34b3bbccdae89171fa0528ca8ae22b32.tar.gz
opie-5bbfde2c34b3bbccdae89171fa0528ca8ae22b32.tar.bz2
added predict time
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp42
-rw-r--r--noncore/settings/netsystemtime/ntp.h1
-rw-r--r--noncore/settings/netsystemtime/ntpbase.ui117
3 files changed, 134 insertions, 26 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
index c2b64f0..04b113b 100644
--- a/noncore/settings/netsystemtime/ntp.cpp
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -1,146 +1,180 @@
#include "ntp.h"
#include <qpushbutton.h>
#include <qregexp.h>
#include <qtable.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qmultilineedit.h>
#include <opie/oprocess.h>
#include <qpe/config.h>
#include <qpe/global.h>
#include <qpe/timeconversion.h>
#include <qpe/tzselect.h>
#include <qpe/timestring.h>
#include <qpe/qpedialog.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
: NtpBase( parent, name, fl )
{
Config cfg("ntp",Config::User);
cfg.setGroup("settings");
_maxOffset = cfg.readNumEntry("maxOffset",5);
_minLookupDiff = cfg.readNumEntry("minLookupDiff",10);
LineEditNtpServer->setText( cfg.readEntry("ntpServer") );
ntpProcess = new OProcess( );
connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
this, SLOT(getNtpOutput(OProcess*,char*,int)));
connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
this, SLOT(ntpFinished(OProcess*)));
connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
+ connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime()));
_nextCorrection = new QTimer( this );
connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) );
slotRunNtp();
readLookups();
}
Ntp::~Ntp()
{
Config cfg("ntp",Config::User);
cfg.setGroup("settings");
cfg.writeEntry("ntpServer", LineEditNtpServer->text());
}
void Ntp::slotRunNtp()
{
TextLabelStartTime->setText(QDateTime::currentDateTime().toString());
ntpProcess->clearArguments();
*ntpProcess << "ntpdate" << LineEditNtpServer->text();
bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
if ( !ret ) {
qDebug("Error while executing ntp");
outPut->append("\nError while executing\n\n");
}
}
void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
{
QString lineStr, lineStrOld;
lineStr = buffer;
lineStr=lineStr.left(buflen);
if (lineStr!=lineStrOld)
{
outPut->append(lineStr);
_ntpOutput += lineStr;
}
lineStrOld = lineStr;
}
void Ntp::ntpFinished(OProcess*)
{
Config cfg("ntp",Config::User);
cfg.setGroup("lookups");
int lastLookup = cfg.readNumEntry("time",0);
int lookupCount = cfg.readNumEntry("count",-1);
int time = TimeConversion::toUTC( QDateTime::currentDateTime() );
cfg.writeEntry("time", time);
float timeShift = getTimeShift();
- if (timeShift = 0.0) return;
+ if (timeShift == 0.0) return;
int secsSinceLast = time - lastLookup;
TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff)
{
lookupCount++;
cfg.writeEntry("count",lookupCount);
cfg.setGroup("lookup_"+QString::number(lookupCount));
_shiftPerSec = timeShift / secsSinceLast;
float nextCorr = _maxOffset / _shiftPerSec;
- qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);qDebug("in %f secs the time will be shifted by %i", nextCorr,_maxOffset);
+ qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);
cfg.writeEntry("secsSinceLast",secsSinceLast);
cfg.writeEntry("timeShift",QString::number(timeShift));
}
}
void Ntp::correctClock()
{
qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
Config cfg("ntp",Config::User);
cfg.setGroup("correction");
int lastTime = cfg.readNumEntry("time",0);
int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
int corr = int((now - lastTime) * _shiftPerSec);
outPut->append( "time will be shifted by "+QString::number(corr)+ "secs");
struct timeval myTv;
myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) );
myTv.tv_usec = 0;
if ( myTv.tv_sec != -1 )
::settimeofday( &myTv, 0 );
Global::writeHWClock();
cfg.writeEntry("time",now);
qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
}
float Ntp::getTimeShift()
{
QString _offset = "offset";
QString _sec = "sec";
QRegExp _reOffset = QRegExp(_offset);
QRegExp _reEndOffset = QRegExp(_sec);
int posOffset = _reOffset.match( _ntpOutput );
int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset );
posOffset += _offset.length() + 1;
QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1);
qDebug("%s", _ntpOutput.latin1());
qDebug("diff = >%s<",diff.latin1());
TextLabelTimeShift->setText(diff);
return diff.toFloat();
}
void Ntp::readLookups()
{
Config cfg("ntp",Config::User);
cfg.setGroup("lookups");
int lookupCount = cfg.readNumEntry("count",-1);
+ float last, shift, shiftPerSec;
+ qDebug("lookupCount = %i",lookupCount);
+ TableLookups->setNumCols( 3 );
+ TableLookups->setNumRows( lookupCount);
+ TableLookups->horizontalHeader()->setLabel(2,"secsSinceLast");
+ TableLookups->horizontalHeader()->setLabel(1,"timeShift");
+ TableLookups->horizontalHeader()->setLabel(0,"shift/s");
+ int cw = TableLookups->width()/4;
+ qDebug("column width %i",cw);
+ TableLookups->setColumnWidth( 0, cw );
+ TableLookups->setColumnWidth( 1, cw );
+ TableLookups->setColumnWidth( 2, cw );
for (int i=0; i < lookupCount; i++)
{
cfg.setGroup("lookup_"+QString::number(i));
- TableLookups->setText( 1,i,cfg.readEntry("secsSinceLast",0));
- TableLookups->setText( 2,i,cfg.readEntry("timeShift",0));
+ last = cfg.readEntry("secsSinceLast",0).toFloat();
+ shift = QString(cfg.readEntry("timeShift",0)).toFloat();
+ qDebug("%i last %f",i,last);
+ qDebug("%i shift %f",i,shift);
+ shiftPerSec = shift / last;
+ _shiftPerSec += shiftPerSec;
+ TableLookups->setText( i,0,QString::number(shiftPerSec));
+ TableLookups->setText( i,1,QString::number(shift));
+ TableLookups->setText( i,2,QString::number(last));
}
+ _shiftPerSec /= lookupCount+1;
+ TextLabelShift->setText(QString::number(_shiftPerSec));
+}
+
+void Ntp::preditctTime()
+{
+ qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
+ Config cfg("ntp",Config::User);
+ cfg.setGroup("lookups");
+ int lastTime = cfg.readNumEntry("time",0);
+ int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
+ int corr = int((now - lastTime) * _shiftPerSec);
+ outPut->append( "time will be shifted by "+QString::number(corr)+ "secs");
+ TextLabelPredTime->setText(QDateTime::currentDateTime().addSecs(corr).toString());
}
diff --git a/noncore/settings/netsystemtime/ntp.h b/noncore/settings/netsystemtime/ntp.h
index 4dc04b1..e3f6d16 100644
--- a/noncore/settings/netsystemtime/ntp.h
+++ b/noncore/settings/netsystemtime/ntp.h
@@ -1,37 +1,38 @@
#ifndef NTP_H
#define NTP_H
#include "ntpbase.h"
#include <qdatetime.h>
#include <qtimer.h>
class OProcess;
class QString;
class Ntp : public NtpBase
{
Q_OBJECT
public:
Ntp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~Ntp();
OProcess *ntpProcess;
private:
QString _ntpOutput;
int _maxOffset;
float _shiftPerSec;
QTimer *_nextCorrection;
int _minLookupDiff;
float getTimeShift();
void readLookups();
private slots:
void slotRunNtp();
void getNtpOutput(OProcess *proc, char *buffer, int buflen);
void ntpFinished(OProcess*);
void correctClock();
+ void preditctTime();
};
#endif
diff --git a/noncore/settings/netsystemtime/ntpbase.ui b/noncore/settings/netsystemtime/ntpbase.ui
index eb09e4a..1c1e1aa 100644
--- a/noncore/settings/netsystemtime/ntpbase.ui
+++ b/noncore/settings/netsystemtime/ntpbase.ui
@@ -1,62 +1,62 @@
<!DOCTYPE UI><UI>
<class>NtpBase</class>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>NtpBase</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>0</x>
<y>0</y>
- <width>296</width>
+ <width>292</width>
<height>337</height>
</rect>
</property>
<property stdset="1">
<name>caption</name>
<string>Network Time</string>
</property>
<property>
<name>layoutMargin</name>
</property>
<property>
<name>layoutSpacing</name>
</property>
<grid>
<property stdset="1">
<name>margin</name>
<number>2</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>1</number>
</property>
<widget row="0" column="0" >
<class>QTabWidget</class>
<property stdset="1">
<name>name</name>
<cstring>TabWidgetMain</cstring>
</property>
<property>
<name>layoutMargin</name>
</property>
<property>
<name>layoutSpacing</name>
</property>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>tabNtp</cstring>
</property>
<attribute>
<name>title</name>
<string>NTP</string>
</attribute>
<grid>
<property stdset="1">
<name>margin</name>
<number>3</number>
@@ -95,228 +95,301 @@
</property>
<grid>
<property stdset="1">
<name>margin</name>
<number>11</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>6</number>
</property>
<widget row="0" column="0" >
<class>QLayoutWidget</class>
<property stdset="1">
<name>name</name>
<cstring>Layout4</cstring>
</property>
<vbox>
<property stdset="1">
<name>margin</name>
<number>0</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>6</number>
</property>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabel1</cstring>
</property>
<property stdset="1">
<name>autoMask</name>
<bool>false</bool>
</property>
<property stdset="1">
<name>text</name>
<string>Start Time:</string>
</property>
</widget>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabelStartTime</cstring>
</property>
<property stdset="1">
<name>text</name>
- <string></string>
+ <string>nan</string>
</property>
</widget>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabel3</cstring>
</property>
<property stdset="1">
<name>text</name>
<string>Time Shift:</string>
</property>
</widget>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabelTimeShift</cstring>
</property>
<property stdset="1">
<name>text</name>
- <string></string>
+ <string>nan</string>
</property>
</widget>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabel5</cstring>
</property>
<property stdset="1">
<name>text</name>
<string>New Time:</string>
</property>
</widget>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabelNewTime</cstring>
</property>
<property stdset="1">
<name>text</name>
- <string></string>
+ <string>nan</string>
</property>
</widget>
<spacer>
<property>
<name>name</name>
<cstring>Spacer1</cstring>
</property>
<property stdset="1">
<name>orientation</name>
<enum>Vertical</enum>
</property>
<property stdset="1">
<name>sizeType</name>
<enum>Expanding</enum>
</property>
<property>
<name>sizeHint</name>
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</vbox>
</widget>
<widget row="1" column="0" >
<class>QLayoutWidget</class>
<property stdset="1">
<name>name</name>
<cstring>Layout6</cstring>
</property>
<hbox>
<property stdset="1">
<name>margin</name>
<number>0</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>6</number>
</property>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabel7</cstring>
</property>
<property stdset="1">
<name>text</name>
<string>NTP Server:</string>
</property>
</widget>
<widget>
<class>QLineEdit</class>
<property stdset="1">
<name>name</name>
<cstring>LineEditNtpServer</cstring>
</property>
</widget>
</hbox>
</widget>
</grid>
</widget>
</grid>
</widget>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>tab</cstring>
</property>
<attribute>
<name>title</name>
<string>Auto Correction</string>
</attribute>
- <widget>
- <class>QTable</class>
- <property stdset="1">
- <name>name</name>
- <cstring>TableLookups</cstring>
- </property>
- <property stdset="1">
- <name>geometry</name>
- <rect>
- <x>10</x>
- <y>10</y>
- <width>270</width>
- <height>220</height>
- </rect>
- </property>
+ <grid>
<property stdset="1">
- <name>numRows</name>
+ <name>margin</name>
<number>2</number>
</property>
<property stdset="1">
- <name>numCols</name>
+ <name>spacing</name>
<number>2</number>
</property>
- </widget>
+ <widget row="0" column="0" >
+ <class>QTable</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>TableLookups</cstring>
+ </property>
+ <property stdset="1">
+ <name>numRows</name>
+ <number>2</number>
+ </property>
+ <property stdset="1">
+ <name>numCols</name>
+ <number>2</number>
+ </property>
+ </widget>
+ <widget row="2" column="0" >
+ <class>QPushButton</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>PushButtonPredict</cstring>
+ </property>
+ <property stdset="1">
+ <name>text</name>
+ <string>predict time</string>
+ </property>
+ </widget>
+ <widget row="1" column="0" >
+ <class>QLayoutWidget</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>Layout5</cstring>
+ </property>
+ <grid>
+ <property stdset="1">
+ <name>margin</name>
+ <number>0</number>
+ </property>
+ <property stdset="1">
+ <name>spacing</name>
+ <number>6</number>
+ </property>
+ <widget row="0" column="1" >
+ <class>QLabel</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>TextLabelShift</cstring>
+ </property>
+ <property stdset="1">
+ <name>text</name>
+ <string>nan</string>
+ </property>
+ </widget>
+ <widget row="1" column="1" >
+ <class>QLabel</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>TextLabelPredTime</cstring>
+ </property>
+ <property stdset="1">
+ <name>text</name>
+ <string>nan</string>
+ </property>
+ </widget>
+ <widget row="0" column="0" >
+ <class>QLabel</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>Mean_shift_label</cstring>
+ </property>
+ <property stdset="1">
+ <name>text</name>
+ <string>Mean shift:</string>
+ </property>
+ </widget>
+ <widget row="1" column="0" >
+ <class>QLabel</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>TextLabel3_2</cstring>
+ </property>
+ <property stdset="1">
+ <name>text</name>
+ <string>Pred. Time:</string>
+ </property>
+ </widget>
+ </grid>
+ </widget>
+ </grid>
</widget>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>TabDebug</cstring>
</property>
<attribute>
<name>title</name>
<string>Debug</string>
</attribute>
<grid>
<property stdset="1">
<name>margin</name>
<number>3</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>6</number>
</property>
<widget row="0" column="0" >
<class>QMultiLineEdit</class>
<property stdset="1">
<name>name</name>
<cstring>outPut</cstring>
</property>
<property stdset="1">
<name>wordWrap</name>
<enum>WidgetWidth</enum>
</property>
</widget>
</grid>
</widget>
</widget>
</grid>
</widget>
</UI>