Project

General

Profile

Bug #1598 » zfCascoCctv.cpp

yufeng wu, 08/31/2021 08:53 AM

 
1
// zfCascoCctv.cpp: implementation of the CCascoCctv class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
#include "zfCascoCctv.h"
5

    
6
#ifdef _DEBUG
7
#undef THIS_FILE
8
static char THIS_FILE[]=__FILE__;
9
#define new DEBUG_NEW
10
#endif
11

    
12
//////////////////////////////////////////////////////////////////////
13
// Construction/Destruction
14
//////////////////////////////////////////////////////////////////////
15
CCascoCctv::CCascoCctv()
16
{
17

    
18
}
19

    
20
CCascoCctv::~CCascoCctv()
21
{
22

    
23
}
24

    
25
sint32 CCascoCctv::TxProc()
26
{
27
	return 1;
28
}
29
//2bytes,11:????ǽ;12:????;21:ѡ??;22:?ָ??л?;23:ѡ????;24:?л???ƵԴ;31:??̨;41:¼??;51:ץ??
30
//1			2			3	  4	  5   6   7     8		9		10			11	12
31
//??ɫֵ ????????LoHi ????ǽ ???? ?? ?ָ? ?? ͨ????ƵԴ ??̨ ??̨????LoHi ¼?? ¼?????
32
int CCascoCctv::fPTZConv(uint8 ptz)//Modified by WYF 20191128
33
{
34
	int	tPtz=0;
35
	tPtz=ptz;
36
	switch(tPtz){
37
/*	case 0://ͣ
38
	case 1://????????
39
	case 2:
40
	case 3:
41
	case 4:
42
		tPtz+=10;
43
		break;
44
	case 11://???ϣ????£????ϣ?????
45
	case 12:
46
	case 13:
47
	case 14:
48
		tPtz+=4;
49
		break;
50
	case 5://?䱶??С????Ȧ??С
51
	case 6:
52
	case 9:
53
	case 10:
54
		tPtz+=15;
55
		break;
56
	case 8://????
57
		tPtz+=14;
58
		break;
59
	case 7://Զ??
60
		tPtz+=16;
61
		break;
62
*/
63
	case 10://ͣ
64
	case 11://????????
65
	case 12:
66
	case 13:
67
	case 14:
68
		tPtz-=10;
69
		break;
70
	case 15://???ϣ????£????ϣ?????
71
	case 16:
72
	case 17:
73
	case 18:
74
		tPtz-=4;
75
		break;
76
	case 20://?䱶??С????Ȧ??С
77
	case 21:
78
	case 24:
79
	case 25:
80
		tPtz-=15;
81
		break;
82
	case 22://????
83
		tPtz-=14;
84
		break;
85
	case 23://Զ??
86
		tPtz-=16;
87
		break;
88
	}	
89
	return tPtz;
90
}
91
sint32 CCascoCctv::RxProc()
92
{
93
	//sint32 rtuno			=	pLink->GetRtuNo();
94
	//unsigned char rtuaddr	=	pRtu->GetRtuAddr(rtuno);
95

    
96
	uint8  buf[512] = {0};
97
	int datalen = 0;
98
	int buflen=pRxBuf->GetReadableSize();
99

    
100
	int nowtime = GetNowSecond();
101
	if ((nowtime-m_LastSendTime) >= 5/*m_RcvTimeOuts*/)		//ÿ֡???ͺ󷵻?(??Ӧ)??ʱ   ??-??
102
	{
103
		pLink->RegisterFrm(FRAME_RX_TIMEOUT);
104
		m_LastSendTime = nowtime;
105
		PrintLog(LOG_WARNING,"3??4 ??·???ݿ??г?ʱ??\n");
106
	}
107
/*	if (m_bCmd && (nowtime-m_LastCmdSendTime >= m_cmdTimeOut))	//?·??????û?л?ȡ????Ӧ?????ʱ
108
	{
109
	}*/
110

    
111
	if (buflen<7)		//?????ݻ????ݲ??????ı???·ͨѶ״̬???????ȴ?
112
		return 1;
113
	datalen = pRxBuf->Read(buf,buflen,DEF_BUFF_NOMOVE);
114
	uint16 d_len=buf[4]*256 + buf[5] + 6;
115
	if(buflen<d_len)	//buf[0] != cnt_sequence/256 || buf[1] != cnt_sequence%256 || buf[6]!= rtuaddr || 
116
	{
117
		//pRxBuf->Move(buflen);
118
		//pLink->RegisterFrm(FRAME_RX_CHECKERR);
119
		PrintLog(LOG_WARNING,"3??4 ModbusHeadУ?????,???ݳ??Ȳ?????buflen=%d,d_len=%d\n",buflen,d_len);
120
		return -1;
121
	}
122
	datalen = pRxBuf->Read(buf,d_len,DEF_BUFF_MOVE);
123
	if (buf[7] == 0x03)
124
	{
125
		if (m_bCmd)
126
		{
127
			pLink->RegisterFrm(FRAME_RX_SUC);
128
			pLink->SetCommStatus(CMST_NORMAL);
129
		/*	m_decodeRlt = -1;	//??Ҫд??ȡ?·?????ؽ???Ÿ?m_lastSequence?ȸ?ֵ
130
			if (m_decodeRlt == -1)
131
			{
132
				m_lastDecodeRlt = 0xffff;
133
			}
134
			else
135
			{
136
				m_lastDecodeRlt = m_decodeRlt;
137
			}*/
138
			m_lastDecodeRlt = m_ReplyDecode;
139
			m_lastSequence = m_sequence;
140
			WriteToRegisterAi(buf);
141
			m_bCmd = FALSE;
142
			PrintLog(LOG_INFORMATION,	"3??4 ֡??%d,??¼?????affair No=%d,decode=%d,?·?????ɹ?\n",buf[0]*256+buf[1], m_lastSequence, m_lastDecodeRlt);
143
		}
144
		else
145
		{
146
			pLink->RegisterFrm(FRAME_RX_SUC);
147
			pLink->SetCommStatus(CMST_NORMAL);
148
			WriteToRegisterAi(buf);
149
		}
150
	}
151
	else if (buf[7] == 0x10)
152
	{
153
		S_RAWCMD rawcmd;
154
		CSI_D_CCTV_CMD cctvCtrl;
155
		memset(&rawcmd, 0, sizeof(rawcmd));
156
		memset(&cctvCtrl, 0, sizeof(cctvCtrl));
157
		uint8 staNum = StationNumConvert(buf[14]);//(buf[13]);
158
		if (staNum)
159
		{	if(buf[20]>=11 && buf[20]<=25 )//PTZ 8???򡢱䱶???۽?????Ȧ
160
				cctvCtrl.Func = 2;
161
			else
162
				cctvCtrl.Func = CCTV_FUNC_MON;
163
			cctvCtrl.Sta = 67-buf[14];//buf[13];//1byte-->2bytes
164
			cctvCtrl.decode = buf[16];//m_FepDecode; //16//buf[14];//1byte-->2bytes
165
			cctvCtrl.Cam = buf[17]*256+buf[18];//buf[15]*256+buf[16];
166
			cctvCtrl.Mon = m_FepDecode;
167
			cctvCtrl.User = 0xFF;
168
			cctvCtrl.Val = buf[22]*0x100+fPTZConv(buf[20]);//speed-ptz
169
			if(buf[23]*256+buf[24]==1)cctvCtrl.Task = cctvCtrl.Task | 0x81;
170
			else cctvCtrl.Task=0;//????--????
171

    
172
			rawcmd.type = DC_K_CMD_CCTVCTRL;
173
			memcpy(rawcmd.data, &cctvCtrl, sizeof(cctvCtrl));
174
			pCmdMem->PutACmd(m_rtuNo, rawcmd);
175
//			PrintLog(LOG_INFORMATION,	"3??4 ֡??%d,Sta=%d,Cam=%d,decode=%d,?·?????ɹ?\n",buf[0]*256+buf[1], buf[13],  buf[15]*256+buf[16], buf[14]);
176
			if(cctvCtrl.Func==1)
177
			PrintLog(LOG_INFORMATION,	"3??4??????ͷ ֡??%d,Sta=%d,Cam=%d,decode=%d,ptz=%d,speed=%d,push=%d?·?????ɹ?\n",buf[0]*256+buf[1], buf[13]*256+buf[14],  buf[17]*256+buf[18], buf[16], buf[19]*256+buf[20], buf[21]*256+buf[22], buf[23]*256+buf[24]);
178
			if(cctvCtrl.Func==2)
179
			PrintLog(LOG_INFORMATION,	"3??4??PTZ ֡??%d,Sta=%d,Cam=%d,decode=%d,ptz=%d,speed=%d,push=%d?·?????ɹ?\n",buf[0]*256+buf[1], buf[13]*256+buf[14],  buf[17]*256+buf[18], buf[16], buf[19]*256+buf[20], buf[21]*256+buf[22], buf[23]*256+buf[24]);
180
			m_bCmd = TRUE;
181
			m_sequence = buf[0]*256 + buf[1];
182
			m_ReplyDecode = buf[14];
183
		}
184
		else
185
		{
186
//			PrintLog(LOG_ERROR,	"3??4 ֡??%d,Sta=%d,Cam=%d,decode=%d,?·?????ʧ?ܣ?վ?Ų?ƥ??\n",buf[0]*256+buf[1], buf[13],  buf[15]*256+buf[16], buf[14]);
187
			PrintLog(LOG_ERROR,	"3??4 ֡??%d,Sta=%d,Cam=%d,decode=%d,?·?????ʧ?ܣ?վ?Ų?ƥ??\n",buf[0]*256+buf[1], buf[13]*256+buf[14],  buf[17]*256+buf[18], buf[16]);
188
		}
189
		pLink->RegisterFrm(FRAME_RX_SUC);
190
		pLink->SetCommStatus(CMST_NORMAL);
191
		//????
192
		WriteToRegister(buf);
193
	}
194
	return 1;
195
}
196

    
197
void CCascoCctv::WriteToRegister(uint8 *buf)
198
{
199
	uint8 txbuff[256] = {0};
200
	uint8 len=0;
201
	//7bytes Head
202
	txbuff[len++]=buf[0];//H Transaction Identifier
203
	txbuff[len++]=buf[1];//L
204
	txbuff[len++]=0;//Protocol Identifier "0 = MODBUS protocol"
205
	txbuff[len++]=0;
206
	txbuff[len++]=0;//len_H
207
	txbuff[len++]=6;//len_L
208
	txbuff[len++]=buf[6];//Unit Identifier
209
	txbuff[len++]=buf[7];		//???????? 
210
	txbuff[len++]=buf[8];//?Ĵ?????ַ??
211
	txbuff[len++]=buf[9];//??
212
	txbuff[len++]=buf[10];//?Ĵ?????????
213
	txbuff[len++]=buf[11];//??
214
	pLink->RegisterFrm(FRAME_TX_SUC);
215
	pTxBuf->Write(txbuff,len);
216
	pLink->SetCommStatus(CMST_RX_CNT);
217
	m_LastSendTime = GetNowSecond();
218
	PrintLog(LOG_INFORMATION,"3??4 ?ظ?0x10????ɹ?");
219
	
220
}
221

    
222
void CCascoCctv::WriteToRegisterAi(uint8 *buf)
223
{
224
	uint8 txbuff[256] = {0};
225
	uint8 len=0;
226
	//7bytes Head
227
	txbuff[len++]=buf[0];//H Transaction Identifier
228
	txbuff[len++]=buf[1];//L
229
	txbuff[len++]=0;//Protocol Identifier "0 = MODBUS protocol"
230
	txbuff[len++]=0;
231
	txbuff[len++]=0;//len_H
232
	txbuff[len++]=7;//len_L
233
	txbuff[len++]=buf[6];//Unit Identifier
234
	txbuff[len++]=buf[7];		//???????? 03
235
	txbuff[len++]=4;//??Ӧ???ֽ??? 2?Ĵ???*2
236
	txbuff[len++]=m_lastSequence/0x100;//affair No ??
237
	txbuff[len++]=m_lastSequence%0x100;//??
238
	txbuff[len++]=m_lastDecodeRlt/0x100;//??
239
	txbuff[len++]=m_lastDecodeRlt%0x100;//??
240
	pLink->RegisterFrm(FRAME_TX_SUC);
241
	pTxBuf->Write(txbuff,len);
242
	pLink->SetCommStatus(CMST_RX_CNT);
243
	m_LastSendTime = GetNowSecond();
244
	PrintLog(LOG_INFORMATION,"3??4 ?ظ?0x03????ɹ???affair No=%d,decode=%d",m_lastSequence,m_lastDecodeRlt);
245
}
246

    
247
void CCascoCctv::Init(S_PROTOCOLCFG *pcfg)
248
{
249
	PRawCtrl	=	pcfg->PRawCtrl;
250
	pRxBuf		=	pcfg->pRxBuf;
251
	pTxBuf		=	pcfg->pTxBuf;
252
	pCmdMem		=	pcfg->pCmdMem;
253
	pRtu		=	pcfg->pRtu;
254
	pLink		=	pcfg->pLink;
255
	pTable		=	pcfg->pTable;
256
	pZfDataCtrl	=	pcfg->pZfDataCtrl;
257
//	pHis		=	pcfg->pHis;
258

    
259
	pRxBuf->Move(pRxBuf->GetReadableSize());
260
	pTxBuf->Move(pTxBuf->GetReadableSize());
261
	m_RcvTimeOuts = pLink->GetRxTimeouts();
262
	//m_bCmdReply = FALSE;
263
	m_bCmd = FALSE;
264
	m_rtuNo = 0;
265
	m_FepDecode = 0;
266
	//m_cascoDecode = 0;
267
	m_cmdTimeOut = 0;
268
	m_LastSendTime = 0;
269
	m_sequence = 0;
270
	m_lastSequence = 0;
271
	m_decodeRlt = 0;
272
	m_lastDecodeRlt = 0xffff;
273
	m_LastCmdSendTime = 0;
274
	m_ReplyDecode = 0;
275

    
276
	if(m_RcvTimeOuts <= 0)
277
		m_RcvTimeOuts = 10;
278
	ReadIni();
279
}
280

    
281
void CCascoCctv::ReadIni()
282
{
283
	char filename[512];
284
	CReadConf readconf;
285
	char *pWorkPath;	
286
	pWorkPath = getenv(SE_EMS_RUN_DIR);
287
	if(pWorkPath!=NULL) 
288
		sprintf(filename,"%s%s",pWorkPath,"/cfg/zfCctvCasco.ini");
289
	m_rtuNo = readconf.ReadLong("FepAndCascoAndMatrixMsg", "CmdRtuNo", filename, "0");
290
	m_FepDecode = readconf.ReadLong("FepAndCascoAndMatrixMsg", "FepDecode", filename, "0");
291
	//m_cascoDecode = readconf.ReadLong("FepAndCascoAndMatrixMsg", "CascoDecode", filename, "0");
292
	m_cmdTimeOut = readconf.ReadLong("FepAndCascoAndMatrixMsg", "CctvSendCmdTimeOut", filename, "0");
293
}
294

    
295
int CCascoCctv::GetNowSecond()
296
{
297
	CSeTime     SE_T;
298
	TCriterionTime tmptime;
299
	SE_T.GetNow(&tmptime);
300
	return (int)tmptime;
301
}
302

    
303
void CCascoCctv::SetExtendedPara(uint16 no, sint32 val){}
304

    
305
uint8 CCascoCctv::StationNumConvert(uint8 staNum)
306
{
307
	int ret = 0;
308
	if(30<=staNum && staNum<=46)ret = 1; 
309
	return ret;
310
}
311

    
312
uint8 CCascoCctv::DecoderPortConvert(uint8 decodeNum)
313
{
314
	return 0;
315
}
316

    
317
//??Լ???󴴽?????
318
#ifdef __unix
319
extern "C" CProtocol* CreateProtocol(char *defpara)
320
#else
321
extern "C" __declspec(dllexport)  CProtocol* CreateProtocol(char *defpara)
322
#endif
323
{
324
	CProtocol *pEpv = new CCascoCctv;
325
	return pEpv;
326
} 
327

    
(1-1/2)