unit astevent_utils; {< Contains objects for retrieving and setting data in AMI packets in standard AMI packet format.} { This file is part of AMIProxyPal. AMIProxyPal is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Foobar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Foobar. If not, see . } {$mode objfpc}{$H+} interface uses Classes, SysUtils, strutils ; type {: Used to get data from a packet. } TAstPacketUtil = class(TObject) public class function GetPacketValue(const APacket, AName: string): string; class function GetPacketType(const APacket: string): string; class function GetEventName(const APacket: string): string; class function GetActionID(const APacket: string): string; class procedure SetActionID(var APacket, ID: string); end; implementation { TAstPacketUtil } class function TAstPacketUtil.GetPacketValue( const APacket, AName: string) : string; var sl: TStringList; iCounter, iPOS: integer; sLine: string; sPacket: string; begin result := ''; sl := TStringList.Create; try sPacket := APacket; sl.Text := sPacket; For iCounter := 0 to sl.Count -1 do begin sLine := sl[iCounter]; if POS(lowercase(AName), lowercase(sLine)) > 0 then begin iPOS := POS(':', sLine); result := Trim(Copy(sLine, iPOS + 1, 256)); exit; end; end; finally sl.free; end; end; class function TAstPacketUtil.GetPacketType( const APacket: string) : string; var SL: TStringList; sTemp: string; iPOS: integer; begin sl := TStringList.create; try sl.Text := APacket; // second line has either Event: or Response: sTemp := sl[0]; iPOS := POS(':', sTemp); if iPOS<= 0 then raise exception.Create('Invalid packet format'); Result := Copy(sTemp, 1, iPOS -1); finally sl.free; end; end; class function TAstPacketUtil.GetEventName( const APacket: string) : string; begin result := TAstPacketUtil.GetPacketValue(APacket, 'Event'); end; class function TAstPacketUtil.GetActionID( const APacket: string) : string; begin result := TAstPacketUtil.GetPacketValue(APacket, 'ActionID'); end; class procedure TAstPacketUtil.SetActionID( var APacket, ID: string) ; var sl: TStringList; sPacket: string; begin sl := TStringList.Create; try sl.CaseSensitive := false; sl.NameValueSeparator := ':'; sl.Text := APacket; sl.Values['ActionID'] := ' ' + ID + '|' + Trim(sl.Values['ActionID']); sPacket := sl.Text; APacket := sPacket; finally sl.free; end; end; end.