一种支持自刷新的状态信息输出
缘起
在编写与设备相关的代码时,有时候需要将部分状态信息展示给用户。状态信息有时候其基本结构并非固定,而是需要通过某种方式读取配置信息,自动生成。
基于代码快速生成的需要,这部分状态信息通常会是一些文本信息。并且这些信息在向最终用户展示时,需要支持自刷新。这里展示了一种不涉及图形界面的信息展示技术。它直接以静态html文件为载体。利用html本身的自刷新功能,不断周期性自动加载.html文件,完成实时状态信息的展示。
最终效果
步骤
编辑html模板文件
注意,里面包含文件自动刷新的部分,以及两个桩埋入点:swxx_title,swxx_body
<meta http-equiv="refresh" content="3">
<html>
<head>
<title>铁鞋标定程序</title>
</head>
<body>
<span id=swxxDate></span><span id=swxxTime></span><br>
<table>
<tr>
<swxx_title/>
</tr>
<tr>
<td>
<p>
<swxx_body/>
</p>
</td>
</tr>
</table>
<script type="text/javascript">
var today=new Date()
var year=today.getFullYear()
var month=today.getMonth()+1
var day=today.getDate()
var day2=today.getDay()
if(day2==1)
day3="一"
else if(day2==2)
day3="二"
else if(day2==3)
day3="三"
else if(day2==4)
day3="四"
else if(day2==5)
day3="五"
else if(day2==6)
day3="六"
else if(day2==7)
day3="七"
var hour=today.getHours()
var min=today.getMinutes()
var sec=today.getSeconds()
var sec2=today.getMilliseconds()
document.getElementById("swxxDate").innerHTML = ""+year+"年"+month+"月"+day+"日"+"星期"+day3;
document.getElementById("swxxTime").innerHTML = "北京时间"+(hour<10?"0":"")+hour+":"+(min<10?"0":"")+min+":"+(sec<10?"0":"")+sec;
</script>
</body>
</html>
.html文件输出
QByteArray PtlTransfer::Transfer(quint8 *buf, int len)
{
QByteArray ba;
DATA_LOAD dl;
//...
QString fileName = QString("%1.%2_%3.%4.htm").arg(rawStationYard._allForeBytes&0xffffff).arg(rawStationYard._bytesAlone.yard).arg(outStationYard._allForeBytes&0xffffff).arg(outStationYard._bytesAlone.yard);
QFile file("temp.dat");
if(gAppConfig.decodeToFile) file.open(QIODevice::Text | QIODevice::WriteOnly | QIODevice::Truncate);
else file.open(QIODevice::Text|QIODevice::ReadOnly);
QTextStream out(&file);
QString strTemplate;
if(gAppConfig.decodeToFile)
{
QFile ft("template.dat");
ft.open(QIODevice::Text|QIODevice::ReadOnly);
QTextStream tst(&ft);
strTemplate = tst.readAll();
QString dumb = QString(QObject::tr("<p>文件名:[原始站号.场号]_[转换后站号.场号]%1</p><br/>")).arg(fileName);
strTemplate = strTemplate.replace("<swxx_title/>", dumb);
}
TgqDataLoadHead &dh= *(TgqDataLoadHead *)(buf+2);
QString body;
QTextStream text_stream(&body); text_stream<<"<p>";
for(int i = 0; i<dh.tgqCnts; ++i)
{
quint8 *pId = &(buf[2+sizeof(TgqDataLoadHead) + i*2]);
text_stream.setFieldWidth(4);
text_stream.setFieldAlignment(QTextStream::AlignRight);
text_stream.setPadChar(' ');
if(gAppConfig.decodeToFile) text_stream << *pId;
*pId = MapTGQId(rawStationYard, *pId);
if(gAppConfig.decodeToFile)
{
text_stream << QString(QObject::tr("[转换后 : "));
text_stream.setFieldWidth(4);
text_stream.setFieldAlignment(QTextStream::AlignRight);
text_stream.setPadChar(' ');
text_stream << *pId << "] ";
static QString wz = QString(QObject::tr("AA"));
static QString st = QString(QObject::tr("BB"));
static QString xt = QString(QObject::tr("CC"));
static QString gz = QString(QObject::tr("DD"));
static QString er = QString(QObject::tr("!!"));
int status = *(pId+1);
switch(status)
{
case 0: text_stream << wz<<" ";break;
case 1: text_stream << st<<" ";break;
case 2: text_stream << xt<<" ";break;
case 3: text_stream << gz<<" ";break;
default: text_stream << er<<" ";break;
}
if((i!=0 && (((i+1)%5)==0))) text_stream<<"<br/>";
}
}
text_stream<<"</p>";
body = body.replace(" ", " ");
strTemplate = strTemplate.replace("<swxx_body/>", body);
if(gAppConfig.decodeToFile)
{
out<<strTemplate;
file.flush();
}
file.close();
if(QFile::exists(fileName)) QFile::remove(fileName);
QFile::copy("temp.dat", fileName); //最后拷贝,减少刷新时文件内容丢失的情形。
//...
return ba;
}
实际使用时,只要用浏览器打开生成的.html文件即可观察最新的状态信息输出。