/*
 * @Author: 张威
 * @Date: 2022-01-27 10:27:26
 * @LastEditors: your name
 * @LastEditTime: 2022-02-22 15:01:51
 * @Description: gRPC接口
 * @FilePath: \lirs\code\GrpcEngine\GrpcEngine.cpp
 * 
 * Copyright © 2022 <Shandong Matrix Software Engineering Co., Ltd>
 */
#include "GrpcEngine.h"

using namespace ai_matrix;

GrpcEngine::GrpcEngine() {}

GrpcEngine::~GrpcEngine() {}

APP_ERROR GrpcEngine::Init()
{
    strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";

    LogInfo << "GrpcEngine Init ok";
    return APP_ERR_OK;
}

APP_ERROR GrpcEngine::DeInit()
{
    LogInfo << "GrpcEngine DeInit ok";
    return APP_ERR_OK;
}

APP_ERROR GrpcEngine::Process()
{
    int ret = APP_ERR_OK;
    while (!isStop_)
    {
        usleep(1000);
    }
    return APP_ERR_OK;
}

/**
 * 函数可以重复进入，阻塞不会影响其他请求到来
 * */

/**
 * 识别状态控制-来车
 * @description: 
 * @param {ServerContext} *context
 * @param {Request} *request
 * @param {Reply} *reply
 * @return {*}
 */
Status GrpcEngine::Come(ServerContext *context, const Request *request, Reply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现
    std::shared_ptr<std::string> pstrCommand = std::make_shared<std::string>("start");
    outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pstrCommand));
    return Status::OK;
}

/**
 * 识别状态控制-车走
 * @description: 
 * @param {ServerContext} *context
 * @param {Request} *request
 * @param {Reply} *reply
 * @return {*}
 */
Status GrpcEngine::Leave(ServerContext *context, const Request *request, Reply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现
    std::shared_ptr<std::string> pstrCommand = std::make_shared<std::string>("end");
    outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pstrCommand));
    
    return Status::OK;
}

/**
 * 识别状态控制-停车
 * @description: 
 * @param {ServerContext} *context
 * @param {Request} *request
 * @param {Reply} *reply
 * @return {*}
 */
Status GrpcEngine::TemporaryStop(ServerContext *context, const Request *request, Reply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现
    
    return Status::OK;
}

/**
 * 识别状态控制-倒车
 * @description: 
 * @param {ServerContext} *context
 * @param {Request} *request
 * @param {Reply} *reply
 * @return {*}
 */
Status GrpcEngine::Reversing(ServerContext *context, const Request *request, Reply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现
    std::shared_ptr<std::string> pstrCommand = std::make_shared<std::string>("back");
    outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pstrCommand));

    return Status::OK;
}

/**
 * 查看“工作站设置”
 * @description: 
 * @param {ServerContext} *context
 * @param {QueryWorkstationRequest} *request
 * @param {QueryWorkstationReply} *reply
 * @return {*}
 */
Status GrpcEngine::QueryWorkstation(ServerContext *context, const QueryWorkstationRequest *request, QueryWorkstationReply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现看后续是否要改
    
    try
    {
        reply->set_name_no(MyYaml::GetIns()->GetStringValue("atlas_poundno"));
    }
    catch(const std::exception& e)
    {
        reply->set_error_msg("查询失败！配置参数读取异常！");
    }

    return Status::OK;
}

/**
 * 修改“工作站设置”
 * @description: 
 * @param {ServerContext} *context
 * @param {UpdateWorkstationRequest} *request
 * @param {UpdateWorkstationReply} *reply
 * @return {*}
 */
Status GrpcEngine::UpdateWorkstation(ServerContext *context, const UpdateWorkstationRequest *request, UpdateWorkstationReply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现

    return Status::OK;
}

/**
 * 查询摄像头配置
 * @description: 
 * @param {ServerContext} *context
 * @param {QueryCameraRequest} *request
 * @param {QueryCameraReply} *reply
 * @return {*}
 */
Status GrpcEngine::QueryCamera(ServerContext *context, const QueryCameraRequest *request, QueryCameraReply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现看后续是否要改
    ai_matrix::DataSourceConfig dataSourceConfig_;
    dataSourceConfig_ = MyYaml::GetIns()->GetDataSourceConfigById(request->channel_id()); //获取摄像机参数

    reply->set_channel_id(request->channel_id());
    reply->set_use(dataSourceConfig_.bUse);
    reply->set_url(dataSourceConfig_.strUrl);
    reply->set_skipinterval(dataSourceConfig_.iSkipInterval);
    reply->set_direction(dataSourceConfig_.iDirection);
    reply->set_left_first(dataSourceConfig_.iLeftFirst);
    reply->set_right_first(dataSourceConfig_.iRightFirst);

    return Status::OK;
}

/**
 * 修改摄像头配置
 * @description: 
 * @param {ServerContext} *context
 * @param {UpdateCameraRequest} *request
 * @param {UpdateCameraReply} *reply
 * @return {*}
 */
Status GrpcEngine::UpdateCamera(ServerContext *context, const UpdateCameraRequest *request, UpdateCameraReply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现

    
    return Status::OK;
}

/**
 * 查询FTP的配置
 * @description: 
 * @param {ServerContext} *context
 * @param {QueryFTPRequest} *request
 * @param {QueryFTPReply} *reply
 * @return {*}
 */
Status GrpcEngine::QueryFTP(ServerContext *context, const QueryFTPRequest *request, QueryFTPReply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现看后续是否需要改动
    try
    {
        reply->set_open(MyYaml::GetIns()->GetIntValue("gc_ftp_open"));
        reply->set_username(MyYaml::GetIns()->GetStringValue("gc_ftp_username"));
        reply->set_password(MyYaml::GetIns()->GetStringValue("gc_ftp_password"));
        reply->set_ip(MyYaml::GetIns()->GetStringValue("gc_ftp_ip"));
        reply->set_port(MyYaml::GetIns()->GetIntValue("gc_ftp_port"));
        reply->set_image_path(MyYaml::GetIns()->GetStringValue("gc_ftp_image_path"));
        reply->set_quit_time(MyYaml::GetIns()->GetIntValue("gc_ftp_quit_time"));
    }
    catch (const std::exception& e)
    {
        reply->set_error_msg("查询失败！配置参数读取异常！");
    }
    
    return Status::OK;
}

/**
 * 修改FTP的配置
 * @description: 
 * @param {ServerContext} *context
 * @param {UpdateFTPRequest} *request
 * @param {UpdateFTPReply} *reply
 * @return {*}
 */
Status GrpcEngine::UpdateFTP(ServerContext *context, const UpdateFTPRequest *request, UpdateFTPReply *reply)
{
    //验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

    //TODO(zhangwei):具体实现

    
    return Status::OK;
}

/**
 * 重启识别程序
 * @description: 
 * @param {ServerContext} *context
 * @param {Request} *request
 * @param {Reply} *reply
 * @return {*}
 */
Status GrpcEngine::RebootAI(ServerContext *context, const Request *request, Reply *reply)
{
	LogInfo << "Restart >>> head:" << request->head();

	//验证请求头
	if (request->head() != this->HEAD)
	{
		reply->set_error_msg(" head: \"" + std::to_string(request->head()) + "\" permission denied !");
		return Status::OK;
	}

	pthread_t id;
	int ret = pthread_create(&id, NULL, system_restart, NULL);

	if (ret != 0)
	{
		reply->set_error_msg("小站重启失败！");
	}

	return Status::OK;
}

// 线程的运行函数
void* GrpcEngine::system_restart(void* args)
{

	sleep(2);

	system("reboot");
	return 0;
}