Initial commit - Delphi MES client project
This commit is contained in:
@@ -0,0 +1,342 @@
|
||||
unit uFile3Json;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
System.Classes, System.SysUtils, System.JSON, System.RegularExpressions, StrUtils;
|
||||
|
||||
type
|
||||
TWaterData = record
|
||||
DataValue: Integer;
|
||||
Status: string;
|
||||
ID: string;
|
||||
Sn: string;
|
||||
HasData: Boolean;
|
||||
end;
|
||||
|
||||
TFileMonitor = class
|
||||
private
|
||||
FInWaterData: TWaterData;
|
||||
FOutWaterData: TWaterData;
|
||||
FDebugInfo: TStringList;
|
||||
function ExtractID(const Line: string): string;
|
||||
function ExtractSn(const Line: string): string;
|
||||
function ExtractStatus(const Line: string): string;
|
||||
function ExtractData(const Line: string): string;
|
||||
function ExtractLastValue(const DataStr: string): Integer;
|
||||
procedure AddDebugInfo(const Info: string);
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
function ProcessFileContent(const Content: TStringList): string;
|
||||
property DebugInfo: TStringList read FDebugInfo;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
constructor TFileMonitor.Create;
|
||||
begin
|
||||
inherited;
|
||||
FDebugInfo := TStringList.Create;
|
||||
FInWaterData.HasData := False;
|
||||
FOutWaterData.HasData := False;
|
||||
end;
|
||||
|
||||
destructor TFileMonitor.Destroy;
|
||||
begin
|
||||
FDebugInfo.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TFileMonitor.AddDebugInfo(const Info: string);
|
||||
begin
|
||||
FDebugInfo.Add(Info);
|
||||
end;
|
||||
|
||||
function TFileMonitor.ExtractID(const Line: string): string;
|
||||
var
|
||||
StartPos, EndPos: Integer;
|
||||
begin
|
||||
Result := '';
|
||||
StartPos := Pos('ID:', Line);
|
||||
if StartPos > 0 then
|
||||
begin
|
||||
EndPos := PosEx(',', Line, StartPos);
|
||||
if EndPos > StartPos then
|
||||
Result := Copy(Line, StartPos + 3, EndPos - StartPos - 3)
|
||||
else
|
||||
Result := Copy(Line, StartPos + 3, MaxInt);
|
||||
|
||||
AddDebugInfo('提取ID: ' + Result);
|
||||
end
|
||||
else
|
||||
begin
|
||||
AddDebugInfo('未找到ID');
|
||||
end;
|
||||
end;
|
||||
|
||||
function TFileMonitor.ExtractSn(const Line: string): string;
|
||||
var
|
||||
StartPos, EndPos: Integer;
|
||||
begin
|
||||
Result := '';
|
||||
StartPos := Pos('SN码:', Line);
|
||||
if StartPos > 0 then
|
||||
begin
|
||||
EndPos := PosEx(',', Line, StartPos);
|
||||
if EndPos > StartPos then
|
||||
Result := Copy(Line, StartPos + 3, EndPos - StartPos - 3)
|
||||
else
|
||||
Result := Copy(Line, StartPos + 3, MaxInt);
|
||||
|
||||
AddDebugInfo('提取SN码: ' + Result);
|
||||
end
|
||||
else
|
||||
begin
|
||||
AddDebugInfo('未找到SN码');
|
||||
end;
|
||||
end;
|
||||
|
||||
function TFileMonitor.ExtractStatus(const Line: string): string;
|
||||
var
|
||||
StartPos, EndPos: Integer;
|
||||
begin
|
||||
Result := '';
|
||||
StartPos := Pos('卡关:', Line);
|
||||
if StartPos > 0 then
|
||||
begin
|
||||
StartPos := StartPos + 3;
|
||||
EndPos := Length(Line);
|
||||
Result := Copy(Line, StartPos, EndPos - StartPos + 1);
|
||||
Result := Trim(Result);
|
||||
AddDebugInfo('提取状态: ' + Result);
|
||||
end
|
||||
else
|
||||
begin
|
||||
AddDebugInfo('未找到状态');
|
||||
end;
|
||||
end;
|
||||
|
||||
function TFileMonitor.ExtractData(const Line: string): string;
|
||||
var
|
||||
StartPos, EndPos: Integer;
|
||||
begin
|
||||
Result := '';
|
||||
StartPos := Pos('[', Line);
|
||||
EndPos := Pos(']', Line);
|
||||
if (StartPos > 0) and (EndPos > StartPos) then
|
||||
begin
|
||||
Result := Copy(Line, StartPos + 1, EndPos - StartPos - 1);
|
||||
AddDebugInfo('提取数据: ' + Result);
|
||||
end
|
||||
else
|
||||
begin
|
||||
AddDebugInfo('未找到数据');
|
||||
end;
|
||||
end;
|
||||
|
||||
function TFileMonitor.ExtractLastValue(const DataStr: string): Integer;
|
||||
var
|
||||
Values: TStringList;
|
||||
LastValue: string;
|
||||
I: Integer;
|
||||
begin
|
||||
Result := 0;
|
||||
Values := TStringList.Create;
|
||||
try
|
||||
try
|
||||
Values.Delimiter := ',';
|
||||
Values.StrictDelimiter := True;
|
||||
Values.DelimitedText := DataStr;
|
||||
|
||||
AddDebugInfo('数据分割结果: ' + IntToStr(Values.Count) + ' 个值');
|
||||
|
||||
// 从后往前找第一个有效的数值
|
||||
for I := Values.Count - 1 downto 0 do
|
||||
begin
|
||||
LastValue := Trim(Values[I]);
|
||||
AddDebugInfo('检查值: ' + LastValue);
|
||||
|
||||
try
|
||||
if TryStrToInt(LastValue, Result) then
|
||||
begin
|
||||
AddDebugInfo('找到有效值: ' + IntToStr(Result));
|
||||
Exit;
|
||||
end;
|
||||
except
|
||||
on E: Exception do
|
||||
begin
|
||||
AddDebugInfo('转换数值时发生异常: ' + E.Message);
|
||||
Result := 0;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
AddDebugInfo('未找到有效数值');
|
||||
except
|
||||
on E: Exception do
|
||||
begin
|
||||
AddDebugInfo('处理数据值时发生异常: ' + E.Message);
|
||||
Result := 0;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
Values.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TFileMonitor.ProcessFileContent(const Content: TStringList): string;
|
||||
var
|
||||
I: Integer;
|
||||
Line, CurrentID, Status, DataStr, CurrentSn: string;
|
||||
LastValue: Integer;
|
||||
LastID, LastSn: string;
|
||||
RootObj: TJSONObject;
|
||||
InWaterDataArray, OutWaterDataArray: TJSONArray;
|
||||
HasInWater, HasOutWater: Boolean;
|
||||
begin
|
||||
|
||||
AddDebugInfo('开始处理文件内容');
|
||||
|
||||
if Content = nil then
|
||||
begin
|
||||
AddDebugInfo('错误:Content为nil');
|
||||
Result := '{}';
|
||||
Exit;
|
||||
end;
|
||||
|
||||
AddDebugInfo('文件行数: ' + IntToStr(Content.Count));
|
||||
|
||||
// 初始化
|
||||
LastID := '';
|
||||
LastSn := '';
|
||||
FInWaterData.HasData := False;
|
||||
FOutWaterData.HasData := False;
|
||||
HasInWater := False;
|
||||
HasOutWater := False;
|
||||
|
||||
// 处理每一行
|
||||
for I := 0 to Content.Count - 1 do
|
||||
begin
|
||||
Line := Content[I];
|
||||
AddDebugInfo('处理行 ' + IntToStr(I) + ': ' + Line);
|
||||
|
||||
CurrentID := ExtractID(Line);
|
||||
CurrentSn := ExtractSn(Line);
|
||||
|
||||
// 如果ID为空,跳过
|
||||
if CurrentID = '' then
|
||||
Continue;
|
||||
|
||||
// 记录最后一个ID
|
||||
LastID := CurrentID;
|
||||
LastSn := CurrentSn;
|
||||
|
||||
Status := ExtractStatus(Line);
|
||||
|
||||
// 检查是否是入水距离值
|
||||
if (Pos('入水距离值', Line) > 0) then
|
||||
begin
|
||||
AddDebugInfo('找到入水距离值');
|
||||
DataStr := ExtractData(Line);
|
||||
if DataStr <> '' then
|
||||
begin
|
||||
LastValue := ExtractLastValue(DataStr);
|
||||
|
||||
// 只保留状态为"合格"的数据
|
||||
// if Status = '合格' then
|
||||
begin
|
||||
AddDebugInfo('入水距离状态合格,记录数据');
|
||||
FInWaterData.DataValue := LastValue;
|
||||
FInWaterData.Status := Status;
|
||||
FInWaterData.Sn := CurrentSn;
|
||||
FInWaterData.ID := CurrentID;
|
||||
FInWaterData.HasData := True;
|
||||
HasInWater := True;
|
||||
end
|
||||
// else
|
||||
// begin
|
||||
// AddDebugInfo('入水距离状态不合格: ' + Status);
|
||||
// end;
|
||||
end;
|
||||
end
|
||||
// 检查是否是出水距离值
|
||||
else if (Pos('出水距离值', Line) > 0) then
|
||||
begin
|
||||
AddDebugInfo('找到出水距离值');
|
||||
DataStr := ExtractData(Line);
|
||||
if DataStr <> '' then
|
||||
begin
|
||||
LastValue := ExtractLastValue(DataStr);
|
||||
|
||||
// 只保留状态为"合格"的数据
|
||||
// if Status = '合格' then
|
||||
begin
|
||||
AddDebugInfo('出水距离状态合格,记录数据');
|
||||
FOutWaterData.DataValue := LastValue;
|
||||
FOutWaterData.Status := Status;
|
||||
FOutWaterData.ID := CurrentID;
|
||||
FOutWaterData.Sn := CurrentSn;
|
||||
FOutWaterData.HasData := True;
|
||||
HasOutWater := True;
|
||||
end
|
||||
// else
|
||||
// begin
|
||||
// AddDebugInfo('出水距离状态不合格: ' + Status);
|
||||
// end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
AddDebugInfo(Format('处理完成,最后ID%s,Sn%s: ' , [LastID,LastSn]));
|
||||
AddDebugInfo('入水数据有效: ' + BoolToStr(HasInWater, True));
|
||||
AddDebugInfo('出水数据有效: ' + BoolToStr(HasOutWater, True));
|
||||
|
||||
RootObj := TJSONObject.Create;
|
||||
try
|
||||
// 添加合并ID(如果需要)
|
||||
if FInWaterData.ID<>'' then
|
||||
begin
|
||||
RootObj.AddPair('SN码', FInWaterData.Sn.Replace(':','').Replace(':','').Replace(';','')); // 或者根据需求选择
|
||||
RootObj.AddPair('ID', FInWaterData.ID); // 或者根据需求选择
|
||||
end
|
||||
else
|
||||
begin
|
||||
RootObj.AddPair('SN码', FOutWaterData.Sn.Replace(':','').Replace(':','').Replace(';','')); // 或者根据需求选择
|
||||
RootObj.AddPair('ID', FOutWaterData.ID); // 或者根据需求选择
|
||||
end;
|
||||
|
||||
// 添加入水数据
|
||||
if HasInWater then
|
||||
begin
|
||||
RootObj.AddPair('距离', TJSONNumber.Create(FInWaterData.DataValue));
|
||||
// RootObj.AddPair('入水SN码', FInWaterData.Sn);
|
||||
RootObj.AddPair('入水卡关状态', FInWaterData.Status);
|
||||
AddDebugInfo('添加入水数据: ' + IntToStr(FInWaterData.DataValue));
|
||||
end
|
||||
else
|
||||
begin
|
||||
AddDebugInfo('无有效入水数据');
|
||||
end;
|
||||
|
||||
// 添加出水数据
|
||||
if HasOutWater then
|
||||
begin
|
||||
RootObj.AddPair('出水距离', TJSONNumber.Create(FOutWaterData.DataValue));
|
||||
// RootObj.AddPair('出水SN码', FOutWaterData.Sn);
|
||||
RootObj.AddPair('卡关', FOutWaterData.Status);
|
||||
AddDebugInfo('添加出水数据: ' + IntToStr(FOutWaterData.DataValue));
|
||||
end
|
||||
else
|
||||
begin
|
||||
AddDebugInfo('无有效出水数据');
|
||||
end;
|
||||
|
||||
Result := RootObj.ToString;
|
||||
AddDebugInfo('最终结果: ' + Result);
|
||||
finally
|
||||
RootObj.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
Reference in New Issue
Block a user