主要使用Tclientsocket控件的程序运行1天速度奇慢,高手来帮我!!! ( 积分: 100 )

  • 主题发起人 主题发起人 末日3000
  • 开始时间 开始时间

末日3000

Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi自带的TclientSocket控件编了一个程序,动态创建clientsock,连接保存于gyh表中的server端的ip地址,解析并处理传上来的信息。但正常运行1天后导致计算机cpu占用100,响应事件速度奇慢,鼠标半天动一下,大家主要帮我看看clientsock动态创建部分以及error时间处理以及时钟不断重新连接部分有没有错误,我想主要问题应该出在这些部分



unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls, Buttons, ScktComp, DB, ADODB,IniFiles,
Grids,StrUtils, OleCtrls, MSWinsockLib_TLB, MCI_TLB;

type
TFrmMain = class(TForm)
StatusBar1: TStatusBar;
Panel1: TPanel;
Image1: TImage;
BitBtnExit: TBitBtn;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label1: TLabel;
TTime: TTimer;
Label2: TLabel;
TSock: TTimer;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
ListBox1: TListBox;
TAlarmChuli: TTimer;
MMControl1: TMMControl;
TimerPlaySnd: TTimer;
Clientsocket: TWinsock;
TDM: TTimer;
BitBtn2: TBitBtn;
BitBtn1: TBitBtn;

procedure FormCreate(Sender: TObject);
procedure BitBtnExitClick(Sender: TObject);
procedure TTimeTimer(Sender: TObject);
procedure TSockTimer(Sender: TObject);
procedure TAlarmChuliTimer(Sender: TObject);
procedure TimerPlaySndTimer(Sender: TObject);
procedure ClientsocketConnectionRequest(Sender: TObject;
requestID: Integer);
procedure ClientsocketError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer;
var CancelDisplay: WordBool);
procedure MyClientDataArrival(Sender:TObject;bytesTotal:integer);
procedure MyClientClose(Sender:TObject);
procedure MyClientError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer;
var CancelDisplay: WordBool);
procedure TDMTimer(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
procedure MyClientSocketConnect(Sender : TObject; Socket: TCustomWinSocket);
procedure MyClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure MyClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure MyClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
// procedure MyClientSocketWrite(Sender: TObject;
// Socket: TCustomWinSocket);
procedure DataChuli(strData:string;Index:integer);
function StringtoInt(inString:string):integer;//字符串到整形数的转换
procedure DTMFchuli(sendSTRING:string);
procedure SaveAlarmInfo(TableName : String; jqbh : String;yhbm : String;xxlx : String;ztz : String;sbbh : String; bjsj : String; jyh : String; jqgs : String;jjsj : String; jjczy : String;kzqxh : String;gsdm : String);//往数据库中添加记录,表名和字段名
procedure xxQuery(Num:LongInt); //将gjjqt,gjj6qt 表中的第Num之后的记录删除
Procedure writeDTMF(recvDTMF:String);
function getCURjqbh():String; //取得警情编号
procedure sendCLIENT(sendSTRING:string); //向客户端发送数据
procedure endSND();
function getCurTime():String;
procedure playSND(WaveFile:string);
function getLJRQ():string; //得到连接日期
procedure RELOAD(TableName:String);
end;
SockNod=record
MyClientSock:TClientsocket; //客户端连接socket
SockFlag:boolean;//sock建立标志
YHBM:string;//用户编码
YHDW:string;//用户单位
YHDZ:string;//用户地址
DMCount:integer;//自动点名计数
end;
MyAlarm=record//待处理警情节点记录
Alarm:string; //警情字符串
index:integer;//产生该警情的clientsock在全局变量mysocknod中的索引号
end;
MyQueue=record //待处理警情队列
AlarmData: array[0..499] of MyAlarm;//数据,最大容量500
AlarmCount:integer; //当前队列内未处理警情数量
Head:integer;//队列头索引
Tail:integer;//队列尾索引
end;
ClientNode=record
IP:string;
SktHandle:TWinsock;
status:boolean;
end;
SendValues=record
op_str:array[0..13] of Byte;
count:integer;
end;
var
g_CZYXM:String; //操作员姓名
FrmMain: TFrmMain;
G_LinkNo:integer;
mySockNod: array of SockNod;
AlarmQueue:myqueue;
//mxj2005-5-16设置监控器静音相关变量
G_OnOff_JY:boolean;//真为置静音,假为置非静音

g_MaxNum:Longint; //信息台中记录显示的最大记录数
g_CurNum:Longint; //信息台中当前记录数
g_JQBH:Longint; //警情编号


playSound:boolean; //是否正在播放声音
g_playSND:Boolean; //是否放音

{播放的具体声音类型}
SoundType:integer; //1、报警 2、故障
GZboolean:boolean; //是否允许报故障,体现火警优先
PlayCount:integer; //已经播放的次数
CanPlayCount:integer; //允许播放的次数

TmpI:integer; //客户端连接的显示中的条数
WillSendStr:string; //要向客户端发送的字符串

AllClient:array[0..10] of ClientNode; //所有连接的客户端信息
Clientquantity:integer; //已经连接的客户端数量
ClientCount:integer; //允许连接的客户端数量
xxNum:Integer; //当前发送信息的信息台显示记录数
implementation

uses UnitData;

{$R *.dfm}


procedure TFrmMain.FormCreate(Sender: TObject);
var
MyAdoquery:Tadoquery;
FileName,DBString:string;
IniFileName:TIniFile;
i:integer;
begin
g_czyxm:='系统';
//读取ini,初始化数据库连接
FileName:=ExtractFilePath(Application.ExeName)+'gstdata.ini';
IniFileName:=TiniFile.Create(FileName);
DBString:='Provider=MSDASQL.1;Persist Security Info=false;Data Source='+IniFileName.ReadString('ITEMS','DB','gstdata')+';Initial Catalog='+IniFileName.ReadString('ITEMS','DB','gstdata');
try
datamodule1.ADOConnection.ConnectionString:=dbstring;
datamodule1.ADOConnection.Open;
except
messagebox(application.handle,'连接数据库服务器失败请检查相关ini文件、数据源及网络环境,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;
{初始化LUWTCP标题头}
stringgrid1.RowCount:=1;
StringGrid1.ColCount:=8;
StringGrid1.ColWidths[0]:=40;
StringGrid1.Cols[0].Text := '序号';
StringGrid1.ColWidths[1]:=100;
StringGrid1.Cols[1].Text:= 'IP地址';
StringGrid1.ColWidths[2]:=60;
StringGrid1.Cols[2].Text := '用户编码';
StringGrid1.ColWidths[3]:=110;
StringGrid1.Cols[3].Text := '设备说明';
StringGrid1.ColWidths[4]:=110;
StringGrid1.Cols[4].Text := '运行状态';
StringGrid1.ColWidths[5]:=140;
StringGrid1.Cols[5].Text := '用户地址';
StringGrid1.ColWidths[6]:=120;
StringGrid1.Cols[6].Text := '登录时间';
StringGrid1.ColWidths[7]:=80;
StringGrid1.Cols[7].Text := '登录端口';
{初始化LVWCLIENT标题头}
StringGrid2.RowCount :=1;
StringGrid2.ColCount:=11;
StringGrid2.ColWidths[0]:=80;
StringGrid2.Cols[0].Text := 'IP地址';
StringGrid2.ColWidths[1]:=60;
StringGrid2.Cols[1].Text := '工作站';
StringGrid2.ColWidths[2]:=60;
StringGrid2.Cols[2].Text := '操作员';
StringGrid2.ColWidths[3]:=50;
StringGrid2.Cols[3].Text := '状态';
StringGrid2.ColWidths[4]:=160;
StringGrid2.Cols[4].Text := '类型';
StringGrid2.ColWidths[5]:=0;
StringGrid2.Cols[5].Text := '对应内线';
StringGrid2.ColWidths[6]:=90;
StringGrid2.Cols[6].Text := '工作日期';
StringGrid2.ColWidths[7]:=90;
StringGrid2.Cols[7].Text := '工作时间';
StringGrid2.ColWidths[8]:=90;
StringGrid2.Cols[8].Text := '通讯日期';
StringGrid2.ColWidths[9]:=90;
StringGrid2.Cols[9].Text := '通讯时间';
StringGrid2.ColWidths[10]:=0;
StringGrid2.Cols[10].Text := '打印机';
//从数据库中读取当前警情编号及相关系统信息
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gcs ');
myadoquery.Open;
if myadoquery.RecordCount<1 then
begin
messagebox(application.handle,'无系统相关设置信息,请通过客户端进行设置,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;


g_JQBH:=myadoquery.fieldbyname('fjqbh').AsInteger;
g_MaxNum:=myadoquery.Fieldbyname('fjls').AsInteger;
if g_JQBH=9999 then
g_JQBH:=0;
except
messagebox(application.handle,'读取系统设置失败,请检查相关ini文件、数据源及网络环境,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;

finally
myadoquery.Close;
myadoquery.Free;
end;
//设置响应客户端socke
clientsocket.LocalPort:=1234;
clientsocket.Listen;

//初始化接警队列
alarmqueue.AlarmCount:=0;
alarmqueue.Head:=0;
alarmqueue.Tail:=0;

//从数据库中读取相关客户信息,初始化socket客户端
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gyh where (FZT=''Y'') and (FipCode<>''no'')');
myadoquery.Open;
if myadoquery.RecordCount<1 then
begin
messagebox(application.handle,'尚未填写用户资料,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;

i:=0;
//初始化mysocknod数组,它是一个全局变量,包含所有连接监控器的clientsocket,
//结构一经建立就不会改变,每次程序运行会从数据库中读取相应监控器设置建立。
setlength(mysocknod,myadoquery.RecordCount);
myadoquery.First;
while not(myadoquery.Eof) do
begin
if length(myadoquery.FieldByName('fipcode').AsString)>=6 then
begin
mysocknod.MyClientSock:=tclientsocket.Create(self);
writedtmf('MYsockNod('+inttostr(i)+').MyClientSock建立,');
mysocknod.SockFlag:=true;
if mysocknod.MyClientSock.Active then
mysocknod.myClientSock.active:=false;
mysocknod.MyClientSock.OnConnect:=MyClientsocketconnect;
mysocknod.MyClientSock.OnError:=MyClientSocketError;
mysocknod.MyClientSock.OnDisconnect:=MyClientSocketDisconnect;
mysocknod.MyClientSock.OnRead:=myclientsocketread;
// mysocknod.MyClientSock.OnWrite:=myclientsocketwrite;
mysocknod.myClientSock.Host:=myadoquery.fieldbyname('fipcode').AsString;
mysocknod.myClientSock.Port:=4660;
mysocknod.MyClientSock.ClientType:=ctnonblocking;
mysocknod.YHBM:=myadoquery.fieldbyname('fbm').AsString;
mysocknod.YHDW:=myadoquery.fieldbyname('fdw').AsString;
mysocknod.YHDZ:=myadoquery.fieldbyname('fdz').AsString;
mysocknod.DMCount:=0;
i:=i+1;
end;
myadoquery.MoveBy(1);
end;
g_linkno:=i; //全局变量负值,为当前连接数目
//连接监控器完毕,启动点名时钟轮流点名
TDM.Enabled:=true;
except
messagebox(application.handle,'读取用户资料失败,请检查相关ini文件、数据源及网络环境,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;

finally
myadoquery.Close;
myadoquery.Free;
end;

end;

procedure TFrmMain.MyClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,index:integer;
begin
for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Socket=socket then
begin
index:=-1;
for j:=1 to stringgrid1.RowCount-1 do
begin
if mysocknod.MyClientSock.Host=stringgrid1.Cells[1,j] then
index:=j;
end;
if index = -1 then
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
index:=stringgrid1.RowCount-1;
end;
StringGrid1.Cells[0,index]:=inttostr(index);
StringGrid1.Cells[1,index]:=mysocknod.MyClientSock.Host;
StringGrid1.Cells[2,index]:=mysocknod.yhbm;
StringGrid1.Cells[3,index]:='GST2000 监控器';
StringGrid1.Cells[4,index]:='连接正常';
StringGrid1.Cells[5,index]:=mysocknod.YHDZ;
StringGrid1.Cells[6,index]:=formatdatetime('yyyy-mm-dd hh:mm:ss',now);
StringGrid1.Cells[7,index]:='4660';
end;
end;
end;

procedure TFrmMain.MyClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
i,j:integer;
begin
for i:=0 to g_linkno do
begin
if mysocknod.SockFlag then
begin
if mysocknod.MyClientSock=sender then
begin
for j:=1 to stringgrid1.RowCount-1 do
if mysocknod.MyClientSock.Host=StringGrid1.Cells[1,j] then
StringGrid1.Cells[4,j]:='连接断开';
mysocknod.MyClientSock.Socket.Close;

end;
end;
end;

end;

procedure TFrmMain.MyClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Socket=socket then
begin
for j:=1 to stringgrid1.RowCount-1 do
if mysocknod.MyClientSock.Host=StringGrid1.Cells[1,j] then
StringGrid1.Cells[4,j]:='连接断开';
writedtmf('mysocknod['+inttostr(i)+'].Myclientsock因错误断开,连接地址:'+mysocknod.MyClientSock.Host);
end;
end;
end;

procedure TFrmMain.MyClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
DataType:integer;
BufSend,BufRecv: array[0..2048] of char; //申请数据接收的缓存;
tmpstring:array[0..4096] of char;
nReceived:integer;
strReceived,strTmp:string;
begin


for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Socket=socket then
begin
//数据的接收和转换
nreceived:=socket.ReceiveBuf(bufrecv,socket.ReceiveLength);
BinToHex(BufRecv,tmpstring,2048); //转化数据
strtmp:=MidStr(tmpstring,1,2*nreceived);
strreceived:='';
j:=2;
while j<=2*nreceived do
begin
strreceived:=strreceived+midstr(strtmp,j,1);
j:=j+2;
end;

//edit1.Text:=strreceived;
//完成数据接收,结果保存于strReceived字符串中
//改变主界面中对应项为连接正常
for j:=1 to stringgrid1.RowCount-1 do
if mysocknod.MyClientSock.Host=StringGrid1.Cells[1,j] then
StringGrid1.Cells[4,j]:='连接正常';
//调用数据处理函数进行处理DataChuli(strData:string,index:integer)
//该函数以接收到的字符串和全局socket数组mysocknod中的索引为传入参数
DataChuli(strreceived,i);

end;
end;

end;

//procedure TFrmMain.MyClientSocketWrite(Sender: TObject;
// Socket: TCustomWinSocket);
//begin
//
//end;

procedure TFrmMain.TSockTimer(Sender: TObject);
var
i:integer;
begin
for i:=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
if (not(mysocknod.MyClientSock.Active)) then
begin
mysocknod.MyClientSock.Open;
writedtmf('mysocknod['+inttostr(i)+'].MyClientSock重新建立连接,连接地址:'+mysocknod.MyClientSock.Host);
end;
end;
end;
end;



procedure TFrmMain.BitBtnExitClick(Sender: TObject);
var
i:integer;
begin
tsock.Enabled:=false;
for i:=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
if mysocknod.MyClientSock.Active then
mysocknod.MyClientSock.Close;

mysocknod.MyClientSock.Free;
end;
mysocknod.MyClientSock:=nil;
mysocknod.SockFlag:=false;
mysocknod.YHBM:='';
mysocknod.YHDW:='';
mysocknod.YHDZ:='';
end;

application.Terminate;
end;

procedure TFrmMain.TTimeTimer(Sender: TObject);
begin
Label1.Caption:=datetimetostr(now);
end;

procedure TFrmMain.DataChuli(strData:string;Index:integer);
var
i,start:integer;
strtmp,strAlarm,strjy:string;

myOk:array[0..21] of string;

HuiMaA: array[0..0] of byte;
HuiMaB: array[0..0] of byte;
begin
huimaa[0]:=10;
huimab[0]:=11;
//数据处理函数,非报警数据进行响应处理,是报警数据则进行交验,正确则写入待处理队列
//对点名回复不做处理,本程序只通过定时器定时对监控器发送点名命令,连接的通断与否
//通过连接socket状态直接判断
if (strdata='C') then
begin
if G_OnOff_JY then
begin
strjy:='设置静音成功!'
end
else
begin
strjy:='设置放音成功!'
end;
ListBox1.AddItem('用户编码:'+mysocknod.YHBM+'; 用户地址:'+mysocknod.YHDZ+'; '+strjy ,frmmain);
exit;
end;
if (strdata='D') then
begin
if G_OnOff_JY then
begin
strjy:='设置静音失败!'
end
else
begin
strjy:='设置放音失败!'
end;
ListBox1.AddItem('用户编码:'+mysocknod.YHBM+'; 用户地址:'+mysocknod.YHDZ+'; '+strjy ,frmmain);
exit;
end;
if length(strdata)>26 then//警情长度为27个字符,小于这个长度的不被处理
begin
strtmp:=strdata;//传递参数给strtmp,以后对strtmp进行处理
start:=pos('D01EE',strtmp);
if start>0 then
begin
while start+26<=length(strtmp) do
begin
stralarm:=copy(strtmp,start+5,22);
strtmp:=copy(strtmp,start+5,length(strtmp)-5);
start:=pos('DO1EE',strtmp);
For i:=1 To 22 do
begin
myOK[i-1]:=MidStr(stralarm,i,1);
end;
if StringtoInt(myOK[21])=((StringtoInt(myOK[0])+StringtoInt(myOK[1])+
StringtoInt(myOK[2])+StringtoInt(myOK[3])+StringtoInt(myOK[4])+
StringtoInt(myOK[5])+StringtoInt(myOK[6])+StringtoInt(myOK[7])+
StringtoInt(myOK[8])+StringtoInt(myOK[9])+StringtoInt(myOK[10])+
StringtoInt(myOK[11])+StringtoInt(myOK[12])+StringtoInt(myOK[13])+
StringtoInt(myOK[14])+StringtoInt(myOK[15])+StringtoInt(myOK[16])+
StringtoInt(myOK[17])+StringtoInt(myOK[18])+StringtoInt(myOK[19])+StringtoInt(myOK[20])) Mod (16)) Then //校验正确
begin //交验正确,回a,加入待处理队列
//edit1.Text:=stralarm+'交验正确';
mysocknod[index].MyClientSock.Socket.SendBuf(huimaa,1);
if alarmqueue.AlarmCount<500 then
begin
alarmqueue.AlarmData[alarmqueue.Tail].Alarm:=stralarm;
alarmqueue.AlarmData[alarmqueue.Tail].index:=index;
alarmqueue.AlarmCount:=alarmqueue.AlarmCount+1;
if alarmqueue.Tail=499 then
alarmqueue.Tail:=0
else
alarmqueue.Tail:=alarmqueue.Tail+1;
end;
end
else //错误,回b
begin
mysocknod[index].MyClientSock.Socket.SendBuf(huimab,1);
end;
end;
end;
end;
end;

{字符串到整形数的转换}
function tfrmmain.StringtoInt(inString:string):integer;
var
TmpStr:string;
TmpintVal,i:integer;
begin
TmpStr:=inString;
if tmpSTR='D' then
begin
TmpintVal:=13;
end
else if tmpSTR='A' then
begin
TmpintVal:=10;
end
else if tmpSTR='B' then
begin
TmpintVal:=11;
end
else if tmpSTR='C' then
begin
TmpintVal:=12;
end
else if tmpSTR='E' then
begin
TmpintVal:=14;
end
else if tmpSTR='F' then
begin
TmpintVal:=15;
end
else
begin
val(TmpSTR,TmpintVal,i);
end;
StringtoInt:=TmpintVal;
end;

procedure TFrmMain.TAlarmChuliTimer(Sender: TObject);
var
MyAlarmData:MyAlarm;
begin
//从待处理队列中读取一条记录进行处理
if alarmqueue.AlarmCount>0 then
begin
myalarmdata:=alarmqueue.AlarmData[alarmqueue.Head];
alarmqueue.AlarmCount:=alarmqueue.AlarmCount-1;
alarmqueue.Head:=alarmqueue.Head+1;
if alarmqueue.Head>=500 then
alarmqueue.Head:=0;
dtmfchuli(myalarmdata.Alarm);
end;
end;

procedure Tfrmmain.DTMFchuli(sendSTRING:string);
var
i,j,k,l,m,n:integer;//用于循环的临时变量


myJQBH:string;//警情编号
strUserID : String; //用户编码
strUserName : String ; //用户名称
strUserAdd : String ; //用户地址
strKzqcj : String ; //控制器厂家
strKzqxh : String ; //控制器型号
strKzqStateInfo : String ; //控制器状态信息
STRJkqStateInfo : string ; //监控器状态信息
strInfoType : String ; //信息类型(00)
//Infotype:integer;//信息类型整数标志
strStateWord : String ; //状态字(00)
Sbbh1,sbbh2,sbbh3 : integer ; //设备编号(000000)
strsbbh:string;//设备编号的传入信息
strSbbhInfo : String ; //设备编号的具体信息
strSbbhType : String ; //设备编号类型
strBjsj : String ; //报警时间(MMDDHHMMSS)
strCheckNum : String ; //校验和
strJqgs : String ; //警情概述

myAdoQuery:TAdoQuery;
begin
if length(sendstring)<22 then
begin
writedtmf('★处理数据长度小于22,不进行处理,内容:' + sendstring);
exit;
end;
//测试
//edit1.Text:='D01EE'+sendstring;
//变量初始化
myJQBH:=' ';//警情编号
strUserID :=' '; //用户编码
strUserName :=' ';// 用户名称
strUserAdd :=' ' ; //用户地址
strKzqcj :=' ' ; //控制器厂家
strKzqxh :=' ' ; //控制器型号
strKzqStateInfo :=' ' ; //控制器状态信息
STRJkqStateInfo :=' ' ; //监控器状态信息
strInfoType :=' ' ; //信息类型(00)
//Infotype:=99;//信息类型整数标志
strStateWord :=' '; //状态字(00)

strsbbh:=' ';//设备编号的传入信息
strSbbhInfo :=' ' ; //设备编号的具体信息
strSbbhType :=' '; //设备编号类型
strBjsj :=' ' ; //报警时间(MMDDHHMMSS)
strCheckNum :=' ' ; //校验和
strJqgs :=' ' ; //警情概述
/////////////////

//开始处理信息
struserid:=copy(sendstring,1,4) ;
strinfotype:=copy(sendstring,5,1);
strstateword:=copy(sendstring,6,2);
sbbh1:=strtoint(copy(sendstring,8,2));
sbbh2:=strtoint(copy(sendstring,10,2));
sbbh3:=strtoint(copy(sendstring,12,2));
strbjsj:=copy(sendstring,14,8);
strchecknum:=copy(sendstring,22,1);
strsbbh:=copy(sendstring,8,6);
strUserName := '未注册用户:' + strUserID ; //用户名称
strUserAdd := '未知地址 ' ; //用户地址
strKzqcj := 'GST' ; //控制器厂家
strKzqxh := '5000' ; //控制器型号
//查询用户信息
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gyh where fbm='''+struserid+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strusername:=myadoquery.fieldbyname('fdw').AsString;
struseradd:=myadoquery.fieldbyname('fdz').AsString;
strkzqcj:=myadoquery.fieldbyname('fgsdm').asstring;
strkzqxh:=myadoquery.fieldbyname('fkzq').AsString;

end;
except
exit;
end;

finally
myadoquery.Free;
end;
//strsbbhinfo:='探头'+copy(sendstring,8,6)+'(没有设置含义!)' ;
strsbbhinfo:=' ' ;
If strInfoType <> '9' Then
begin //如果信息类型不是监控器,则翻译探头号
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gzx where fgsdm='''+strkzqcj+''' and fkzq='''+ strkzqxh+'''');
myadoquery.Open;
if myadoquery.RecordCount >0 then
begin
myadoquery.First;
If myadoquery.FieldByName('ffd').AsInteger = 1 Then
begin
If (myadoquery.fieldbyname('FKS1').asinteger >= 1) And (myadoquery.fieldbyname('FKS1').asinteger <= 6) And (myadoquery.fieldbyname('FCD1').AsInteger >= 1) Then
strSbbhInfo := copy(strSbbh, myadoquery.fieldbyname('FKS1').asinteger, myadoquery.fieldbyname('FCD1').asinteger) + Trim(myadoquery.fieldbyname('FHY1').AsString)
Else
Writedtmf( '公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!');
end
Else If myadoquery.fieldbyname('ffd').asinteger = 2 Then
begin
If (myadoquery.fieldbyname('FKS1').asinteger >= 1) And (myadoquery.fieldbyname('FKS1').asinteger <= 6) And (myadoquery.fieldbyname('FCD1').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger <= 6) And (myadoquery.fieldbyname('FCD2').asinteger >= 1) Then
strSbbhInfo := copy(strSbbh, myadoquery.fieldbyname('FKS1').asinteger, myadoquery.fieldbyname('FCD1').asinteger) + Trim(myadoquery.fieldbyname('FHY1').AsString) + copy(strSbbh, myadoquery.fieldbyname('FKS2').asinteger, myadoquery.fieldbyname('FCD2').asinteger) + Trim(myadoquery.fieldbyname('FHY2').asstring)
Else
Writedtmf( '公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!');
end
Else If myadoquery.fieldbyname('ffd').asinteger = 3 Then
begin
If (myadoquery.fieldbyname('FKS1').asinteger >= 1) And( myadoquery.fieldbyname('FKS1').asinteger <= 6 )And( myadoquery.fieldbyname('FCD1').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger <= 6) And (myadoquery.fieldbyname('FCD2').asinteger >= 1) And (myadoquery.fieldbyname('FKS3').asinteger >= 1) And (myadoquery.fieldbyname('FKS3').asinteger <= 6) And (myadoquery.fieldbyname('FCD3').asinteger >= 1) Then
strSbbhInfo :=copy(strSbbh, myadoquery.fieldbyname('FKS1').asinteger, myadoquery.fieldbyname('FCD1').asinteger) + Trim(myadoquery.fieldbyname('FHY1').AsString) + copy(strSbbh, myadoquery.fieldbyname('FKS2').asinteger, myadoquery.fieldbyname('FCD2').asinteger) + trim(myadoquery.fieldbyname('FHY2').AsString) + copy(strSbbh, myadoquery.fieldbyname('FKS3').asinteger, myadoquery.fieldbyname('FCD3').asinteger) + Trim(myadoquery.fieldbyname('FHY3').AsString)
Else
Writedtmf( '公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!')
end
Else
begin
Writedtmf ('公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!');
end;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
End;
If (strtoint(strInfoType) < 8) Or (strtoint(strInfoType) >= 11) Then //'信息类型小于8,状态字代表设备代码类别,通过状态字在GSM表中检索设备代码含义
begin
//strSbbhType := '未知类型(数据库中未设置!)';
strSbbhType := ' ';
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gsm where fgsdm='''+strkzqcj+''' and fdm='''+ strstateword+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strsbbhtype:=myadoquery.fieldbyname('flx').AsString;
end;
except
exit;
end;

finally
myadoquery.Close;
myadoquery.Free;
end;
end;

If strInfoType = '8' Then// '信息类型等于8,状态字代表控制器状态,通过状态字在GKZ表中检索控制器状态
begin
//strKzqStateInfo := '未知状态(数据库中未设置!)';
strKzqStateInfo := ' ';
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gkz where fdm='''+strstateword+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strkzqstateinfo:=myadoquery.fieldbyname('flx').AsString;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
end;
If strInfoType = '9' Then// '信息类型等于9,状态字代表监控器状态,通过状态字在GJKB表中检索控制器状态
begin
//strJkqStateInfo := '未知状态(数据库中未设置!)';
strJkqStateInfo := ' ';
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gJKB where fdm='''+strstateword+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strjkqstateinfo:=myadoquery.fieldbyname('flx').AsString;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
end;
myJQBH:=getCURjqbh(); //生成警情编号

if strinfotype='0' then //判断信息类型
//误报 '同时写入Gwb库,Gjjqt库
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType + '--'+ '误报';
SaveAlarmInfo( 'gwb', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs,formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
SendClient ('1@03@' +myJQBH) ;
end
else if strinfotype='1' then
//自动火警,无人
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType + '--'+ '自动火警(无人)';
SaveAlarmInfo ('gjj', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);


if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=1;
TimerPlaySnd.Enabled:=True;
end;

SendClient( '1@01@' + myJQBH);
end
else if strinfotype='2' then
//启动
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType +'--' + '启动';
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1 ;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
SendClient ('1@02@' + myJQBH);
end
else if strinfotype='3' then
//停动
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType + '--' + '停动';
SaveAlarmInfo('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

SendClient ('1@03@' + myJQBH );
end
else if strinfotype='4' then
//探测器故障
begin
strJqgs := strUserName + '--' +strSbbhInfo + '--' + strSbbhType + '--' + '探测器故障';
SaveAlarmInfo ('gjjgz', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=2;
TimerPlaySnd.Enabled:=True;
end;

SendClient ('1@04@' + myJQBH);
end
else if strinfotype='5' then
//隔离
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--'+ strSbbhType +'--' + '隔离';
SaveAlarmInfo('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

SendClient ('1@05@'+ myJQBH);
end
else if strinfotype='6' then
//取消隔离
begin
strJqgs := strUserName + '--'+strSbbhInfo +'--'+ strSbbhType +'--'+'取消隔离';
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
SendClient ('1@06@'+ myJQBH);

end
else if strinfotype='7' then
//确认火警
begin
strJqgs := strUserName +'--'+ strSbbhInfo + '--'+strSbbhType +'--'+'确认火警';
SaveAlarmInfo( 'gjj', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=1;
TimerPlaySnd.Enabled:=True;
end;

SendClient ('1@07@' + myJQBH);
end
else if strinfotype='8' then
//控制器状态
begin
strJqgs := strUserName + '--' + '控制器状态' + '--' + strKzqStateInfo;
//是报故障
If (strStateWord = '02') Or (strStateWord = '04') Or (strStateWord = '0D') Or (strStateWord = '0E') Then
begin
SaveAlarmInfo( 'gjjgz', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=2;
TimerPlaySnd.Enabled:=True;
end;
SendClient ('1@08YN@' + myJQBH);
end
//是报故障恢复
Else If (strStateWord = '03') Or (strStateWord = '05') Then
begin
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );
SendClient ('1@08NY@' + myJQBH);
// datamodule1.ADOCommand1.CommandText:= ' update gjjgz set fcjsj=''' + GetCurTime() + ''',fcjczy=''自动'' where fbm=''' + strUserID +''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' insert into gjjgzcl select * from gjjgz where fbm=''' + strUserID + ''' and fztz=''' + '0'+inttostr(strtoint(strStateWord)-1) + '''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjgz where fbm=''' + strUserID + ''' and fztz=''' + '0'+inttostr(strtoint(strStateWord)-1) + '''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz=''' + '0'+inttostr(strtoint(strStateWord)-1) + '''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz=''' + strStateWord +'''';
// datamodule1.ADOCommand1.Execute;


end
//既不是报故障也不是报故障恢复
Else
begin
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs,formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SendClient ('1@08NN@' + myJQBH);
End;

g_CurNum := g_CurNum + 1 ;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
end
else if strinfotype='9' then
//监控器状态
begin
strJqgs := strUserName + '--' + '监控器状态' + '--' + strJkqStateInfo;
//是报故障
If (strStateWord = '01') Or (strStateWord = '03') Or (strStateWord = '05') or(strstateword='08')Then
begin
SaveAlarmInfo( 'gjjgz', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=2;
TimerPlaySnd.Enabled:=True;
end;
SendClient ('1@09YN@' + myJQBH);
end
//是报故障恢复
Else If (strStateWord = '02') Or (strStateWord = '04') Or (strStateWord = '06') Then
begin
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );
SendClient ('1@09NY@' + myJQBH);
// datamodule1.ADOCommand1.CommandText:= ' update gjjgz set fcjsj=''' + GetCurTime() + ''',fcjczy=''自动'' where fbm=''' + strUserID +''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' insert into gjjgzcl select * from gjjgz where fbm=''' + strUserID + ''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjgz where fbm=''' + strUserID + ''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz='''+ strStateWord +'''';
// datamodule1.ADOCommand1.Execute;


end
//火警,特殊处理
else if (strstateword='0B') then
begin
SaveAlarmInfo( 'gjj', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=1;
TimerPlaySnd.Enabled:=True;
end;
SendClient ('1@09NN@' + myJQBH);
end
//既不是报故障也不是报故障恢复
Else
begin
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SendClient ('1@09NN@' + myJQBH);
End;

g_CurNum := g_CurNum + 1 ;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

end
else if strinfotype='A' then
//控制器注册
begin
strJqgs := strUserName +'--'+'控制器注册';
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum) ;
SendClient ('1@0A@'+ myJQBH);
end;
statusbar1.Panels[0].Text:='用户编码:'+struserid+'; 警情概述:'+strjqgs;
end;


procedure tfrmmain.SaveAlarmInfo(TableName : String; jqbh : String;yhbm : String;xxlx : String;ztz : String;sbbh : String; bjsj : String; jyh : String; jqgs : String;jjsj : String; jjczy : String;kzqxh : String;gsdm : String);//往数据库中添加记录,表名和字段名
var
myADOQuery:tadoquery;
begin
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from '+trim(tablename) + ' where fjqbh=''' + jqbh+'''' );
myadoquery.Open;
If myadoquery.RecordCount <= 0 Then
begin
myadoquery.Insert;
myadoquery.Edit;
myadoquery.FieldByName('fjqbh').AsString:=jqbh;//警情编号
myadoquery.FieldByName('fbm').AsString:=yhbm;//用户编码
myadoquery.FieldByName('fxxlx').AsString:=xxlx;//信息类型
myadoquery.FieldByName('fztz').AsString:=ztz;//状态字
myadoquery.FieldByName('fsbbh').AsString:=sbbh;//设备编号
myadoquery.FieldByName('fsj').AsString:=bjsj;//报警时间(mmddhhmm)
myadoquery.FieldByName('fjyh').AsString:=jyh;//校验和
myadoquery.FieldByName('fms').AsString:=jqgs;//警情概述
myadoquery.FieldByName('fjjsj').AsString:=jjsj;//接警时间(yyyymmddhhmmss)
myadoquery.FieldByName('fjjczy').AsString:=jjczy;//接警操作员
myadoquery.FieldByName('fkzq').AsString:=kzqxh;//控制器型号
myadoquery.FieldByName('fgsdm').AsString:=gsdm;//公司代码/
myadoquery.UpdateBatch;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
end;

{获取警情编号}
function Tfrmmain.getCURjqbh():String;
var


SLen:integer;
tmpstr,resultStr:string;

begin
try
g_JQBH:=g_JQBH+1;
if g_jqbh=9999 then
g_jqbh:=0000;
tmpstr:=InttoStr(g_JQBH);
SLen:=Length(tmpstr);
Case SLen of
1:
resultStr:='000'+tmpstr;
2:
resultStr:='00'+tmpstr;
3:
resultStr:='0'+tmpstr;
4:
resultStr:=tmpstr;
end;
datamodule1.ADOQueryJQBH.Close;
datamodule1.ADOQueryJQBH.SQL.Clear;
datamodule1.ADOQueryJQBH.SQL.Add('select * from gcs');
datamodule1.ADOQueryJQBH.Open;
datamodule1.adoqueryjqbh.edit;
if datamodule1.ADOQueryJQBH.RecordCount=1 then
begin
datamodule1.ADOQueryJQBH.FieldByName('fjqbh').AsInteger:=g_jqbh;
datamodule1.ADOQueryJQBH.UpdateBatch;
end;
datamodule1.ADOQueryJQBH.Close;
getCURjqbh:=formatdatetime('yyyymmdd',now)+resultStr;
except
getcurjqbh:=formatdatetime('yyyymmdd',now)+inttostr(g_jqbh) ;

end;
end;

{DTMF数据写入文件中}
Procedure Tfrmmain.writeDTMF(recvDTMF:String);
var
filevar: textfile;
FileNameStr:string;
begin
FileNameStr:=ExtractFilePath(Application.ExeName)+'TXT/';
if Length(Trim(recvDTMF))>0 then
begin
FileNameStr:=FileNameStr+formatdatetime('yyyymmdd',now)+'.txt';
assignfile(filevar,FileNameStr);
if FileExists(FileNameStr) then
begin
append(filevar);
end
else
begin
rewrite(filevar); // 新建立
end;
writeln(filevar,recvDTMF+':'+getCURtime);
closefile(filevar);
end;
end;

{向客户端发送数据}
procedure Tfrmmain.sendCLIENT(sendSTRING:string);
var
k,i:integer;
WillSendString,WillSendStringClient:string;
kk:OleVariant;
begin
if (sendSTRING<>'') and (not(varisnull(sendSTRING))) and (not(varisempty(sendSTRING)))then
begin
WillSendString:=sendSTRING;
kk:=OleVariant(sendSTRING);
k:=1;
while k<=Clientquantity do
begin
if AllClient[k].SktHandle.State=7 then
begin
WillSendStringClient:=WillSendString+'##';
AllClient[k].SktHandle.SendData(WillSendStringClient);
writeDTMF('!!发送数据给客户端:'+inttostr(integer(AllClient[k].SktHandle))+':'+inttostr(AllClient[k].SktHandle.LocalPort)+'数据内容:'+ WillSendStringClient);
end;
k:=k+1;
end;
end;
end;
{将gjjqt表中的第Num之后的记录删除}
procedure Tfrmmain.xxQuery(Num:Longint);
var
seleJQBH:String;
TimeStr:String;
mydataset:tadodataset;
myadocommand:tadocommand;
begin
Try
/////////////
try
mydataset:=tadodataset.Create(nil);
mydataset.Close;
mydataset.Connection:=datamodule1.ADOConnection;
mydataset.CommandType:=cmdtext;
mydataset.CommandText:='select fjqbh,fms from gjjqt order by fjqbh asc';
mydataset.Open;
myadocommand:=tadocommand.Create(nil);
myadocommand.Connection:=datamodule1.ADOConnection;
myadocommand.CommandType:=cmdtext;
With mydataset do
begin
if mydataset.RecordCount>Num then
begin
mydataset.First;
mydataset.MoveBy(mydataset.RecordCount-Num+1);
seleJQBH:=Trim(Recordset.Fields['FJQBH'].value);
TimeStr:=formatdatetime('yyyymmddhhmmss',now);

myadocommand.CommandText:='update gjjqt set fcjsj ='+''''+TimeStr+''''+',fcjczy='+''''+'自动'+'''' +' where fjqbh< '+''''+seleJQBH+'''';
myadocommand.Execute;
myadocommand.CommandText:='insert into gcjqt select * from gjjqt where fjqbh<'+''''+seleJQBH+'''';
myadocommand.Execute;
myadocommand.CommandText:='delete from GJJqt where fJQBH <'+''''+seleJQBH+'''';
myadocommand.Execute;
end;
g_CurNum:=Num;
end;
except
exit;
end;
Finally
mydataset.Close;
mydataset.Free;
myadocommand.Free;
end;
end;


procedure TFrmMain.TimerPlaySndTimer(Sender: TObject);
begin
//播放声音
if PlayCount<CanPlayCount then //声音播放CanPlayCount遍
begin
case SoundType of
1: //播放报警声音
begin
GZboolean:=False;
playSND('报警.wav');
end;
2: //播放故障声音
begin
GZboolean:=False;
playSND('故障.wav');
end;
end;
inc(PlayCount);
end
else
begin
PlayCount:=0;
PlaySound:=False;
GZboolean:=true;
TimerPlaySnd.Enabled:=False;
end;
end;

{结束放音操作}
procedure Tfrmmain.endSND();
begin
try
MMControl1.FileName:='';
MMControl1.Notify:=False;
MMControl1.Wait:=False;
MMControl1.Command:='sound';
except
abort;
end;
end;

function tfrmmain.getCurTime():String;
begin
getCurTime:=MidStr(formatdatetime('yyyymmddhhmmss',now),9,6);
end;

{播放声音文件}
procedure tfrmmain.playSND(WaveFile:string);
var
WavFileName:string;
begin
WavFileName:=ExtractFilePath(Application.ExeName)+'WAV/';
MMControl1.FileName:=WavFileName+WaveFile;
MMControl1.Notify:=False;
MMControl1.Wait:=False;
MMControl1.Command:='sound';
end;


procedure TFrmMain.ClientsocketConnectionRequest(Sender: TObject;
requestID: Integer);
var
strIp,strHost,DateStr,TimeStr:string;
systemtime:TSystemTime;
DateTime:TDateTime;
BeepI:integer;
begin
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);
BeepI:=0;
strIp:=clientSocket.RemoteHostIP;
strHost:=clientSocket.RemoteHost;
//显示在LvwClient中,并需要连接数据库提取相关的信息
TmpI:=1;
while TmpI<StringGrid2.RowCount do
begin
if Trim(AllClient[TmpI].IP)=Trim(StrIp) then
begin
//已经存在了此客户端IP,置状态为 '连接正常'
Allclient[TmpI].SktHandle:=Twinsock.Create(self);

Allclient[TmpI].SktHandle.OnDataArrival:=MyClientDataArrival;
Allclient[TmpI].SktHandle.OnClose:=MyClientClose;
Allclient[TmpI].SktHandle.OnError:=MyClientError;
Allclient[TmpI].IP:=strIp;
Allclient[TmpI].status:=True;
Allclient[TmpI].SktHandle.accept(requestID);
if Allclient[TmpI].SktHandle.State=7 then
begin
StringGrid2.Cells[3,TmpI]:='联通';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
end;
break;
end;
TmpI:=TmpI+1;
end;
if true then
begin
if TmpI=StringGrid2.RowCount then //不存在此IP
begin
Allclient[TmpI].SktHandle:=Twinsock.Create(self);
Allclient[TmpI].SktHandle.OnDataArrival:=MyClientDataArrival;
Allclient[TmpI].SktHandle.OnClose:=MyClientClose;
Allclient[TmpI].SktHandle.OnError:=MyClientError;
Allclient[TmpI].IP:=strIp;
Allclient[TmpI].SktHandle.accept(requestID);
writeDTMF('!!客户端:'+Allclient[TmpI].IP+'对应socket索引:'+inttostr(Integer(Allclient[TmpI].SktHandle))+'成功登录服务器!');
if Allclient[TmpI].SktHandle.State=7 then
begin
StringGrid2.RowCount:=StringGrid2.RowCount+1;
StringGrid2.Cells[0,TmpI]:=strIp;
StringGrid2.Cells[3,TmpI]:='联通';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
Allclient[TmpI].status:=True;
Clientquantity:=Clientquantity+1;
end;
end;
end
else
begin
while BeepI<10 do
begin
beep;
BeepI:=BeepI+1;
end;
end;

end;

procedure TFrmMain.ClientsocketError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer; var CancelDisplay: WordBool);
var
strIp:string;
begin
TmpI:=1;
strIp:=Clientsocket.RemoteHostIP;
while TmpI<StringGrid2.RowCount-1 do
begin
if StringGrid2.Cells[1,TmpI]=StrIp then
begin
//已经存在了此客户端IP,置状态为 '连接正常'
StringGrid2.Cells[3,TmpI]:='断开';
Allclient[TmpI].SktHandle.Close ;//孟祥君2005-1-27更改,关闭错误服务器socket端
break;
end;
TmpI:=TmpI+1;
end;
end;

{响应客户端的数据到达事件}
procedure Tfrmmain.MyClientDataArrival(Sender:TObject;bytesTotal:integer);
var
TmpSendValue:SendValues;
TmpSendValBYTE:array[0..13] of Byte;
///////2005-3-1增加针对于启动监控器灭火
tmpValIP,index,StrLen:integer;
sock1:twinsock;
TmpHostIP,DateStr,TimeStr,TmpSendStr,sTR1, Str4:string;
systemtime:TSystemTime;
DateTime:TDateTime;
// Info:widestring;
StrData:Olevariant;
n1:integer;

xxSTR,QTSTR:String; //信息台发送的信息标识,进行识别是否为“管理信息”记录
cFirst:String;




begin
try
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);
tmpValIP:=1;

sock1:=Twinsock(Sender);
TmpHostIP:=sock1.RemoteHostIP;



//通过IP取得索引值,比较所有的已连接的IP值
while tmpValIP<=Clientquantity do
begin
if AllClient[tmpValIP].IP=TmpHostIP then
begin
index:=TmpValIP;
break;
end;
tmpValIP:=tmpValIP+1;
end;
sock1.GetData(StrData,8);
StrLen:=length(StrData);
//对Info字符串进行操作的编程
cFirst:=midStr(StrData,1,1);
//显示的信息
StringGrid2.Cells[8,index]:=DateStr;
stringGrid2.Cells[9,index]:=TimeStr;


////////////////////////////////////
if cFirst='0' then //登录
begin
StringGrid2.cells[2,index]:=MidStr(StrData,3,StrLen-2);
StringGrid2.cells[3,index]:='联通';
StringGrid2.cells[6,index]:=DateStr;
StringGrid2.cells[7,index]:=TimeStr;
StringGrid2.cells[8,index]:=DateStr;
StringGrid2.cells[9,index]:=TimeStr;
TmpSendStr:='4@'+getLJRQ;
sendCLIENT(TmpSendStr);
end;
if cFirst='1' then //工作站类型、是否显示地图、是否有打印机
StringGrid2.cells[4,index]:=MidStr(StrData,3,StrLen-2);
if cFirst='2' then //工作站标识
begin
StringGrid2.cells[1,index]:=MidStr(StrData,3,StrLen-2);
TmpSendStr:='4@'+getLJRQ;
sendCLIENT(TmpSendStr);
end;
if cFirst='5' then //监控仪处警
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJ set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into Gcj select * from GJJ where fJQBH='''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJ where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='7' then //监控仪误报
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJ set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into GWB select * from GJJ where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJ where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='9' then //开始巡检
//startXJ;
if cFirst='B' then //结束巡检
//endXJ;
if cFirst='C' then //监控仪上报故障处理
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJgz set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='insert into Gjjgzcl select * from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D02@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='D' then //监控仪上报故障误报
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJgz set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into GWB select * from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='G' then // 监控器其它故障信息
begin
xxSTR:=Midstr(StrData, 2, 1);
If xxSTR='J' Then //监控器的管理信息
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJqt set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into Gcjqt select * from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
end;
if cFirst='H' then //
begin
QTSTR:=MidStr(StrData, 2, 1);
If QTSTR='J' Then //监控器的管理信息
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJqt set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into Gwb select * from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr); //刷新信息台
end
else
begin
With datamodule1.ADOCommandClient do
begin
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
end;

if cFirst='N' then //信息台中允许显示的最大记录数
begin
xxNum:=StrtoInt(MidStr(StrData,3,StrLen-2));
If g_CurNum>xxNum Then //当前记录数
xxQuery(xxNum);
end;
if cFirst='T' then //经确认的火警,将发送到119指挥中心
begin
n1:=pos('@',StrData);
Str4:=copy(StrData,n1+1,length(StrData)-n1);
//n2:=pos('@',StrData);
Str1:=MidStr(StrData,1,n1-1);
end;
if cFirst='R' then //客户机向服务器发送更改系统用户/总线/设备代码等信息后重新加载数据
begin
RELOAD(Midstr(StrData, 3, 3));
end;
except
end;


end;

procedure Tfrmmain.MyClientClose(Sender:TObject);
var
strIp,DateStr,TimeStr:string;
systemtime:TSystemTime;
DateTime:TDateTime;
sock1:twinsock;
j:integer;
begin
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);

TmpI:=1;
j:=0;
sock1:=Twinsock(Sender);
strIp:=sock1.RemoteHostIP;
while TmpI<StringGrid2.RowCount do
begin
if Trim(StringGrid2.Cells[0,TmpI])=Trim(StrIp) then
begin
//已经存在了此客户端IP,置状态为 '断开'
StringGrid2.Cells[3,TmpI]:='断开';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
while j<stringgrid2.RowCount do
begin
if StringGrid2.Cells[0,TmpI]=strIp then
begin
Allclient[j].status:=False;
break;
end;
j:=j+1;
end;
break;
end;
TmpI:=TmpI+1;
end;
end;

procedure Tfrmmain.MyClientError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer;
var CancelDisplay: WordBool);
var
strIp,DateStr,TimeStr:string;
systemtime:TSystemTime;
DateTime:TDateTime;
sock1:twinsock;
j:integer;
begin
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);

TmpI:=1;
j:=0;
sock1:=Twinsock(Sender);
strIp:=sock1.RemoteHostIP;
while TmpI<StringGrid2.RowCount do
begin
if Trim(StringGrid2.Cells[0,TmpI])=Trim(StrIp) then
begin
//已经存在了此客户端IP,置状态为 '断开'
StringGrid2.Cells[3,TmpI]:='断开';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
while j<stringgrid2.RowCount do
begin
if StringGrid2.Cells[0,TmpI]=strIp then
begin
Allclient[j].status:=False;
break;
end;
j:=j+1;
end;
break;
end;
TmpI:=TmpI+1;
end;
end;

//取得系统时间的日期
function Tfrmmain.getLJRQ():string;
begin


getLJRQ:=formatdatetime('yyyymmdd',now);

end;

{系统设置发生变化后要求重新装入用户/总线/各公司代码/监控仪警情说明等信息 }
procedure tfrmmain.RELOAD(TableName:String);
begin
StatusBar1.Panels[0].Text:='系统配置发生变化,请重启服务器程序!';
playSND('故障.wav');
end;

procedure TFrmMain.TDMTimer(Sender: TObject);
var
i:integer;
sendbuf:array[0..2] of BYTE;
begin
sendbuf[0]:=13;
sendbuf[1]:=13;
sendbuf[2]:=13;

for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Active then
mysocknod.MyClientSock.Socket.SendBuf(SENDBUF,3);
end;
end;

procedure TFrmMain.BitBtn2Click(Sender: TObject);
var
i:integer;
SendFY:array[0..3] of byte;
begin
listbox1.Items.Clear;
G_OnOff_JY:=false;
sendfy[0]:=9;
sendfy[1]:=14;
sendfy[2]:=1;
sendfy[3]:=15;


for i :=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
mysocknod.MyClientSock.Socket.SendBuf(sendfy,4);
end;
end;

end;

procedure TFrmMain.BitBtn1Click(Sender: TObject);
var
i:integer;
SendJY:array[0..7] of byte;
begin
listbox1.Items.Clear;
G_OnOff_JY:=TRUE;
sendjy[0]:=9;
sendjy[1]:=14;
sendjy[2]:=0;
sendjy[3]:=14;

for i :=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
mysocknod.MyClientSock.Socket.SendBuf(sendjy,4);
end;
end;

end;

end.
 
我用delphi自带的TclientSocket控件编了一个程序,动态创建clientsock,连接保存于gyh表中的server端的ip地址,解析并处理传上来的信息。但正常运行1天后导致计算机cpu占用100,响应事件速度奇慢,鼠标半天动一下,大家主要帮我看看clientsock动态创建部分以及error时间处理以及时钟不断重新连接部分有没有错误,我想主要问题应该出在这些部分



unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls, Buttons, ScktComp, DB, ADODB,IniFiles,
Grids,StrUtils, OleCtrls, MSWinsockLib_TLB, MCI_TLB;

type
TFrmMain = class(TForm)
StatusBar1: TStatusBar;
Panel1: TPanel;
Image1: TImage;
BitBtnExit: TBitBtn;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label1: TLabel;
TTime: TTimer;
Label2: TLabel;
TSock: TTimer;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
ListBox1: TListBox;
TAlarmChuli: TTimer;
MMControl1: TMMControl;
TimerPlaySnd: TTimer;
Clientsocket: TWinsock;
TDM: TTimer;
BitBtn2: TBitBtn;
BitBtn1: TBitBtn;

procedure FormCreate(Sender: TObject);
procedure BitBtnExitClick(Sender: TObject);
procedure TTimeTimer(Sender: TObject);
procedure TSockTimer(Sender: TObject);
procedure TAlarmChuliTimer(Sender: TObject);
procedure TimerPlaySndTimer(Sender: TObject);
procedure ClientsocketConnectionRequest(Sender: TObject;
requestID: Integer);
procedure ClientsocketError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer;
var CancelDisplay: WordBool);
procedure MyClientDataArrival(Sender:TObject;bytesTotal:integer);
procedure MyClientClose(Sender:TObject);
procedure MyClientError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer;
var CancelDisplay: WordBool);
procedure TDMTimer(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
procedure MyClientSocketConnect(Sender : TObject; Socket: TCustomWinSocket);
procedure MyClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure MyClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure MyClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
// procedure MyClientSocketWrite(Sender: TObject;
// Socket: TCustomWinSocket);
procedure DataChuli(strData:string;Index:integer);
function StringtoInt(inString:string):integer;//字符串到整形数的转换
procedure DTMFchuli(sendSTRING:string);
procedure SaveAlarmInfo(TableName : String; jqbh : String;yhbm : String;xxlx : String;ztz : String;sbbh : String; bjsj : String; jyh : String; jqgs : String;jjsj : String; jjczy : String;kzqxh : String;gsdm : String);//往数据库中添加记录,表名和字段名
procedure xxQuery(Num:LongInt); //将gjjqt,gjj6qt 表中的第Num之后的记录删除
Procedure writeDTMF(recvDTMF:String);
function getCURjqbh():String; //取得警情编号
procedure sendCLIENT(sendSTRING:string); //向客户端发送数据
procedure endSND();
function getCurTime():String;
procedure playSND(WaveFile:string);
function getLJRQ():string; //得到连接日期
procedure RELOAD(TableName:String);
end;
SockNod=record
MyClientSock:TClientsocket; //客户端连接socket
SockFlag:boolean;//sock建立标志
YHBM:string;//用户编码
YHDW:string;//用户单位
YHDZ:string;//用户地址
DMCount:integer;//自动点名计数
end;
MyAlarm=record//待处理警情节点记录
Alarm:string; //警情字符串
index:integer;//产生该警情的clientsock在全局变量mysocknod中的索引号
end;
MyQueue=record //待处理警情队列
AlarmData: array[0..499] of MyAlarm;//数据,最大容量500
AlarmCount:integer; //当前队列内未处理警情数量
Head:integer;//队列头索引
Tail:integer;//队列尾索引
end;
ClientNode=record
IP:string;
SktHandle:TWinsock;
status:boolean;
end;
SendValues=record
op_str:array[0..13] of Byte;
count:integer;
end;
var
g_CZYXM:String; //操作员姓名
FrmMain: TFrmMain;
G_LinkNo:integer;
mySockNod: array of SockNod;
AlarmQueue:myqueue;
//mxj2005-5-16设置监控器静音相关变量
G_OnOff_JY:boolean;//真为置静音,假为置非静音

g_MaxNum:Longint; //信息台中记录显示的最大记录数
g_CurNum:Longint; //信息台中当前记录数
g_JQBH:Longint; //警情编号


playSound:boolean; //是否正在播放声音
g_playSND:Boolean; //是否放音

{播放的具体声音类型}
SoundType:integer; //1、报警 2、故障
GZboolean:boolean; //是否允许报故障,体现火警优先
PlayCount:integer; //已经播放的次数
CanPlayCount:integer; //允许播放的次数

TmpI:integer; //客户端连接的显示中的条数
WillSendStr:string; //要向客户端发送的字符串

AllClient:array[0..10] of ClientNode; //所有连接的客户端信息
Clientquantity:integer; //已经连接的客户端数量
ClientCount:integer; //允许连接的客户端数量
xxNum:Integer; //当前发送信息的信息台显示记录数
implementation

uses UnitData;

{$R *.dfm}


procedure TFrmMain.FormCreate(Sender: TObject);
var
MyAdoquery:Tadoquery;
FileName,DBString:string;
IniFileName:TIniFile;
i:integer;
begin
g_czyxm:='系统';
//读取ini,初始化数据库连接
FileName:=ExtractFilePath(Application.ExeName)+'gstdata.ini';
IniFileName:=TiniFile.Create(FileName);
DBString:='Provider=MSDASQL.1;Persist Security Info=false;Data Source='+IniFileName.ReadString('ITEMS','DB','gstdata')+';Initial Catalog='+IniFileName.ReadString('ITEMS','DB','gstdata');
try
datamodule1.ADOConnection.ConnectionString:=dbstring;
datamodule1.ADOConnection.Open;
except
messagebox(application.handle,'连接数据库服务器失败请检查相关ini文件、数据源及网络环境,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;
{初始化LUWTCP标题头}
stringgrid1.RowCount:=1;
StringGrid1.ColCount:=8;
StringGrid1.ColWidths[0]:=40;
StringGrid1.Cols[0].Text := '序号';
StringGrid1.ColWidths[1]:=100;
StringGrid1.Cols[1].Text:= 'IP地址';
StringGrid1.ColWidths[2]:=60;
StringGrid1.Cols[2].Text := '用户编码';
StringGrid1.ColWidths[3]:=110;
StringGrid1.Cols[3].Text := '设备说明';
StringGrid1.ColWidths[4]:=110;
StringGrid1.Cols[4].Text := '运行状态';
StringGrid1.ColWidths[5]:=140;
StringGrid1.Cols[5].Text := '用户地址';
StringGrid1.ColWidths[6]:=120;
StringGrid1.Cols[6].Text := '登录时间';
StringGrid1.ColWidths[7]:=80;
StringGrid1.Cols[7].Text := '登录端口';
{初始化LVWCLIENT标题头}
StringGrid2.RowCount :=1;
StringGrid2.ColCount:=11;
StringGrid2.ColWidths[0]:=80;
StringGrid2.Cols[0].Text := 'IP地址';
StringGrid2.ColWidths[1]:=60;
StringGrid2.Cols[1].Text := '工作站';
StringGrid2.ColWidths[2]:=60;
StringGrid2.Cols[2].Text := '操作员';
StringGrid2.ColWidths[3]:=50;
StringGrid2.Cols[3].Text := '状态';
StringGrid2.ColWidths[4]:=160;
StringGrid2.Cols[4].Text := '类型';
StringGrid2.ColWidths[5]:=0;
StringGrid2.Cols[5].Text := '对应内线';
StringGrid2.ColWidths[6]:=90;
StringGrid2.Cols[6].Text := '工作日期';
StringGrid2.ColWidths[7]:=90;
StringGrid2.Cols[7].Text := '工作时间';
StringGrid2.ColWidths[8]:=90;
StringGrid2.Cols[8].Text := '通讯日期';
StringGrid2.ColWidths[9]:=90;
StringGrid2.Cols[9].Text := '通讯时间';
StringGrid2.ColWidths[10]:=0;
StringGrid2.Cols[10].Text := '打印机';
//从数据库中读取当前警情编号及相关系统信息
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gcs ');
myadoquery.Open;
if myadoquery.RecordCount<1 then
begin
messagebox(application.handle,'无系统相关设置信息,请通过客户端进行设置,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;


g_JQBH:=myadoquery.fieldbyname('fjqbh').AsInteger;
g_MaxNum:=myadoquery.Fieldbyname('fjls').AsInteger;
if g_JQBH=9999 then
g_JQBH:=0;
except
messagebox(application.handle,'读取系统设置失败,请检查相关ini文件、数据源及网络环境,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;

finally
myadoquery.Close;
myadoquery.Free;
end;
//设置响应客户端socke
clientsocket.LocalPort:=1234;
clientsocket.Listen;

//初始化接警队列
alarmqueue.AlarmCount:=0;
alarmqueue.Head:=0;
alarmqueue.Tail:=0;

//从数据库中读取相关客户信息,初始化socket客户端
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gyh where (FZT=''Y'') and (FipCode<>''no'')');
myadoquery.Open;
if myadoquery.RecordCount<1 then
begin
messagebox(application.handle,'尚未填写用户资料,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;

i:=0;
//初始化mysocknod数组,它是一个全局变量,包含所有连接监控器的clientsocket,
//结构一经建立就不会改变,每次程序运行会从数据库中读取相应监控器设置建立。
setlength(mysocknod,myadoquery.RecordCount);
myadoquery.First;
while not(myadoquery.Eof) do
begin
if length(myadoquery.FieldByName('fipcode').AsString)>=6 then
begin
mysocknod.MyClientSock:=tclientsocket.Create(self);
writedtmf('MYsockNod('+inttostr(i)+').MyClientSock建立,');
mysocknod.SockFlag:=true;
if mysocknod.MyClientSock.Active then
mysocknod.myClientSock.active:=false;
mysocknod.MyClientSock.OnConnect:=MyClientsocketconnect;
mysocknod.MyClientSock.OnError:=MyClientSocketError;
mysocknod.MyClientSock.OnDisconnect:=MyClientSocketDisconnect;
mysocknod.MyClientSock.OnRead:=myclientsocketread;
// mysocknod.MyClientSock.OnWrite:=myclientsocketwrite;
mysocknod.myClientSock.Host:=myadoquery.fieldbyname('fipcode').AsString;
mysocknod.myClientSock.Port:=4660;
mysocknod.MyClientSock.ClientType:=ctnonblocking;
mysocknod.YHBM:=myadoquery.fieldbyname('fbm').AsString;
mysocknod.YHDW:=myadoquery.fieldbyname('fdw').AsString;
mysocknod.YHDZ:=myadoquery.fieldbyname('fdz').AsString;
mysocknod.DMCount:=0;
i:=i+1;
end;
myadoquery.MoveBy(1);
end;
g_linkno:=i; //全局变量负值,为当前连接数目
//连接监控器完毕,启动点名时钟轮流点名
TDM.Enabled:=true;
except
messagebox(application.handle,'读取用户资料失败,请检查相关ini文件、数据源及网络环境,程序将退出!','系统提示',MB_OK) ;
application.Terminate ;
end;

finally
myadoquery.Close;
myadoquery.Free;
end;

end;

procedure TFrmMain.MyClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,index:integer;
begin
for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Socket=socket then
begin
index:=-1;
for j:=1 to stringgrid1.RowCount-1 do
begin
if mysocknod.MyClientSock.Host=stringgrid1.Cells[1,j] then
index:=j;
end;
if index = -1 then
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
index:=stringgrid1.RowCount-1;
end;
StringGrid1.Cells[0,index]:=inttostr(index);
StringGrid1.Cells[1,index]:=mysocknod.MyClientSock.Host;
StringGrid1.Cells[2,index]:=mysocknod.yhbm;
StringGrid1.Cells[3,index]:='GST2000 监控器';
StringGrid1.Cells[4,index]:='连接正常';
StringGrid1.Cells[5,index]:=mysocknod.YHDZ;
StringGrid1.Cells[6,index]:=formatdatetime('yyyy-mm-dd hh:mm:ss',now);
StringGrid1.Cells[7,index]:='4660';
end;
end;
end;

procedure TFrmMain.MyClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
i,j:integer;
begin
for i:=0 to g_linkno do
begin
if mysocknod.SockFlag then
begin
if mysocknod.MyClientSock=sender then
begin
for j:=1 to stringgrid1.RowCount-1 do
if mysocknod.MyClientSock.Host=StringGrid1.Cells[1,j] then
StringGrid1.Cells[4,j]:='连接断开';
mysocknod.MyClientSock.Socket.Close;

end;
end;
end;

end;

procedure TFrmMain.MyClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Socket=socket then
begin
for j:=1 to stringgrid1.RowCount-1 do
if mysocknod.MyClientSock.Host=StringGrid1.Cells[1,j] then
StringGrid1.Cells[4,j]:='连接断开';
writedtmf('mysocknod['+inttostr(i)+'].Myclientsock因错误断开,连接地址:'+mysocknod.MyClientSock.Host);
end;
end;
end;

procedure TFrmMain.MyClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
DataType:integer;
BufSend,BufRecv: array[0..2048] of char; //申请数据接收的缓存;
tmpstring:array[0..4096] of char;
nReceived:integer;
strReceived,strTmp:string;
begin


for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Socket=socket then
begin
//数据的接收和转换
nreceived:=socket.ReceiveBuf(bufrecv,socket.ReceiveLength);
BinToHex(BufRecv,tmpstring,2048); //转化数据
strtmp:=MidStr(tmpstring,1,2*nreceived);
strreceived:='';
j:=2;
while j<=2*nreceived do
begin
strreceived:=strreceived+midstr(strtmp,j,1);
j:=j+2;
end;

//edit1.Text:=strreceived;
//完成数据接收,结果保存于strReceived字符串中
//改变主界面中对应项为连接正常
for j:=1 to stringgrid1.RowCount-1 do
if mysocknod.MyClientSock.Host=StringGrid1.Cells[1,j] then
StringGrid1.Cells[4,j]:='连接正常';
//调用数据处理函数进行处理DataChuli(strData:string,index:integer)
//该函数以接收到的字符串和全局socket数组mysocknod中的索引为传入参数
DataChuli(strreceived,i);

end;
end;

end;

//procedure TFrmMain.MyClientSocketWrite(Sender: TObject;
// Socket: TCustomWinSocket);
//begin
//
//end;

procedure TFrmMain.TSockTimer(Sender: TObject);
var
i:integer;
begin
for i:=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
if (not(mysocknod.MyClientSock.Active)) then
begin
mysocknod.MyClientSock.Open;
writedtmf('mysocknod['+inttostr(i)+'].MyClientSock重新建立连接,连接地址:'+mysocknod.MyClientSock.Host);
end;
end;
end;
end;



procedure TFrmMain.BitBtnExitClick(Sender: TObject);
var
i:integer;
begin
tsock.Enabled:=false;
for i:=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
if mysocknod.MyClientSock.Active then
mysocknod.MyClientSock.Close;

mysocknod.MyClientSock.Free;
end;
mysocknod.MyClientSock:=nil;
mysocknod.SockFlag:=false;
mysocknod.YHBM:='';
mysocknod.YHDW:='';
mysocknod.YHDZ:='';
end;

application.Terminate;
end;

procedure TFrmMain.TTimeTimer(Sender: TObject);
begin
Label1.Caption:=datetimetostr(now);
end;

procedure TFrmMain.DataChuli(strData:string;Index:integer);
var
i,start:integer;
strtmp,strAlarm,strjy:string;

myOk:array[0..21] of string;

HuiMaA: array[0..0] of byte;
HuiMaB: array[0..0] of byte;
begin
huimaa[0]:=10;
huimab[0]:=11;
//数据处理函数,非报警数据进行响应处理,是报警数据则进行交验,正确则写入待处理队列
//对点名回复不做处理,本程序只通过定时器定时对监控器发送点名命令,连接的通断与否
//通过连接socket状态直接判断
if (strdata='C') then
begin
if G_OnOff_JY then
begin
strjy:='设置静音成功!'
end
else
begin
strjy:='设置放音成功!'
end;
ListBox1.AddItem('用户编码:'+mysocknod.YHBM+'; 用户地址:'+mysocknod.YHDZ+'; '+strjy ,frmmain);
exit;
end;
if (strdata='D') then
begin
if G_OnOff_JY then
begin
strjy:='设置静音失败!'
end
else
begin
strjy:='设置放音失败!'
end;
ListBox1.AddItem('用户编码:'+mysocknod.YHBM+'; 用户地址:'+mysocknod.YHDZ+'; '+strjy ,frmmain);
exit;
end;
if length(strdata)>26 then//警情长度为27个字符,小于这个长度的不被处理
begin
strtmp:=strdata;//传递参数给strtmp,以后对strtmp进行处理
start:=pos('D01EE',strtmp);
if start>0 then
begin
while start+26<=length(strtmp) do
begin
stralarm:=copy(strtmp,start+5,22);
strtmp:=copy(strtmp,start+5,length(strtmp)-5);
start:=pos('DO1EE',strtmp);
For i:=1 To 22 do
begin
myOK[i-1]:=MidStr(stralarm,i,1);
end;
if StringtoInt(myOK[21])=((StringtoInt(myOK[0])+StringtoInt(myOK[1])+
StringtoInt(myOK[2])+StringtoInt(myOK[3])+StringtoInt(myOK[4])+
StringtoInt(myOK[5])+StringtoInt(myOK[6])+StringtoInt(myOK[7])+
StringtoInt(myOK[8])+StringtoInt(myOK[9])+StringtoInt(myOK[10])+
StringtoInt(myOK[11])+StringtoInt(myOK[12])+StringtoInt(myOK[13])+
StringtoInt(myOK[14])+StringtoInt(myOK[15])+StringtoInt(myOK[16])+
StringtoInt(myOK[17])+StringtoInt(myOK[18])+StringtoInt(myOK[19])+StringtoInt(myOK[20])) Mod (16)) Then //校验正确
begin //交验正确,回a,加入待处理队列
//edit1.Text:=stralarm+'交验正确';
mysocknod[index].MyClientSock.Socket.SendBuf(huimaa,1);
if alarmqueue.AlarmCount<500 then
begin
alarmqueue.AlarmData[alarmqueue.Tail].Alarm:=stralarm;
alarmqueue.AlarmData[alarmqueue.Tail].index:=index;
alarmqueue.AlarmCount:=alarmqueue.AlarmCount+1;
if alarmqueue.Tail=499 then
alarmqueue.Tail:=0
else
alarmqueue.Tail:=alarmqueue.Tail+1;
end;
end
else //错误,回b
begin
mysocknod[index].MyClientSock.Socket.SendBuf(huimab,1);
end;
end;
end;
end;
end;

{字符串到整形数的转换}
function tfrmmain.StringtoInt(inString:string):integer;
var
TmpStr:string;
TmpintVal,i:integer;
begin
TmpStr:=inString;
if tmpSTR='D' then
begin
TmpintVal:=13;
end
else if tmpSTR='A' then
begin
TmpintVal:=10;
end
else if tmpSTR='B' then
begin
TmpintVal:=11;
end
else if tmpSTR='C' then
begin
TmpintVal:=12;
end
else if tmpSTR='E' then
begin
TmpintVal:=14;
end
else if tmpSTR='F' then
begin
TmpintVal:=15;
end
else
begin
val(TmpSTR,TmpintVal,i);
end;
StringtoInt:=TmpintVal;
end;

procedure TFrmMain.TAlarmChuliTimer(Sender: TObject);
var
MyAlarmData:MyAlarm;
begin
//从待处理队列中读取一条记录进行处理
if alarmqueue.AlarmCount>0 then
begin
myalarmdata:=alarmqueue.AlarmData[alarmqueue.Head];
alarmqueue.AlarmCount:=alarmqueue.AlarmCount-1;
alarmqueue.Head:=alarmqueue.Head+1;
if alarmqueue.Head>=500 then
alarmqueue.Head:=0;
dtmfchuli(myalarmdata.Alarm);
end;
end;

procedure Tfrmmain.DTMFchuli(sendSTRING:string);
var
i,j,k,l,m,n:integer;//用于循环的临时变量


myJQBH:string;//警情编号
strUserID : String; //用户编码
strUserName : String ; //用户名称
strUserAdd : String ; //用户地址
strKzqcj : String ; //控制器厂家
strKzqxh : String ; //控制器型号
strKzqStateInfo : String ; //控制器状态信息
STRJkqStateInfo : string ; //监控器状态信息
strInfoType : String ; //信息类型(00)
//Infotype:integer;//信息类型整数标志
strStateWord : String ; //状态字(00)
Sbbh1,sbbh2,sbbh3 : integer ; //设备编号(000000)
strsbbh:string;//设备编号的传入信息
strSbbhInfo : String ; //设备编号的具体信息
strSbbhType : String ; //设备编号类型
strBjsj : String ; //报警时间(MMDDHHMMSS)
strCheckNum : String ; //校验和
strJqgs : String ; //警情概述

myAdoQuery:TAdoQuery;
begin
if length(sendstring)<22 then
begin
writedtmf('★处理数据长度小于22,不进行处理,内容:' + sendstring);
exit;
end;
//测试
//edit1.Text:='D01EE'+sendstring;
//变量初始化
myJQBH:=' ';//警情编号
strUserID :=' '; //用户编码
strUserName :=' ';// 用户名称
strUserAdd :=' ' ; //用户地址
strKzqcj :=' ' ; //控制器厂家
strKzqxh :=' ' ; //控制器型号
strKzqStateInfo :=' ' ; //控制器状态信息
STRJkqStateInfo :=' ' ; //监控器状态信息
strInfoType :=' ' ; //信息类型(00)
//Infotype:=99;//信息类型整数标志
strStateWord :=' '; //状态字(00)

strsbbh:=' ';//设备编号的传入信息
strSbbhInfo :=' ' ; //设备编号的具体信息
strSbbhType :=' '; //设备编号类型
strBjsj :=' ' ; //报警时间(MMDDHHMMSS)
strCheckNum :=' ' ; //校验和
strJqgs :=' ' ; //警情概述
/////////////////

//开始处理信息
struserid:=copy(sendstring,1,4) ;
strinfotype:=copy(sendstring,5,1);
strstateword:=copy(sendstring,6,2);
sbbh1:=strtoint(copy(sendstring,8,2));
sbbh2:=strtoint(copy(sendstring,10,2));
sbbh3:=strtoint(copy(sendstring,12,2));
strbjsj:=copy(sendstring,14,8);
strchecknum:=copy(sendstring,22,1);
strsbbh:=copy(sendstring,8,6);
strUserName := '未注册用户:' + strUserID ; //用户名称
strUserAdd := '未知地址 ' ; //用户地址
strKzqcj := 'GST' ; //控制器厂家
strKzqxh := '5000' ; //控制器型号
//查询用户信息
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gyh where fbm='''+struserid+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strusername:=myadoquery.fieldbyname('fdw').AsString;
struseradd:=myadoquery.fieldbyname('fdz').AsString;
strkzqcj:=myadoquery.fieldbyname('fgsdm').asstring;
strkzqxh:=myadoquery.fieldbyname('fkzq').AsString;

end;
except
exit;
end;

finally
myadoquery.Free;
end;
//strsbbhinfo:='探头'+copy(sendstring,8,6)+'(没有设置含义!)' ;
strsbbhinfo:=' ' ;
If strInfoType <> '9' Then
begin //如果信息类型不是监控器,则翻译探头号
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gzx where fgsdm='''+strkzqcj+''' and fkzq='''+ strkzqxh+'''');
myadoquery.Open;
if myadoquery.RecordCount >0 then
begin
myadoquery.First;
If myadoquery.FieldByName('ffd').AsInteger = 1 Then
begin
If (myadoquery.fieldbyname('FKS1').asinteger >= 1) And (myadoquery.fieldbyname('FKS1').asinteger <= 6) And (myadoquery.fieldbyname('FCD1').AsInteger >= 1) Then
strSbbhInfo := copy(strSbbh, myadoquery.fieldbyname('FKS1').asinteger, myadoquery.fieldbyname('FCD1').asinteger) + Trim(myadoquery.fieldbyname('FHY1').AsString)
Else
Writedtmf( '公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!');
end
Else If myadoquery.fieldbyname('ffd').asinteger = 2 Then
begin
If (myadoquery.fieldbyname('FKS1').asinteger >= 1) And (myadoquery.fieldbyname('FKS1').asinteger <= 6) And (myadoquery.fieldbyname('FCD1').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger <= 6) And (myadoquery.fieldbyname('FCD2').asinteger >= 1) Then
strSbbhInfo := copy(strSbbh, myadoquery.fieldbyname('FKS1').asinteger, myadoquery.fieldbyname('FCD1').asinteger) + Trim(myadoquery.fieldbyname('FHY1').AsString) + copy(strSbbh, myadoquery.fieldbyname('FKS2').asinteger, myadoquery.fieldbyname('FCD2').asinteger) + Trim(myadoquery.fieldbyname('FHY2').asstring)
Else
Writedtmf( '公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!');
end
Else If myadoquery.fieldbyname('ffd').asinteger = 3 Then
begin
If (myadoquery.fieldbyname('FKS1').asinteger >= 1) And( myadoquery.fieldbyname('FKS1').asinteger <= 6 )And( myadoquery.fieldbyname('FCD1').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger >= 1) And (myadoquery.fieldbyname('FKS2').asinteger <= 6) And (myadoquery.fieldbyname('FCD2').asinteger >= 1) And (myadoquery.fieldbyname('FKS3').asinteger >= 1) And (myadoquery.fieldbyname('FKS3').asinteger <= 6) And (myadoquery.fieldbyname('FCD3').asinteger >= 1) Then
strSbbhInfo :=copy(strSbbh, myadoquery.fieldbyname('FKS1').asinteger, myadoquery.fieldbyname('FCD1').asinteger) + Trim(myadoquery.fieldbyname('FHY1').AsString) + copy(strSbbh, myadoquery.fieldbyname('FKS2').asinteger, myadoquery.fieldbyname('FCD2').asinteger) + trim(myadoquery.fieldbyname('FHY2').AsString) + copy(strSbbh, myadoquery.fieldbyname('FKS3').asinteger, myadoquery.fieldbyname('FCD3').asinteger) + Trim(myadoquery.fieldbyname('FHY3').AsString)
Else
Writedtmf( '公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!')
end
Else
begin
Writedtmf ('公司代码为:' + strKzqcj + '控制器型号为:'+ strKzqxh + '总线分段设置错误!请检查!');
end;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
End;
If (strtoint(strInfoType) < 8) Or (strtoint(strInfoType) >= 11) Then //'信息类型小于8,状态字代表设备代码类别,通过状态字在GSM表中检索设备代码含义
begin
//strSbbhType := '未知类型(数据库中未设置!)';
strSbbhType := ' ';
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gsm where fgsdm='''+strkzqcj+''' and fdm='''+ strstateword+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strsbbhtype:=myadoquery.fieldbyname('flx').AsString;
end;
except
exit;
end;

finally
myadoquery.Close;
myadoquery.Free;
end;
end;

If strInfoType = '8' Then// '信息类型等于8,状态字代表控制器状态,通过状态字在GKZ表中检索控制器状态
begin
//strKzqStateInfo := '未知状态(数据库中未设置!)';
strKzqStateInfo := ' ';
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gkz where fdm='''+strstateword+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strkzqstateinfo:=myadoquery.fieldbyname('flx').AsString;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
end;
If strInfoType = '9' Then// '信息类型等于9,状态字代表监控器状态,通过状态字在GJKB表中检索控制器状态
begin
//strJkqStateInfo := '未知状态(数据库中未设置!)';
strJkqStateInfo := ' ';
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from gJKB where fdm='''+strstateword+'''');
myadoquery.Open;
if myadoquery.RecordCount>0 then
begin
myadoquery.First;
strjkqstateinfo:=myadoquery.fieldbyname('flx').AsString;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
end;
myJQBH:=getCURjqbh(); //生成警情编号

if strinfotype='0' then //判断信息类型
//误报 '同时写入Gwb库,Gjjqt库
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType + '--'+ '误报';
SaveAlarmInfo( 'gwb', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs,formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
SendClient ('1@03@' +myJQBH) ;
end
else if strinfotype='1' then
//自动火警,无人
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType + '--'+ '自动火警(无人)';
SaveAlarmInfo ('gjj', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);


if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=1;
TimerPlaySnd.Enabled:=True;
end;

SendClient( '1@01@' + myJQBH);
end
else if strinfotype='2' then
//启动
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType +'--' + '启动';
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1 ;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
SendClient ('1@02@' + myJQBH);
end
else if strinfotype='3' then
//停动
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--' + strSbbhType + '--' + '停动';
SaveAlarmInfo('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

SendClient ('1@03@' + myJQBH );
end
else if strinfotype='4' then
//探测器故障
begin
strJqgs := strUserName + '--' +strSbbhInfo + '--' + strSbbhType + '--' + '探测器故障';
SaveAlarmInfo ('gjjgz', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=2;
TimerPlaySnd.Enabled:=True;
end;

SendClient ('1@04@' + myJQBH);
end
else if strinfotype='5' then
//隔离
begin
strJqgs := strUserName + '--' + strSbbhInfo + '--'+ strSbbhType +'--' + '隔离';
SaveAlarmInfo('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

SendClient ('1@05@'+ myJQBH);
end
else if strinfotype='6' then
//取消隔离
begin
strJqgs := strUserName + '--'+strSbbhInfo +'--'+ strSbbhType +'--'+'取消隔离';
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
SendClient ('1@06@'+ myJQBH);

end
else if strinfotype='7' then
//确认火警
begin
strJqgs := strUserName +'--'+ strSbbhInfo + '--'+strSbbhType +'--'+'确认火警';
SaveAlarmInfo( 'gjj', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=1;
TimerPlaySnd.Enabled:=True;
end;

SendClient ('1@07@' + myJQBH);
end
else if strinfotype='8' then
//控制器状态
begin
strJqgs := strUserName + '--' + '控制器状态' + '--' + strKzqStateInfo;
//是报故障
If (strStateWord = '02') Or (strStateWord = '04') Or (strStateWord = '0D') Or (strStateWord = '0E') Then
begin
SaveAlarmInfo( 'gjjgz', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=2;
TimerPlaySnd.Enabled:=True;
end;
SendClient ('1@08YN@' + myJQBH);
end
//是报故障恢复
Else If (strStateWord = '03') Or (strStateWord = '05') Then
begin
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );
SendClient ('1@08NY@' + myJQBH);
// datamodule1.ADOCommand1.CommandText:= ' update gjjgz set fcjsj=''' + GetCurTime() + ''',fcjczy=''自动'' where fbm=''' + strUserID +''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' insert into gjjgzcl select * from gjjgz where fbm=''' + strUserID + ''' and fztz=''' + '0'+inttostr(strtoint(strStateWord)-1) + '''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjgz where fbm=''' + strUserID + ''' and fztz=''' + '0'+inttostr(strtoint(strStateWord)-1) + '''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz=''' + '0'+inttostr(strtoint(strStateWord)-1) + '''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz=''' + strStateWord +'''';
// datamodule1.ADOCommand1.Execute;


end
//既不是报故障也不是报故障恢复
Else
begin
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs,formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SendClient ('1@08NN@' + myJQBH);
End;

g_CurNum := g_CurNum + 1 ;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);
end
else if strinfotype='9' then
//监控器状态
begin
strJqgs := strUserName + '--' + '监控器状态' + '--' + strJkqStateInfo;
//是报故障
If (strStateWord = '01') Or (strStateWord = '03') Or (strStateWord = '05') or(strstateword='08')Then
begin
SaveAlarmInfo( 'gjjgz', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh,strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=2;
TimerPlaySnd.Enabled:=True;
end;
SendClient ('1@09YN@' + myJQBH);
end
//是报故障恢复
Else If (strStateWord = '02') Or (strStateWord = '04') Or (strStateWord = '06') Then
begin
SaveAlarmInfo ('gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj );
SendClient ('1@09NY@' + myJQBH);
// datamodule1.ADOCommand1.CommandText:= ' update gjjgz set fcjsj=''' + GetCurTime() + ''',fcjczy=''自动'' where fbm=''' + strUserID +''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' insert into gjjgzcl select * from gjjgz where fbm=''' + strUserID + ''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjgz where fbm=''' + strUserID + ''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz='''+ '0'+inttostr(strtoint(strStateWord)-1) +'''';
// datamodule1.ADOCommand1.Execute;
// datamodule1.ADOCommand1.CommandText:= ' delete from gjjqt where fbm=''' + strUserID + ''' and fztz='''+ strStateWord +'''';
// datamodule1.ADOCommand1.Execute;


end
//火警,特殊处理
else if (strstateword='0B') then
begin
SaveAlarmInfo( 'gjj', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
if g_playSND=True then
if (playSound=False) and (GZboolean=True) then
begin
endSND();
PlaySound:=True;
SoundType:=1;
TimerPlaySnd.Enabled:=True;
end;
SendClient ('1@09NN@' + myJQBH);
end
//既不是报故障也不是报故障恢复
Else
begin
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);
SendClient ('1@09NN@' + myJQBH);
End;

g_CurNum := g_CurNum + 1 ;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum);

end
else if strinfotype='A' then
//控制器注册
begin
strJqgs := strUserName +'--'+'控制器注册';
SaveAlarmInfo( 'gjjqt', myJQBH, strUserID, strInfoType, strStateWord, strSbbh, strBjsj, strCheckNum, strJqgs, formatdatetime('yyyymmddhhmmss',Now), g_CZYXM, strKzqxh, strKzqcj);

g_CurNum := g_CurNum + 1;
If g_CurNum > g_MaxNum Then
XXQuery (g_MaxNum) ;
SendClient ('1@0A@'+ myJQBH);
end;
statusbar1.Panels[0].Text:='用户编码:'+struserid+'; 警情概述:'+strjqgs;
end;


procedure tfrmmain.SaveAlarmInfo(TableName : String; jqbh : String;yhbm : String;xxlx : String;ztz : String;sbbh : String; bjsj : String; jyh : String; jqgs : String;jjsj : String; jjczy : String;kzqxh : String;gsdm : String);//往数据库中添加记录,表名和字段名
var
myADOQuery:tadoquery;
begin
try
try
myadoquery:=tadoquery.Create(nil);
myadoquery.Close;
myadoquery.Connection:=datamodule1.ADOConnection;
myadoquery.SQL.Clear;
myadoquery.SQL.Add('select * from '+trim(tablename) + ' where fjqbh=''' + jqbh+'''' );
myadoquery.Open;
If myadoquery.RecordCount <= 0 Then
begin
myadoquery.Insert;
myadoquery.Edit;
myadoquery.FieldByName('fjqbh').AsString:=jqbh;//警情编号
myadoquery.FieldByName('fbm').AsString:=yhbm;//用户编码
myadoquery.FieldByName('fxxlx').AsString:=xxlx;//信息类型
myadoquery.FieldByName('fztz').AsString:=ztz;//状态字
myadoquery.FieldByName('fsbbh').AsString:=sbbh;//设备编号
myadoquery.FieldByName('fsj').AsString:=bjsj;//报警时间(mmddhhmm)
myadoquery.FieldByName('fjyh').AsString:=jyh;//校验和
myadoquery.FieldByName('fms').AsString:=jqgs;//警情概述
myadoquery.FieldByName('fjjsj').AsString:=jjsj;//接警时间(yyyymmddhhmmss)
myadoquery.FieldByName('fjjczy').AsString:=jjczy;//接警操作员
myadoquery.FieldByName('fkzq').AsString:=kzqxh;//控制器型号
myadoquery.FieldByName('fgsdm').AsString:=gsdm;//公司代码/
myadoquery.UpdateBatch;
end;
except
exit;
end;
finally
myadoquery.Close;
myadoquery.Free;
end;
end;

{获取警情编号}
function Tfrmmain.getCURjqbh():String;
var


SLen:integer;
tmpstr,resultStr:string;

begin
try
g_JQBH:=g_JQBH+1;
if g_jqbh=9999 then
g_jqbh:=0000;
tmpstr:=InttoStr(g_JQBH);
SLen:=Length(tmpstr);
Case SLen of
1:
resultStr:='000'+tmpstr;
2:
resultStr:='00'+tmpstr;
3:
resultStr:='0'+tmpstr;
4:
resultStr:=tmpstr;
end;
datamodule1.ADOQueryJQBH.Close;
datamodule1.ADOQueryJQBH.SQL.Clear;
datamodule1.ADOQueryJQBH.SQL.Add('select * from gcs');
datamodule1.ADOQueryJQBH.Open;
datamodule1.adoqueryjqbh.edit;
if datamodule1.ADOQueryJQBH.RecordCount=1 then
begin
datamodule1.ADOQueryJQBH.FieldByName('fjqbh').AsInteger:=g_jqbh;
datamodule1.ADOQueryJQBH.UpdateBatch;
end;
datamodule1.ADOQueryJQBH.Close;
getCURjqbh:=formatdatetime('yyyymmdd',now)+resultStr;
except
getcurjqbh:=formatdatetime('yyyymmdd',now)+inttostr(g_jqbh) ;

end;
end;

{DTMF数据写入文件中}
Procedure Tfrmmain.writeDTMF(recvDTMF:String);
var
filevar: textfile;
FileNameStr:string;
begin
FileNameStr:=ExtractFilePath(Application.ExeName)+'TXT/';
if Length(Trim(recvDTMF))>0 then
begin
FileNameStr:=FileNameStr+formatdatetime('yyyymmdd',now)+'.txt';
assignfile(filevar,FileNameStr);
if FileExists(FileNameStr) then
begin
append(filevar);
end
else
begin
rewrite(filevar); // 新建立
end;
writeln(filevar,recvDTMF+':'+getCURtime);
closefile(filevar);
end;
end;

{向客户端发送数据}
procedure Tfrmmain.sendCLIENT(sendSTRING:string);
var
k,i:integer;
WillSendString,WillSendStringClient:string;
kk:OleVariant;
begin
if (sendSTRING<>'') and (not(varisnull(sendSTRING))) and (not(varisempty(sendSTRING)))then
begin
WillSendString:=sendSTRING;
kk:=OleVariant(sendSTRING);
k:=1;
while k<=Clientquantity do
begin
if AllClient[k].SktHandle.State=7 then
begin
WillSendStringClient:=WillSendString+'##';
AllClient[k].SktHandle.SendData(WillSendStringClient);
writeDTMF('!!发送数据给客户端:'+inttostr(integer(AllClient[k].SktHandle))+':'+inttostr(AllClient[k].SktHandle.LocalPort)+'数据内容:'+ WillSendStringClient);
end;
k:=k+1;
end;
end;
end;
{将gjjqt表中的第Num之后的记录删除}
procedure Tfrmmain.xxQuery(Num:Longint);
var
seleJQBH:String;
TimeStr:String;
mydataset:tadodataset;
myadocommand:tadocommand;
begin
Try
/////////////
try
mydataset:=tadodataset.Create(nil);
mydataset.Close;
mydataset.Connection:=datamodule1.ADOConnection;
mydataset.CommandType:=cmdtext;
mydataset.CommandText:='select fjqbh,fms from gjjqt order by fjqbh asc';
mydataset.Open;
myadocommand:=tadocommand.Create(nil);
myadocommand.Connection:=datamodule1.ADOConnection;
myadocommand.CommandType:=cmdtext;
With mydataset do
begin
if mydataset.RecordCount>Num then
begin
mydataset.First;
mydataset.MoveBy(mydataset.RecordCount-Num+1);
seleJQBH:=Trim(Recordset.Fields['FJQBH'].value);
TimeStr:=formatdatetime('yyyymmddhhmmss',now);

myadocommand.CommandText:='update gjjqt set fcjsj ='+''''+TimeStr+''''+',fcjczy='+''''+'自动'+'''' +' where fjqbh< '+''''+seleJQBH+'''';
myadocommand.Execute;
myadocommand.CommandText:='insert into gcjqt select * from gjjqt where fjqbh<'+''''+seleJQBH+'''';
myadocommand.Execute;
myadocommand.CommandText:='delete from GJJqt where fJQBH <'+''''+seleJQBH+'''';
myadocommand.Execute;
end;
g_CurNum:=Num;
end;
except
exit;
end;
Finally
mydataset.Close;
mydataset.Free;
myadocommand.Free;
end;
end;


procedure TFrmMain.TimerPlaySndTimer(Sender: TObject);
begin
//播放声音
if PlayCount<CanPlayCount then //声音播放CanPlayCount遍
begin
case SoundType of
1: //播放报警声音
begin
GZboolean:=False;
playSND('报警.wav');
end;
2: //播放故障声音
begin
GZboolean:=False;
playSND('故障.wav');
end;
end;
inc(PlayCount);
end
else
begin
PlayCount:=0;
PlaySound:=False;
GZboolean:=true;
TimerPlaySnd.Enabled:=False;
end;
end;

{结束放音操作}
procedure Tfrmmain.endSND();
begin
try
MMControl1.FileName:='';
MMControl1.Notify:=False;
MMControl1.Wait:=False;
MMControl1.Command:='sound';
except
abort;
end;
end;

function tfrmmain.getCurTime():String;
begin
getCurTime:=MidStr(formatdatetime('yyyymmddhhmmss',now),9,6);
end;

{播放声音文件}
procedure tfrmmain.playSND(WaveFile:string);
var
WavFileName:string;
begin
WavFileName:=ExtractFilePath(Application.ExeName)+'WAV/';
MMControl1.FileName:=WavFileName+WaveFile;
MMControl1.Notify:=False;
MMControl1.Wait:=False;
MMControl1.Command:='sound';
end;


procedure TFrmMain.ClientsocketConnectionRequest(Sender: TObject;
requestID: Integer);
var
strIp,strHost,DateStr,TimeStr:string;
systemtime:TSystemTime;
DateTime:TDateTime;
BeepI:integer;
begin
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);
BeepI:=0;
strIp:=clientSocket.RemoteHostIP;
strHost:=clientSocket.RemoteHost;
//显示在LvwClient中,并需要连接数据库提取相关的信息
TmpI:=1;
while TmpI<StringGrid2.RowCount do
begin
if Trim(AllClient[TmpI].IP)=Trim(StrIp) then
begin
//已经存在了此客户端IP,置状态为 '连接正常'
Allclient[TmpI].SktHandle:=Twinsock.Create(self);

Allclient[TmpI].SktHandle.OnDataArrival:=MyClientDataArrival;
Allclient[TmpI].SktHandle.OnClose:=MyClientClose;
Allclient[TmpI].SktHandle.OnError:=MyClientError;
Allclient[TmpI].IP:=strIp;
Allclient[TmpI].status:=True;
Allclient[TmpI].SktHandle.accept(requestID);
if Allclient[TmpI].SktHandle.State=7 then
begin
StringGrid2.Cells[3,TmpI]:='联通';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
end;
break;
end;
TmpI:=TmpI+1;
end;
if true then
begin
if TmpI=StringGrid2.RowCount then //不存在此IP
begin
Allclient[TmpI].SktHandle:=Twinsock.Create(self);
Allclient[TmpI].SktHandle.OnDataArrival:=MyClientDataArrival;
Allclient[TmpI].SktHandle.OnClose:=MyClientClose;
Allclient[TmpI].SktHandle.OnError:=MyClientError;
Allclient[TmpI].IP:=strIp;
Allclient[TmpI].SktHandle.accept(requestID);
writeDTMF('!!客户端:'+Allclient[TmpI].IP+'对应socket索引:'+inttostr(Integer(Allclient[TmpI].SktHandle))+'成功登录服务器!');
if Allclient[TmpI].SktHandle.State=7 then
begin
StringGrid2.RowCount:=StringGrid2.RowCount+1;
StringGrid2.Cells[0,TmpI]:=strIp;
StringGrid2.Cells[3,TmpI]:='联通';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
Allclient[TmpI].status:=True;
Clientquantity:=Clientquantity+1;
end;
end;
end
else
begin
while BeepI<10 do
begin
beep;
BeepI:=BeepI+1;
end;
end;

end;

procedure TFrmMain.ClientsocketError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer; var CancelDisplay: WordBool);
var
strIp:string;
begin
TmpI:=1;
strIp:=Clientsocket.RemoteHostIP;
while TmpI<StringGrid2.RowCount-1 do
begin
if StringGrid2.Cells[1,TmpI]=StrIp then
begin
//已经存在了此客户端IP,置状态为 '连接正常'
StringGrid2.Cells[3,TmpI]:='断开';
Allclient[TmpI].SktHandle.Close ;//孟祥君2005-1-27更改,关闭错误服务器socket端
break;
end;
TmpI:=TmpI+1;
end;
end;

{响应客户端的数据到达事件}
procedure Tfrmmain.MyClientDataArrival(Sender:TObject;bytesTotal:integer);
var
TmpSendValue:SendValues;
TmpSendValBYTE:array[0..13] of Byte;
///////2005-3-1增加针对于启动监控器灭火
tmpValIP,index,StrLen:integer;
sock1:twinsock;
TmpHostIP,DateStr,TimeStr,TmpSendStr,sTR1, Str4:string;
systemtime:TSystemTime;
DateTime:TDateTime;
// Info:widestring;
StrData:Olevariant;
n1:integer;

xxSTR,QTSTR:String; //信息台发送的信息标识,进行识别是否为“管理信息”记录
cFirst:String;




begin
try
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);
tmpValIP:=1;

sock1:=Twinsock(Sender);
TmpHostIP:=sock1.RemoteHostIP;



//通过IP取得索引值,比较所有的已连接的IP值
while tmpValIP<=Clientquantity do
begin
if AllClient[tmpValIP].IP=TmpHostIP then
begin
index:=TmpValIP;
break;
end;
tmpValIP:=tmpValIP+1;
end;
sock1.GetData(StrData,8);
StrLen:=length(StrData);
//对Info字符串进行操作的编程
cFirst:=midStr(StrData,1,1);
//显示的信息
StringGrid2.Cells[8,index]:=DateStr;
stringGrid2.Cells[9,index]:=TimeStr;


////////////////////////////////////
if cFirst='0' then //登录
begin
StringGrid2.cells[2,index]:=MidStr(StrData,3,StrLen-2);
StringGrid2.cells[3,index]:='联通';
StringGrid2.cells[6,index]:=DateStr;
StringGrid2.cells[7,index]:=TimeStr;
StringGrid2.cells[8,index]:=DateStr;
StringGrid2.cells[9,index]:=TimeStr;
TmpSendStr:='4@'+getLJRQ;
sendCLIENT(TmpSendStr);
end;
if cFirst='1' then //工作站类型、是否显示地图、是否有打印机
StringGrid2.cells[4,index]:=MidStr(StrData,3,StrLen-2);
if cFirst='2' then //工作站标识
begin
StringGrid2.cells[1,index]:=MidStr(StrData,3,StrLen-2);
TmpSendStr:='4@'+getLJRQ;
sendCLIENT(TmpSendStr);
end;
if cFirst='5' then //监控仪处警
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJ set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into Gcj select * from GJJ where fJQBH='''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJ where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='7' then //监控仪误报
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJ set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into GWB select * from GJJ where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJ where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='9' then //开始巡检
//startXJ;
if cFirst='B' then //结束巡检
//endXJ;
if cFirst='C' then //监控仪上报故障处理
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJgz set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='insert into Gjjgzcl select * from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D02@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='D' then //监控仪上报故障误报
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJgz set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into GWB select * from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJgz where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
if cFirst='G' then // 监控器其它故障信息
begin
xxSTR:=Midstr(StrData, 2, 1);
If xxSTR='J' Then //监控器的管理信息
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJqt set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into Gcjqt select * from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
end;
if cFirst='H' then //
begin
QTSTR:=MidStr(StrData, 2, 1);
If QTSTR='J' Then //监控器的管理信息
begin
With datamodule1.ADOCommandClient do
begin

CommandText:='update GJJqt set fcjczy='+''''+g_CZYXM+''''+',fcjsj='+''''+formatdatetime('yyyymmddhhmmss',now)+''''+'where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='insert into Gwb select * from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';

Execute;
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr); //刷新信息台
end
else
begin
With datamodule1.ADOCommandClient do
begin
CommandText:='delete from GJJqt where fJQBH='+''''+Midstr(StrData, 3, 12)+'''';
Execute;
end;
TmpSendStr:='1@D00@'+Midstr(StrData, 3, 12);
sendCLIENT(TmpSendStr);
end;
end;

if cFirst='N' then //信息台中允许显示的最大记录数
begin
xxNum:=StrtoInt(MidStr(StrData,3,StrLen-2));
If g_CurNum>xxNum Then //当前记录数
xxQuery(xxNum);
end;
if cFirst='T' then //经确认的火警,将发送到119指挥中心
begin
n1:=pos('@',StrData);
Str4:=copy(StrData,n1+1,length(StrData)-n1);
//n2:=pos('@',StrData);
Str1:=MidStr(StrData,1,n1-1);
end;
if cFirst='R' then //客户机向服务器发送更改系统用户/总线/设备代码等信息后重新加载数据
begin
RELOAD(Midstr(StrData, 3, 3));
end;
except
end;


end;

procedure Tfrmmain.MyClientClose(Sender:TObject);
var
strIp,DateStr,TimeStr:string;
systemtime:TSystemTime;
DateTime:TDateTime;
sock1:twinsock;
j:integer;
begin
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);

TmpI:=1;
j:=0;
sock1:=Twinsock(Sender);
strIp:=sock1.RemoteHostIP;
while TmpI<StringGrid2.RowCount do
begin
if Trim(StringGrid2.Cells[0,TmpI])=Trim(StrIp) then
begin
//已经存在了此客户端IP,置状态为 '断开'
StringGrid2.Cells[3,TmpI]:='断开';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
while j<stringgrid2.RowCount do
begin
if StringGrid2.Cells[0,TmpI]=strIp then
begin
Allclient[j].status:=False;
break;
end;
j:=j+1;
end;
break;
end;
TmpI:=TmpI+1;
end;
end;

procedure Tfrmmain.MyClientError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer;
var CancelDisplay: WordBool);
var
strIp,DateStr,TimeStr:string;
systemtime:TSystemTime;
DateTime:TDateTime;
sock1:twinsock;
j:integer;
begin
GetLocalTime(SystemTime); //读取北京东8区时间
DateTime:=SystemTimeToDateTime(SystemTime);
DateStr:=DateToStr(DateTime);
TimeStr:=Timetostr(DateTime);

TmpI:=1;
j:=0;
sock1:=Twinsock(Sender);
strIp:=sock1.RemoteHostIP;
while TmpI<StringGrid2.RowCount do
begin
if Trim(StringGrid2.Cells[0,TmpI])=Trim(StrIp) then
begin
//已经存在了此客户端IP,置状态为 '断开'
StringGrid2.Cells[3,TmpI]:='断开';
StringGrid2.Cells[8,TmpI]:=DateStr;
StringGrid2.Cells[9,TmpI]:=TimeStr;
while j<stringgrid2.RowCount do
begin
if StringGrid2.Cells[0,TmpI]=strIp then
begin
Allclient[j].status:=False;
break;
end;
j:=j+1;
end;
break;
end;
TmpI:=TmpI+1;
end;
end;

//取得系统时间的日期
function Tfrmmain.getLJRQ():string;
begin


getLJRQ:=formatdatetime('yyyymmdd',now);

end;

{系统设置发生变化后要求重新装入用户/总线/各公司代码/监控仪警情说明等信息 }
procedure tfrmmain.RELOAD(TableName:String);
begin
StatusBar1.Panels[0].Text:='系统配置发生变化,请重启服务器程序!';
playSND('故障.wav');
end;

procedure TFrmMain.TDMTimer(Sender: TObject);
var
i:integer;
sendbuf:array[0..2] of BYTE;
begin
sendbuf[0]:=13;
sendbuf[1]:=13;
sendbuf[2]:=13;

for i:=0 to g_linkno-1 do
begin
if mysocknod.MyClientSock.Active then
mysocknod.MyClientSock.Socket.SendBuf(SENDBUF,3);
end;
end;

procedure TFrmMain.BitBtn2Click(Sender: TObject);
var
i:integer;
SendFY:array[0..3] of byte;
begin
listbox1.Items.Clear;
G_OnOff_JY:=false;
sendfy[0]:=9;
sendfy[1]:=14;
sendfy[2]:=1;
sendfy[3]:=15;


for i :=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
mysocknod.MyClientSock.Socket.SendBuf(sendfy,4);
end;
end;

end;

procedure TFrmMain.BitBtn1Click(Sender: TObject);
var
i:integer;
SendJY:array[0..7] of byte;
begin
listbox1.Items.Clear;
G_OnOff_JY:=TRUE;
sendjy[0]:=9;
sendjy[1]:=14;
sendjy[2]:=0;
sendjy[3]:=14;

for i :=0 to g_linkno-1 do
begin
if mysocknod.SockFlag then
begin
mysocknod.MyClientSock.Socket.SendBuf(sendjy,4);
end;
end;

end;

end.
 
好长啊
string的内存泄露
 
真长,谁有时间看啊
 
难道大富翁就没有高手了么?来帮我看看啊
 
多人接受答案了。
 
后退
顶部