{+---------------------------------------------------------------------------+ / Filename: SurveillanceController.pas / Created: 3.1.2002 / Author: Kai Lahti / Company: Gestapo / Copyright: ©Gestapo-project 2002 / Description: This component manages client component and all the devices thru devicecontroller. This component decides if alarm is sent. SensorID Sensor 0 ComPort1CTS 1 ComPort1DSR 2 ComPort1Ring 3 ComPort1RLSD 4 ComPort1RTS 5 ComPort2CTS 6 ComPort2DSR 7 ComPort2Ring 8 ComPort2RLSD 9 ComPort2RTS 10 Joystick1x 11 Joystick1y 12 Joystick2x 13 Joystick2y 14 Camera 1 15 Camera 2 16 Camera 3 17 Camera 4 / Version: 1.0 / Open Issues: +---------------------------------------------------------------------------+} unit SurveillanceController; interface uses Windows, Messages, SysUtils, Classes, CommunicationClient, DeviceController, QDialogs, ScktComp, IniFiles, helptools, ExtCtrls; type TStatusEvent = procedure(sStatus : string) of object; TClientErrorEvent = procedure() of object; type TSurveillanceController = class(TComponent) private nErrorCount : integer; DeviceController : TDeviceController; Client : TCommunicationClient; sFIDName : string; sPreviousPictureFilename : string; FOnStatus: TStatusEvent; ConnectionTimer : TTimer; // verifyes that connection is alive nVerifyCounter : integer; CameraTimer : TTimer; FOnClientError: TClientErrorEvent; FOnSocketError: TClientErrorEvent; procedure ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var nErrorCode: Integer); procedure SocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var nErrorCode: Integer); procedure MotionDetect(nSensorID: integer; sValue: string); procedure WetDetect(nSensorID: integer; sValue: string); procedure OnOffDetect(nSensorID: integer; sValue: string); procedure TemperatureDetect(nSensorID: integer; sValue : string); procedure PictureGrapped(nSensorID: integer); procedure GrabPicture(Sender : TObject); procedure SetIDName(sNewName : string); procedure ProcessCommand(Sender: TObject; sText: string); procedure LoadSettings(); procedure SaveSettings(); procedure VerifyConnection(Sender : TObject); protected public constructor Create(AOwner : TComponent); override; destructor Destroy(); override; procedure Connect; procedure LoadLocalSettings(sfilename: string = '.\localconfig.ini'); procedure Prepare; procedure Start; procedure Stop; published property IDName : string read sFIDName write SetIDName; property OnStatus : TStatusEvent read FOnStatus write FOnStatus; property OnClientError : TClientErrorEvent read FOnClientError write FOnClientError; property OnSocketError : TClientErrorEvent read FOnSocketError write FOnSocketError; end; procedure Register; implementation procedure Register; begin RegisterComponents('Gestapo', [TSurveillanceController]); end; { TSurveillanceController } // Constructor constructor TSurveillanceController.Create(AOwner: TComponent); begin inherited; nErrorCount := 0; client := TCommunicationClient.Create(self); client.Port := 10079; client.HostIP := '127.0.0.1'; // LocalHost client.Name := 'ClientName'; client.OnError := ClientError; client.Socket.OnErrorEvent := SocketError; client.OnReadText := ProcessCommand; OnStatus := nil; OnClientError := nil; OnSocketError := nil; ConnectionTimer := TTimer.Create(self); ConnectionTimer.Enabled := false; ConnectionTimer.Interval := 5000; ConnectionTimer.OnTimer := VerifyConnection; nVerifyCounter := 0; DeviceController := TDeviceController.Create(self); DeviceController.OnMotionDetect := MotionDetect; DeviceController.OnWetDetect := WetDetect; DeviceController.OnOnOffDetect := OnOffDetect; DeviceController.OnTemperatureDetect := TemperatureDetect; DeviceController.OnPictureGrapped := PictureGrapped; CameraTimer := TTimer.Create(self); CameraTimer.Interval := 3600000; CameraTimer.OnTimer := GrabPicture; CameraTimer.Enabled := false; end; // Destructor destructor TSurveillanceController.Destroy; begin CameraTimer.Free; DeviceController.Free; client.Free; inherited; end; // This is done if client error occurs // OnClientError event is left for future (because problems on system boot) procedure TSurveillanceController.ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var nErrorCode: Integer); begin nErrorCode := 0; // if Assigned(OnClientError) Then // OnClientError(); if Assigned(OnStatus) Then OnStatus('ClientError'); end; // This is done if client error occurs // OnSocketError event is left for future (because problems on system boot) procedure TSurveillanceController.SocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var nErrorCode: Integer); begin nErrorCode := 0; // if Assigned(OnSocketError) Then // OnSocketError(); if Assigned(OnStatus) Then OnStatus('SocketError'); end; // Sends DU_CHECK message to dataunit and if there is no ansver before fourth // message has been send, this procedure causes OnClientError procedure TSurveillanceController.VerifyConnection(Sender: TObject); begin client.SendText('DU_CHECK'); nVerifyCounter := nVerifyCounter + 1; if nVerifyCounter < 4 then exit; if Assigned(OnClientError) Then OnClientError(); end; // This is done when motion is detected. // It takes picture and sends log information and picture to dataunit. // If day time is between alarm limits, alarm is sent. // Pictures are taken by capture interval wich is 3 seconds when motion is // detected and user configured when there is no motion (default 1 hour). procedure TSurveillanceController.MotionDetect(nSensorID: integer; sValue: string); var sdatehelp, stimehelp, sSensor : string; begin sSensor := DeviceController.Devices[nSensorID].sDeviceType + '_' + DeviceController.Devices[nSensorID].sDeviceName; sdatehelp := DateToStr(Date); stimehelp := TimeToStr(Time); client.SendText('DU_SAVELOG ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Log sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if svalue = 'on' then begin with DeviceController.Devices[nSensorID] do begin if (LimitsStart < LimitsEnd) and (Time > LimitsStart) and (Time < LimitsEnd) or (LimitsStart > LimitsEnd) and (Time < LimitsStart) and (Time > LimitsEnd) then begin client.SendText('DU_SUALARM ' + sdatehelp + ' ' + stimehelp + ' ' + IDName + ' ' + sSensor + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Alarm sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); DeviceController.Grab_Frame; CameraTimer.Interval := 3000; end; end; // with DeviceController.Devices[nSensorID] end else begin if DeviceController.Devices[14].Interval >= 10000 then CameraTimer.Interval := DeviceController.Devices[14].Interval else CameraTimer.Interval := 3600000; end; end; // This is done when wet is detected. // It sends log information to dataunit. // If day time is between alarm limits, alarm is sent. procedure TSurveillanceController.WetDetect(nSensorID: integer; sValue: string); var sdatehelp, stimehelp, sSensor : string; begin sSensor := DeviceController.Devices[nSensorID].sDeviceType + '_' + DeviceController.Devices[nSensorID].sDeviceName; sdatehelp := DateToStr(Date); stimehelp := TimeToStr(Time); client.SendText('DU_SAVELOG ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Log sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if svalue = 'on' then begin with DeviceController.Devices[nSensorID] do begin if (LimitsStart < LimitsEnd) and (Time > LimitsStart) and (Time < LimitsEnd) or (LimitsStart > LimitsEnd) and (Time < LimitsStart) and (Time > LimitsEnd) then begin client.SendText('DU_SUALARM ' + sdatehelp + ' ' + stimehelp + ' ' + IDName + ' ' + sSensor + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Alarm sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); end; end; // with DeviceController.Devices[nSensorID] end; end; // This is done when on/off is detected. // It sends log information to dataunit. // If day time is between alarm limits, alarm is sent. procedure TSurveillanceController.OnOffDetect(nSensorID: integer; sValue: string); var sdatehelp, stimehelp, sSensor : string; begin sSensor := DeviceController.Devices[nSensorID].sDeviceType + '_' + DeviceController.Devices[nSensorID].sDeviceName; sdatehelp := DateToStr(Date); stimehelp := TimeToStr(Time); client.SendText('DU_SAVELOG ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Log sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if svalue = 'on' then with DeviceController.Devices[nSensorID] do begin if (LimitsStart < LimitsEnd) and (Time > LimitsStart) and (Time < LimitsEnd) or (LimitsStart > LimitsEnd) and (Time < LimitsStart) and (Time > LimitsEnd) then begin client.SendText('DU_SUALARM ' + sdatehelp + ' ' + stimehelp + ' ' + IDName + ' ' + sSensor + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Alarm sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); end; end; // with DeviceController.Devices[nSensorID] end; // This is done when temperature is changed. // It sends log information to dataunit. // If temperature is over and day time is between alarm limits, alarm is sent. procedure TSurveillanceController.TemperatureDetect(nSensorID: integer; sValue: string); var sdatehelp, stimehelp, sSensor : string; begin sdatehelp := DateToStr(Date); stimehelp := TimeToStr(Time); sSensor := DeviceController.Devices[nSensorID].sDeviceType + '_' + DeviceController.Devices[nSensorID].sDeviceName; client.SendText('DU_SAVELOG ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + sValue + ' 1'); if Assigned(OnStatus) Then OnStatus('Log sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); if DeviceController.Devices[nSensorID].sLimitsMin <> 'off' then if StrToInt(svalue) < StrToInt(DeviceController.Devices[nSensorID].sLimitsMin) then begin with DeviceController.Devices[nSensorID] do begin if (LimitsStart < LimitsEnd) and (Time > LimitsStart) and (Time < LimitsEnd) or (LimitsStart > LimitsEnd) and (Time < LimitsStart) and (Time > LimitsEnd) then begin client.SendText('DU_SUALARM ' + sdatehelp + ' ' + stimehelp + ' ' + IDName + ' ' + sSensor + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Alarm sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); end; end; // with DeviceController.Devices[SensorID] end; if DeviceController.Devices[nSensorID].sLimitsMax <> 'off' then if StrToInt(svalue) > StrToInt(DeviceController.Devices[nSensorID].sLimitsMax) then begin with DeviceController.Devices[nSensorID] do begin if (LimitsStart < LimitsEnd) and (Time > LimitsStart) and (Time < LimitsEnd) or (LimitsStart > LimitsEnd) and (Time < LimitsStart) and (Time > LimitsEnd) then begin client.SendText('DU_SUALARM ' + sdatehelp + ' ' + stimehelp + ' ' + IDName + ' ' + sSensor + ' ' + svalue + ' 1'); if Assigned(OnStatus) Then OnStatus('Alarm sent: ' + sSensor + ' ' + sdatehelp + ' ' + stimehelp + ' ' + svalue + ' 1'); end; end; // with DeviceController.Devices[nSensorID] end; end; // When picture is taken this procedure send it and log information to dataunit procedure TSurveillanceController.PictureGrapped(nSensorID: integer); var sFileName, sCamName, sbmpfile, sdatehelp, stimehelp, shelp : string; begin sCamName := DeviceController.Devices[nSensorID].sDeviceType + '_' + DeviceController.Devices[nSensorID].sDeviceName; sdatehelp := DateToStr(Date); stimehelp := TimeToStr(Time); shelp := sdatehelp; sbmpfile := ParseString(shelp,'.',1) + '_'; // year sbmpfile := sbmpfile + ParseString(shelp,'.',1) + '_'; // month sbmpfile := sbmpfile + shelp + '_'; // day shelp := stimehelp; sbmpfile := sbmpfile + ParseString(shelp,':') + '_'; // hour sbmpfile := sbmpfile + ParseString(shelp,':') + '_'; // min sbmpfile := sbmpfile + shelp + '.jpg'; // sec sFileName := '.\log\' + IDName + '\' + sCamName + '\' + sbmpfile; if sFilename = sPreviousPictureFilename then exit; sPreviousPictureFilename := sFilename; BmpToJpeg('.\capture.bmp',50); sleep(500); client.SendFile('.\capture.jpg',sFileName); client.SendText('DU_SAVELOG ' + sCamName + ' ' + sdatehelp + ' ' + stimehelp + ' ' + sFileName); if Assigned(OnStatus) Then OnStatus('Log sent: ' + sCamName + ' ' + sdatehelp + ' ' + stimehelp + ' ' + sFileName); end; // Captures picture from selected camera procedure TSurveillanceController.GrabPicture(Sender: TObject); begin DeviceController.Grab_Frame; end; // Connects to dataunit procedure TSurveillanceController.Connect; begin client.Start; end; // Prepares all devices for use. // Searches cameras makes sure that all files and directories // are made in dataunit. procedure TSurveillanceController.Prepare; var i : integer; begin with DeviceController do begin if cameras.SearchCameras > 0 then begin Devices[14].bEnabled := true; cameras.SelectCamera(0); cameras.SetFilename('.\capture.bmp'); end; client.SendText('DU_SUISTHERECONFIG'); Client.SendText('DU_MAKESUDIR'); for i:=0 to 17 do begin if (i = 4) or (i = 9) then continue; // 4 and 9 Comport RTS Pins if Devices[i].bEnabled then Client.SendText('DU_MAKESENSORFILE ' + Devices[i].sDeviceType + '_' + Devices[i].sDeviceName); end; for i:=14 to Cameras.getCameraCount-1 + 14 do Client.SendText('DU_MAKECAMDIR ' + Devices[i].sDeviceType + '_' + Devices[i].sDeviceName); end; end; // When everything is ready this can be called and it starts devices and timers procedure TSurveillanceController.Start; begin DeviceController.StartSurvey; ConnectionTimer.Enabled := true; CameraTimer.Enabled := true; end; // Stops devices procedure TSurveillanceController.Stop; begin DeviceController.StopSurvey; end; // Loads local settings wich contains information about Sensors. // (Wich comport pin is wich sensor and joystick port calibration values) procedure TSurveillanceController.LoadLocalSettings( sfilename: string = '.\localconfig.ini'); var inifile : TIniFile; begin inifile := TIniFile.Create(sfilename); with DeviceController do begin Devices[0].sDeviceType := inifile.ReadString('comport1','CTS','Disabled'); if Devices[0].sDeviceType = 'Motion Detector' then Devices[0].sDeviceType := 'Motion_Detector' else if Devices[0].sDeviceType = 'Wet/Dry Sensor' then Devices[0].sDeviceType := 'Wet_Dry_Sensor' else if Devices[0].sDeviceType = 'On/Off Sensor' then Devices[0].sDeviceType := 'On_Off_Sensor' else Devices[0].sDeviceType := 'Disabled'; if Devices[0].sDeviceType <> 'Disabled' then Devices[0].bEnabled := true; Devices[1].sDeviceType := inifile.ReadString('comport1','DSR','Disabled'); if Devices[1].sDeviceType = 'Motion Detector' then Devices[1].sDeviceType := 'Motion_Detector' else if Devices[1].sDeviceType = 'Wet/Dry Sensor' then Devices[1].sDeviceType := 'Wet_Dry_Sensor' else if Devices[1].sDeviceType = 'On/Off Sensor' then Devices[1].sDeviceType := 'On_Off_Sensor' else Devices[1].sDeviceType := 'Disabled'; if Devices[1].sDeviceType <> 'Disabled' then Devices[1].bEnabled := true; Devices[2].sDeviceType := inifile.ReadString('comport1','Ring','Disabled'); if Devices[2].sDeviceType = 'Motion Detector' then Devices[2].sDeviceType := 'Motion_Detector' else if Devices[2].sDeviceType = 'Wet/Dry Sensor' then Devices[2].sDeviceType := 'Wet_Dry_Sensor' else if Devices[2].sDeviceType = 'On/Off Sensor' then Devices[2].sDeviceType := 'On_Off_Sensor' else Devices[2].sDeviceType := 'Disabled'; if Devices[2].sDeviceType <> 'Disabled' then Devices[2].bEnabled := true; Devices[3].sDeviceType := inifile.ReadString('comport1','RLSD','Disabled'); if Devices[3].sDeviceType = 'Motion Detector' then Devices[3].sDeviceType := 'Motion_Detector' else if Devices[3].sDeviceType = 'Wet/Dry Sensor' then Devices[3].sDeviceType := 'Wet_Dry_Sensor' else if Devices[3].sDeviceType = 'On/Off Sensor' then Devices[3].sDeviceType := 'On_Off_Sensor' else Devices[3].sDeviceType := 'Disabled'; if Devices[3].sDeviceType <> 'Disabled' then Devices[3].bEnabled := true; DeviceController.Devices[4].sDeviceType := inifile.ReadString('comport1', 'RTS', 'Disabled'); if Devices[4].sDeviceType = 'Local Alarm' then Devices[4].sDeviceType := 'Local_Alarm' else Devices[4].sDeviceType := 'Disabled'; if Devices[4].sDeviceType <> 'Disabled' then Devices[4].bEnabled := true; bComPort1CTSStatusDefault := StrToBool(inifile.ReadString('comport1', 'CTSInvert', '-1')); bComPort1DSRStatusDefault := StrToBool(inifile.ReadString('comport1', 'DSRInvert', '-1')); bComPort1RingStatusDefault := StrToBool(inifile.ReadString('comport1', 'RingInvert', '-1')); bComPort1RLSDStatusDefault := StrToBool(inifile.ReadString('comport1', 'RLSDInvert', '-1')); bComPort1RTSStatusDefault := StrToBool(inifile.ReadString('comport1', 'RTSInvert', '-1')); Devices[5].sDeviceType := inifile.ReadString('comport2','CTS','Disabled'); if Devices[5].sDeviceType = 'Motion Detector' then Devices[5].sDeviceType := 'Motion_Detector' else if Devices[5].sDeviceType = 'Wet/Dry Sensor' then Devices[5].sDeviceType := 'Wet_Dry_Sensor' else if Devices[5].sDeviceType = 'On/Off Sensor' then Devices[5].sDeviceType := 'On_Off_Sensor' else Devices[5].sDeviceType := 'Disabled'; if Devices[5].sDeviceType <> 'Disabled' then Devices[5].bEnabled := true; Devices[6].sDeviceType := inifile.ReadString('comport2','DSR','Disabled'); if Devices[6].sDeviceType = 'Motion Detector' then Devices[6].sDeviceType := 'Motion_Detector' else if Devices[6].sDeviceType = 'Wet/Dry Sensor' then Devices[6].sDeviceType := 'Wet_Dry_Sensor' else if Devices[6].sDeviceType = 'On/Off Sensor' then Devices[6].sDeviceType := 'On_Off_Sensor' else Devices[6].sDeviceType := 'Disabled'; if Devices[6].sDeviceType <> 'Disabled' then Devices[6].bEnabled := true; Devices[7].sDeviceType := inifile.ReadString('comport2','Ring','Disabled'); if Devices[7].sDeviceType = 'Motion Detector' then Devices[7].sDeviceType := 'Motion_Detector' else if Devices[7].sDeviceType = 'Wet/Dry Sensor' then Devices[7].sDeviceType := 'Wet_Dry_Sensor' else if Devices[7].sDeviceType = 'On/Off Sensor' then Devices[7].sDeviceType := 'On_Off_Sensor' else Devices[7].sDeviceType := 'Disabled'; if Devices[7].sDeviceType <> 'Disabled' then Devices[7].bEnabled := true; Devices[8].sDeviceType := inifile.ReadString('comport2','RLSD','Disabled'); if Devices[8].sDeviceType = 'Motion Detector' then Devices[8].sDeviceType := 'Motion_Detector' else if Devices[8].sDeviceType = 'Wet/Dry Sensor' then Devices[8].sDeviceType := 'Wet_Dry_Sensor' else if Devices[8].sDeviceType = 'On/Off Sensor' then Devices[8].sDeviceType := 'On_Off_Sensor' else Devices[8].sDeviceType := 'Disabled'; if Devices[8].sDeviceType <> 'Disabled' then Devices[8].bEnabled := true; Devices[9].sDeviceType := inifile.ReadString('comport2','RTS','Disabled'); if Devices[9].sDeviceType = 'Local Alarm' then Devices[9].sDeviceType := 'Local_Alarm' else Devices[9].sDeviceType := 'Disabled'; if Devices[9].sDeviceType <> 'Disabled' then Devices[9].bEnabled := true; bComPort2CTSStatusDefault := StrToBool(inifile.ReadString('comport2', 'CTSInvert', '-1')); bComPort2DSRStatusDefault := StrToBool(inifile.ReadString('comport2', 'DSRInvert', '-1')); bComPort2RingStatusDefault := StrToBool(inifile.ReadString('comport2', 'RingInvert', '-1')); bComPort2RLSDStatusDefault := StrToBool(inifile.ReadString('comport2', 'RLSDInvert', '-1')); bComPort2RTSStatusDefault := StrToBool(inifile.ReadString('comport2', 'RTSInvert', '-1')); njoystick1xoldvalue := 23; njoystick1xmin := StrToInt(inifile.ReadString('joystick1', 'x-axel_min', '1')); njoystick1xminc := StrToInt(inifile.ReadString('joystick1', 'x-axel_minc', '1')); njoystick1xFactor := StrToInt(inifile.ReadString('joystick1', 'x-axel_factor', '1')); Devices[10].bEnabled := not(StrToBool(inifile.ReadString('joystick1', 'x-axel_bEnabled', '-1'))); Devices[10].sDeviceType := 'Temperature_Sensor'; njoystick1yoldvalue := 23; njoystick1ymin := StrToInt(inifile.ReadString('joystick1', 'y-axel_min','1')); njoystick1yminc := StrToInt(inifile.ReadString('joystick1', 'y-axel_minc', '1')); njoystick1yFactor := StrToInt(inifile.ReadString('joystick1', 'y-axel_factor', '1')); Devices[11].bEnabled := not(StrToBool(inifile.ReadString('joystick1', 'y-axel_bEnabled', '-1'))); Devices[11].sDeviceType := 'Temperature_Sensor'; njoystick2xoldvalue := 23; njoystick2xmin := StrToInt(inifile.ReadString('joystick2', 'x-axel_min', '1')); njoystick2xminc := StrToInt(inifile.ReadString('joystick2', 'x-axel_minc', '1')); njoystick2xFactor := StrToInt(inifile.ReadString('joystick2', 'x-axel_factor', '1')); Devices[12].bEnabled := not(StrToBool(inifile.ReadString('joystick2', 'x-axel_bEnabled', '-1'))); Devices[12].sDeviceType := 'Temperature_Sensor'; njoystick2yoldvalue := 23; njoystick2ymin := StrToInt(inifile.ReadString('joystick2', 'y-axel_min', '1')); njoystick2yminc := StrToInt(inifile.ReadString('joystick2', 'y-axel_minc', '1')); njoystick2yFactor := StrToInt(inifile.ReadString('joystick2', 'y-axel_factor', '1')); Devices[13].bEnabled := not(StrToBool(inifile.ReadString('joystick2', 'y-axel_bEnabled', '-1'))); Devices[13].sDeviceType := 'Temperature_Sensor'; Devices[14].sDeviceType := 'Camera'; Devices[15].sDeviceType := 'Camera'; Devices[16].sDeviceType := 'Camera'; Devices[17].sDeviceType := 'Camera'; Client.HostIP := inifile.ReadString('dataunit','dataunitip',''); Client.Port := StrToInt(inifile.ReadString('dataunit','dataunitport','')); IDName := inifile.ReadString('surveillanceunit','surveillanceunit',''); end; // with DeviceController inifile.Free; end; // Sets surveillance unit ID name procedure TSurveillanceController.SetIDName(sNewName: string); begin sFIDName := sNewName; Client.Name := sNewName; end; // Loads settings wich has been received from dataunit procedure TSurveillanceController.LoadSettings; var inifile : TIniFile; nNumberOfDevices,nNumberOfAlarmModes,i,j : integer; sDeviceName,sLimitName,sIniDeviceName : string; begin inifile := TIniFile.Create('.\' + IDName + '.ini'); nNumberOfDevices := StrToInt(inifile.ReadString('General','NumberOf','0')); for i:=0 to nNumberOfDevices-1 do begin sDeviceName := 'device' + IntToStr(i); if not(inifile.SectionExists(sDeviceName)) then continue; sIniDeviceName := inifile.ReadString(sDeviceName,'DeviceName',''); for j:=0 to 17 do begin if sIniDeviceName <> DeviceController.Devices[j].sDeviceName then continue; DeviceController.Devices[j].Interval := 1000*StrToInt( inifile.ReadString(sDeviceName,'CaptionInterval','3600')); nNumberOfAlarmModes := StrToInt( inifile.ReadString(sDeviceName,'AlarmModes','0')); if nNumberOfAlarmModes >= 1 then //for j:=1 to nNumberOfAlarmModes do begin sLimitName := sDeviceName + 'Limits1'; if not(inifile.SectionExists(sLimitName)) then break; //inifile.ReadString(LimitName,'priority','1'); DeviceController.Devices[j].LimitsStart := StrToTime( inifile.ReadString(sLimitName,'Start','0:00')); DeviceController.Devices[j].LimitsEnd := StrToTime( inifile.ReadString(sLimitName,'End','23:59')); DeviceController.Devices[j].sLimitsMin := inifile.ReadString(sLimitName,'Min','Off'); DeviceController.Devices[j].sLimitsMax := inifile.ReadString(sLimitName,'Max','Off'); break; end; end; end; // for i:=0 to nNumberOfDevices-1 inifile.Free; end; // Generates default config file, which is sen tto the data unit when // surveillance unit starts up for the first time procedure TSurveillanceController.SaveSettings; var inifile : TIniFile; nSensorID,i : integer; sDeviceName, sLimitName : string; begin inifile := TIniFile.Create('.\' + IDName + '.ini'); inifile.WriteString('General','UnitName', IDName); i:=-1; // DeviceNumber for inifile nSensorID:=-1; // SensorID while nSensorID < 17 do begin nSensorID:=nSensorID+1; if not DeviceController.Devices[nSensorID].bEnabled then continue; i:=i+1; sDeviceName := 'device' + IntToStr(i); inifile.WriteString(sDeviceName, 'sensorID', IntToStr(nSensorID)); inifile.WriteString(sDeviceName, 'Type', DeviceController.Devices[nSensorID].sDeviceType); inifile.WriteString(sDeviceName, 'DeviceName', DeviceController.Devices[nSensorID].sDeviceName); inifile.WriteString(sDeviceName, 'CaptionInterval', IntToStr(Round( DeviceController.Devices[nSensorID].Interval/1000))); inifile.WriteString(sDeviceName, 'AlarmModes', IntToStr(1)); // there can be only 1 AlarmMode sLimitName := sDeviceName+'Limits'+IntToStr(1); // 1 is number of AlarmModes inifile.WriteString(sLimitName,'priority',IntToStr(1)); inifile.WriteString(sLimitName,'Start','0:00'); inifile.WriteString(sLimitName,'End','23:59'); inifile.WriteString(sLimitName,'Min','off'); inifile.WriteString(sLimitName,'Max','off'); end; inifile.WriteString('General','NumberOf', IntToStr(i+1)); inifile.Free; end; // Process command what has been sent from dataunit procedure TSurveillanceController.ProcessCommand(Sender: TObject; sText: string); var scommand : string; begin scommand :=ParseString(sText); if scommand = '' then scommand := sText; // This is received when new config file exits in dataunit if scommand = 'SU_NEWCONFIGEXISTS' then begin client.GetFile('.\config\' + IDName + '.ini','.\' + IDName + '.ini'); LoadSettings; if Assigned(OnStatus) Then OnStatus('New settings loaded'); end; // This is received when config file allready exits in dataunit if scommand = 'SU_CONFIGNOTEXISTS' then begin SaveSettings; client.SendFile('.\' + IDName + '.ini','.\config\' + IDName + '.ini'); if Assigned(OnStatus) Then OnStatus('Settings saved and sended to dataunit'); end; // This is received when configuration unit wants realtime image if scommand = 'SU_TAKEPICTURE' then begin DeviceController.Grab_Frame; sleep(1000); client.SendText('DU_NEWPICTUREEXISTS'); end; // This answer to DU_CHECK, if we dont receive this we know that there is // connection problem to dataunit. if scommand = 'CHECK' then begin nVerifyCounter := 0; end; end; end.