{*******************************************************}
{                                                       }
{       GSM Demo project for                            }
{       nrCommLib Communication Library for Delphi      }
{       Copyright (c) 1997-2008 Roman Novgorodov        }
{                                                       }
{       e-mail:noro@deepsoftware.com                    }
{       DeepSoftware                                    }
{       http://www.deepsoftware.com/                    }
{                                                       }
{*******************************************************}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, nrclasses, nratcmd, nrgsm, nrcomm, StdCtrls, nrcommbox,
  nrlogfile, Spin, nrgsmpdu, ComCtrls, ExtCtrls, XPMan;

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    Button1: TButton;
    nrDeviceBox1: TnrDeviceBox;
    nrComm1: TnrComm;                      
    nrGsm1: TnrGsm;
    ListBox1: TListBox;
    Label1: TLabel;
    Pages: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Label4: TLabel;
    eMem: TComboBox;
    Label5: TLabel;
    eNo: TSpinEdit;
    Button4: TButton;
    Button6: TButton;
    Label6: TLabel;
    Memo2: TMemo;
    Button5: TButton;
    Label3: TLabel;
    Edit1: TEdit;
    Button2: TButton;
    Memo1: TMemo;
    TabSheet3: TTabSheet;
    Button3: TButton;
    lvInfo: TListView;
    Label7: TLabel;
    chConfirm: TCheckBox;
    pgTerminal: TTabSheet;
    Memo3: TMemo;
    pgBook: TTabSheet;
    lvPhones: TListView;
    Panel1: TPanel;
    ePhoneBookType: TComboBox;
    Button7: TButton;
    Label8: TLabel;
    nrLogFile1: TnrLogFile;
    pgOptions: TTabSheet;
    Label2: TLabel;
    cbOnNewSms: TComboBox;
    cbStatus: TComboBox;
    Label10: TLabel;
    XPManifest1: TXPManifest;
    Label11: TLabel;
    ePin: TEdit;
    pgDriver: TTabSheet;
    chAutodetect: TCheckBox;
    Label9: TLabel;
    eDrivers: TComboBox;
    bLoadDriver: TButton;
    bDelAll: TButton;
    Button8: TButton;
    eCmd: TEdit;
    bSendCommand: TButton;
    Label12: TLabel;
    chAlert: TCheckBox;
    procedure CheckBox1Click(Sender: TObject);
    procedure nrGsm1AfterInit(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure nrGsm1GsmError(Sender: TObject; sData: String);
    procedure nrGsm1Ring(Sender: TObject; sData: String);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure nrGsm1SmsReceived(Sender: TObject; aMem: String;
      idSms: Integer; aSms: TnrPduSms);
    procedure nrGsm1SmsListItem(Sender: TObject; aMem: String;
      idSms: Integer; aSms: TnrPduSms);
    procedure Button6Click(Sender: TObject);
    procedure cbOnNewSmsChange(Sender: TObject);
    procedure nrGsm1SmsSent(Sender: TObject; aMem: String; idSms: Integer;
      aSms: TnrPduSms);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure nrGsm1PhoneListFinish(Sender: TObject);
    procedure nrGsm1PhoneListProgress(Sender: TObject;
      var flBreak: Boolean);
    procedure Memo3DblClick(Sender: TObject);
    procedure chAutodetectClick(Sender: TObject);
    procedure bLoadDriverClick(Sender: TObject);
    procedure bDelAllClick(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure bSendCommandClick(Sender: TObject);
  private
    { Private declarations }
    procedure WMDeviceChange(var Msg:TMessage); message WM_DEVICECHANGE;
    procedure AddInfo(sCapt, sVal: string);
    procedure ReloadDriveList;
    procedure SendAlertSms;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses clipbrd;

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var i:integer;
begin
  ReloadDriveList;
  chAutodetect.Checked := nrGsm1.Autodetect;
  Pages.ActivePageIndex := 0;
  for i := 0 to lvInfo.Items.Count - 1 do lvInfo.Items[i].SubItems.Add('...');
  cbOnNewSms.ItemIndex := Integer(nrGsm1.NewSmsMode);
  ePin.Text := nrGsm1.PIN;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  try
    nrGsm1.NewSmsMode := TgsmNewSmsMode(cbOnNewSms.ItemIndex);
    nrGsm1.Autodetect := chAutodetect.Checked;
    nrGsm1.PIN := ePin.Text;
    if not nrGsm1.Autodetect then nrGsm1.DriverIndex := eDrivers.ItemIndex;

    nrComm1.Active := CheckBox1.Checked;
  except
    CheckBox1.Checked := nrComm1.Active;
    raise;
  end;
end;

procedure TForm1.AddInfo(sCapt, sVal: string);
var Itm:TListItem;
begin
  if Trim(sVal) = '' then exit;
  with lvInfo do begin
    Itm := Items.Add;
    Itm.Caption := sCapt;
    Itm.SubItems.Add(sVal);
  end;
end;

procedure TForm1.nrGsm1AfterInit(Sender: TObject);
begin
  ListBox1.Items.Add('========= GSM device connected ===========');
  lvInfo.Clear;
  AddInfo('Manufacturer', nrGsm1.DeviceManufacturer);
  AddInfo('Device', nrGsm1.DeviceName);
  AddInfo('Version', nrGsm1.DeviceVersion);
  AddInfo('IMEI', nrGsm1.DeviceIMEI);
  AddInfo('Driver', nrGsm1.Driver.Caption);

  eMem.Items.Text := nrGsm1.MemTypesRead.Text;
  eMem.ItemIndex := eMem.Items.Count - 1;
  ListBox1.TopIndex := ListBox1.Count - 1;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if not chAlert.Checked
    then nrGSm1.SmsSend(Edit1.Text,Memo1.Text, chConfirm.Checked)
    else SendAlertSms;
end;

procedure TForm1.nrGsm1GsmError(Sender: TObject; sData: String);
begin
  ListBox1.Items.Add('GSM Error: ' + sData);
  ListBox1.TopIndex := ListBox1.Count - 1;
  ListBox1.ItemIndex := ListBox1.Count - 1;
end;

procedure TForm1.nrGsm1Ring(Sender: TObject; sData: String);
begin
  ListBox1.Items.Add('RING: ' + sData);
  ListBox1.TopIndex := ListBox1.Count - 1;
  ListBox1.ItemIndex := ListBox1.Count - 1;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  nrGsm1.RunDiagnostic;
  ShowMessage('Log  file with diagnostic onformation has been saved as '#13+
               nrLogFile1.FileName +
               ' file.'#13'Please send this log to DeepSoftware.Com tech support service.');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  nrComm1.ConfigDialog;
end;

procedure TForm1.WMDeviceChange(var Msg: TMessage);
begin
  nrComm1.WMDeviceChange(Msg);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  nrGSM1.MemoryRead :=  eMem.Text;
  if nrGSM1.SmsDelete(eNo.Value)
    then ListBox1.AddItem('OK',nil)
    else ListBox1.AddItem('Error',nil) ;
end;

procedure TForm1.nrGsm1SmsReceived(Sender: TObject; aMem: String;
  idSms: Integer; aSms: TnrPduSms);
begin

  if aMem <> ''
    then ListBox1.Items.Add('Sms is received "' + aMem+'",'+ IntToStr(idSms))
    else ListBox1.Items.Add('Sms is received ');

  if aSms <> nil then begin
    if aSms.Report
      then ListBox1.Items.Add(aSms.ReportText)
      else ListBox1.Items.Add('SMS: From: ' + aSms.Phone + ' text: '+ aSms.Text);
  end;

  ListBox1.TopIndex := ListBox1.Count - 1;
  ListBox1.ItemIndex := ListBox1.Count - 1;

end;

procedure TForm1.nrGsm1SmsListItem(Sender: TObject; aMem: String;
  idSms: Integer; aSms: TnrPduSms);
begin
  if aSms <> nil then begin
    if aSms.Report then begin
      Memo2.Lines.Add('REPORT: ' + aMem + ',' + IntToStr(idSms) + ' ======= ');
      Memo2.Lines.Add(aSms.ReportText);
    end else begin
      Memo2.Lines.Add('SMS: ' + aMem + ',' + IntToStr(idSms) + ' ======= ');
      Memo2.Lines.Add(aSms.Text);
    end;
  end else Memo2.Lines.Add('======= GET SMS LIST DONE!!! =============');
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  nrGSM1.MemoryRead :=  eMem.Text;
  nrGSM1.SmsList(False, TSmsStatus(cbStatus.ItemIndex));
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  nrGSM1.MemoryRead :=  eMem.Text;
  with nrGSM1.SmsGet(eNo.Value) do
    if not IsEmpty then begin
      if Report
        then Memo2.Text := ReportText
        else Memo2.Text := Text;
      ListBox1.AddItem('OK',nil);
    end else ListBox1.AddItem('Error', nil);
end;

procedure TForm1.cbOnNewSmsChange(Sender: TObject);
begin
  nrGsm1.NewSmsMode := TgsmNewSmsMode(cbOnNewSms.ItemIndex);
end;

procedure TForm1.nrGsm1SmsSent(Sender: TObject; aMem: String;
  idSms: Integer; aSms: TnrPduSms);
begin
  ListBox1.Items.Add('sms has been sent');
  ListBox1.TopIndex := ListBox1.Count - 1;
  ListBox1.ItemIndex := ListBox1.Count - 1;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  nrGsm1.PhoneListType := TnrPhoneBookType(ePhoneBookType.ItemIndex);
  nrGsm1.PhoneBookRead;
  Label8.Caption := 'Reading ... ';
end;

procedure TForm1.nrGsm1PhoneListFinish(Sender: TObject);
var i : integer;
    itm:TListItem;
begin
  lvPhones.Clear;
  for i := 0 to nrGsm1.PhoneList.Count - 1 do begin
    itm := lvPhones.Items.Add;
    itm.Caption := nrGsm1.PhoneList.Item[i].Caption;
    itm.SubItems.Add(nrGsm1.PhoneList.Item[i].Phone);
  end;
  Label8.Caption := 'Ready. Total:'+IntToStr(nrGsm1.PhoneList.Count);
end;

procedure TForm1.nrGsm1PhoneListProgress(Sender: TObject;
  var flBreak: Boolean);
begin
  Label8.Caption := 'Read :'+ IntToStr(nrGsm1.PhoneList.Count);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // hide terminal page ...
  lvInfo.Items.Add;

  if (ParamCount > 0) and (ParamStr(1) = 'd')
    then exit;
    
  nrComm1.Terminal := nil;
  pgTerminal.Free;
end;

procedure TForm1.Memo3DblClick(Sender: TObject);
var s:string;
    i:integer;
begin
  // insert from clipboard into terminal ...
  s := Clipboard.AsText;
  s := StringReplace(s, '\1Ah', #$1a, []);
  s := StringReplace(s, '\0Dh', #$0D, []);
  for i := 1 to Length(s) do PostMessage(Memo3.Handle, WM_CHAR, DWord(s[i]),0 );

end;

procedure TForm1.chAutodetectClick(Sender: TObject);
begin
  eDrivers.Enabled := not chAutodetect.Checked;
  bLoadDriver.Enabled := eDrivers.Enabled;
  bDelAll.Enabled := eDrivers.Enabled;
end;

procedure TForm1.bLoadDriverClick(Sender: TObject);
var dlg:TOpenDialog;
begin
  dlg := TOpenDialog.Create(Self);
  dlg.Filter := '*.inf|*.inf|*.*|*.*'; 
  if dlg.Execute
    then nrGsm1.AddDriverFromFile('', dlg.FileName);
  dlg.Free;
  ReloadDriveList;
end;

procedure TForm1.ReloadDriveList;
var i : integer;
begin
  eDrivers.Clear;
  for i := 0 to nrGsm1.DriverCount - 1
    do eDrivers.Items.Add(nrGsm1.Drivers[i].Caption);
end;

procedure TForm1.bDelAllClick(Sender: TObject);
begin
  nrGsm1.DriversClearAll;
  ReloadDriveList;
end;

procedure TForm1.Button8Click(Sender: TObject);
var s:string;
begin
  s := nrGSm1.SmsSendWaitRefID(Edit1.Text, Memo1.Text, chConfirm.Checked);
  ShowMessage('SMS is sent. Ref#:' + s);
end;

procedure TForm1.bSendCommandClick(Sender: TObject);
var s:string;
begin
  s := nrGsm1.CmdSendAndWaitResult(eCmd.Text);
  ShowMessage('Reply:' + s);
end;

procedure TForm1.SendAlertSms;
var pdu:TnrPduSms;
begin
  pdu := TnrPduSms.Create(false);
  pdu.Text := Memo1.Text;
  pdu.Phone := Edit1.Text;
  pdu.Confirm := chConfirm.Checked;
  pdu.Alert := True;
  nrGsm1.SmsSend(pdu);
  pdu.Free;
end;

end.
