interface IEnteredExitedSubtotal {
Hour: number;
Today: number;
Total: number;
TotalInTimeSection: number;
}
interface IInsideSubtotal {
Total: number;
}
interface CameraData {
Channel: number;
EnteredSubtotal: IEnteredExitedSubtotal;
ExitedSubtotal: IEnteredExitedSubtotal;
InsideSubtotal: IInsideSubtotal;
RuleName: string;
UTC: number;
}
let data = `summary.Channel=123
summary.EnteredSubtotal.Hour=22
summary.EnteredSubtotal.Today=60
summary.EnteredSubtotal.Total=1769
summary.EnteredSubtotal.TotalInTimeSection=60
summary.ExitedSubtotal.Hour=19
summary.ExitedSubtotal.Today=52
summary.ExitedSubtotal.Total=1705
summary.ExitedSubtotal.TotalInTimeSection=52
summary.InsideSubtotal.Total=324
summary.RuleName=NumberStat
summary.UTC=1565870249`;
resolveCameraData(data: string): CameraData {
// 定義資料格式
let result = {
Channel: 0,
EnteredSubtotal: {
Hour: 0,
Today: 0,
Total: 0,
TotalInTimeSection: 0,
},
ExitedSubtotal: {
Hour: 0,
Today: 0,
Total: 0,
TotalInTimeSection: 0,
},
InsideSubtotal: { Total: 0 },
RuleName: '',
UTC: 0,
};
// 用 \n 拆分字串 為 陣列
let tempData1 = data.split('\n');
// console.log(' ~ ', tempData1);
for (let loopData1 of tempData1) {
// 去掉頭尾空白
loopData1 = loopData1.trim();
// 去掉特殊符號,以 空白 取代
loopData1 = loopData1.replace('\r', '');
loopData1 = loopData1.replace('\t', '');
// summary.,所以用 空白 取代
loopData1 = loopData1.replace('summary.', '');
// console.log('loopData1 ~ ', loopData1);
// 用 = 拆分字串 為 陣列
let tempData2 = loopData1.split('=');
console.log('tempData2 ~ ', tempData2);
if (tempData2.length < 2) {
continue;
}
if (result[tempData2[0]] !== undefined) {
// 判斷 result 裡面的變數型別
let typeofResult = typeof result[tempData2[0]];
switch (typeofResult) {
case 'string':
result[tempData2[0]] = tempData2[1];
break;
case 'number':
if (!isNaN(parseInt(tempData2[1]))) {
result[tempData2[0]] = parseInt(tempData2[1]);
}
break;
}
}
let tempData3 = tempData2[0].split('.');
// console.log('tempData3 ~ ', tempData3);
if (tempData3.length < 2) {
continue;
}
if (isNaN(parseInt(tempData2[1]))) {
continue;
}
if (
result[tempData3[0]] !== undefined &&
result[tempData3[0]][tempData3[1]] !== undefined
) {
result[tempData3[0]][tempData3[1]] =
parseInt(tempData2[1]);
}
}
return result;
}
文章標籤
全站熱搜
