Files
webcamservice/WebSocketService.cpp
2024-12-15 16:18:16 +08:00

297 lines
15 KiB
C++
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "WebSocketService.h"
#include "GetCameraDeviceID.h"
#include "VideoProc.h"
#include "json.hpp"
WebSocketService* WebSocketService::getInstance()
{
static WebSocketService _instance;
return &_instance;
}
WebSocketService::WebSocketService()
{
ACTIONS[u8"open_cam"] = action_open_cam;
ACTIONS[u8"close_cam"] = action_close_cam;
ACTIONS[u8"start_check_face"] = action_start_check_face;
ACTIONS[u8"stop_check_face"] = action_stop_check_face;
ACTIONS[u8"take_photo"] = action_take_photo;
ACTIONS[u8"start_auto_take_photo"] = action_start_auto_take_photo;
ACTIONS[u8"stop_auto_take_photo"] = action_stop_auto_take_photo;
ACTIONS[u8"face_image_compare"] = action_face_compare;
ACTIONS[u8"get_camera"] = action_get_camera;
MSG[err_unknow] = u8"未知的错误";
MSG[err_ok] = u8"操作成功";
MSG[err_is_opened] = u8"摄像头已经被打开";
MSG[err_is_closed] = u8"摄像头已经被关闭";
MSG[err_no_face] = u8"未检测到人脸";
MSG[err_multiple_face] = u8"检测到多个人脸";
MSG[err_time_out] = u8"操作超时";
MSG[err_open_cam] = u8"打开摄像头失败";
MSG[err_keep_face] = u8"请保持正脸状态";
MSG[err_ok_open_cam] = u8"打开摄像头成功";
MSG[err_ok_close_cam] = u8"关闭摄像头成功";
}
WebSocketService::~WebSocketService()
{
}
void WebSocketService::Start(uint16_t port)
{
static auto fun = [this](uint16_t port) {
try {
m_web_server.set_access_channels(websocketpp::log::alevel::none);
m_web_server.clear_access_channels(websocketpp::log::alevel::none);
m_web_server.init_asio();
m_web_server.set_open_handler(bind(&WebSocketService::OnWebSocketOpen, this, &m_web_server, ::_1));
m_web_server.set_close_handler(bind(&WebSocketService::OnWebSocketClose, this, &m_web_server, _1));
m_web_server.set_message_handler(bind(&WebSocketService::OnWebSocketMessage, this, &m_web_server, _1, _2));
m_web_server.set_max_http_body_size(1024 * 1024);
m_web_server.set_max_message_size(1024 * 1024);
m_web_server.listen(port);
m_web_server.start_accept();
m_web_server.run();
}
catch (websocketpp::exception const& e) {
std::cout << e.what() << std::endl;
}
catch (const std::exception & e) {
std::cout << e.what() << std::endl;
}
catch (...) {
std::cout << "未知异常" << std::endl;
}
};
std::thread(fun, port).detach();
}
void WebSocketService::Stop()
{
m_web_server.stop();
}
void WebSocketService::OnWebSocketOpen(websocketsvr *server, websocketpp::connection_hdl hdl)
{
std::cout << "New Connection" << std::endl;
m_hdl = hdl;
}
void WebSocketService::OnWebSocketClose(websocketsvr *server, websocketpp::connection_hdl hdl)
{
std::cout << "OnWebSocketClose" << std::endl;
// if (m_connection == hdl.lock().get())
// {
// m_connection = nullptr;
// }
//OnClose();
}
void WebSocketService::OnWebSocketMessage(websocketsvr *server, websocketpp::connection_hdl hdl, message_ptr msg)
{
std::string input = msg->get_payload();
//NowLogStringAEx("%s", input.c_str());
m_hdl = hdl;
//std::cout << "WebSocketService::OnWebSocketMessage => " << input << std::endl;
auto data = nlohmann::json::parse(input);
try
{
std::string action_name = data["action"].get<std::string>();
int action = ACTIONS[action_name];
nlohmann::json root;
root["action"] = action_name;
root["action_code"] = action;
switch ((MSG_ACTION)action) {
case action_open_cam: {//""
int cam_id = data["camera_id"].get<int>();
ERR_MSG ret = (ERR_MSG)VideoProc::getInstance()->openCam(cam_id);
root["err_code"] = ret;
root["err_msg"] = MSG[ret];
server->send(hdl.lock(), root.dump(), websocketpp::frame::opcode::text);
break;
}
case action_close_cam: {//
ERR_MSG ret = (ERR_MSG)VideoProc::getInstance()->close();
root["err_code"] = ret;
root["err_msg"] = MSG[ret];
server->send(hdl.lock(), root.dump(), websocketpp::frame::opcode::text);
break;
}
case action_start_check_face: {//
std::string input_img = data["input_img"].get<std::string>();
int time_out = data["time_out"].get<int>();
VideoProc::getInstance()->startCheckFace(input_img, time_out);
break;
}
case action_stop_check_face: {//
VideoProc::getInstance()->stopCheckFace();
break;
}
case action_take_photo: {//
VideoProc::getInstance()->TakePhoto();
break;
}
case action_start_auto_take_photo: {//
int nTime = data["time_out"].get<int>();
VideoProc::getInstance()->StartAutoTakePhoto(nTime);
break;
}
case action_stop_auto_take_photo: {//
VideoProc::getInstance()->StopAutoTakePhoto();
break;
}
case action_face_compare: {//
std::string img1 = data["image1"].get<std::string>();
std::string img2 = data["image2"].get<std::string>();
VideoProc::getInstance()->FaceCompare(img1, img2);
break;
}
case action_get_camera: {//
vector<CameraDev> cameras;
EnumCameraDevices(cameras);
nlohmann::json root;
auto devs = nlohmann::json::array();
for (size_t i = 0; i < cameras.size(); i++)
{
nlohmann::json oj;
oj["id"] = cameras[i].devID;
oj["name"] = cameras[i].devName;
devs.push_back(oj);
}
root["err_code"] = err_ok;
root["err_msg"] = MSG[err_ok];;
root["result"] = devs;
server->send(hdl.lock(), root.dump(), websocketpp::frame::opcode::text);
break;
}
default: {
break;
}
}
}
catch (const std::exception& e){
std::cout << "input err => " << e.what() << std::endl;
}
/*
Json::Reader doc;
Json::Value root;
try
{
if (doc.parse(input, root))
{
std::string action = root["action"].asString();
int action_code = 1;
switch (action_code)
{
case msg_open_cam:
{
// if (CMainDlg::m_this->m_bCamRun)
// {
// WebSocketRespon(server, hdl, err_is_opened);
// }
// else
// {
// std::thread(&CMainDlg::ShowThread, CMainDlg::m_this, server, hdl).detach();
// }
break;
}
case msg_close_cam:
{
// if (CMainDlg::m_this->m_bCamRun)
// {
// CMainDlg::m_this->CloseCam();
// WebSocketRespon(server, hdl, err_ok_close_cam);
// }
// else
// {
// WebSocketRespon(server, hdl, err_is_closed);
// }
break;
}
case msg_exit_app:
{
exit(0);
break;
}
default:
break;
}
}
}
catch (websocketpp::exception const& e)
{
std::cout << "line: " << __LINE__ << "\t" << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << "line: " << __LINE__ << "\t" << e.what() << std::endl;
}
catch (...)
{
}
*/
}
void WebSocketService::WebSocketRespon(websocketsvr *server, websocketpp::connection_hdl& hdl, ERR_MSG err_code, std::string param)
{
try
{
if (server != nullptr)
{
int len = param.length() + 512;
char* ret = new char[len];
memset(ret, 0, len);
server->send(hdl.lock(), std::string(ret), websocketpp::frame::opcode::binary);
delete ret;
ret = nullptr;
}
}
catch (websocketpp::exception const& e)
{
//NowLogStringAEx("%s ", e.what());
}
catch (const std::exception& e)
{
//NowLogStringAEx("%s ", e.what());
}
catch (...)
{
}
}
void WebSocketService::SendMsg(std::string msg)
{
try
{
std::lock_guard<std::mutex> lock(m_mt_send);
m_web_server.send(m_hdl.lock(), msg.c_str(), msg.length(), websocketpp::frame::opcode::TEXT);
}
catch (websocketpp::exception const& e)
{
std::cout << "line: " << __LINE__ << "\t" << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << "line: " << __LINE__ << "\t" << e.what() << std::endl;
}
}