//02.03.2022 663-CSdemoR Демо работы с CS_DLL.DLL - чтение.
//Параметр: имяФайлаЦХ
//Требуется доступ к CS_DLL.DLL
//

unit UnitCSdemoR;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, Series;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Chart1: TChart;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  ICyclickStorage=interface(IInterface)
    function ConnectToCS(sFile:PChar;sMutex:PChar;TimeOut:integer;bWrite:Boolean):integer;stdcall;
    function DisconnectCS:integer;stdcall;
    function GetCSHead:pointer;stdcall;
    function GetHeadTail(var iHead:integer;var iTail:integer):integer;stdcall;
    function NormalizeIndex(i:integer):integer;stdcall;
    function BeginWrite:integer;stdcall;
    function EndWrite(t:TDateTime;nLen:integer):integer;stdcall;
    function Write(var buf;nBufSize:integer):integer;stdcall;
    function WriteFile(t:TDateTime;sFile:PChar):integer;stdcall;
    function Read(i:integer;var buf):integer;stdcall;
    function Read2(i:integer;var buf;ofs,len:integer):integer;stdcall;
    function GetRHead(i:integer;var t:TDateTime;var n:integer):Boolean;stdcall;
    function GetT(i:integer):TDateTime;stdcall;
    function Dicho(t:TDateTime;iHead,iTail,iMode:integer):integer;stdcall;
  end;  //ICyclickStorage

function CreateCSobj:ICyclickStorage;stdcall; external 'CySt_DLL.dll';

var
  cs:ICyclickStorage;
  iCsHead:integer;
  i:integer;
  t:array[0..999]of double;
  Y:array[0..999]of double;
  Coll: array[0..9] of TColor;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if not FileExists(ParamStr(1)) then
    begin
    Application.Terminate;
    exit;
    end;
  cs:=CreateCSobj;
  cs.ConnectToCS(PChar(ParamStr(1)),'CSdemoMutex',10000,false);
  cs.GetHeadTail(iCsHead,i);

  chart1.BottomAxis.DateTimeFormat:='hh:mm:ss';

  Coll[0]:=$A26480;
  Coll[1]:=clGreen;
  Coll[2]:=clBlue;
  Coll[3]:=clPurple;
  Coll[4]:=clFuchsia;
  Coll[5]:=clNavy;
  Coll[6]:=clGray;
  Coll[7]:=clMaroon;
  Coll[8]:=clOlive;
  Coll[9]:=clTeal;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var tR:TDateTime; nn:integer; a:array of byte; ai:array of integer;
    i,j,k,m,n:integer;
begin
  cs.GetHeadTail(i,j);
  if i = iCsHead then
    exit;
  while chart1.SeriesCount > 0 do
    chart1.Series[chart1.SeriesCount-1].Destroy;
  iCsHead:=i;
  cs.GetRHead(iCsHead,tR,nn);
  SetLength(a,nn);
  cs.Read(iCsHead,a[0]);
  move(a[0],m,sizeof(integer));
  SetLength(ai,m);
  move(a[4],ai[0],m*sizeof(integer));
  k:=(1+m)*sizeof(integer);
  for j:=0 to m-1 do
    begin
    n:=ai[j];
    move(a[k],t[0],n*sizeof(double));
    k:=k+n*sizeof(double);
    move(a[k],Y[0],n*sizeof(double));
    k:=k+n*sizeof(double);
    Chart1.AddSeries(TFastLineSeries.Create(Self));
    TFastLineSeries(Chart1.Series[j]).LinePen.Width:=2;
    TFastLineSeries(Chart1.Series[j]).LinePen.Color:=Coll[j];
    Chart1.Series[j].XValues.DateTime:=true;
    for i:=0 to n-1 do
      Chart1.Series[j].AddXY(t[i],Y[i]);
    end;
  ai:=nil;
  a:=nil;
end;

end.
