{******************************************************************************}
{ @UnitName    : HDFileOperate_u                                                 }
{ @Project     : AcuteEye                                                      }
{ @Copyright   :                                                               }
{ @Author      : SKZHEN                                                       }
{ @CreateDate  : 2012-08-08 20:58:38                                            }
{ @LastUpdate  : 2012-08-08 18:49:49 by SKZHEN                                 }
{ @Description :                                                               }
{ @Comment     :                                                               }
{ @History     :                                                               }
{******************************************************************************}

unit HDFileOperate;

interface

uses SysUtils, Forms, iniFiles;

const
  MaxHDCameraCount = 36;
  HDFileName = 'HDCamera.ini';

type
  //接口操作类型
  THDSetType = (stAdd, stUpdate);
  //高清摄像头基本信息
  THDCameraInfo = packed record
    IP: String[25]; //高清摄像头IP地址
    CameraPort: Integer;  //视频端口
    THeadPort: Integer;   //云台端口
    CameraLength: Integer; //单个文件录像时长(单位秒)
  end;
  //高清摄像头信息控制
  THDCamerasData = class
  private
    HDFile: TIniFile;
    function OpenHDFile: Boolean;
    function CloseHDFile: Boolean;
  public
    HDUserName: string;
    HDPassword: string;
    //是否显示主码流
    IfShowMainCode: Boolean;
    //连接方式
    bIsTcp: Integer;
    HDCameraData: THDCAmeraInfo;

    function InitHDCameraData: Boolean;   //初始化高清摄像头信息
    function SaveHDCameraData: Boolean;   //保存高清摄像头信息
                                          //设置摄像头信息  AIndex=0默认为修改，为其他则为添加
                                          //AIndex 摄像头索引号，从1开始，也可以看成是总高清摄像头数
    function SetHDCameraData(AIP: String; ACameraPort, ATHeadPort:Integer;
      UserName, Password: String; UseTCP: Integer; MainCode: Boolean): Boolean;
  end;

  procedure InitHDCamerasData;

var
  HDCamerasData: THDCamerasData;


implementation

procedure InitHDCamerasData;
begin
  HDCamerasData := THDCamerasData.Create;
  HDCamerasData.InitHDCameraData;
end;


{ THDCamerasData }
function THDCamerasData.CloseHDFile: Boolean;
begin
  Result := True;
  try
    if Assigned(HDFile) then
      HDFile.Free;
  except
    Result := False;
  end;
end;

function THDCamerasData.InitHDCameraData: Boolean;
var
  I: Integer;
begin
  Result := True;
  try
    if not openHDFile then Exit;
    HDUserName := HDFile.ReadString('CAMERA', 'UserName', '1');
    HDPassword := HDFile.ReadString('CAMERA', 'Password', '1');
    IfShowMainCode := HDFile.ReadBool('CAMERA', 'IfShowMainCode', True);
    bIsTcp := HDFile.ReadInteger('CAMERA', 'bIsTcp', 0);
    HDCameraData.IP := HDFile.ReadString('CAMERA', 'IP', '0.0.0.0');
    HDCameraData.CameraPort := HDFile.ReadInteger('CAMERA', 'CameraPort', 9000);
    HDCameraData.TheadPort := HDFile.ReadInteger('CAMERA', 'TheadPort', 9001);
    HDCameraData.CameraLength := HDFile.ReadInteger('CAMERA', 'CameraLength', 240);


    CloseHDFile;
  except
    Result := False;
  end;
end;

function THDCamerasData.OpenHDFile: Boolean;
begin
  Result := True;
  try
    HDFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + HDFileName);
  except
    Result := False;
  end;

end;

function THDCamerasData.SaveHDCameraData: Boolean;
var
  I: Integer;
begin
  Result := True;
  try
    if not openHDFile then Exit;
    HDFile.WriteString('CAMERA', 'UserName', HDUserName);
    HDFile.WriteString('CAMERA', 'Password', HDPassword);
    HDFile.WriteBool('CAMERA', 'IfShowMainCode', IfShowMainCode);
    HDFile.WriteInteger('CAMERA', 'bIsTcp', bIsTcp);
    HDFile.WriteString('CAMERA', 'IP', HDCameraData.IP);
    HDFile.WriteInteger('CAMERA', 'CameraPort', HDCameraData.CameraPort);
    HDFile.WriteInteger('CAMERA', 'TheadPort', HDCameraData.TheadPort);
    CloseHDFile;
  except
    Result := False;
  end;
end;

function THDCamerasData.SetHDCameraData(AIP: String; ACameraPort, ATHeadPort:Integer;
  UserName, Password: String; UseTCP: Integer; MainCode: Boolean): Boolean;
begin
  Result := True;
  try
    HDUserName := UserName;
    HDPassword := Password;
    bIsTcp := UseTCP;
    IfShowMainCode := MainCode;
    HDCameraData.IP := AIP;
    HDCameraData.CameraPort := ACameraPort;
    HDCameraData.TheadPort := ATHeadPort;

    SaveHDCameraData;
  except
    Result := False;
  end;

  saveHDCameraData;
end;

end.
