Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
6
Indexable
SECCAuthorizationSetup::SECCAuthorizationSetup(void* comm_session): StateSECC(comm_session, timeouts.V2G_EVCC_COMMUNICATION_SETUP_TIMEOUT, "SECCAuthorizationSetup")
{

};

void SECCAuthorizationSetup::process_message(void* message)
{
    
    char buffer[1024];
    int clientfd = getClientFd();
    std::cout << "SECCAuthorizationSetup " << clientfd << std::endl;
    
    while(true)
    {
        int received_bytes = receive_data_server(clientfd, buffer, sizeof(buffer));
        int sock_fd;
        std::cout << "11111111112" << endl;
        if(received_bytes > 0)
        {   
            buffer[received_bytes] = '\0';
            
            nlohmann::json j = nlohmann::json::parse(buffer);
            
            AuthorizationSetupReq received_req(j["SessionID"].get<std::string>(), j["Timestamp"].get<int>());
            received_req.Header.SessionID = j["SessionID"];
            received_req.Header.Timestamp = j["Timestamp"];
            sock_fd = j["Sockfd"];

            std::cout << "Received from client: " << received_req.Header.SessionID << std::endl;
            std::cout << "Received from client: " << received_req.Header.Timestamp << std::endl;
            std::cout << "Received from client: sock_fd " << sock_fd << std::endl;
            


            std::time_t now_time = std::time(nullptr);
            std::string(std::ctime(&now_time));

            ResponseCode responseCode;

            std::vector<AuthEnum> AuthorizationServices;

            PnCAuthSetupResParams PnC_ASResAuthorizationMode;
            EIMAuthSetupResParams EIM_ASResAuthorizationMode;

            AuthorizationSetupRes auth_setup_res(received_req.Header.SessionID,20 ,responseCode.OK,AuthorizationServices,true,PnC_ASResAuthorizationMode,EIM_ASResAuthorizationMode);
            
            nlohmann::json k ;
            k["SessionID"] = auth_setup_res.Header.SessionID;
            k["Timestamp"] = auth_setup_res.Header.Timestamp;
            k["ResponseCode"] = responseCode.OK;

            // for (const auto& service : AuthorizationServices) {
            //     k["AuthorizationServices"].push_back(static_cast<int>(service));
            // }   

            k["PnCAuthSetupResParams"] = true;
            k["EIMAuthSetupResParams"] = true;

            std::string serialized_data = k.dump();

            EVCCAuthorizationSetup evccAuthorizationSetup(nullptr,sock_fd,clientfd);
            std::cout << "while1" << std::endl;

            std::thread sendThread([&]{
                while(true)
                {
                    evccAuthorizationSetup.process_message(nullptr);
                    int bytes_sent = send_data_server(clientfd, serialized_data.c_str());
                    std::cout << "Sent bytes: " << bytes_sent << " to clientfd: " << clientfd << std::endl;
                    
                    std::this_thread::sleep_for(std::chrono::milliseconds(500));  //// 可以根據需要調整發送頻率
                }
            }
            );
            if(sendThread.joinable())
            {
                std::cout << "Thread created successfully" << std::endl;
            }else{
                std::cerr << "Failed to create thread!" << std::endl;
            }
            sendThread.detach();
            std::cout << "while2" << std::endl;
            break;   
        }else{
            std::cerr << "Error receiving data" << std::endl;
            break;
        }
        std::cout << "while0" << std::endl;
         
    }
    

};