/* * jhws.cpp * * Created on: 2022年4月22日 * Author: pengzc */ #include "jhws.h" #include "base/http_client.h" #include "base/json_object.h" #include "base/base.hpp" #include "json/json.h" #include "core/houseinfo.h" #include "utils/Log.h" #include #include #include #include #include "storage/StoragePreferences.h" #include "sip_config.h" #include "core/urlencode.h" #include "core/utilities.h" namespace jhws { const char* kBaseUrl = "http://wuye.jhws.top"; const char* bearer = "bearer "; std::string authorization = StoragePreferences::getString(SIP_JHWS_TOKEN, ""); int ErrorInfo(base::HttpResponse response) { Json::Reader reader; Json::Value root; LOGD("连接失败"); LOGD("返回体:%s",response.Body().c_str()); LOGD("状态码:%d", response.StatusCode()); LOGD("错误码:%d", response.ErrorCode()); LOGD("错误信息:%s", response.ErrorMessage().c_str()); LOGD("http信息:%s", response.ToString().c_str()); reader.parse(response.Body(), root); if(response.StatusCode() == 0 && response.ErrorCode() == 28) { return 1; } if(response.StatusCode() == 400 && response.ErrorCode() == 0) { if(root["code"].asInt() == 400) { return -1; } return root["code"].asInt(); } if(response.StatusCode() == 401 && response.ErrorCode() == 0 && root["code"].asInt() == 1001) { return 4; } return 2; } // int ErrorJson(base::HttpResponse response) { // Json::Reader reader; // Json::Value root; // // if(!reader.parse(response.Body(), root)) { // LOGD("解析错误不是Json格式!"); // return 2; // } // // if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) { // LOGD("解析数据体:%s", root.toStyledString().c_str()); // return 3; // } // return 2; // } int GetToken(const std::string& id, const std::string& secret, const std::string& encrypted, j::house_setting* profile) { base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/client/anon/client_token", ""); req.AppendHeader("client_id", id); req.AppendHeader("client_secret", secret); req.AppendHeader("encrypted", encrypted); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); Json::Reader reader; Json::Value root; //判断返回是否正确 if(response.StatusCode() != 200) { return ErrorInfo(response); } //判断是否为Json格式 if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data")){ return 2; } std::string accessToken; accessToken = root["data"]["accessToken"].asString(); profile->Token = bearer + std::string(accessToken); authorization = profile->Token; // StoragePreferences::putString(SIP_JHWS_TOKEN, profile->Token); return 0; } int GetCommunities(jhws::CommunityList& list, j::house_setting* profile) { if(authorization == ""){ int j = GetToken("778558024368394240", "d1330c96195c3b7b653cd60bb0ea5ded", "false", profile); if(j == 1) return 1; } // LOGD("author: %s", authorization.c_str()); std::string paramurl; encodeUrl(profile->name, paramurl); paramurl = "?code=" + std::string(profile->code) + "&name=" + std::string(paramurl); // LOGD("paramurl: %s", paramurl.c_str()); base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/community/grant/communitys" + std::string(paramurl), ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); if(response.StatusCode() != 200) { return ErrorInfo(response); } Json::Reader reader; Json::Value root; jhws::Community s; if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) { LOGD("解析数据体:%s", root.toStyledString().c_str()); return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.code = root["data"][i]["code"].asInt(); s.name = root["data"][i]["name"].asString(); s.communityId = root["data"][i]["id"].asString(); list.push_back(s); } return 0; } int GetAreas(jhws::AreaList& list, j::house_setting* profile) { std::string Paramurl; std::string communityId = profile->communityId; // LOGD("Area这里:%s", communityId.c_str()); base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/area/"+std::string(communityId)+"/areas", ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); if(response.StatusCode() != 200) { return ErrorInfo(response); } Json::Reader reader; Json::Value root; jhws::Area s; if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data") || root["data"][0].isNull()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.code = root["data"][i]["code"].asInt(); s.name = root["data"][i]["name"].asString(); s.areaId = root["data"][i]["id"].asString(); list.push_back(s); } return 0; } int GetBuildings(BuildingList& list, j::house_setting* profile) { std::string Paramurl; std::string areaId = profile->areaId; // LOGD("build这里:%s", areaId.c_str()); base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/building/"+std::string(areaId)+"/buildings", ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); // LOGD("得到的数据:%s", response.Body().c_str()); if(response.StatusCode() != 200) { return ErrorInfo(response); } Json::Reader reader; Json::Value root; jhws::Building s; if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data") || root["data"][0].isNull()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.code = root["data"][i]["code"].asInt(); s.name = root["data"][i]["name"].asString(); s.buildId = root["data"][i]["id"].asString(); list.push_back(s); } return 0; } int GetUnits(UnitList& list, j::house_setting* profile) { std::string buildId = profile->buildId; base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/unit/"+std::string(buildId)+"/units", ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); // LOGD("得到的数据:%s", response.Body().c_str()); if(response.StatusCode() != 200) { return ErrorInfo(response); } Json::Reader reader; Json::Value root; jhws::Unit s; if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data") || root["data"][0].isNull()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.code = root["data"][i]["code"].asInt(); s.name = root["data"][i]["name"].asString(); s.unitId = root["data"][i]["id"].asString(); list.push_back(s); } return 0; } int GetFloors(FloorList& list, j::house_setting* profile) { std::string unitId = profile->unitId; base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/floor/"+std::string(unitId)+"/floors", ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); // LOGD("得到的数据:%s", response.Body().c_str()); if(response.StatusCode() != 200) { return ErrorInfo(response); } Json::Reader reader; Json::Value root; jhws::Floor s; if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data") || root["data"][0].isNull()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.code = root["data"][i]["code"].asInt(); s.name = root["data"][i]["name"].asString(); s.floorId = root["data"][i]["id"].asString(); list.push_back(s); } return 0; } int GetRooms(RoomList& list, j::house_setting* profile) { std::string floorId = profile->floorId; base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/room/"+std::string(floorId)+"/rooms", ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); // LOGD("得到的数据:%s", response.Body().c_str()); if(response.StatusCode() != 200) { return ErrorInfo(response); } Json::Reader reader; Json::Value root; jhws::Room s; if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } if(!root.isMember("data") || root["data"][0].isNull()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.code = root["data"][i]["code"].asInt(); s.name = root["data"][i]["name"].asString(); s.roomId = root["data"][i]["id"].asString(); list.push_back(s); } return 0; } int BindDevice(j::house_setting* profile) { Json::Value body; Json::FastWriter writer; body["uid"] = GetDeviceUid(); body["versionNumber"] = StoragePreferences::getString(JHWS_VERSION_NUMBER, to_string(GetVersionNo())); body["type"] = "indoor"; body["hardwareType"] = "linux"; body["communityId"] = profile->communityId; body["areaId"] = profile->areaId; body["buildingId"] = profile->buildId; body["unitId"] = profile->unitId; body["floorId"] = profile->floorId; body["roomId"] = profile->roomId; body["password"] = profile->password; writer.write(body); LOGD("body: %s", body.toStyledString().c_str()); base::HttpClient client; base::HttpRequest req("POST", std::string(kBaseUrl) + "/api/device/grant/" + std::string(profile->replace), body.toStyledString()); req.AppendHeader("Authorization", authorization); req.ContentType("application/json"); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); Json::Reader reader; Json::Value root; if(response.StatusCode() != 200) { return ErrorInfo(response); } if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); return 2; } //LOGD("收到返回信息:%s", response.Body().c_str()); if(!root.isMember("code") && root["code"].isNull()) { return 3; } return 0; } int GetDevice(DeviceList& list, j::house_setting* profile) { std::string paramurl; std::string type = "indoor"; paramurl = "?type=" + std::string(type) + "&communityId=" + std::string(profile->communityId) + "&unitId=" + std::string(profile->unitId); base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/device/grant/gates" + std::string(paramurl), ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); Json::Reader reader; Json::Value root; jhws::Device s; if(response.StatusCode() != 200) { return ErrorInfo(response); } if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!"); LOGD("解析错误不是Json格式!%s", response.Body().c_str()); return 2; } if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { if(root["data"][i]["type"].asString() == "outdoor") { s.type = root["data"][i]["type"].asString(); s.deviceNumber = root["data"][i]["deviceNumber"].asString(); s.areaname = root["data"][i]["areaName"].asString(); s.buildname = root["data"][i]["buildingName"].asString(); s.unitname = root["data"][i]["unitName"].asString(); s.communitycode = root["data"][i]["communityCode"].asInt(); s.areacode = root["data"][i]["areaCode"].asInt(); s.buildcode = root["data"][i]["buildingCode"].asInt(); s.unitcode = root["data"][i]["unitCode"].asInt(); list.push_back(s); } else if(root["data"][i]["type"].asString() == "wall") { s.type = root["data"][i]["type"].asString(); s.deviceNumber = root["data"][i]["deviceNumber"].asString(); s.areaname = root["data"][i]["areaName"].asString(); s.communitycode = root["data"][i]["communityCode"].asInt(); s.areacode = root["data"][i]["areaCode"].asInt(); list.push_back(s); } } return 0; } int GetUpdate(Update& list) { std::string paramurl; std::string uid = GetDeviceUid(); // LOGD("deviceuid: %s", uid.c_str()); std::string communityId = StoragePreferences::getString(SIP_JHWS_COMMUNITYID, ""); std::string roomId = StoragePreferences::getString(SIP_JHWS_ROOMID, ""); paramurl = "?uid="+ std::string(uid) + "&deviceType=indoor&communityId=" + std::string(communityId) + "&roomId=" + std::string(roomId); // LOGD("updateTime: %s", roomId.c_str()); base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/update/grant" + std::string(paramurl), ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); Json::Reader reader; Json::Value root; if(response.StatusCode() != 200) { return ErrorInfo(response); } if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!%s", response.Body().c_str()); return 2; } if(!root.isMember("data") || root["data"].isNull()) { return 3; } long long num1 = (long long)root["data"]["device"].asDouble(); long long num2 = (long long)root["data"]["notice"].asDouble(); list.device = to_string(num1); list.notice = to_string(num2); // LOGD("---------1---------"); return 0; } int GetMessage(MessageList& list) { // LOGD("---------2---------"); std::string paramurl; std::string communityId = StoragePreferences::getString(SIP_JHWS_COMMUNITYID, ""); std::string roomId = StoragePreferences::getString(SIP_JHWS_ROOMID, ""); std::string updateTime = StoragePreferences::getString(SIP_JHWS_UPDATETIME, "0"); // std::string updateTime = "0"; paramurl = "?communityId=" + std::string(communityId) + "&roomId=" + std::string(roomId) + "&updateTime=" + std::string(updateTime); // LOGD("updateTime: %s", updateTime.c_str()); base::HttpClient client; base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/notice/grant/notices" + std::string(paramurl), ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); Json::Reader reader; Json::Value root; jhws::Message s; if(response.StatusCode() != 200) { return ErrorInfo(response); } if(!reader.parse(response.Body(), root)) { LOGD("解析错误不是Json格式!%s", response.Body().c_str()); return 2; } if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) { return 3; } for(unsigned int i = 0; i < root["data"].size(); i++) { s.content = root["data"][i]["content"].asString(); s.title = root["data"][i]["title"].asString(); long long num = (long long)root["data"][i]["updateTime"].asDouble(); s.updateTime = to_string(num); list.push_back(s); } StoragePreferences::putString(SIP_JHWS_UPDATETIME, list.back().updateTime); // LOGD("---------3---------"); return 0; } int Alarm(j::house_setting* profile) { std::string paramurl; std::string type = "indoor"; paramurl = "?type=" + std::string(type) + "&communityId=" + std::string(profile->communityId) + "&roomId=" + std::string(profile->roomId); base::HttpClient client; base::HttpRequest req("POST", std::string(kBaseUrl) + "/api/alarm/grant" + std::string(paramurl), ""); req.AppendHeader("Authorization", authorization); client.SetConnectionTimeout(2000); client.SetTimeout(3000); base::HttpResponse response = client.Do(req); Json::Reader reader; Json::Value root; if(response.StatusCode() != 200) { return ErrorInfo(response); } if(!reader.parse(response.Body(), root)) { return 2; } if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) { return 3; } if(root.isMember("data")) { if(root["data"].isMember("type")) { std::string type = root["data"]["type"].asString(); } if(root["data"].isMember("communityId")) { std::string communityId = root["data"]["communityId"].asString(); } if(root["data"].isMember("unitId")) { std::string unitId = root["data"]["unitId"].asString(); } } else { LOGD("错误没有名为data的key!"); } return 0; } } /* namespace jhws */