60 lines
1.5 KiB
PowerShell
60 lines
1.5 KiB
PowerShell
$filePath = 'c:\Users\MyName\Desktop\PLC_Comm_D13_VCL\uMain.pas'
|
|
|
|
$oldCode = @"
|
|
procedure TfMain.OnMQTTMessage(const ATopic, APayload: string);
|
|
begin
|
|
// mmMQTTmsg.Lines.Add(ATopic+' '+APayload);
|
|
Edit2.Text := ATopic+' '+APayload;
|
|
end;
|
|
"@
|
|
|
|
$newCode = @"
|
|
procedure TfMain.OnMQTTMessage(const ATopic, APayload: string);
|
|
var
|
|
JsonObj: TJSONObject;
|
|
i, DONum: Integer;
|
|
Pair: TJSONPair;
|
|
KeyStr, ValStr: string;
|
|
begin
|
|
// mmMQTTmsg.Lines.Add(ATopic+' '+APayload);
|
|
Edit2.Text := ATopic+' '+APayload;
|
|
|
|
try
|
|
JsonObj := TJSONObject.ParseJSONValue(APayload) as TJSONObject;
|
|
if Assigned(JsonObj) then
|
|
begin
|
|
try
|
|
for i := 0 to JsonObj.Count - 1 do
|
|
begin
|
|
Pair := JsonObj.Pairs[i];
|
|
KeyStr := Pair.JsonString.Value;
|
|
ValStr := Pair.JsonValue.Value;
|
|
|
|
if (Length(KeyStr) >= 5) and (Copy(KeyStr, 1, 3) = 'DO_') then
|
|
begin
|
|
DONum := StrToIntDef(Copy(KeyStr, 4, Length(KeyStr) - 3), -1);
|
|
if DONum > 0 then
|
|
begin
|
|
if ValStr = '1' then
|
|
SendModbus(slvID, cmdFSC, 15 + DONum, 1)
|
|
else if ValStr = '0' then
|
|
SendModbus(slvID, cmdFSC, 15 + DONum, 0);
|
|
end;
|
|
end;
|
|
end;
|
|
finally
|
|
JsonObj.Free;
|
|
end;
|
|
end;
|
|
except
|
|
on E: Exception do printlog('MQTT Msg Error: ' + E.Message);
|
|
end;
|
|
end;
|
|
"@
|
|
|
|
$content = Get-Content $filePath -Encoding Default -Raw
|
|
|
|
$content = $content.Replace($oldCode, $newCode)
|
|
|
|
Set-Content -Path $filePath -Value $content -Encoding Default
|