Project

General

Profile

Feature #2758 » ZJ_ACS.cpp

yan hong, 07/20/2023 05:05 PM

 
1
#pragma once
2
#include "ZJ_ACS.h"
3
#include "ZJ_ACSclient.h"
4
#include "ZJ_ACSserver.h"
5
#include "iostream"
6
#include "fstream"
7

    
8
#ifdef WIN32
9
#include <direct.h>
10
#include <io.h>
11
#else
12
#include <sys/stat.h>
13
#include <sys/io.h>
14
#include <stdio.h>
15
#include <unistd.h>
16
#include <sys/types.h>
17
#endif
18

    
19
#ifndef WIN32
20
#include <iconv.h>
21
#endif
22

    
23
#define far
24
typedef void far  *LPVOID;
25

    
26
unsigned long long GetTimeStamp()
27
{
28
	unsigned long long stampNow = 0;
29
	time_t timer;
30
	timer = time(NULL);
31
	stampNow = timer;
32

    
33
	return stampNow;
34
}
35

    
36
void Inver2byte(uint8* Src, int num)
37
{
38
	int t = 0;
39
	for (int i = 0; i < num / 2; i++)
40
	{
41
		t = Src[2 * i];
42
		Src[2 * i] = Src[2 * i + 1];
43
		Src[2 * i + 1] = t;
44
	}
45
}
46

    
47

    
48
char *GB2312ToUtf8(const char *chrGbk, int rtuno)
49
{
50
#ifdef WIN32
51
	int len = MultiByteToWideChar(CP_ACP, 0, chrGbk, -1, NULL, 0);
52
	wchar_t* wstr = new wchar_t[len + 1];
53
	memset(wstr, 0, len + 1);
54
	MultiByteToWideChar(CP_ACP, 0, chrGbk, -1, wstr, len);
55
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
56
	char* str = new char[len + 1];
57
	memset(str, 0, len + 1);
58
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
59
	if (wstr) delete[] wstr;
60
#else
61
	size_t wlen = 0;
62
	size_t allLen = 0;
63
	char *inbuf = (char*)chrGbk;
64
	size_t inlen = strlen(inbuf);
65
	size_t outlen = allLen = inlen * 4;
66
	char *unicode = new char[outlen];
67
	char* outbuf = (char*)unicode;
68
	char* from_charset = "gb2312";
69
	char* to_charset = "utf-8";
70
	iconv_t cd;
71
	char** pin = &inbuf;
72
	char** pout = &outbuf;
73
	cd = iconv_open(to_charset, from_charset);
74
	if (cd == 0)
75
		kprintf(10, rtuno, 4, "rds change charset iconv false!");
76
	memset(outbuf, 0, outlen);
77
	if (iconv(cd, pin, &inlen, pout, &outlen) == -1){
78
		kprintf(10, rtuno, 4, "rds change charset iconv false!inlen=%d,outlen=%d,Msg=%s\n", inlen, (int)outlen, inbuf);
79
	}
80
	wlen = allLen - outlen + 1;
81
	//Inver2byte((uint8*)(unicode), 2 * wlen);
82
	char * str = unicode;
83
	iconv_close(cd);
84
#endif
85
	return str;
86
}
87

    
88
char *Utf8ToGB2312(const char *chrUtf8, int rtuno)
89
{
90
#ifdef WIN32
91
	int len = MultiByteToWideChar(CP_UTF8, 0, chrUtf8, -1, NULL, 0);
92
	wchar_t* wstr = new wchar_t[len + 1];
93
	memset(wstr, 0, len + 1);
94
	MultiByteToWideChar(CP_UTF8, 0, chrUtf8, -1, wstr, len);
95
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
96
	char* str = new char[len + 1];
97
	memset(str, 0, len + 1);
98
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
99
	if (wstr) delete[] wstr;
100
#else
101
	size_t wlen = 0;
102
	size_t allLen = 0;
103
	char *inbuf = (char*)chrUtf8;
104
	size_t inlen = strlen(inbuf);
105
	size_t outlen = allLen = inlen * 4;
106
	char *unicode = new char[outlen];
107
	char* outbuf = (char*)unicode;
108
	char* from_charset = "utf-8";
109
	char* to_charset = "gb2312";
110
	iconv_t cd;
111
	char** pin = &inbuf;
112
	char** pout = &outbuf;
113
	cd = iconv_open(to_charset, from_charset);
114
	if (cd == 0)
115
		kprintf(10, rtuno, 4, "rds change charset iconv false!");
116
	memset(outbuf, 0, outlen);
117
	if (iconv(cd, pin, &inlen, pout, &outlen) == -1){
118
		kprintf(10, rtuno, 4, "rds change charset iconv false!inlen=%d,outlen=%d,Msg=%s\n", inlen, (int)outlen, inbuf);
119
	}
120
	wlen = allLen - outlen + 1;
121
	//Inver2byte((uint8*)(unicode + 1), 2 * wlen);
122
	char * str = unicode;
123
	iconv_close(cd);
124
#endif
125
	return str;
126
}
127

    
128
bool is_str_utf8(const char* str)
129
{
130
	unsigned int nBytes = 0;//UFT8????1-6???ֽڱ???,ASCII??һ???ֽ?  
131
	unsigned char chr = *str;
132
	bool bAllAscii = true;
133

    
134
	for (unsigned int i = 0; str[i] != '\0'; ++i){
135
		chr = *(str + i);
136
		//?ж??Ƿ?ASCII????,???????,˵???п?????UTF8,ASCII??7λ????,???λ???Ϊ0,0xxxxxxx 
137
		if (nBytes == 0 && (chr & 0x80) != 0){
138
			bAllAscii = false;
139
		}
140

    
141
		if (nBytes == 0) {
142
			//???????ASCII??,Ӧ???Ƕ??ֽڷ?,?????ֽ???  
143
			if (chr >= 0x80) {
144

    
145
				if (chr >= 0xFC && chr <= 0xFD){
146
					nBytes = 6;
147
				}
148
				else if (chr >= 0xF8){
149
					nBytes = 5;
150
				}
151
				else if (chr >= 0xF0){
152
					nBytes = 4;
153
				}
154
				else if (chr >= 0xE0){
155
					nBytes = 3;
156
				}
157
				else if (chr >= 0xC0){
158
					nBytes = 2;
159
				}
160
				else{
161
					return false;
162
				}
163

    
164
				nBytes--;
165
			}
166
		}
167
		else{
168
			//???ֽڷ??ķ????ֽ?,ӦΪ 10xxxxxx 
169
			if ((chr & 0xC0) != 0x80){
170
				return false;
171
			}
172
			//????Ϊ??Ϊֹ
173
			nBytes--;
174
		}
175
	}
176

    
177
	//Υ??UTF8??????? 
178
	if (nBytes != 0)  {
179
		return false;
180
	}
181

    
182
	if (bAllAscii){ //???ȫ??????ASCII, Ҳ??UTF8
183
		return true;
184
	}
185

    
186
	return true;
187
}
188

    
189

    
190
unsigned int seprocess(LPVOID septr) {
191
	CZJACS* rtxpPara = NULL;
192
	rtxpPara = (CZJACS*)septr;
193

    
194
	rtxpPara->strCallback.iotprotocols = rtxpPara;
195
	rtxpPara->strCallback.rtuno = rtxpPara->iotpLink->GetRtuNo();
196
	rtxpPara->restfulServer();
197

    
198
}
199

    
200
unsigned int rxprocess(LPVOID rxptr)
201
{
202
	CZJACS* rtxpPara = NULL;
203

    
204
	rtxpPara = (CZJACS*)rxptr;
205
	sint32 rtuno = rtxpPara->iotpLink->GetRtuNo();
206
	while (rtxpPara->rxflag)
207
	{
208
		if (rtxpPara->subscribeflag == true || rtxpPara->subscribeflag2 == true)
209
		{
210
			if (rtxpPara->subscribeflag == true)
211
			{
212
				rtxpPara->PostSubscribe(1024);
213
			}
214
			if (rtxpPara->subscribeflag2 == true)
215
			{
216
				rtxpPara->PostSubscribe(255);
217
			}
218
		}
219
		
220
		SeSleep(500);
221
	}
222

    
223
	return 0;
224
}
225

    
226
unsigned int txprocess(LPVOID txptr)
227
{
228
	CZJACS* rtxpPara = NULL;
229

    
230
	rtxpPara = (CZJACS*)txptr;
231
	sint32 rtuno = rtxpPara->iotpLink->GetRtuNo();
232

    
233
	while (rtxpPara->txflag)
234
	{
235
		S_RAWCMD rawcmd;
236

    
237
		while (rtxpPara->iotpCmdMem->GetCmdNum(rtuno) > 0)
238
		{
239
			if (rtxpPara->iotpCmdMem->GetACmd(rtuno, &rawcmd) == 0)
240
				break;
241

    
242
			rtxpPara->PrintLog(LOG_INFORMATION, "?յ?%d???ն˿???????", rtuno);
243
		
244
			uint8 len = 0;
245
			ST_TRACK_ADDR 	m_cmdaddr;     //?????ⲿ???ͨ???˱????????ⲿ?????ַ??Ϣ
246
			m_cmdaddr = rawcmd.src;
247
			switch (rawcmd.type)
248
			{
249
				case DC_K_CMD_DOOPER: //ң??	
250
				{
251
					S_DO_CTL *cmdinfo;
252
					cmdinfo = (S_DO_CTL *)(&rawcmd.data);
253
					uint16 ykno = (uint16)cmdinfo->ptAddr;//ң?غ?
254
					int i;
255
					switch (cmdinfo->funcCode)
256
					{
257
						case DC_K_CTL_DOSEL://ѡ??
258
							//ֱ?ӷ?У
259
							rawcmd.type = DC_K_CMDRET_DOOPER;
260
							rawcmd.len = sizeof(cmdinfo);
261
							rawcmd.src = m_cmdaddr;
262
							cmdinfo->retCode = 1;							
263
							rtxpPara->iotpCmdMem->RptAResult(rtuno, rawcmd);
264
							break;
265

    
266
						case DC_K_CTL_DOEXE://ִ??
267
						{
268
							uint16 ykvalue;
269
							ykvalue = cmdinfo->ctlVal;
270

    
271
							if (ykno == 1 && ykvalue == 1) //Զ?̿???
272
							{
273
								rtxpPara->RemoteOpened();
274
							}
275
							
276
						}
277
							break;
278
						case DC_K_CTL_DODEL://????
279
							break;
280
					}
281
				}
282
				break;
283

    
284
				case DC_K_CMD_AOOPER:			
285
					break;
286

    
287
				default: //?????????·????????		
288
					break;
289
			}
290

    
291
			rtxpPara->iotpCmdMem->DelACmd(rtuno);
292
		}
293

    
294
		SeSleep(500);
295

    
296
	}
297

    
298
	return 0;
299
}
300

    
301
CZJACS::CZJACS()
302
{
303
	rxflag = false;
304
	txflag = false;
305
}
306

    
307
CZJACS::~CZJACS()
308
{
309
	if (rxflag == true)
310
	{
311
		rxflag = false;
312
		SeSleep(10);
313
		SE_EndThread(rxthread);
314
	}
315
	if (txflag == true)
316
	{
317
		txflag = false;
318
		SeSleep(10);
319
		SE_EndThread(txthread);
320
	}
321
	if (seflag == true) {
322
		seflag = false;
323
		SeSleep(10);
324
		SE_EndThread(sethread);
325
	}
326

    
327
	if (subid_Verification != -1)
328
	{
329
		string id;
330
		id = std::to_string(subid_Verification);
331
		DeleteSubscribe(id);
332
	}	
333

    
334
	if (subid_Alarm != -1)
335
	{
336
		string id;
337
		id = std::to_string(subid_Alarm);
338
		DeleteSubscribe(id);
339
	}
340
}
341

    
342

    
343
void CZJACS::Init(S_PROTOCOLCFG* pcfg)
344
{
345
	PRawCtrl = pcfg->PRawCtrl;
346
	pCmdMem = pcfg->pCmdMem;
347
	pRtu = pcfg->pRtu;
348
	pLink = pcfg->pLink;
349

    
350
	iotPRawCtrl = pcfg->PRawCtrl;
351
	iotpRxBuf = pcfg->pRxBuf;
352
	iotpTxBuf = pcfg->pTxBuf;
353
	iotpCmdMem = pcfg->pCmdMem;
354
	iotpRtu = pcfg->pRtu;
355
	iotpLink = pcfg->pLink;
356

    
357
	rxflag = true;
358
	txflag = true;
359
	seflag = true;
360
	subscribeflag = false;
361
	subscribeflag2 = false;
362

    
363
	subid_Alarm = -1;
364
	subid_Verification = -1;
365

    
366
	PrintLog(LOG_INFORMATION, "????INIT !");
367

    
368
	ReadIni();
369

    
370
	PrintLog(LOG_INFORMATION, "????readini??? !");
371

    
372
	if (rxflag)
373
	{
374
		rxthread = SE_CreateThread((PTHREAD_FUNC_DEF)rxprocess, this, 100);
375
	}
376
	PrintLog(LOG_INFORMATION, "????rx?߳? !");
377

    
378
	if (txflag)
379
	{
380
		txthread = SE_CreateThread((PTHREAD_FUNC_DEF)txprocess, this, 100);
381
	}
382
	PrintLog(LOG_INFORMATION, "????tx?߳? !");
383

    
384
	if (seflag) {
385
		sethread = SE_CreateThread((PTHREAD_FUNC_DEF)seprocess, this, 100);
386
	}
387
	PrintLog(LOG_INFORMATION, "??????????߳? !");
388
	//????restful???񣬽??ܲ???????????Ϣ	
389
//	strCallback.iotprotocols = this;
390
//	strCallback.rtuno = iotpLink->GetRtuNo();
391
//	restfulServer();
392

    
393
}
394

    
395
void CZJACS::restfulServer()
396
{
397
	//restful????IJ???
398
	std::string port;
399
	port = std::to_string(client_port);
400

    
401
	RestfulServer restServer;
402
	int ret = 1;
403
	while (ret == 1)
404
	{
405
		PrintLog(LOG_INFORMATION, "????restful????ˡ?????");
406

    
407
		restServer.Start(port, &strCallback);
408
		restServer.Close();
409

    
410
		PrintLog(LOG_ERROR, "restful??????˳???????");
411
		SeSleep(5000);
412
	}
413
	
414
}
415

    
416

    
417

    
418
//????????
419
int CZJACS::PostSubscribe(int type)
420
{
421
	int status = 0;
422
	sint32 rtuno = iotpLink->GetRtuNo();
423

    
424
	char tmp_header[128] = { 0 };
425
	sprintf(tmp_header, "Content-Type:application/json\r\n");
426
	string startmic_header = tmp_header;
427

    
428
	string urls = server_urls + "/LAPI/V1.0/System/Event/Subscription";
429
	string body;
430
	string callback;
431
	
432

    
433
	Json::Value bodysender;
434
	
435
	bodysender["AddressType"] = 0;
436
	bodysender["IPAddress"] = client_ip;
437
	bodysender["Port"] = client_port;
438

    
439
#ifdef WIN32
440
	long long a = 4294967295;
441
	bodysender["Duration"] = a;
442
#else
443
	bodysender["Duration"] = 4294967295;
444
#endif
445

    
446
	
447
	bodysender["Type"] = type;
448

    
449
	if (type == 1024)
450
	{
451
		bodysender["SubscribePersonCondition"]["LibIDNum"] = 65535;
452
		bodysender["SubscribePersonCondition"]["LibIDList"][0];
453
	}
454
	
455
	Json::FastWriter bodywrite;
456
	body = bodywrite.write(bodysender);
457
	
458
	status = Fclient::Sendreq(urls, startmic_header, body, callback, 1);
459

    
460
	if (callback.size() <= 0)
461
	{	
462
		if (type == 1024){
463
			PrintLog(LOG_ERROR, "??????Ա???鶩?ķ???Ϊ??!");
464
		}
465
		else{
466
			PrintLog(LOG_ERROR, "?????ŴŸ澯???ķ???Ϊ??!");
467
		}
468
		return 0;
469
	}
470

    
471
	Json::Reader reader(Json::Features::strictMode());
472
	Json::Value callback_result;
473
	if (reader.parse(callback, callback_result))
474
	{
475
		string result = callback_result["Response"]["ResponseString"].asString();
476
		string status = callback_result["Response"]["StatusString"].asString();
477
		int flag = result.compare("Succeed");
478
		if (!flag)
479
		{
480
			if (type == 1024)
481
			{
482
				subid_Verification = callback_result["Response"]["Data"]["ID"].asInt();
483
				PrintLog(LOG_INFORMATION, "??????Ա???鶩?ijɹ???????idΪ %d ", subid_Verification);
484

    
485

    
486
				subscribeflag = false;
487
			}
488
			else
489
			{
490
				int subid_Alarm = callback_result["Response"]["Data"]["ID"].asInt();
491
				PrintLog(LOG_INFORMATION, "?????ŴŸ澯???ijɹ???????idΪ %d ", subid_Alarm);
492
				subscribeflag2 = false;
493
			}
494
			
495
		}
496

    
497
		else
498
		{
499
			if (type == 1024)
500
			{
501
				PrintLog(LOG_ERROR, "??????Ա???鶩??ʧ?ܣ????ش???%s  ", status.c_str());
502
			}
503
			else
504
			{
505
				PrintLog(LOG_ERROR, "?????ŴŸ澯????ʧ?ܣ????ش???%s  ", status.c_str());
506
			}
507
			
508
		}
509

    
510

    
511
	}
512

    
513
	return 1;
514

    
515
}
516

    
517
//ɾ??????
518
int CZJACS::DeleteSubscribe(string id)
519
{
520
	int status = 0;
521
	sint32 rtuno = iotpLink->GetRtuNo();
522

    
523
	char tmp_header[128] = { 0 };
524
	sprintf(tmp_header, "Content-Type:application/json\r\n");
525
	string stopmic_header = tmp_header;
526

    
527
	string urls = server_urls + "/LAPI/V1.0/System/Event/Subscription/" + id;
528
	string body;
529
	string callback;
530

    
531
	Json::Value bodysender;
532
	Json::FastWriter bodywrite;
533
	body = bodywrite.write(bodysender);
534

    
535
	status = Fclient::Sendreq(urls, stopmic_header, body, callback, 2);
536

    
537

    
538
	if (callback.size() <= 0)
539
	{
540
		PrintLog(LOG_ERROR, "ɾ?????ķ???Ϊ??!");
541
		return 0;
542
	}
543

    
544
	Json::Reader reader(Json::Features::strictMode());
545
	Json::Value callback_result;
546

    
547
	if (reader.parse(callback, callback_result))
548
	{
549
		string result = callback_result["Response"]["ResponseString"].asString();
550
		string status = callback_result["Response"]["StatusString"].asString();
551
		int flag = result.compare("Succeed");
552
		if (!flag)
553
		{
554
			PrintLog(LOG_INFORMATION, "ɾ?????ijɹ???????idΪ %d ", atoi(id.c_str()));
555
		}
556

    
557
		else
558
		{
559
			PrintLog(LOG_ERROR, "ɾ??????ʧ?ܣ????ش???%s   ", status.c_str());
560
		}
561

    
562

    
563
	}
564

    
565
	return 1;
566
}
567

    
568

    
569
//Զ?̿???
570
int CZJACS::RemoteOpened()
571
{
572
	int status = 0;
573
	sint32 rtuno = iotpLink->GetRtuNo();
574

    
575
	char tmp_header[128] = { 0 };
576
	sprintf(tmp_header, "Content-Type:application/json\r\n");
577
	string start_header = tmp_header;
578

    
579
	string urls = server_urls + "/LAPI/V1.0/PACS/Controller/RemoteOpened";
580
	string body;
581
	string callback;
582

    
583
	Json::Value bodysender;
584
	
585
	Json::FastWriter bodywrite;
586
	body = bodywrite.write(bodysender);
587

    
588
	status = Fclient::Sendreq(urls, start_header, body, callback, 3);
589
	
590
	if (callback.size() <= 0)
591
	{
592
		PrintLog(LOG_ERROR, "???ſ??Ʒ??ؽ??Ϊ?գ?");
593
		return 0;
594
	}
595

    
596
	Json::Reader reader(Json::Features::strictMode());
597
	Json::Value openresult;
598
	if (reader.parse(callback, openresult))
599
	{
600
		string result = openresult["Response"]["ResponseString"].asString();
601
		string status = openresult["Response"]["StatusString"].asString();
602
		int flag = result.compare("Succeed");
603

    
604
		if (!flag)
605
		{
606
			PrintLog(LOG_INFORMATION, "???ſ????¿سɹ???");
607
		}
608

    
609
		else
610
		{			
611
			PrintLog(LOG_ERROR, "???ſ????¿?ʧ?ܣ????ش???%s   ", status.c_str());
612
		}
613

    
614
	
615
	}
616

    
617
	return 1;
618

    
619
}
620

    
621

    
622

    
623
void CZJACS::ReadIni()
624
{
625
	char filename[512];
626
	CReadConf readconf;
627

    
628

    
629
	char* pWorkPath = getenv("SEROOT");
630

    
631

    
632
	if (pWorkPath != NULL)
633
		sprintf(filename, "%s%s", pWorkPath, "/cfg/ZJ_ACS.ini");
634

    
635
	int face_flag, door_flag;
636
	char urls_tmp[128];
637
	char ip_tmp[128];
638
	char port_tmp[128];
639
	string head_tmp = "http://";
640
	readconf.ReadString("LoginInfo", "server_urls", urls_tmp, filename, "192.168.2.65:80");
641
	readconf.ReadString("LoginInfo", "client_ip", ip_tmp, filename, "192.168.2.45");
642
	client_port=readconf.ReadLong("LoginInfo", "client_port", filename, "6666");
643
	face_flag = readconf.ReadLong("LoginInfo", "face_flag", filename, "0");
644
	door_flag = readconf.ReadLong("LoginInfo", "door_flag", filename, "0");
645
	if (face_flag == 1) {
646
		subscribeflag = true;
647
	}
648
	if (door_flag == 1) {
649
		subscribeflag2 = true;
650
	}
651
	server_urls = head_tmp + urls_tmp;
652
	client_ip = ip_tmp;
653
}
654

    
655
sint32 CZJACS::TxProc()
656
{
657
	return 1;
658
}
659

    
660
sint32 CZJACS::RxProc()
661
{
662
	return 1;
663
}
664

    
665

    
666

    
667
#if defined(WIN32)
668
extern "C" __declspec(dllexport)
669
#else 
670
extern "C"
671
#endif
672
CProtocol	*CreateProtocol(char *defpara)
673
{
674
	return (CProtocol*)(new CZJACS());
675
}
676

    
677
/*
678
#ifdef __unix
679
extern "C" CProtocol* CreateProtocol(char *defpara)
680
#else
681
extern "C" __declspec(dllexport)  CProtocol* CreateProtocol(char *defpara)
682
#endif
683
{
684
CProtocol *pEpv = new CZJACS;
685
return pEpv;
686
}
687
*/
(13-13/13)