Project

General

Profile

Feature #2758 » ZJ_ACSserver.cpp

yan hong, 06/19/2023 03:34 PM

 
1
#include "ZJ_ACSserver.h"
2
#include "mongoose.h"
3

    
4
//???ص?ǰʱ???  ?Ұѵ?ǰʱ??ת??Ϊtm?ṹ?浽????????У?ָ???ַ??
5
uint64_netmo GetTimeStamp(tm &localNow)
6
{
7
	uint64_netmo stampNow = 0;
8
	time_t timer;
9
	timer = time(NULL);
10
	stampNow = timer;
11
#ifdef WIN32
12
	localtime_s(&localNow, &timer);
13
#else
14
	localtime_r(&timer, &localNow);
15
#endif
16
	return stampNow;
17
}
18

    
19
//ʱ???ת??Ϊʱ??
20
std::string convertTime(long long timestamp)
21
{
22
	time_t tick = (time_t)(timestamp);//ת??ʱ??
23
	struct tm tm;
24
	char s[40];
25
	tm = *localtime(&tick);
26
	strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
27
	std::string str(s);
28
	return str;
29
}
30

    
31
void handler_getDotList(struct http_message *hm, int type,void * para) //type 1:??Ա??¼ 2?????ų?ʱ?澯 3???Ŵſ???
32
{
33
	putRdb *iotprotocol = (putRdb*)para;
34

    
35
	/* Get form variables */
36
	std::string receiveStr(hm->body.p, 0, hm->body.len);
37
	Json::Reader reader(Json::Features::strictMode());
38
	Json::Value root;
39
	//temp TODO
40
	uint64_netmo startTime = 0, endTime = 0;
41
	std::string outStr;
42
	if (reader.parse(receiveStr, root))
43
	{
44
		if (type == 1)
45
		{
46
			float temperature = 0;
47
			long long timestamp;
48
			std::string time;
49
			int status;
50
			long long personid = 0;
51
			std::string cardid;
52
			cardid.clear();
53

    
54
			temperature = root["FaceInfoList"][0]["Temperature"].asFloat();
55
			timestamp = root["FaceInfoList"][0]["Timestamp"].asDouble();
56
			time = convertTime(timestamp);
57
			status = root["LibMatInfoList"][0]["MatchStatus"].asInt();
58
			personid = root["LibMatInfoList"][0]["MatchPersonID"].asDouble();
59
			if (root["CardInfoList"].size() > 0)
60
			{
61
				cardid = root["CardInfoList"][0]["CardID"].asString();
62
			}
63

    
64
			
65
			int rtuno = iotprotocol->rtuno;
66
			iotprotocol->putAYc(rtuno, 1, temperature);		//?¶?
67
			iotprotocol->putAYx(rtuno, 1, status);			//ƥ??״̬
68

    
69

    
70
			if (status == 1)
71
			{
72
				char putlog[255] = { 0 };
73
				sprintf(putlog, "?Ž?????ɹ????ɼ?ʱ??(%s),????(%0.1f ??),????[%lld],????[%s]", time.c_str(), temperature, personid, cardid.c_str());
74
				iotprotocol->printLog(1, putlog);
75
				iotprotocol->putasoe(rtuno,2, personid, timestamp);
76
			}
77
			else
78
			{
79
				char putlog[255] = { 0 };
80
				sprintf(putlog, "?Ž?????ʧ?ܣ??ɼ?ʱ??(%s),??????(%d),????(%0.1f ??),????[%lld],????[%s]", time.c_str(), status, temperature, personid, cardid.c_str());
81
				iotprotocol->printLog(1, putlog);
82

    
83
				if (cardid.size() > 0 && personid == 0)
84
				{
85
					long long id;
86
					id = strtoll(cardid.c_str(), NULL, 10);
87
					iotprotocol->putasoe(rtuno, 3, id, timestamp);
88
				}
89
				else
90
				{
91
					iotprotocol->putasoe(rtuno, 3, personid, timestamp);
92
				}
93
				
94
			}
95
		}
96

    
97
		else if (type == 2)
98
		{
99
			long long timestamp;
100
			std::string time;
101
			timestamp = root["AlarmInfo"]["Timestamp"].asDouble();
102
			time = convertTime(timestamp);
103

    
104
			int rtuno = iotprotocol->rtuno;
105
			iotprotocol->putAYx(rtuno, 12, 1);
106

    
107
			char putlog[255] = { 0 };
108
			sprintf(putlog, "?Ž??????ŴŸ澯?ϱ???????ʱ??(%s),???ͣ??Ŵſ??ų?ʱ?澯", time.c_str());
109
			iotprotocol->printLog(1, putlog);
110
		}
111
		
112
		else
113
		{
114
			long long timestamp;
115
			std::string time;
116
			timestamp = root["StatusInfo"]["DoorStatus"]["Timestamp"].asDouble();
117
			time = convertTime(timestamp);
118

    
119
			int status = 0;
120
			status = root["StatusInfo"]["DoorStatus"]["Status"].asInt();
121

    
122
			int rtuno = iotprotocol->rtuno;
123
			iotprotocol->putAYx(rtuno, 11, status);
124

    
125
			char putlog[255] = { 0 };
126
			if (status == 0)
127
			{
128
				iotprotocol->putAYx(rtuno, 12, 0);
129
				sprintf(putlog, "?Ž??????ŴŸ澯?ϱ???????ʱ??(%s),???ͣ??ŴŹ??Ÿ澯", time.c_str());
130
			}
131
			else if (status == 1)
132
			{
133
				sprintf(putlog, "?Ž??????ŴŸ澯?ϱ???????ʱ??(%s),???ͣ??Ŵſ??Ÿ澯", time.c_str());
134
			}
135
			
136
			iotprotocol->printLog(1, putlog);
137
		}
138
	}
139
	root.clear();
140
	
141
}
142

    
143
static void ev_handler(mg_connection *nc, int ev, void *ev_data, void *pUser)
144
{
145
	struct http_message *hm = (struct http_message *) ev_data;
146

    
147
	switch (ev) {
148
	case MG_EV_HTTP_REQUEST:
149
		if (mg_vcmp(&hm->uri, "/LAPI/V1.0/System/Event/Notification/PersonVerification") == 0)
150
		{
151
			handler_getDotList(hm, 1,pUser);
152
		}
153
		else if (mg_vcmp(&hm->uri, "/LAPI/V1.0/Channel/0/Event/CommonAlarm/OverTimeAlarmOn") == 0)
154
		{
155
			handler_getDotList(hm, 2, pUser);
156
		}
157
		else if (mg_vcmp(&hm->uri, "/LAPI/V1.0/Channel/0/Event/Status/DoorStatus") == 0)
158
		{
159
			handler_getDotList(hm, 3, pUser);
160
		}
161
		break;
162
	default:
163
		break;
164
	}
165
}
166

    
167
bool RestfulServer::Start(const std::string &port/*,bool &bSpecialAccess*/, void *pParam)
168
{
169
	putRdb *iotprotocol = (putRdb*)pParam;
170
	char putlog[255] = { 0 };
171

    
172
	struct mg_mgr m_mgr;
173
	struct mg_connection *m_nc;
174
	struct mg_bind_opts m_bind_opts;
175
	const char *err_str;
176
	mg_mgr_init(&m_mgr, NULL);
177
	memset(&m_bind_opts, 0, sizeof(m_bind_opts));
178
	m_bind_opts.error_string = &err_str;
179
	m_nc = mg_bind_opt(&m_mgr, port.c_str(), ev_handler, pParam, m_bind_opts);
180
	if (m_nc == NULL) 
181
	{
182
		sprintf(putlog, "restful????˽???ʧ?? ?˿ڣ? %s, ???󷵻أ?%s   " ,port.c_str(),*m_bind_opts.error_string);
183
		iotprotocol->printLog(1, putlog);
184
		
185
		return false;
186
	}
187

    
188
	mg_set_protocol_http_websocket(m_nc);
189
	
190
	sprintf(putlog, "restful????˽?????ɡ???????ʼ?????Ž??????ϱ???Ϣ");
191
	iotprotocol->printLog(1, putlog);
192

    
193
	struct tm timeTm;
194
	static uint64_netmo nPreStamp = GetTimeStamp(timeTm);		//??ǰʱ???
195
	uint64_netmo nTimeNow = 0;
196
	for (;;)
197
	{
198
		mg_mgr_poll(&m_mgr, 1000);
199
		nTimeNow = GetTimeStamp(timeTm);		
200
		nPreStamp = nTimeNow;
201
	}
202

    
203
	mg_mgr_free(&m_mgr);
204
	return true;
205
}
206

    
207
bool RestfulServer::Close()
208
{
209
	return true;
210
}
211

    
(7-7/13)