#include "ZJ_ACSserver.h" #include "mongoose.h" //返回当前时间戳 且把当前时间转换为tm结构存到输入参数中(指针地址) uint64_netmo GetTimeStamp(tm &localNow) { uint64_netmo stampNow = 0; time_t timer; timer = time(NULL); stampNow = timer; #ifdef WIN32 localtime_s(&localNow, &timer); #else localtime_r(&timer, &localNow); #endif return stampNow; } //时间戳转化为时间 std::string convertTime(long long timestamp) { time_t tick = (time_t)(timestamp);//转换时间 struct tm tm; char s[40]; tm = *localtime(&tick); strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm); std::string str(s); return str; } void handler_getDotList(struct http_message *hm, int type,void * para) //type 1:人员记录 2:开门超时告警 3:门磁开关 { putRdb *iotprotocol = (putRdb*)para; /* Get form variables */ std::string receiveStr(hm->body.p, 0, hm->body.len); Json::Reader reader(Json::Features::strictMode()); Json::Value root; //temp TODO uint64_netmo startTime = 0, endTime = 0; std::string outStr; if (reader.parse(receiveStr, root)) { if (type == 1) { float temperature = 0; long long timestamp; std::string time; int status; long long personid = 0; std::string cardid; cardid.clear(); temperature = root["FaceInfoList"][0]["Temperature"].asFloat(); timestamp = root["FaceInfoList"][0]["Timestamp"].asDouble(); time = convertTime(timestamp); status = root["LibMatInfoList"][0]["MatchStatus"].asInt(); personid = root["LibMatInfoList"][0]["MatchPersonID"].asDouble(); if (root["CardInfoList"].size() > 0) { cardid = root["CardInfoList"][0]["CardID"].asString(); } int rtuno = iotprotocol->rtuno; iotprotocol->putAYc(rtuno, 1, temperature); //温度 iotprotocol->putAYx(rtuno, 1, status); //匹配状态 if (status == 1) { char putlog[255] = { 0 }; sprintf(putlog, "门禁核验成功:采集时间(%s),测温(%0.1f ℃),工号[%lld],卡号[%s]", time.c_str(), temperature, personid, cardid.c_str()); iotprotocol->printLog(1, putlog); iotprotocol->putasoe(rtuno,2, personid, timestamp); } else { char putlog[255] = { 0 }; sprintf(putlog, "门禁核验失败:采集时间(%s),核验结果(%d),测温(%0.1f ℃),工号[%lld],卡号[%s]", time.c_str(), status, temperature, personid, cardid.c_str()); iotprotocol->printLog(1, putlog); if (cardid.size() > 0 && personid == 0) { long long id; id = strtoll(cardid.c_str(), NULL, 10); iotprotocol->putasoe(rtuno, 3, id, timestamp); } else { iotprotocol->putasoe(rtuno, 3, personid, timestamp); } } } else if (type == 2) { long long timestamp; std::string time; timestamp = root["AlarmInfo"]["Timestamp"].asDouble(); time = convertTime(timestamp); int rtuno = iotprotocol->rtuno; iotprotocol->putAYx(rtuno, 12, 1); char putlog[255] = { 0 }; sprintf(putlog, "门禁主机门磁告警上报:报警时间(%s),类型:门磁开门超时告警", time.c_str()); iotprotocol->printLog(1, putlog); } else { long long timestamp; std::string time; timestamp = root["StatusInfo"]["DoorStatus"]["Timestamp"].asDouble(); time = convertTime(timestamp); int status = 0; status = root["StatusInfo"]["DoorStatus"]["Status"].asInt(); int rtuno = iotprotocol->rtuno; iotprotocol->putAYx(rtuno, 11, status); char putlog[255] = { 0 }; if (status == 0) { iotprotocol->putAYx(rtuno, 12, 0); sprintf(putlog, "门禁主机门磁告警上报:报警时间(%s),类型:门磁关门告警", time.c_str()); } else if (status == 1) { sprintf(putlog, "门禁主机门磁告警上报:报警时间(%s),类型:门磁开门告警", time.c_str()); } iotprotocol->printLog(1, putlog); } } root.clear(); } static void ev_handler(mg_connection *nc, int ev, void *ev_data, void *pUser) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/LAPI/V1.0/System/Event/Notification/PersonVerification") == 0) { handler_getDotList(hm, 1,pUser); } else if (mg_vcmp(&hm->uri, "/LAPI/V1.0/Channel/0/Event/CommonAlarm/OverTimeAlarmOn") == 0) { handler_getDotList(hm, 2, pUser); } else if (mg_vcmp(&hm->uri, "/LAPI/V1.0/Channel/0/Event/Status/DoorStatus") == 0) { handler_getDotList(hm, 3, pUser); } break; default: break; } } bool RestfulServer::Start(const std::string &port/*,bool &bSpecialAccess*/, void *pParam) { putRdb *iotprotocol = (putRdb*)pParam; char putlog[255] = { 0 }; struct mg_mgr m_mgr; struct mg_connection *m_nc; struct mg_bind_opts m_bind_opts; const char *err_str; mg_mgr_init(&m_mgr, NULL); memset(&m_bind_opts, 0, sizeof(m_bind_opts)); m_bind_opts.error_string = &err_str; m_nc = mg_bind_opt(&m_mgr, port.c_str(), ev_handler, pParam, m_bind_opts); if (m_nc == NULL) { sprintf(putlog, "restful服务端建立失败 端口: %s, 错误返回:%s " ,port.c_str(),*m_bind_opts.error_string); iotprotocol->printLog(1, putlog); return false; } mg_set_protocol_http_websocket(m_nc); sprintf(putlog, "restful服务端建立完成。。。开始接收门禁主机上报信息"); iotprotocol->printLog(1, putlog); struct tm timeTm; static uint64_netmo nPreStamp = GetTimeStamp(timeTm); //当前时间戳 uint64_netmo nTimeNow = 0; for (;;) { mg_mgr_poll(&m_mgr, 1000); nTimeNow = GetTimeStamp(timeTm); nPreStamp = nTimeNow; } mg_mgr_free(&m_mgr); return true; } bool RestfulServer::Close() { return true; }