Project

General

Profile

Feature #2953 » line4c3.cpp

yan hong, 04/18/2023 10:43 AM

 
1
#pragma once
2
#include "line4c3.h"
3
#include "Line4C3client.h"
4

    
5
#define APPID_INTERVAL	(1296000*2/3-2)		//(15 days,10d)
6
#define	TOKEN_INTERVAL	(3600/2-2)		//(1 hours,0.5h)
7
int timeout_RegAppid;
8
int timeout_token;
9
unsigned long GetTimeStamp()
10
{
11
	unsigned long stampNow = 0;
12
	time_t timer;
13
	timer = time(NULL);
14
	stampNow = timer;
15

    
16
	return stampNow;
17
}
18

    
19
UINT rxprocess(LPVOID rxptr)
20
{
21
	CLine5C3* rtxpPara = NULL;
22
	rtxpPara = (CLine5C3*)rxptr;
23
	sint32 rtuno = rtxpPara->Get_rtuno();
24
	while (!rtxpPara->rxflag)
25
	{	
26
	  if(rtxpPara->cmd_flag){//no cmd
27
		string authName = rtxpPara->Get_authName();
28
		int userNum = rtxpPara->Get_userNum();
29
		int appid_nowtime = GetTimeStamp();
30
		if (((appid_nowtime - rtxpPara->appid_lasttime) > timeout_RegAppid) || ((appid_nowtime - rtxpPara->appid_lasttime)<0) || (rtxpPara->appid_flag == true))
31
		{
32
			rtxpPara->postappid(authName, userNum);
33
			//rtxpPara->appid_flag = false;
34
			if (!rtxpPara->appid_flag)
35
			{
36
				rtxpPara->appid_lasttime = appid_nowtime;
37
			}
38
		}
39
		int token_nowtime = GetTimeStamp();
40
		if (!rtxpPara->appid_flag && (((token_nowtime - rtxpPara->token_lasttime) > timeout_token) || ((token_nowtime - rtxpPara->token_lasttime) <0) || (rtxpPara->token_flag == true)))
41
		{
42
			string appid = rtxpPara->Get_appid();
43
			rtxpPara->posttokenid(appid, token_nowtime);
44
			if (!rtxpPara->token_flag)
45
			{
46
				rtxpPara->token_lasttime = token_nowtime;
47
			}
48
		}
49
		int zone_nowtime = GetTimeStamp();
50
		if ((((zone_nowtime - rtxpPara->zone_lasttime) > 8) || ((zone_nowtime - rtxpPara->zone_lasttime) < 0)) && rtxpPara->zone_flag && !(rtxpPara->appid_flag) && !(rtxpPara->token_flag))
51
		{
52
			rtxpPara->getcall_stations();	//Get user_id 
53
			rtxpPara->getsubsystems();
54
//			//rtxpPara->getall_broadcast_lines(rtuno);
55
//			rtxpPara->getall_zones();
56
			rtxpPara->getzones();
57
			rtxpPara->zone_lasttime = zone_nowtime;
58
		}
59
	  }
60
	  Sleep(100);
61
	}
62
	return 0;
63
}
64

    
65
UINT txprocess(LPVOID txptr)
66
{
67
	//////Test///////////////////////////////
68
//	int userid[8] = {5051,5052,5053,5054,0,0,0,0};
69

    
70
	CLine5C3* rtxpPara = NULL;
71

    
72
	rtxpPara = (CLine5C3*)txptr;
73
	sint32 rtuno = rtxpPara->Get_rtuno();
74
	int i=0;
75
	while (!rtxpPara->txflag)
76
	{
77
		S_RAWCMD rawcmd;
78
		while (rtxpPara->Get_CmdNum() > 0)
79
		{
80
			if (rtxpPara->Get_ACmd(rtuno, &rawcmd) == 0)
81
				break;
82

    
83
			if (rawcmd.type == DC_K_CMD_PAOPRATE)
84
			{
85
				CSI_D_PA_CMD* PaCmd;
86
				PaCmd = (CSI_D_PA_CMD*)rawcmd.data;
87

    
88
				switch (PaCmd->Func)
89
				{
90
				case PA_FUNC_MIKE:
91
				{	rtxpPara->cmd_flag=false;
92
					uint16 zone = PaCmd->Zone;
93
					int m_nAreaNum = rtxpPara->Get_AreaNum();
94
					
95
					vector<uint16> zones_tmp;
96
					for (i = 0; i < m_nAreaNum; ++i)
97
					{
98
						int j = (0xFFFF) & (zone & 1);
99
						if (j == 1)
100
						{
101
							zones_tmp.push_back(i + 1);
102
						}
103
						zone >>= 1;
104
					}
105

    
106
					uint32 station = PaCmd->Station;
107
					int m_nStationNum = rtxpPara->Get_StationNum();
108
					vector<uint32> subsystems_tmp;
109
					for (i = 0; i < m_nStationNum; ++i)
110
					{
111
						int j = (0xFFFFFFFF) & (station & 1);
112
						if (j == 1)
113
						{
114
							
115
//							subsystems_tmp.push_back(i + 1);
116
							subsystems_tmp.push_back(i);
117
						}
118
						station >>= 1;
119
					}
120
					vector<uint32> match = rtxpPara->stationid(subsystems_tmp);
121

    
122
					uint8 user = PaCmd->User;
123
					int call_station_id = rtxpPara->callstationid(user-1);//user=1-8
124
//					if (call_station_id < 1)call_station_id = userid[user-1];//Test Init
125

    
126
					rtxpPara->post_startmic(call_station_id, match, zones_tmp);
127
					rtxpPara->Put_AYx(rtuno, YX_MIC, rtxpPara->startmic_flag);
128
				}
129
				break;
130
				case PA_FUNC_CLOSE:
131
				{	rtxpPara->cmd_flag=false;
132
					switch (PaCmd->Val)
133
					{
134
					case PA_CLOSE_MIKE:
135
						{
136
						uint8 user = PaCmd->User;
137
						int call_station_id = rtxpPara->callstationid(user-1);//user=1-8
138
//						if (call_station_id < 1)call_station_id = userid[user-1];//Test Init
139
						rtxpPara->post_stopmic(call_station_id);
140
						rtxpPara->Put_AYx(rtuno, YX_MIC, rtxpPara->stopmic_flag);
141
						}
142
					break;
143
					default:
144
						break;
145
					}
146
				}
147
				break;				
148
				default:
149
					break;
150
				}
151
			}
152
			rtxpPara->Del_ACmd(rtuno);
153
		}
154
		Sleep(100);
155
	}
156
	return 0;
157
}
158

    
159
void CLine5C3::Put_AYx(uint16 Rtuno, uint16 YxNo, uint8 YxValue)
160
{
161
	PRawCtrl->PutAYx(Rtuno, YxNo, YxValue);
162
}
163

    
164
sint32 CLine5C3::Get_rtuno()
165
{
166
	return (pLink->GetRtuNo());
167
}
168

    
169
sint32 CLine5C3::Get_CmdNum()
170
{
171
	sint32 rtuno = pLink->GetRtuNo();
172
	return (pCmdMem->GetCmdNum(rtuno));
173
}
174

    
175
sint32 CLine5C3::Get_ACmd(sint32 rtuno, S_RAWCMD* cmd)
176
{
177
	return pCmdMem->GetACmd(rtuno, cmd);
178
}
179

    
180
sint32 CLine5C3::Del_ACmd(sint32 rtuno)
181
{
182
	return pCmdMem->DelACmd(rtuno);
183
}
184

    
185
int CLine5C3::Get_AreaNum()
186
{
187
	return AreaNum;
188
}
189

    
190
int CLine5C3::Get_StationNum()
191
{
192
	return StationNum;
193
}
194

    
195
string CLine5C3::Get_authName()
196
{
197
	return authName;
198
}
199

    
200
int CLine5C3::Get_userNum()
201
{
202
	return userNum;
203
}
204

    
205
string CLine5C3::Get_appid()
206
{
207
	return app_id;
208
}
209

    
210
string CLine5C3::Get_tokenid()
211
{
212
	return token_id;
213
}
214

    
215
int CLine5C3::callstationid(uint8 user)
216
{
217
	return map_user[user];
218
}
219

    
220
vector<uint32> CLine5C3::stationid(vector<uint32> sta)
221
{	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
222
//	uint32 staID[32] = { 1519, 1523, 1527, 1531, 1534, 1537, 1540, 1544, 1547, 1550, 1553, 1561, 1564, 1568, 1572, 1575, 1578, 1581, 1584, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
223
	vector<uint32> res;
224
	int i = 0,staNum=0;
225
	staNum=map_station.size();
226
/*	if(map_station.size()<1){
227
		staNum=32;
228
		for (i = 0; i < 32; ++i)
229
			map_station[i] = staID[i];//Test Init
230
	}
231
	*/
232
	for (i = 0; i < sta.size(); ++i)
233
	{
234
		if(i<staNum)
235
			res.push_back(map_station[sta[i]]);
236
		else
237
			break;
238
	}
239
	return res;
240
}
241

    
242
void CLine5C3::Init(S_PROTOCOLCFG* pcfg)
243
{
244
	PRawCtrl = pcfg->PRawCtrl;
245
	pCmdMem = pcfg->pCmdMem;
246
	pRtu = pcfg->pRtu;
247
	pLink = pcfg->pLink;
248
	timeout_RegAppid=pLink->GetKwhScanInterval();//ACC-COUNT????ٻ???????
249
	if(timeout_RegAppid<120)timeout_RegAppid=1296000;
250
	timeout_token=pLink->GetAllDataScanInterval();//ALL-DATAȫ????ɨ??????
251
	if(timeout_token<30)timeout_token=3600;
252
	
253
	AreaNum = 16;
254
	StationNum = 32;
255

    
256
	rxflag = true;
257
	txflag = true;
258

    
259
	appid_flag = true;
260
	token_flag = true;
261
	zone_flag = true;
262
	cmd_flag=true;
263
	appid_lasttime = 0;
264
	token_lasttime = 0;
265
	zone_lasttime = 0;
266

    
267
	startmic_flag = 0;
268
	stopmic_flag = 0;
269

    
270
	//authName = "sios";
271
	//userNum = 1234;
272
	ReadIni();
273
	PrintLog(LOG_WARNING, "RTU[%d] ReadIni [SEROOT/cfg/Pa5line.ini]!",Get_rtuno());
274
	if (rxflag)
275
	{
276
		sint32 rxthread = SE_CreateThread((PTHREAD_FUNC_DEF)rxprocess, this, 100);
277
		rxflag=false;
278
	PrintLog(LOG_WARNING, "RTU[%d] Create [rxprocess] thread OK! rxflag=false!",Get_rtuno());
279
	}
280
	if (txflag)
281
	{
282
		sint32 txthread = SE_CreateThread((PTHREAD_FUNC_DEF)txprocess, this, 100);
283
		txflag=false;
284
	PrintLog(LOG_WARNING, "RTU[%d] Create [txprocess] thread OK! txflag=false!",Get_rtuno());
285
	}	
286
}
287

    
288
int CLine5C3::postappid(string authName, int userNum)
289
{
290
	int status = 0;
291
	sint32 rtuno = Get_rtuno();
292
	string urls = server_urls + "/auth/regappid";
293
	string appid_header = "Content-Type:application/json\r\n";
294
	string appid_body;
295
	string appid_callback;
296
	Json::Value bodysender;
297
	if (authName.size() > 0)
298
	{
299
		bodysender["authName"] = authName;
300
	}
301
	else
302
	{
303
		PrintLog(LOG_ERROR, "authName is NULL!");
304
	}
305
	if (userNum >= 0)
306
	{
307
		bodysender["userNum"] = userNum;
308
	}
309
	else
310
	{
311
		PrintLog(LOG_ERROR, "userNum < 0!");
312
	}
313
	Json::FastWriter bodywrite;
314
	appid_body = bodywrite.write(bodysender);	
315
	status = Fclient::Sendreq(urls, appid_header, appid_body, appid_callback);	
316
	if (status == 1){
317
		Put_AYx(rtuno, STATUS, 1);
318
	appid_flag = false;
319
	token_flag = true;
320
	}
321
	else{
322
		Put_AYx(rtuno, STATUS, 0);	
323
	appid_flag = true;
324
	token_flag = true;
325
	zone_flag = true;
326
	}
327
	/*
328
	//json??????????
329
	Json::Value appidback;
330
	appidback["success"] = true;
331
	appidback["code"] = 200;
332
	appidback["message"] = "";
333
	appidback["regappid"] = "ef08e274512c41d885a755edc7870e9d";
334
	Json::FastWriter appidwrite;
335
	appid_callback = appidwrite.write(appidback);
336
	PrintLog(LOG_INFORMATION, "appid_callback is setting!");
337
	*/
338
	if (appid_callback.size() <= 0)
339
	{
340
		PrintLog(LOG_ERROR, "appid_callback is NULL!");
341
		return 0;
342
	}
343
	Json::Reader reader(Json::Features::strictMode());
344
	Json::Value parse_appid;
345
	if (reader.parse(appid_callback, parse_appid))
346
	{
347
		bool success = parse_appid["success"].asBool();
348
		int code = parse_appid["code"].asInt();
349
		string message = parse_appid["message"].asString();
350
		string regappid = parse_appid["regappid"].asString();
351

    
352
		if (success == true)
353
		{
354
			appid_flag = false; 
355
			token_flag = true;
356
			app_id = regappid;
357
			PrintLog(LOG_INFORMATION, "regappid success, regappid??%s", regappid.c_str());
358
		}
359
		else
360
		{
361
	appid_flag = true;
362
	token_flag = true;
363
	zone_flag = true;
364
			PrintLog(LOG_INFORMATION, "RTU[%d] regappid occured error, ʧ?????ɣ?%s, ???룺%d", rtuno,message.c_str(), code);
365
		}
366
	}
367
	return 1;
368
}
369

    
370

    
371
int CLine5C3::posttokenid(string appid, int timeKey)
372
{
373
	int status = 0;
374
	sint32 rtuno = Get_rtuno();
375
	string urls = server_urls + "/auth/gettoken";
376
	string token_header = "Content-Type:application/json\r\n";
377
	string token_body;
378
	string token_callback;
379
	Json::Value bodysender;
380
	if (app_id.size() > 0)
381
	{
382
		bodysender["appid"] = app_id;
383
	}
384
	else
385
	{
386
		PrintLog(LOG_ERROR, "app_id is NULL!");
387
	}
388
	//int timeKey = GetTimeStamp();
389
	if (timeKey >= 0)
390
	{
391
		bodysender["timeKey"] = timeKey;
392
//		bodysender["timeKey"] = 1678929319;
393
	}
394
	else
395
	{
396
		PrintLog(LOG_ERROR, "timeKey < 0!");
397
	}
398

    
399
	Json::FastWriter bodywrite;
400
	token_body = bodywrite.write(bodysender);
401
	status = Fclient::Sendreq(urls, token_header, token_body, token_callback);
402
	if (status == 1){
403
		Put_AYx(rtuno, STATUS, 1);
404
		token_flag = false;
405
	}
406
	else{
407
		Put_AYx(rtuno, STATUS, 0);
408
	appid_flag = true;
409
	token_flag = true;
410
	zone_flag = true;
411
	}
412
	/*
413
	//json??????????
414
	Json::Value tokenback;
415
	tokenback["success"] = true;
416
	tokenback["code"] = 200;
417
	tokenback["message"] = "";
418
	tokenback["token"] = "ThirdBearer xxx.xxx.xxx";
419
	Json::FastWriter tokenwrite;
420
	token_callback = tokenwrite.write(tokenback);
421
	PrintLog(LOG_INFORMATION, "token_callback is setting!");
422
	*/
423

    
424
	if (token_callback.size() <= 0)
425
	{
426
		PrintLog(LOG_ERROR, "token_callback is NULL!");
427
		return 0;
428
	}
429

    
430
	Json::Reader reader(Json::Features::strictMode());
431
	Json::Value parse_token;
432
	if (reader.parse(token_callback, parse_token))
433
	{
434
		bool success = parse_token["success"].asBool();
435
		int code = parse_token["code"].asInt();
436
		string message = parse_token["message"].asString();
437
		string token = parse_token["token"].asString();
438
		if (success == true)
439
		{
440
			token_flag = false;
441
			token_id = token;
442
			PrintLog(LOG_INFORMATION, "success_tokenid??%s", token.c_str());
443
		}
444
		else
445
		{
446
	appid_flag = true;
447
	token_flag = true;
448
	zone_flag = true;
449
			PrintLog(LOG_INFORMATION, "RTU[%d] gettoken occured error, ʧ?????ɣ?%s, ???룺%d", rtuno,message.c_str(), code);
450
		}
451
	}
452

    
453
	return 1;
454
}
455

    
456
int CLine5C3::getcall_stations()
457
{
458
	int status = 0,i=0;
459
	sint32 rtuno = Get_rtuno();
460

    
461
	char tmp_header[128] = { 0 };
462
	sprintf(tmp_header, "Content-Type:application/json\r\nAccessTokenThird:%s\r\n", token_id.c_str());
463
	token_header = tmp_header;
464
	string urls = server_urls + "/manual/api/pa/call_stations";
465
	string call_stations_data = "";
466
	string call_stations_callback;
467
	status = Fclient::Sendreq(urls, token_header, call_stations_data, call_stations_callback);
468
	if (status == 1)
469
	{
470
		Put_AYx(rtuno, STATUS, 1);
471
//		zone_flag=false;
472
	}
473
	else
474
	{
475
		Put_AYx(rtuno, STATUS, 0);
476
	appid_flag = true;
477
	token_flag = true;
478
	zone_flag = true;
479
	}
480
	/*
481
	//json??????????
482
	Json::Value callstationback;
483
	callstationback[0]["id"] = 5051;
484
	callstationback[0]["name"] = "?е?1";
485
	callstationback[0]["status"] = "on";
486
	callstationback[1]["id"] = 5052;
487
	callstationback[1]["name"] = "?е?2";
488
	callstationback[1]["status"] = "on";
489
	callstationback[2]["id"] = 5053;
490
	callstationback[2]["name"] = "?е?3";
491
	callstationback[2]["status"] = "on";
492
	Json::FastWriter callstationwrite;
493
	call_stations_callback = callstationwrite.write(callstationback);
494
	PrintLog(LOG_INFORMATION, "call_stations_callback is setting!");
495
	*/
496

    
497
	if (call_stations_callback.size() <= 0)
498
	{
499
		PrintLog(LOG_ERROR, "call_stations_callback is NULL!");
500
		return 0;
501
	}
502
	Json::Reader reader(Json::Features::strictMode());
503
	Json::Value parse_call_station_callback;
504
	if (reader.parse(call_stations_callback, parse_call_station_callback))
505
	{
506
		bool success = parse_call_station_callback["success"].asBool();
507
		if (!success) {
508
			appid_flag = true;
509
			token_flag = true;
510
			zone_flag = true;
511
		}
512
		int size = 0;
513
		if (parse_call_station_callback["result"].isArray())
514
		{
515
			size = parse_call_station_callback["result"].size();
516
		}
517
		else
518
			return 0;
519
		PrintLog(LOG_INFORMATION, "Num of call_stations is %d", size);
520
		vector<cstation>().swap(call_stations);
521
		for (i = 0; i < size; ++i)
522
		{
523
			int id = parse_call_station_callback["result"][i]["id"].asInt();
524
			string name = parse_call_station_callback["result"][i]["name"].asString();
525
			string status = parse_call_station_callback["result"][i]["status"].asString();
526

    
527
			cstation cstation_tmp;
528
			cstation_tmp.id = id;
529
			cstation_tmp.name = name;
530
			cstation_tmp.status = status;
531
			call_stations.push_back(cstation_tmp);
532
		}
533
		for (i = 0; i < 8; ++i)
534
		{
535
			if (call_stations.size() <= i)
536
				break;
537

    
538
			map_user[i] = call_stations[i].id;
539
			
540
		}
541
	}
542
	return 1;
543
}
544

    
545
int CLine5C3::getsubsystems()
546
{
547
	int status = 0,i=0;
548
	sint32 rtuno = Get_rtuno();
549
	char tmp_header[128] = { 0 };
550

    
551
	sprintf(tmp_header, "Content-Type:application/json\r\nAccessTokenThird:%s\r\n", token_id.c_str());
552
	token_header = tmp_header;
553
	string urls = server_urls + "/manual/api/pa/subsystems";
554
	string subsystems_data = "";
555
	string subsystems_callback;
556
	status = Fclient::Sendreq(urls, token_header, subsystems_data, subsystems_callback);
557
	if (status == 1)
558
	{
559
		Put_AYx(rtuno, STATUS, 1);
560
//		zone_flag=false;
561
	}
562
	else
563
	{
564
		Put_AYx(rtuno, STATUS, 0);
565
	appid_flag = true;
566
	token_flag = true;
567
	zone_flag = true;
568
	}
569
	if (subsystems_callback.size() <= 0)
570
	{
571
		PrintLog(LOG_ERROR, "subsystems_callback is NULL!");
572
		return 0;
573
	}
574
	Json::Reader reader(Json::Features::strictMode());
575
	Json::Value parse_subsystems_callback;
576
	if (reader.parse(subsystems_callback, parse_subsystems_callback))
577
	{
578
		bool success = parse_subsystems_callback["success"].asBool();
579
		if (!success) {
580
			appid_flag = true;
581
			token_flag = true;
582
			zone_flag = true;
583
		}
584
		int size = 0;
585
		if (parse_subsystems_callback["result"].isArray())
586
		{
587
			size = parse_subsystems_callback["result"].size();
588
		}
589
		else
590
			return 0;
591
		PrintLog(LOG_INFORMATION, "Num of subsystems is %d", size);
592
		vector<subsystem>().swap(subsystems);
593
		for (i = 0; i < size; ++i)
594
		{
595
			int id = parse_subsystems_callback["result"][i]["id"].asInt();
596
			string name = parse_subsystems_callback["result"][i]["name"].asString();
597

    
598
			subsystem subsystem_tmp;
599
			subsystem_tmp.id = id;
600
			subsystem_tmp.name = name;
601
			subsystems.push_back(subsystem_tmp);
602
		}
603
		for (i = 0; i < StationNum; ++i)
604
		{
605
			if (subsystems.size() <= i)
606
				break;
607
			map_station[i] = subsystems[i].id;
608
		}
609
	}
610
	return 1;
611
}
612

    
613

    
614
int CLine5C3::getzones()
615
{
616
	int status = 0;
617
	sint32 rtuno = Get_rtuno();
618
	appid_flag = false;
619

    
620
	char tmp_header[128] = { 0 };
621
	sprintf(tmp_header, "Content-Type:application/json\r\nAccessTokenThird:%s\r\n", token_id.c_str());
622
	token_header = tmp_header;
623

    
624
	string urls = server_urls + "/manual/api/pa/zones";
625
	string zones_data = "";
626
	string zones_callback;
627

    
628
	status = Fclient::Sendreq(urls, token_header, zones_data, zones_callback);
629
	if (status == 1){
630
		Put_AYx(rtuno, STATUS, 1);
631
//		zone_flag=false;
632
	}
633
	else {
634
		Put_AYx(rtuno, STATUS, 0);
635
		appid_flag = true;
636
//appid_flag = true;
637
		token_flag = true;
638
		zone_flag = true;
639
	}
640
	/*
641
	//json??????????
642
	Json::Value callstationback;
643
	callstationback["result"][0]["id"] = 1;
644
	callstationback["result"][0]["name_en"] = "up";
645
	callstationback["result"][0]["name_zh"] = "????վ̨";
646
	callstationback["result"][1]["id"] = 2;
647
	callstationback["result"][1]["name_en"] = "down";
648
	callstationback["result"][1]["name_zh"] = "????վ̨";
649
	callstationback["result"][2]["id"] = 3;
650
	callstationback["result"][2]["name_en"] = "hall";
651
	callstationback["result"][2]["name_zh"] = "վ??";
652
	callstationback["result"][3]["id"] = 4;
653
	callstationback["result"][3]["name_en"] = "office";
654
	callstationback["result"][3]["name_zh"] = "?豸?칫??";
655
	callstationback["result"][4]["id"] = 5;
656
	callstationback["result"][4]["name_en"] = "exit";
657
	callstationback["result"][4]["name_zh"] = "??վ?????";
658
	callstationback["result"][5]["id"] = 6;
659
	callstationback["result"][5]["name_en"] = "transfer";
660
	callstationback["result"][5]["name_zh"] = "????ͨ??";
661
	Json::FastWriter callstationwrite;
662
	zones_callback = callstationwrite.write(callstationback);
663
	PrintLog(LOG_INFORMATION, "call_stations_callback is setting!");
664
	*/
665

    
666
	if (zones_callback.size() <= 0)
667
	{
668
		PrintLog(LOG_ERROR, "zones_callback is NULL!");
669
		return 0;
670
	}
671

    
672
	Json::Reader reader(Json::Features::strictMode());
673
	Json::Value parse_zones_callback;
674
	if (reader.parse(zones_callback, parse_zones_callback))
675
	{
676
		bool success = parse_zones_callback["success"].asBool();
677
		if (!success) {
678
			appid_flag = true;
679
			token_flag = true;
680
			zone_flag = true;
681
		}
682
		int size = 0;
683
		if (parse_zones_callback["result"].isArray())
684
		{
685
			size = parse_zones_callback["result"].size();
686
		}
687
		else
688
			return 0;
689

    
690
		PrintLog(LOG_INFORMATION, "Num of zones id %d", size);
691

    
692
		for (int i = 0; i < size; ++i)
693
		{
694
			int id = parse_zones_callback["result"][i]["id"].asInt();
695
			string name_en = parse_zones_callback["result"][i]["name_en"].asString();
696
			string name_zh = parse_zones_callback["result"][i]["name_zh"].asString();
697

    
698
			zone zone_tmp;
699
			zone_tmp.id = id;
700
			zone_tmp.name_en = name_en;
701
			zone_tmp.name_zh = name_zh;
702

    
703
			zones.push_back(zone_tmp);
704
		}
705
	}
706

    
707
	return 1;
708
}
709

    
710

    
711
/*
712
int CLine5C3::getall_zones()
713
{
714
	int status = 0;
715
	sint32 rtuno = Get_rtuno();
716
	char tmp_header[128] = { 0 };
717

    
718
	sprintf(tmp_header, "Content-Type:application/json\r\nAccessTokenThird:%s\r\n", token_id.c_str());
719
	token_header = tmp_header;
720
	string urls = server_urls + "/api/pa/all_zones";
721
	string all_zones_data = "";
722
	string all_zones_callback;
723
	status = Fclient::Sendreq(urls, token_header, all_zones_data, all_zones_callback);
724
	if (status == 1){
725
		Put_AYx(rtuno, STATUS, 1);
726
		zone_flag=false;
727
	}
728
	else
729
	{
730
		Put_AYx(rtuno, STATUS, 0);
731
	appid_flag = true;
732
	token_flag = true;
733
	zone_flag = true;
734
	}
735
	if (all_zones_callback.size() <= 0)
736
	{
737
		PrintLog(LOG_ERROR, "all_zones_callback is NULL!");
738
		return 0;
739
	}
740
	Json::Reader reader(Json::Features::strictMode());
741
	Json::Value parse_all_zones_callback;
742
	if (reader.parse(all_zones_callback, parse_all_zones_callback))
743
	{
744
		int size = 0;
745
		if (parse_all_zones_callback["result"].isArray())
746
		{
747
			size = parse_all_zones_callback["result"].size();
748
		}
749
		else
750
			return 0;
751

    
752
		for (int i = 0; i < size; ++i)
753
		{
754
			int zones_size = parse_all_zones_callback["result"][i]["zones"].size();
755
			int j = 0;
756

    
757
			while (j < zones_size)
758
			{
759
				string status = parse_all_zones_callback["result"][i]["zones"][j]["status"].asString();
760
				
761
				if (status == "free")
762
					Put_AYx(rtuno, 16 * i + j + 1, 1);
763
				else
764
					Put_AYx(rtuno, 16 * i + j + 1, 0);
765
				++j;
766
			}
767
		}
768
	}
769
	return 1;
770
}
771

    
772
 */
773

    
774
int CLine5C3::post_startmic(int userid, vector<uint32> stations, vector<uint16> zones)
775
{
776
	int status = 0;
777
	sint32 rtuno = Get_rtuno();
778
	startmic_flag = 0;
779
	char tmp_header[128] = { 0 };
780

    
781
	sprintf(tmp_header, "Content-Type:application/json\r\nAccessTokenThird:%s\r\n", token_id.c_str());
782
	string startmic_header = tmp_header;
783
	string urls = server_urls + "/manual/api/pa/start_mic";
784
	string startmic_body;
785
	string startmic_callback;
786
	Json::Value bodysender;
787
	if (userid > 0)
788
	{
789
//		bodysender["call_station_id"] = 5051;
790
		bodysender["call_station_id"] = userid;
791
	}
792
	else
793
	{
794
		PrintLog(LOG_WARNING, "call_station_id <= 0");
795
	}
796
	if (stations.size() > 0)
797
	{
798
//		bodysender["subsystem_ids"].append(1523);//1539
799
		for (int i = 0; i < stations.size(); ++i)
800
		{
801
			bodysender["subsystem_ids"].append(stations[i]);
802
		}
803
	}
804
	else
805
	{
806
		PrintLog(LOG_ERROR, "subsystems_ids is NULL!");
807
	}
808
	if (zones.size() > 0)
809
	{
810
		for (int i = 0; i < zones.size(); ++i)
811
		{
812
			bodysender["zone_ids"].append(zones[i]);
813
		}
814
	}
815
	else
816
	{
817
		PrintLog(LOG_ERROR, "zone_ids is NULL!");
818
	}
819
	Json::FastWriter bodywrite;
820
	startmic_body = bodywrite.write(bodysender);
821
	status = Fclient::Sendreq(urls, startmic_header, startmic_body, startmic_callback);
822
	cmd_flag=true;
823
	if (status == 1){
824
		Put_AYx(rtuno, STATUS, 1);
825
	}
826
	else
827
	{
828
		Put_AYx(rtuno, STATUS, 0);
829
	appid_flag = true;
830
	token_flag = true;
831
	zone_flag = true;
832
	}
833
	if (startmic_callback.size() <= 0)
834
	{
835
		PrintLog(LOG_ERROR, "startmic_callback is NULL!");
836
		return 0;
837
	}
838
	Json::Reader reader(Json::Features::strictMode());
839
	Json::Value parse_startmic;
840
	if (reader.parse(startmic_callback, parse_startmic))
841
	{
842

    
843
		bool success = parse_startmic["success"].asBool();
844
		int codeVal = parse_startmic["code"].asInt();
845
		string msg=parse_startmic["message"].asString();
846
//		string reason = parse_startmic["result"][0]["reason"].asString();
847
//		string result = parse_startmic["result"][0]["result"].asString();
848
		if (success == true)
849
		{
850
			startmic_flag = 1;
851
			PrintLog(LOG_INFORMATION, "RTU[%d] Rx startmic Answer success!",rtuno);
852
		}
853
		else
854
		{
855
			startmic_flag = 0;
856
			PrintLog(LOG_ERROR, "RTU[%d] Rx startmic Answer failure!",rtuno);
857
		}
858
		int size = 0;
859
		if (parse_startmic["result"].isArray())
860
		{
861
			size = parse_startmic["result"].size();
862
		}
863
		else
864
			return 0;
865
		PrintLog(LOG_INFORMATION, "Num of startmic_callback_information is %d", size);
866
		for (int i = 0; i < size; ++i)
867
		{
868
			int id = parse_startmic["result"][i]["id"].asInt();
869
			bool result = parse_startmic["result"][i]["result"].asBool();
870
			string reason = parse_startmic["result"][i]["reason"].asString();
871

    
872
			if (result)
873
			{
874
				startmic_flag = 1;
875
			}
876
		}
877
		
878
	}
879
	return 1;
880
}
881

    
882
int CLine5C3::post_stopmic(int userid)
883
{
884
	int status = 0;
885
	sint32 rtuno = Get_rtuno();
886
	stopmic_flag = 0;
887
	char tmp_header[128] = { 0 };
888

    
889
	sprintf(tmp_header, "Content-Type:application/json\r\nAccessTokenThird:%s\r\n", token_id.c_str());
890
	string stopmic_header = tmp_header;
891
	string urls = server_urls + "/manual/api/pa/stop_mic";
892
	string stopmic_body;
893
	string stopmic_callback;
894
	Json::Value bodysender;
895
	if (userid > 0)
896
	{
897
		//bodysender["call_station_id"] = 5051;
898
		bodysender["call_station_id"] = userid;
899
	}
900
	else
901
	{
902
		PrintLog(LOG_WARNING, "call_station_id <= 0");
903
	}
904
	Json::FastWriter bodywrite;
905
	stopmic_body = bodywrite.write(bodysender);
906
	status = Fclient::Sendreq(urls, stopmic_header, stopmic_body, stopmic_callback);
907
	cmd_flag=true;
908
	if (status == 1){
909
		Put_AYx(rtuno, STATUS, 1);
910
	}
911
	else
912
	{
913
		Put_AYx(rtuno, STATUS, 0);
914
	appid_flag = true;
915
	token_flag = true;
916
	zone_flag = true;
917
	}
918
	if (stopmic_callback.size() <= 0)
919
	{
920
		PrintLog(LOG_ERROR, "stopmic_callback is NULL!");
921
		return 0;
922
	}
923
	Json::Reader reader(Json::Features::strictMode());
924
	Json::Value parse_stopmic;
925
	if (reader.parse(stopmic_callback, parse_stopmic))
926
	{
927
		bool success = parse_stopmic["success"].asBool();
928
		int codeVal = parse_stopmic["code"].asInt();
929
		string msg=parse_stopmic["message"].asString();
930
//		string reason = parse_stopmic["result"][0]["reason"].asString();
931
//		string result = parse_stopmic["result"][0]["result"].asString();
932
		if (success == true)
933
		{
934
			stopmic_flag = 1;
935
			PrintLog(LOG_INFORMATION, "RTU[%d] Rx stopmic  Answer success!",rtuno);
936
		}
937
		else
938
		{
939
			stopmic_flag = 0;
940
			PrintLog(LOG_ERROR, "RTU[%d] Rx stopmic  Answer failure!",rtuno);
941
		}
942
		int size = 0;
943
		if (parse_stopmic["result"].isArray())
944
		{
945
			size = parse_stopmic["result"].size();
946
		}
947
		else
948
			return 0;
949
		PrintLog(LOG_INFORMATION, "stop_mic");
950
		for (int i = 0; i < size; ++i)
951
		{
952
			bool result = parse_stopmic["result"][i]["result"].asBool();
953
			string reason = parse_stopmic["result"][i]["reason"].asString();
954
			if (result)
955
			{
956
				stopmic_flag = 1;
957
			}
958
		}	
959
	}
960
	return 1;
961
}
962

    
963
void CLine5C3::ReadIni()
964
{
965
	char filename[512];
966
	CReadConf readconf;
967
	char* pWorkPath = getenv("FEPROOT");
968
	if (pWorkPath != NULL)
969
		sprintf(filename, "%s%s", pWorkPath, "/cfg/Pa4line.ini");	
970
	char urls_tmp[128];
971
	char auth_tmp[128];
972
	char user_tmp[128];
973
	string head_tmp = "http://";
974
	readconf.ReadString("LoginInfo", "server_urls", urls_tmp, filename, "10.5.0.92:8086");
975
	readconf.ReadString("LoginInfo", "authName", auth_tmp, filename, "sios");
976
	readconf.ReadString("LoginInfo", "userNum", user_tmp, filename, "1234");
977
	server_urls  = head_tmp + urls_tmp;
978
	//strcat(server_urls, urls_tmp);
979
	authName = auth_tmp;
980
	userNum = atoi(user_tmp);
981
}
982

    
983
sint32 CLine5C3:: TxProc()
984
{
985
	return 1;
986
}
987

    
988
sint32 CLine5C3::RxProc()
989
{
990
	return 1;
991
}
992

    
993
#if defined(WIN32)
994
extern "C" __declspec(dllexport)
995
#else 
996
extern "C"
997
#endif
998
CProtocol	*CreateProtocol(char *defpara)
999
{
1000
	return (CProtocol*)(new CLine5C3());
1001
}
1002
/*
1003
#ifdef __unix
1004
extern "C" CProtocol* CreateProtocol(char *defpara)
1005
#else
1006
extern "C" __declspec(dllexport)  CProtocol* CreateProtocol(char *defpara)
1007
#endif
1008
{
1009
	CProtocol *pEpv = new CLine5C3;
1010
	return pEpv;
1011
}
1012
*/
(3-3/9)