unit SyncSDKDefine;

interface

{$IFDEF fpc}
{$MODE delphi}
{$ENDIF}

uses SysUtils;

const
  // 操作类型定义
  // 下载   download
  Sync_Action_DownLoad = $0;
  // 上传   upload
  Sync_Action_UpLoad = $1;
  // 删除   delete
  Sync_Action_Delete = $2;
  //

  // 协议类型定义   Protocol Type
  SDK_ProtocolType_None = 0;
  SDK_ProtocolType_Sttp = 1;

  // 数据类型定义   Data type define
  // 无                 //nil
  Sync_DataType_nil = $FFFF;
  // 单品               //Goods ,plu
  Sync_DataType_PLU = $0000;
  // 部门              //Department
  Sync_DataType_Department = $0001;
  // 单位              //unit of weight
  Sync_DataType_Unit = $0002;
  // 热键             //Common product hotkeys
  Sync_DataType_Hotkey = $0003;
  // 自定义条码      //Custom barcode
  Sync_DataType_CustomBarcode = $0004;
  // 信息1          //Note1
  Sync_DataType_Note1 = $0005;
  // 信息2          //Note2
  Sync_DataType_Note2 = $0006;
  // 信息3          //Note3
  Sync_DataType_Note3 = $0007;
  // 信息4          //Note4
  Sync_DataType_Note4 = $0008;
  // 广告词         //advertising slogan
  Sync_DataType_Advertisement = $000B;
  // 系统参数       //system parameters
  Sync_DataType_SystemOptions = $000C;
  // 时间          // time
  Sync_DataType_Time = $000D;
  // 操作员        //operator
  Sync_DataType_Operator = $000E;
  // wifi
  Sync_DataType_Wifi = $000F;
  // 流水帐 支持上传和删除，不支持下载
  // The journal supports uploading and deleting, but does not support downloading
  Sync_DataType_UPDeleteWaterRecord = $0011;

  // 下载C-K1参数   //download C-K1 parameter
  Sync_CK1_Parameter = $0013;
  // 下载C-K1热键  //download C-K1 hotkey
  Sync_CK1_HotKey = $0014;

  // STTPLog
  Sync_DataType_STTPLog = $0015;
  // ChangePriceLog
  Sync_DataType_ChangePriceLog = $0016;

  // Weight
  Sync_DataType_ScaleWeight = $0017;

  // NewChangePriceLog
  Sync_DataType_NewChangePriceLog = $0018;
  // UpLogFileZip
  Sync_DataType_UpLogFileZip = $0019;

  // 上传文件 下位机数据库  //Upload files to the lower computer database
  Sync_DataType_UpDBFile = $001B;

  // 上传指定LFCode的商品资料   //Upload specified LFCode Product information
  Sync_DataType_OnePLU = $001C;

  // 字库         //font library
  Sync_DataType_Font = $1000;
  // 下位机主程序 //Main program of lower computer
  Sync_DataType_Firmware = $1001;
  // AD模块    //AD module
  Sync_DataType_ADModule = $1002;
  // 打印模块   //Printing module
  Sync_DataType_PrinterModule = $1003;

  // 标签元件  //Label components
  Sync_DataType_Label = $2000;
  // 标签背纹   //Tag back pattern
  Sync_DataType_LabelBackGround = $2001;
  // 标签文件  //label file
  Sync_DataType_LabelFile = $2002;

  // 下载压缩文件 //Download compressed files
  Sync_DataType_ZipFile = $3000;
  // CK1的热键    // CK1 Hot keys
  Sync_DataType_CK1_Hotkey = $3001;
  // 下载CK1的程序   //download CK1 The program
  Sync_DataType_CK1_Program = $3002;
  //触控秤参数 //Sync_DataType_TouchParamSetting
  Sync_DataType_TouchParamSetting = $3003;
  //上传下载触控秤热键     //Upload and download touch scale hotkeys
  Sync_DataType_TouchScaleHotKeySetting = $3004;

// 错误信息 // Error Information
  // 执行成功 // Execution Successful
  SDK_Err_Success = $0;
  // 进度事件 // Progress Event
  SDK_Err_Progress = $1;
  // 手动停止 // Manual Stop
  SDK_Err_Terminate = $2;

  // 错误信息 // Error Information
  // 不支持的协议类型 // Unsupported Protocol Type
  SDK_Err_ProtocolTypeNotSupport = $100;
  // 不支持的数据类型 // Unsupported Data Type
  SDK_Err_DataTypeNotSupport = $101;
  // 无法打开输入文件 // Cannot Open Input File
  SDK_Err_CannotOpenInputFile = $102;
  // 字段数与内容数不匹配 // Field Count Mismatch Content Count
  SDK_Err_ImportDataFailed = $103;
  // 通讯数据异常 // Communication Data Exception
  SDK_Err_CommDataFailed = $104;
  // 解析数据异常 // Parse Data Exception
  SDK_Err_ParseDataFailed = $105;
  // 无法创建输出文件 // Cannot Create Output File
  SDK_Err_CannotCreateOutputFile = $106;
  // 通信Param值错误超过次数 // Param Value Error Exceeded Limit
  SDK_Err_ParamError = $107;
  // 无法连接 // Cannot Connect
  SDK_Err_CannotConnect = $108;

  // 没有此命令 // Command Not Found
  SDK_Err_NotHaveThisCommand = $109;
  // 数据超长 // Data Out of Range
  SDK_Err_DataOutOfRange = $10A;
  // 写入失败 // Write Error
  SDK_Err_WriteError = $10B;
  // 剩余空间不足 // Disk Space Insufficient
  SDK_Err_DiskNotEnough = $10C;
  // 读取失败 // Read Error
  SDK_Err_ReadError = $10D;
  // 包号错误 // Package Number Error
  SDK_Err_PackageNoError = $10E;
  // 命令错误 // Command Error
  SDK_Err_CommandError = $10F;
  // 数据错误 // Data Error
  SDK_Err_DataError = $110;
  // 缺纸 // Paper Out
  SDK_Err_MissPaper = $111;
  // MD5校验失败 // MD5 Check Failed
  SDK_Err_MD5CheckError = $112;

  // 错误应答包数据长度不是1错误 // Error Response Package Length Not 1
  SDK_Err_ErrorPackageLenError = $113;
  // 应答包数据长度不对 // Response Package Length Incorrect
  SDK_Err_PackageLenError = $114;
  // 数据错误, 且不是错误命令 // Data Error But Not Error Command
  SDK_Err_DataErrNotErrCode = $115;

  // 下位机没有此号标签 // Device Missing Label Number
  SDK_Err_HaveNoThisLabelNo = $116;
  // 下载程序时，MD5校验超时 // MD5 Check Timeout During Download
  SDK_Err_MD5WaitTimeOut = $117;
  // 没有收到机器的回应包 // No Response From Device
  SDK_Err_DeviceNoRespond = $118;
  // 文件大小不正确 // File Size Incorrect
  SDK_Err_FileSizeError = $119;

  // 键盘类型不支持 // Hotkey Type Not Supported
  SDK_Err_HotkeyTypeNotSupport = $120;
  // 下载文件格式错误 // Download File Format Error
  SDK_Err_FileFormatError = $121;
  // 文件太小 // File Too Small
  SDK_Err_FileTooSmall = $122;
  // 文件太大 // File Too Large
  SDK_Err_FileTooLarge = $123;
  // 文件名不正确 // File Name Incorrect
  SDK_Err_FileNameErr = $124;
  // 没有此错误命令 // No Such Error Command
  SDK_Err_NoThisErrorCode = $1FF;

  // 格式 // Format
  // 时间格式 // Time Format
  SDK_DateTimeFormat = 'YYYY-MM-DD HH:mm:SS';

  // 任务失败 // Task Failed
  SDK_Task_Failed = THandle(-1);

  type
  // 任务句柄 // Task Handle
  TSDKTaskHandle = THandle;

  // 16字节字符串 // 16-byte String
  TSDKBytes16 = array [0 .. 15] of Byte;
  // 7字节字符串 // 7-byte String
  TSDKBytes7 = array [0 .. 6] of Byte;
  TSDKBytes17 = array [0 .. 16] of Byte;
  // 设备信息 // Device Information
  PSDKDeviceInfo = ^TSDKDeviceInfo;

  // 总共256个字节 // Total 256 Bytes
  TSDKDeviceInfo = packed Record
    Addr: UInt32; // 设备IP // Device IP
    Port: UInt32; // 端口 // Port
    ProtocolType: UInt32; // 协议类型 // Protocol Type
    DeviceNo: TSDKBytes16; // 设备号 // Device Number
    Version: UInt32; // 下位机软件版本 // Firmware Version
    LanguageID: UInt8; // 下位机语言类别 // Language Type
    KeyID: UInt8; // 键盘类别 // Keyboard Type
    PLUStorage: UInt16; // PLU可存储数量 // PLU Storage Capacity
    Note1Storage: UInt16; // Note1可存储数量 // Note1 Storage Capacity
    Note2Storage: UInt16; // Note2可存储数量 // Note2 Storage Capacity
    Note3Storage: UInt16; // Note3可存储数量 // Note3 Storage Capacity
    Note4Storage: UInt16; // Note4可存储数量 // Note4 Storage Capacity
    BoardType: Byte; // 主板类型 // Motherboard Type
    PrintEnableDate: TSDKBytes7; // 日期格式 YYYYMMDDhhmmss // Date Format YYYYMMDDhhmmss
    PrinterKm: Double; // 打印公里数 // Print Kilometer Count
    PrinterPaperCount: UInt32; // 打印纸张数 // Printed Paper Count
    EthernetMode: Byte; // 以太网模式 // Ethernet Mode
    MacAddr: TSDKBytes17; // MAC地址 // MAC Address
    Reserve: Array [0 .. 173] of Byte; // 保留字段 // Reserved Field
  End;

  /// <summary>
  /// <para>
  /// 进度回调函数
  /// </para>
  /// <para>
  /// nErrorCode:错误号  //
  /// Index:当前行
  /// Total:总行数
  /// lpUserData:用户数据指针
  /// </para>
  /// </summary>
  /// <remarks>
  /// 开始执行：发送一个Index=0, Total=总数 的包处理过程：
  /// 发送一个Index=当前完成行, Total=总数 的包. 继续执行
  /// 结束：发送一个Index=总数, Total=总数 的包. 继续执行
  /// 异常：ErrorCodek, Index=异常所在行。退出。
  /// </remarks>

 /// <summary>
 /// <para>
 /// Progress Callback Function
 /// </para>
 /// <para>
 /// nErrorCode: Error code  //
 /// Index: Current row
 /// Total: Total rows
 /// lpUserData: User data pointer
 /// </para>
 /// </summary>
 /// <remarks>
 /// Start execution: Send a packet with Index=0, Total=total count.
 /// Processing: Send a packet with Index=currently completed row, Total=total count. Continue execution.
 /// End: Send a packet with Index=total count, Total=total count. Continue execution.
 /// Exception: ErrorCode != 0, Index=row where exception occurred. Exit.
 /// </remarks>

  TSDKOnProgressEvent = procedure(nErrorCode, Index, Total: Cardinal;
    lpUserData: Pointer); stdcall;

function ConvertByte16ToStr(Byte16: TSDKBytes16): {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF};
function ConvertByte17ToStr(Byte17: TSDKBytes17): {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF};
function ConvertStrToByte16(Str: {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF}): TSDKBytes16;
function ConvertStrToByte17(Str: {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF}): TSDKBytes17;

implementation

function ConvertByte16ToStr(Byte16: TSDKBytes16): {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF};
var
  i: Integer;
begin
  Result := ''; // 不加这句运行起来还会错  //Without adding this sentence, it will still run incorrectly
  for i := Low(Byte16) to High(Byte16) do
    Result := Result + {$IFDEF Linux} UTF8Char {$ELSE} AnsiChar{$ENDIF}(Byte16[i]);
  Result := {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF}(Trim(String(Result)));
end;

function ConvertStrToByte16(Str: {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF}): TSDKBytes16;
var
  i: Integer;
begin
  FillChar(Result, Length(Result), 0);
  // 版本号一定是小于16个Byte的   //The version number must be less than 16 Byte of
  for i := 1 to Length(Str) do
    Result[i - 1] := Ord(Str[i]);
end;

function ConvertStrToByte17(Str: {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF}): TSDKBytes17;
var
  i: Integer;
begin
  FillChar(Result, Length(Result), 0);
  for i := 1 to Length(Str) do
    Result[i - 1] := Ord(Str[i]);
end;


function ConvertByte17ToStr(Byte17: TSDKBytes17): {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF};
var
  i: Integer;
begin
  Result := ''; // 不加这句运行起来还会错  ////Without adding this sentence, it will still run incorrectly
  for i := Low(Byte17) to High(Byte17) do
    Result := Result + {$IFDEF Linux} UTF8Char {$ELSE} AnsiChar{$ENDIF}(Byte17[i]);
  Result := {$IFDEF Linux} UTF8String {$ELSE} AnsiString{$ENDIF}(Trim(String(Result)));
end;

end.
