Repository: ossrs/srs
File: trunk/src/protocol/srs_sip_stack.cpp (C++)

Code before:
        req->version = method_uri_ver.at(0);
        req->uri = req->from;

        vector<string> str = srs_string_split(req->to, "@");
        std::string ss = str.empty() ? "" : str.at(0);
        req->sip_auth_id = srs_string_replace(ss, "sip:", "");
  
    }else {//request first line text :MESSAGE sip:34020000002000000001@3402000000 SIP/2.0
        req->cmdtype = SrsSipCmdRequest;
        req->method= method_uri_ver.at(0);
        req->uri = method_uri_ver.size() > 1 ? method_uri_ver.at(1) : "";
        req->version = method_uri_ver.size() > 2 ? method_uri_ver.at(2) : "";

        vector<string> str = srs_string_split(req->from, "@");
        std::string ss = str.empty() ? "" : str.at(0);
        req->sip_auth_id = srs_string_replace(ss, "sip:", "");
    }

    req->sip_username =  req->sip_auth_id;

    //Content-Type: Application/MANSCDP+xml
    if (!strcasecmp(req->content_type.c_str(),"application/manscdp+xml")){
        //xml to map
        if ((err = parse_xml(body, req->xml_body_map, req->item_list)) != srs_success) {
            return srs_error_wrap(err, "sip parse xml");
        };
        
        //Response Cmd
        if (req->xml_body_map.find("Response") != req->xml_body_map.end()){
            std::string cmdtype = req->xml_body_map["Response@CmdType"];
            if (cmdtype == "Catalog"){
                //Response@DeviceList@Item@DeviceID:3000001,3000002
                std::vector<std::string> vec_device_id = srs_string_split(req->xml_body_map["Response@DeviceList@Item@DeviceID"], ",");
                //Response@DeviceList@Item@Status:ON,OFF
                std::vector<std::string> vec_device_status = srs_string_split(req->xml_body_map["Response@DeviceList@Item@Status"], ",");
                /*    edit by xbpeng 20201217  start */
                //Response@DeviceList@Item@Name:平湖出入口,地铁A口    
                std::vector<std::string> vec_device_name = srs_string_split(req->xml_body_map["Response@DeviceList@Item@Name"], ",");//GBK encoding
                //Response@DeviceList@Item@ParentID:64010000001110000001,64010000001110000002
                std::vector<std::string> vec_device_parent_id = srs_string_split(req->xml_body_map["Response@DeviceList@Item@ParentID"], ",");
                //Response@DeviceList@Item@Parental:1,0
                std::vector<std::string> vec_device_parental = srs_string_split(req->xml_body_map["Response@DeviceList@Item@Parental"], ",");
        
                //map key:"device_id" value:"status,parental,parentid,name" 
                for(int i=0 ; i< (int)vec_device_id.size(); i++){
                    std::string value = "";
                    if ((int)vec_device_status.size() == (int)vec_device_id.size()) {
                        value = value + vec_device_status.at(i);
                    }
                    value = value + ",";
                    if ((int)vec_device_parental.size() == (int)vec_device_id.size()) {

Review comment (suggestion):
As mentioned above, I have already improved this part on the develop branch.
The original interface only had id, status, and invite_status. I added name to facilitate displaying the name on the interface and performing operations. I also filtered out items without status.

On /api/v1/gb28181?action=sip_query_devicelist, I have created a comprehensive directory interface.

`TRANS_BY_GPT3`

Code after:
        };
        
        //Response Cmd
        if (req->xml_body_map.find("Response") != req->xml_body_map.end()){
            std::string cmdtype = req->xml_body_map["Response@CmdType"];
            if (cmdtype == "Catalog"){
                #if 0
                //Response@DeviceList@Item@DeviceID:3000001,3000002
                std::vector<std::string> vec_device_id = srs_string_split(req->xml_body_map["Response@DeviceList@Item@DeviceID"], ",");
                //Response@DeviceList@Item@Status:ON,OFF
                std::vector<std::string> vec_device_status = srs_string_split(req->xml_body_map["Response@DeviceList@Item@Status"], ",");
                 
                //map key:devicd_id value:status 
                for(int i=0 ; i< (int)vec_device_id.size(); i++){
                    std::string status = "";
                    if ((int)vec_device_status.size() > i) {
                        status = vec_device_status.at(i);
                    }
              
                    req->device_list_map[vec_device_id.at(i)] = status;
                }
                #endif
                for(int i=0 ; i< (int)req->item_list.size(); i++){
                    std::map<std::string, std::string> one_item = req->item_list.at(i);
                    std::string status;
                    if (one_item.find("Status") != one_item.end() && one_item.find("Name") != one_item.end()) {
                        status = one_item["Status"] + "," + one_item["Name"];
                    } else {
                        // if no Status, it's not a camera but a group
                        continue;
                    }
                    req->device_list_map[one_item["DeviceID"]] = status;
                }
            }else{
                //TODO: fixme
                srs_trace("sip: Response cmdtype=%s not processed", cmdtype.c_str());
            }
        } //Notify Cmd
        else if (req->xml_body_map.find("Notify") !=  req->xml_body_map.end()){
            std::string cmdtype = req->xml_body_map["Notify@CmdType"];
            if (cmdtype == "Keepalive"){
                //TODO: ????
                std::vector<std::string> vec_device_id = srs_string_split(req->xml_body_map["Notify@Info@DeviceID"], ",");
                for(int i=0; i< (int)vec_device_id.size(); i++){
                    //req->device_list_map[vec_device_id.at(i)] = "OFF";
                }
            }else{
               //TODO: fixme
               srs_trace("sip: Notify cmdtype=%s not processed", cmdtype.c_str());
            }
        }// end if(req->xml_body_map)