{+----------------------------------------------------------------------------+ / Filename: DataUnitMain.pas / Created: 4.1.2002 / Author: Antti Krats / Company: Gestapo / Copyright: ©Gestapo-project 2002 / Description: This Class contains the CommunicationServer. It's purpose is to share and save information to connected surveillance, alarm and configuration units. The main procedure in this class is ProcessCommand which determines which command is received and takes action. Also small kind of user interface that gives little information whats happening is integrated to this class. / Version: 1.0 / Open Issues: User Interface should be in its own class. +----------------------------------------------------------------------------+} unit dataunitmain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, DataController, StdCtrls, ComCtrls, CommunicationServer, QDialogs, helptools, Menus, ScktComp, ExtCtrls, shellapi, About, Dialogs; type TFormDataController = class(TForm) StatusBar: TStatusBar; MemoLog: TMemo; ListBoxClients: TListBox; MainMenu1: TMainMenu; F1: TMenuItem; Exit1: TMenuItem; Help1: TMenuItem; About1: TMenuItem; EditConnectionCount: TEdit; MemoCommands: TMemo; ButtonClear: TButton; TimerDeleteLog: TTimer; Label1: TLabel; Label2: TLabel; Bevel1: TBevel; Label3: TLabel; Label4: TLabel; Reboot1: TMenuItem; Log1: TMenuItem; Savelog1: TMenuItem; Viewoldlog1: TMenuItem; OpenDialogLog: TOpenDialog; Clearlastactions1: TMenuItem; Label5: TLabel; procedure FormCreate(Sender: TObject); procedure Exit1Click(Sender: TObject); procedure ButtonClearClick(Sender: TObject); procedure TimerDeleteLogTimer(Sender: TObject); procedure MemoLogChange(Sender: TObject); procedure About1Click(Sender: TObject); procedure Reboot1Click(Sender: TObject); procedure Savelog1Click(Sender: TObject); procedure Viewoldlog1Click(Sender: TObject); procedure Clearlastactions1Click(Sender: TObject); procedure MemoCommandsChange(Sender: TObject); private nUnitsConnected : integer; sAlarmUnit : string; sConfigurationUnit : string; Controller : TDataController; Server : TCommunicationServer; procedure ReBoot(); procedure ProcessCommand(Sender: TObject; sText: string; sClientName : string); procedure ProcessFile(Sender: TObject; sFileName : string; sClientName : string); procedure UpdateMemoClients(Sender : TObject); procedure SaveMemoLog(); procedure Status(stext : string); procedure ClientDisconnected(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); public { Public declarations } end; var FormDataController: TFormDataController; implementation {$R *.dfm} //Initializes DataController and starts Communication server procedure TFormDataController.FormCreate(Sender: TObject); begin Controller := TDataController.Create(self); Statusbar.SimpleText := 'Deleting old log files...'; //informs about deleting //files Controller.sLogpath := '.\log\'; //sets datacontrollers Controller.sConfigpath := '.\config\'; //path and filenames Controller.sAlarmpath := '.\alarms\'; Controller.sSUalarmsfilename := 'su_sent.dat'; Controller.sAUalarmsfilename := 'au_received.dat'; Controller.sLogextension := '.dat'; Controller.sCfgextension := '.ini'; Controller.sPictureextension := '.jpg'; Controller.sAlllogfile := 'alllog'; Controller.szSeparator := ' '; //used to separate //messages and things //like that Controller.SetDaysToKeep(7); //how many days old log //files are kept Controller.DeleteOldLog; //deletes old logs Statusbar.SimpleText := 'Initializing Server...'; Server := TCommunicationServer.Create(self); Server.Port := 10079; Server.OnClientReadText := ProcessCommand; Server.OnClientReadFile := ProcessFile; Server.OnClientConnectCount := UpdateMemoClients; Server.Socket.OnClientError := ClientDisconnected; Server.Start; Statusbar.SimpleText := 'System ready.'; end; //Processes communicationcommands and takes action by them procedure TFormDataController.ProcessCommand(Sender: TObject; sText: string; sClientName : string); var sCommand : string; sUnitName : string; sHelpString : string; List : TStringList; i : integer; begin memoCommands.Lines.Add(sText); sCommand := ParseString(sText,' '); //puts command in sCommand variable if sCommand = '' then sCommand := sText; //if text contains only one word //parsestring won't do anything //Makes directory for Surveillance Unit if sCommand = 'DU_MAKESUDIR' then begin Controller.CreateDirectory(Controller.sLogpath + sClientName); Exit; end; //Makes camera directory for Surveillance Unit if sCommand = 'DU_MAKECAMDIR' then begin Controller.CreateDirectory(Controller.sLogpath + sClientName + '\' + sText); Exit; end; //Makes sensorfile for Surveillance Unit if sCommand = 'DU_MAKESENSORFILE' then begin Controller.CreateFile(Controller.sLogpath + sClientName + '\' + sText + Controller.sLogextension); Exit; end; //Saves text under directory which name is Surveillance Units name. if sCommand = 'DU_SAVELOG' then begin controller.SaveToLog(sText, sClientName); Status(sText); Exit; end; // if sCommand = 'DU_SAVECONFIG' then begin ShowMessage('What the hell is this?'); Controller.SaveToConfig(sText,sClientName); for i:=0 to Server.Connection_count-1 do server.SendText(Server.GetConnectionName(i),'SU_NEWCONFIGEXISTS'); Status(sClientName + ' saved new config.'); Exit; end; //Sends message back to sender: go fecth a config or config does not exist. if sCommand = 'DU_GETCONFIGNAMES' then begin List := controller.GiveFileNames(controller.sConfigPath,'*.*',false); if List = nil then begin Server.SendText(sClientName,'CU_CONFIGERROR Config not found.'); Exit; end; sHelpString := StringListToString(List,'|'); server.SendText(sClientName,'CU_TAKECONFIGS ' + sHelpString); List.Free; Exit; end; //Sends message to Configuration Unit that contains SUs name and picture name if sCommand = 'DU_GIVELATESTPICTUREFILENAME' then begin sUnitName := sText; sHelpString := controller.GiveLatestFileName(controller.sLogPath + sUnitName + '\camera_cam0\'); if sHelpString = '' then begin Server.SendText(sClientName,'CU_NOTIFICATION Picture not found.'); Exit; end; sHelpString := controller.sLogPath + sUnitName + '\camera_cam0\' + sHelpString; Server.SendText(sClientName,'CU_TAKELATESTPICTURE ' + sUnitName + ' ' + sHelpString); Exit; end; //Sends a stringlist (converted to string) to Configuration Unit. //List contains Surveillance Units all picture names. if sCommand = 'DU_GETALLPICTURES' then begin sUnitName := sText; List := controller.GiveFileNames(controller.sLogPath + sUnitName + '\Camera_Cam0\','*.*',false); // list := controller.GivePictureNames(controller.logpath + SUName + '\Camera_cam0' + controller.logextension); if List = nil then begin Server.SendText(sClientName,'CU_NOTIFICATION Picture not found.'); Exit; end; sHelpString := StringListToString(list,'|'); Server.SendText(sClientName,'CU_TAKEALLPICTURES ' + sUnitName + ' ' + sHelpString); List.Free; Exit; end; //Sends Surveillance Units logfilenames in string (converted from stringlist). if sCommand = 'DU_GIVELOGFILENAMES' then begin sUnitName := sText; List := controller.GiveFileNames(controller.sLogPath + sUnitName + '\','*.*',false); if List = nil then begin Server.SendText(sClientName,'CU_NOTIFICATION Log file not found'); Exit; end; Server.SendText(sClientName,'CU_TAKELOGFILES ' + sUnitName + ' ' + StringListToString(list,'|')); List.Free; Exit; end; //Sends string(converted from stringlist) to unit that sended //DU_GIVEALARMFILENAME. String contains all files that are in alarms path. if sCommand = 'DU_GIVEALARMFILENAME' then begin List := controller.GiveFileNames(controller.sAlarmPath,'*.*',false); if List = nil then begin server.SendText(sClientName,'CU_NOTIFICATION Alarm File not found.'); exit; end; sHelpString := List.Strings[0]; server.SendText(sClientName,'CU_TAKEALARMLOGFILE ' + sHelpString); list.Free; Exit; end; //Tells unit that it has new configuration. if sCommand = 'DU_NEWCONFIGEXISTS' then begin sUnitName := sText; Sleep(1000); if sUnitName = sAlarmUnit then Server.SendText(sUnitName,'AU_NEWCONFIGEXISTS') else Server.SendText(sUnitName,'SU_NEWCONFIGEXISTS'); Exit; end; //Sends Surveillance Unit a message SU_TAKEPICTURE, if SU isn't online //message is send to Configuration Unit that Surveillance Unit is offline. if sCommand = 'DU_TAKENEWPICTURE' then begin sUnitName := sText; for i := 0 to ListBoxClients.Count -1 do if ListBoxClients.Items[i] = sUnitName then begin Server.SendText(sUnitName,'SU_TAKEPICTURE'); Exit; end; Server.SendText(sConfigurationUnit,'CU_NOTIFICATION Surveillance Unit offline'); Exit; end; //Saves surveillance units notification to sualarmfile if sCommand = 'DU_SUALARM' then begin controller.SaveToAlarm(sText,sClientName,controller.sSUAlarmsFilename); Server.SendText(sAlarmUnit,'AU_ALARM ' + sText); Exit; end; //Tells that Surveillance Unit has new picture. //command sends that pictures name to Configuration Unit. if sCommand = 'DU_NEWPICTUREEXISTS' then begin sUnitName := sClientName; sHelpString := controller.GiveLatestFileName(controller.sLogPath + sUnitName + '\Camera_cam0\'); if sHelpString = '' then begin server.SendText(sClientName,'CU_NOTIFICATION Picture not found.'); exit; end; sHelpString := controller.sLogPath + sUnitName + '\Camera_cam0\' + sHelpString; Sleep(1000); server.SendText(sConfigurationUnit,'CU_TAKELATESTPICTURE ' + sUnitName + ' ' + sHelpString); Exit; end; //Saves Alarm Units sent alrms to log if sCommand = 'DU_AUALARM' then begin sHelpString := ParseString(sText,' '); Controller.SaveToAlarm(sText,sClientName, sHelpString + controller.sLogExtension); Status(sText); Exit; end; //Puts Alarm Units name to private attribute if sCommand = 'DU_AUINIT' then begin sAlarmUnit := sClientName; Status('Alarm Unit initialized.'); Exit; end; //Is used to check if connection is still alive if sCommand = 'DU_CHECK' then begin Server.SendText(sClientName,'CHECK'); Exit; end; //Puts Configuration Units name to private attribute if sCommand = 'DU_CUINIT' then begin sConfigurationUnit := sClientName; Status('Configuration Unit initialized.'); Exit; end; //Is used to check if there exists config for Surveillance Unit if sCommand = 'DU_SUISTHERECONFIG' then begin list := controller.GiveFileNames(controller.sConfigPath,'*.*',false); for i:=0 to list.Count-1 do begin sHelpString := list.Strings[i]; sHelpString := ParseString(sHelpString,'\',1); sHelpString := ParseString(sHelpString,'.'); if sHelpString = sClientName then begin Server.SendText(sClientName,'SU_NEWCONFIGEXISTS'); Exit; end; end; Server.SendText(sClientName,'SU_CONFIGNOTEXISTS'); Exit; end; end; //Updates ListBoxClients, occurs when unit connects or disconnects to DataUnit procedure TFormDataController.UpdateMemoClients(Sender: TObject); var i : integer; begin ListBoxClients.Items.Clear; nUnitsConnected := Server.Connection_count; EditConnectionCount.Text := IntToStr(Server.Connection_count); for i:=0 to Server.Connection_count-1 do ListBoxClients.Items.Add(Server.GetConnectionName(i)); ListBoxClients.Sorted := true; end; //Closes DataUnit program. procedure TFormDataController.Exit1Click(Sender: TObject); begin Close(); end; //Notifies that file was saved. procedure TFormDataController.ProcessFile(Sender: TObject; sFileName, sClientName: string); begin Status(sClientName + ' saved file ' + sFileName + '.'); end; //Handles unwanted client disconnect. Reboots whole program and starts it again. procedure TFormDataController.ClientDisconnected(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin ErrorCode := 0; Status('ComServer error, booting DataUnit!'); SaveMemoLog(); ReBoot; end; //Clears Last actions and Last commands memos. procedure TFormDataController.ButtonClearClick(Sender: TObject); begin MemoLog.Clear; MemoCommands.Clear; end; //Deletes old logfiles. procedure TFormDataController.TimerDeleteLogTimer(Sender: TObject); begin Status('Deleting old files...'); StatusBar.SimpleText := 'Deleting old log files...'; Controller.DeleteOldLog; end; //Keeps 20 last actions at Last actions memo. procedure TFormDataController.MemoLogChange(Sender: TObject); begin if MemoLog.Lines.Count > 20 then begin MemoLog.Lines.Delete(0); end; end; //Displays about dialog. procedure TFormDataController.About1Click(Sender: TObject); begin AboutBox.ShowModal; end; //ReBoots Data Unit program. procedure TFormDataController.reboot; begin Server.Close; Close; ShellExecute(handle, 'open', '.\dataunit.exe', nil, nil, SW_SHOW); end; //Calls Reboot procedure. procedure TFormDataController.Reboot1Click(Sender: TObject); begin ReBoot; end; //Saves Last actions to a file procedure TFormDataController.SaveMemoLog; var sFileName, sDateHelp, sTimeHelp, sHelp : string; F : TextFile; i : integer; sOldPath : string; begin sOldPath := getcurrentdir(); sDateHelp := DateToStr(Date); sTimeHelp := TimeToStr(Time); sHelp := sDateHelp; sFileName := ParseString(sHelp,'.',1) + '_'; // year sFileName := sFileName + ParseString(sHelp,'.',1) + '_'; // month sFileName := sFileName + sHelp + '_'; // day sHelp := sTimeHelp; sFileName := sFileName + ParseString(sHelp,':') + '_'; // hour sFileName := sFileName + ParseString(sHelp,':') + '_'; // min sFileName := sFileName + sHelp + '.dat'; // sec if not DirectoryExists('.\actions\') then CreateDir('.\actions\'); SetCurrentDir('.\actions\'); AssignFile(F,sFileName); Rewrite(F); for i:=0 to MemoLog.Lines.Count - 1 do Writeln(F, MemoLog.Lines.Strings[i]); Flush(F); CloseFile(F); SetCurrentDir(sOldPath); end; //Saves Last action to file by user. procedure TFormDataController.Savelog1Click(Sender: TObject); begin SaveMemoLog(); end; //Opens open dialog where user can open logfile with notepad. procedure TFormDataController.Viewoldlog1Click(Sender: TObject); var sFilename : string; begin if OpenDialogLog.Execute then sFilename := OpenDialogLog.FileName else Exit; ShellExecute(handle, 'open', 'notepad.exe', PChar(sFilename), nil, SW_SHOW); end; //Clears Last actions and Last commands memos. procedure TFormDataController.Clearlastactions1Click(Sender: TObject); begin MemoLog.Clear; MemoCommands.Clear; end; //Adds text to Last actions. Date and time is added in the beginning of text. procedure TFormDataController.Status(sText: string); var sTime : string; sDate : string; begin sTime := TimeToStr(Time); sDate := DateToStr(Date); MemoLog.Lines.Add(sTime + ' ' + sDate + ' > ' + sText); end; //Shows only 7 last commands in Memo commands. procedure TFormDataController.MemoCommandsChange(Sender: TObject); begin if MemoCommands.Lines.Count > 7 then begin MemoCommands.Lines.Delete(0); end; end; end.