123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659 |
- /*
- * 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 <list>
- #include <vector>
- #include <iterator>
- #include <stdlib.h>
- #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 */
|