Commit c6c06710 authored by lyong's avatar lyong

feat:增加RTC故障措施和增加RTC故障写时间

parent 740368c8
......@@ -49,9 +49,15 @@ function setTextColorByText(that, id, value) {
if (value === "测试失败") {
color = "#ff0000";
}
else if (value === "检测中...") {
else if (value === "ING") {
color = "#ff8400";
}
else if (value === "RTCERROR") {
color = "#ff0000";
}
else if (value === "RTCSUCCESS") {
color = "#00ff00";
}
else {
color = "#00ff00";
}
......@@ -60,34 +66,35 @@ function setTextColorByText(that, id, value) {
var testCnt = 3
function updateRtcInfo() {
var rtc = getRtcTime();
that.crtLocalTime = rtc;
//console.log("hwRtc:" + rtc);
setTextColorById(that, 'id_crtLocalTime', that.crtLocalTime);
var values = [that.crtLocalTime];
var allValuesValid = values.every(value => value !== "NA");
console.log("allValuesValid:" + allValuesValid);
// 根据检查结果设置烧录测试状态
function updateRtcInfo() {
const testCnt = 3 //测试3次
var g = getRtcTime();
var rtc = g.time
that.crtLocalTime = rtc;
//console.log("hwRtc:" + rtc);
const allValuesValid = g.err === 0; //0为成功检测到时间
console.log(`[RTC] 第 ${cntTime} 次检测,状态:${allValuesValid == true ? "有效" : "无效"}`);
if (allValuesValid == 1) {
if (allValuesValid) {
cntSuccess += 1;
} else {
cntSuccess = 0;
}
if (cntTime < testCnt) {
that.crtTextRtcText = "检测中...";
setTextColorByText(that, 'id_crtTextRtcText', that.crtTextRtcText);
if (g.err == 1) {
setRtcTime();
console.log("!!g.msg:" + g.msg);
setTextColorByText(that, 'id_crtTextRtcText', "RTCERROR");
that.crtTextRtcText = g.msg + ", 开始初始化RTC时间";
} else {
that.crtTextRtcText = "检测中...";
setTextColorByText(that, 'id_crtTextRtcText', "ING");
}
that.$setY("id_yes", -480);
return; // 不继续判断
}
......@@ -125,7 +132,7 @@ function updateRtcInfo() {
var text = state ? "测试通过" : "测试失败";
that.crtTextRtcText = text + ",如需RTC时间流逝测试,请重进"
setTextColorByText(that, 'id_crtTextRtcText', that.crtTextRtcText);
//globalThis.factoryTest.rtc = state ? 1 : 2;
var testVal = state ? 1 : 2;
......@@ -137,7 +144,7 @@ function updateRtcInfo() {
var global = require("factory_dataStorage.js");
global.setFactoryData("rtc", testVal);
// if (testVal == 2) {
exitTestRTC();
exitTestRTC();
//}
} else {
oneTest();
......@@ -147,21 +154,21 @@ function updateRtcInfo() {
oneTest();
}
} else {
oneTest();
}
// exitTestRTC();
} else {
if (cntSuccess <= 0) {
setRtcTime();
that.crtTextRtcText = g.msg + ",RTC无效效时间!";
setTextColorByText(that, 'id_crtTextRtcText', "RTCERROR");
}
else {
}
}
// that.crtTextRtcText = cntSuccess > testCnt ? "测试通过" : "测试失败";
// setTextColorByText(that, 'id_crtTextRtcText', that.crtTextRtcText);
// globalThis.factoryTest.rtc = cntSuccess > testCnt ? 1 : 2;
// that.$setY("id_yes", cntSuccess > testCnt ? 0 : -480);
// console.log("[RTC] 测试完成,最终状态:" + (cntSuccess > testCnt ? "通过" : "失败"));
// cntTime = 0;
// 可选:停止定时器
// exitTestRTC();
checkAutoFactory();
}
......@@ -172,12 +179,10 @@ function oneTest() {
that.crtfilesLocalTime = iso;
system("echo '" + iso + "' > " + tmpPath);
system("sync");
console.log("[RTC] one tets ok");
setTextColorByText(that, 'id_crtTextRtcText', "ING");
that.crtTextRtcText = "请断电重启 测试第二阶段掉电保存"
setTimeout(() => {
that.crtTextRtcText = "请断电重启 测试第二阶段掉电保存"
}, 300);
}
function checkAutoFactory() {
......@@ -201,59 +206,85 @@ function timeGT(a, b) {
return a.s > b.s;
}
/**
* 读取 RTC 并检测异常
* @returns {obj} {err: 0|1, msg: '', time: ''}
* err=0 正常;err=1 异常
*/
function getRtcTime() {
var partFilePath = "../../../../../dev/rtc1";
const partFilePath = "../../../../../dev/rtc1";
if (!fs.exists(partFilePath)) {
return "NA";
return { err: 1, msg: 'RTC device not found', time: 'NA' };
}
system('hwclock -f /dev/rtc1 > /tmp/hwRtc.txt');
// 1. 执行 hwclock
system('hwclock -f /dev/rtc1 > /tmp/hwRtc.txt 2>&1'); // 把 stderr 也重定向
const raw = fs.read('/tmp/hwRtc.txt');
if (!raw) return 'NA';
if (!raw) return { err: 1, msg: 'Read empty', time: 'NA' };
/* 典型行:Thu Jan 1 18:22:21 1970 0.000000 seconds */
var p = 0;
// 2. 检测 Invalid argument
if (raw.includes('Invalid argument')) {
return { err: 1, msg: 'Invalid argument', time: 'NA' };
}
/* 3. 正常解析年月日时分秒(沿用你原逻辑) */
const monTbl = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let p = 0;
// 星期
while (p < raw.length && raw[p] !== ' ') p++;
while (p < raw.length && raw[p] === ' ') p++;
// 月
var monSt = p;
let monSt = p;
while (p < raw.length && raw[p] !== ' ') p++;
var mon = raw.slice(monSt, p);
const mon = raw.slice(monSt, p);
while (p < raw.length && raw[p] === ' ') p++;
// 日
var daySt = p;
let daySt = p;
while (p < raw.length && raw[p] !== ' ') p++;
var day = raw.slice(daySt, p).trim();
const day = raw.slice(daySt, p).trim();
while (p < raw.length && raw[p] === ' ') p++;
// 时间 HH:MM:SS[.xxxxxx]
var timeSt = p;
// 时间
let timeSt = p;
while (p < raw.length && raw[p] !== ' ') p++;
var timeFull = raw.slice(timeSt, p); // 可能 19:38:24.123456
var time = timeFull.split('.')[0]; // 只取 19:38:24
const timeFull = raw.slice(timeSt, p); // HH:MM:SS[.xxx]
const time = timeFull.split('.')[0];
while (p < raw.length && raw[p] === ' ') p++;
// 年
var yearSt = p;
let yearSt = p;
while (p < raw.length && raw[p] !== ' ') p++;
var year = raw.slice(yearSt, p);
const year = raw.slice(yearSt, p);
const monTbl = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const mo = monTbl.indexOf(mon) + 1;
if (mo === 0) return 'NA';
if (mo === 0) return { err: 1, msg: 'Month parse fail', time: '1970-01-01T00:00:00' };
const mm = String(mo).padStart(2, '0');
const dd = String(day).padStart(2, '0');
return `${year}-${mm}-${dd}T${time}:00`;
}
const iso = `${year}-${String(mo).padStart(2, '0')}-${String(day).padStart(2, '0')}T${time}:00`;
// 4. 1970 年判定
if (year.startsWith('1970')) {
return { err: 1, msg: 'RTC time is 1970', time: iso };
}
// 5. 正常
return { err: 0, msg: '', time: iso };
}
function setRtcTime() {
//date -s "1970-10-03 19:50:00"
//hwclock -w -f /dev/rtc1
var librtc = require("rtc.js");
librtc.ctrl("set", {
year: userData.time.year,
month: userData.time.month,
day: userData.time.day,
hour: userData.time.hour,
minute: userData.time.minute,
second: 0,
});
}
module.exports = {
factoryTestRTCInit: factoryTestRTCInit,
exitTestRTC: exitTestRTC,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment