代码拉取完成,页面将自动刷新
#include "dateclocksetting.h"
#include "ui_dateclocksetting.h"
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
//#include <time.h>
#include <unistd.h>
void convertDateTimeToTm(const QDateTime &dt, struct tm &tm)
{
const int wday[8]={0,1,2,3,4,5,6,0};
tm.tm_sec=dt.time().second();/* Seconds.[0-60](1 leap second)*/
tm.tm_min=dt.time().minute();/* Minutes.[0-59] */
tm.tm_hour=dt.time().hour();/* Hours. [0-23] */
tm.tm_mday=dt.date().day();/* Day.[1-31] */
tm.tm_mon=dt.date().month()-1;/* Month.[0-11]*/
tm.tm_year=dt.date().year()-1900;/* Year- 1900.*/
tm.tm_wday=wday[dt.date().dayOfWeek()];
tm.tm_yday=dt.date().dayOfYear()-1;
tm.tm_isdst=-1;/*DST.[-1/0/1]*/
}
/**
* @brief writeRTC
* @param pUTCTime
* 参数为UTC时间,会自动把UTC时间转换为系统时间
* @return
*/
bool writeRTC(struct tm *pUTCTime)
{
int fd, retcal;
fd = open("/dev/rtc0",O_RDWR|O_NOCTTY); //打开设备
if (fd == -1)
{
perror("error1");
}
struct rtc_time rtc_tm;
rtc_tm.tm_year = pUTCTime->tm_year;
rtc_tm.tm_mon = pUTCTime->tm_mon;
rtc_tm.tm_mday = pUTCTime->tm_mday;
rtc_tm.tm_hour = pUTCTime->tm_hour;
rtc_tm.tm_min = pUTCTime->tm_min;
rtc_tm.tm_sec = pUTCTime->tm_sec;
retcal = ioctl(fd, RTC_SET_TIME, &rtc_tm); //写入设备
if (retcal == -1)
{
perror("error2");
}
close(fd);
return fd!=-1 && retcal!=-1;
}
/**
* @brief setDateTime
* @param systemDT
* 参数为系统时间
*/
void setDateTime(const QDateTime &systemDT)
{
struct tm systemTime,utcTime;
convertDateTimeToTm(systemDT, systemTime);
convertDateTimeToTm(systemDT.toUTC(), utcTime);
/**
* stimt()等函数设置的是系统时钟,不会修改硬件时钟,系统重启后仍会加载硬件时钟。
* */
//设置硬件时钟
writeRTC(&utcTime); //hwclock -r查看硬件时钟
//设置系统时钟
time_t t = mktime(&systemTime);
// stime(&t);
}
DateClockSetting::DateClockSetting(QWidget *parent) :
QWidget(parent),
ui(new Ui::DateClockSetting)
{
ui->setupUi(this);
m_Timer = new QTimer;
m_Timer->setInterval(500);
m_Clock = new Clock(ui->Clock);
m_WebTimer = new QTimer;
m_WebTimer->setInterval(5*60*1000);
SetQRadioButtonStyleSheet();
m_webTimeGetter = new webTimeGetter;
if(DataDictionary::GetInstance()->GetMisData(MIS_DATE_CLOCK_TYPE).toUInt() == CLOCK_NET_TYPE)
{
m_webTimeGetter->tryGet();
m_WebTimer->start();
}
connect(m_Timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
connect(m_WebTimer,SIGNAL(timeout()),m_webTimeGetter,SLOT(tryGet()));
connect(DataDictionary::GetInstance(),SIGNAL(DevParamDataChange(DevParamDataIndex_t)),\
this,SLOT(DevParamDataChange(DevParamDataIndex_t)));
}
DateClockSetting::~DateClockSetting()
{
delete ui;
}
void DateClockSetting::showEvent(QShowEvent *event)
{
m_ModifySta = false;
//SelectButtonClicked(ui->NetWorkGetTime);
SetClockTypeButton(DataDictionary::GetInstance()->GetMisData(MIS_DATE_CLOCK_TYPE).toUInt());
SelectButtonEnable(false);
NoModifyDateTime();
m_Timer->start();
}
void DateClockSetting::hideEvent(QHideEvent *event)
{
m_Timer->stop();
}
void DateClockSetting::ShowCurrentDateTime()
{
ui->Date->setSelectedDate(QDate::currentDate());
ui->Date->showToday();
}
void DateClockSetting::ShowCurrentClock()
{
m_Clock->update();
}
void DateClockSetting::ShowTime()
{
ShowCurrentDateTime();
ShowCurrentClock();
}
void DateClockSetting::SetQRadioButtonStyleSheet(QRadioButton *button,Color_t C)
{
if(C == CAR_BUTTON_GREEN)
{
button->setStyleSheet("QRadioButton\
{\
color: rgb(15, 188, 18);\
image-position:left;\
padding-left:20px;\
font: 24px 'Ubuntu';\
font: bold;\
border:0px solid;\
border-color: rgb(53,65,139);\
padding-left: 0px; \
}\
QRadioButton::indicator\
{\
width:30px;\
height:30px;\
}");
}
else if(C == CAR_BUTTON_RED){
button->setStyleSheet("QRadioButton\
{\
color: rgb(204, 0, 0);\
image-position:left;\
padding-left:20px;\
font: 24px 'Ubuntu';\
font: bold;\
border:0px solid;\
border-color: rgb(53,65,139);\
padding-left: 0px; \
}\
QRadioButton::indicator\
{\
width:30px;\
height:30px;\
}");
}
else if(C == CAR_BUTTON_WHITE){
button->setStyleSheet("QRadioButton\
{\
color: rgb(255, 255, 255);\
image-position:left;\
padding-left:20px;\
font: 24px 'Ubuntu';\
font: bold;\
border:0px solid;\
border-color: rgb(53,65,139);\
padding-left: 0px; \
}\
QRadioButton::indicator\
{\
width:30px;\
height:30px;\
}");
}
}
void DateClockSetting::SetQRadioButtonStyleSheet()
{
SetQRadioButtonStyleSheet(ui->NetWorkGetTime,CAR_BUTTON_WHITE);
SetQRadioButtonStyleSheet(ui->BBGetTime,CAR_BUTTON_WHITE);
SetQRadioButtonStyleSheet(ui->MenuSetTime,CAR_BUTTON_WHITE);
}
void DateClockSetting::ModifyDateTime()
{
ui->Ok->show();
ui->Cancel->show();
ui->ModifyDate->hide();
if(ui->MenuSetTime->isChecked())
{
ui->Time->show();
ui->Time->setEnabled(true);
ui->Date->setEnabled(true);
}
m_WebTimer->stop();
}
void DateClockSetting::NoModifyDateTime()
{
ui->Ok->hide();
ui->Cancel->hide();
ui->ModifyDate->show();
ui->Time->hide();
ui->Time->setEnabled(false);
ui->Date->setEnabled(false);
}
void DateClockSetting::on_Ok_clicked()
{
NoModifyDateTime();
m_Timer->start();
SelectButtonEnable(false);
ui->ModifyDate->setEnabled(true);
if(ui->NetWorkGetTime->isChecked())
{
DataDictionary::GetInstance()->SetMisData(MIS_DATE_CLOCK_TYPE,CLOCK_NET_TYPE);
m_webTimeGetter->tryGet();
m_WebTimer->start();
}
else if(ui->BBGetTime->isChecked())
{
DataDictionary::GetInstance()->SetMisData(MIS_DATE_CLOCK_TYPE,CLOCK_BB_TYPE);
}
else if(ui->MenuSetTime->isChecked())
{
DataDictionary::GetInstance()->SetMisData(MIS_DATE_CLOCK_TYPE,CLOCK_MENU_TYPE);
QDateTime DateTime;
DateTime.setDate(ui->Date->selectedDate());
DateTime.setTime(ui->Time->time());
setDateTime(DateTime);
}
}
void DateClockSetting::on_Cancel_clicked()
{
NoModifyDateTime();
m_Timer->start();
SelectButtonEnable(false);
ui->ModifyDate->setEnabled(true);
SetClockTypeButton(DataDictionary::GetInstance()->GetMisData(MIS_DATE_CLOCK_TYPE).toUInt());
}
void DateClockSetting::SelectButtonEnable(bool s)
{
ui->NetWorkGetTime->setEnabled(s);
ui->MenuSetTime->setEnabled(s);
ui->BBGetTime->setEnabled(s);
}
void DateClockSetting::SelectButtonClicked(QRadioButton *b)
{
ui->NetWorkGetTime->setChecked(false);
ui->MenuSetTime->setChecked(false);
ui->BBGetTime->setChecked(false);
b->setChecked(true);
}
void DateClockSetting::on_ModifyDate_clicked()
{
m_Timer->stop();
SelectButtonEnable(true);
// if(m_ModifySta)
// {
// m_ModifySta = false;
// }
// else {
ModifyDateTime();
m_ModifySta = true;
ui->ModifyDate->setEnabled(false);
// }
//
// m_Timer->stop();
// if(ui->NetWorkGetTime->isChecked())
// {
// m_Timer->start();
// }
// else if(ui->BBGetTime->isChecked())
// {
// m_Timer->start();
// }
// else if(ui->MenuSetTime->isChecked())
// {
// ModifyDateTime();
// }
// else
// {
// m_Timer->start();
// }
}
#if 0
RtcDateTime::RtcDateTime(QWidget *parent) :
QWidget(parent),
ui(new Ui::RtcDateTime)
{
ui->setupUi(this);
NoModifyDateTime();
//ui->ModifyDate->hide();
ui->Time->setDisplayFormat("HH:mm:ss");
m_Clock = new Clock(ui->Clock);
m_Timer = new QTimer;
m_Timer->setInterval(500);
connect(m_Timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
}
RtcDateTime::~RtcDateTime()
{
delete ui;
}
void RtcDateTime::showEvent(QShowEvent *event)
{
m_Timer->start();
}
void RtcDateTime::ModifyDateTime()
{
ui->Ok->show();
ui->Cancel->show();
ui->ModifyDate->hide();
ui->Time->setEnabled(true);
ui->Date->setEnabled(true);
}
void RtcDateTime::NoModifyDateTime()
{
ui->Ok->hide();
ui->Cancel->hide();
ui->ModifyDate->show();
ui->Time->setEnabled(false);
ui->Date->setEnabled(false);
}
void RtcDateTime::ShowCurrentDateTime()
{
ui->Date->setSelectedDate(QDate::currentDate());
ui->Date->showToday();
ui->Time->setTime(QTime::currentTime());
}
void RtcDateTime::ShowCurrentClock()
{
m_Clock->update();
}
void RtcDateTime::on_ModifyDate_clicked()
{
ModifyDateTime();
m_Timer->stop();
}
void RtcDateTime::on_Cancel_clicked()
{
NoModifyDateTime();
m_Timer->start();
}
void RtcDateTime::ShowTime()
{
ShowCurrentDateTime();
ShowCurrentClock();
}
void RtcDateTime::on_Ok_clicked()
{
QDateTime DateTime;
DateTime.setDate(ui->Date->selectedDate());
DateTime.setTime(ui->Time->time());
setDateTime(DateTime);
NoModifyDateTime();
m_Timer->start();
}
#endif
void DateClockSetting::on_NetWorkGetTime_clicked()
{
ui->Time->hide();
ui->Time->setEnabled(false);
ui->Date->setEnabled(false);
}
void DateClockSetting::on_BBGetTime_clicked()
{
ui->Time->hide();
ui->Time->setEnabled(false);
ui->Date->setEnabled(false);
}
void DateClockSetting::on_MenuSetTime_clicked()
{
ui->Time->show();
ui->Time->setEnabled(true);
ui->Date->setEnabled(true);
}
void DateClockSetting::SetClockTypeButton(uint32_t s)
{
if(s == CLOCK_BB_TYPE)
{
SelectButtonClicked(ui->BBGetTime);
}
else if(s == CLOCK_NET_TYPE)
{
SelectButtonClicked(ui->NetWorkGetTime);
}
else if(s == CLOCK_MENU_TYPE)
{
SelectButtonClicked(ui->MenuSetTime);
}
}
void DateClockSetting::DevParamDataChange(DevParamDataIndex_t Index)
{
switch (Index) {
case D_TIME_STAMP:
{
if(DataDictionary::GetInstance()->GetMisData(MIS_DATE_CLOCK_TYPE).toUInt() == CLOCK_BB_TYPE)
{
QDateTime Time;
qint64 T = DataDictionary::GetInstance()->GetDevParamData(Index).toULong();
Time.setSecsSinceEpoch(T);
setDateTime(Time);
}
}
break;
default:
break;
}
}
#include <iostream>
webTimeGetter::webTimeGetter()
{
hostList.push_back("time.windows.com");
hostList.push_back("time.nist.gov");
udpsocket = new QUdpSocket(this);
connect(udpsocket,SIGNAL(connected()),this,SLOT(connectSucess()));
connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readingData()));
}
webTimeGetter::~webTimeGetter()
{
}
void webTimeGetter::readingData()
{
QByteArray newTime;
QDateTime epoch(QDate(1900, 1, 1));
QDateTime unixStart(QDate(1970, 1, 1));
do
{
newTime.resize(udpsocket->pendingDatagramSize());
udpsocket->read(newTime.data(), newTime.size());
}while(udpsocket->hasPendingDatagrams());
QByteArray TransmitTimeStamp ;
TransmitTimeStamp=newTime.right(8);
quint32 seconds=TransmitTimeStamp[0];
quint8 temp=0;
for(int j=1;j<=3;j++)
{
seconds=seconds<<8;
temp=TransmitTimeStamp[j];
seconds=seconds+temp;
}
time.setTime_t(seconds-epoch.secsTo(unixStart));
std::cout<<time.date().year()<<std::endl;
std::cout<<time.date().month()<<std::endl;
std::cout<<time.date().day()<<std::endl;
std::cout<<time.time().hour()<<std::endl;
std::cout<<time.time().minute()<<std::endl;
setDateTime(time);
udpsocket->disconnectFromHost();
udpsocket->close();
}
void webTimeGetter::connectSucess()
{
qint8 LI=0;
qint8 VN=3;
qint8 MODE=3;
qint8 STRATUM=0;
qint8 POLL=4;
qint8 PREC=-6;
QDateTime epoch(QDate(1900,1,1));
qint32 second=quint32(epoch.secsTo(QDateTime::currentDateTime()));
qint32 temp=0;
QByteArray timeRequest(48, 0);
timeRequest[0]=(LI <<6) | (VN <<3) | (MODE);
timeRequest[1]=STRATUM;
timeRequest[2]=POLL;
timeRequest[3]=PREC & 0xff;
timeRequest[5]=1;
timeRequest[9]=1;
timeRequest[40]=(temp=(second&0xff000000)>>24);
temp=0;
timeRequest[41]=(temp=(second&0x00ff0000)>>16);
temp=0;
timeRequest[42]=(temp=(second&0x0000ff00)>>8);
temp=0;
timeRequest[43]=((second&0x000000ff));
udpsocket->flush();
udpsocket->write(timeRequest);
udpsocket->flush();
}
void webTimeGetter::tryGet()
{
udpsocket->connectToHost(hostList.at(0),123);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。