ExpelDevice.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-02-16 15:25:46
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-07-21 16:23:13
  8. */
  9. #ifndef __EXPELDEVICE_H_
  10. #define __EXPELDEVICE_H_
  11. #include <iostream>
  12. #include <rapidjson/document.h>
  13. #include <rapidjson/rapidjson.h>
  14. #include <rapidjson/stringbuffer.h>
  15. #include <rapidjson/writer.h>
  16. #include "Util/SqlPool.h"
  17. #include "deviceBase.h"
  18. using namespace toolkit;
  19. using namespace std;
  20. enum DeviceType {
  21. DeviceGasV1 = 0,
  22. DeviceGasV2 = 1,
  23. DeviceUlt = 2,
  24. DevicePlayer = 3,
  25. DeviceGasV3 = 4
  26. };
  27. class ExpelDevice
  28. {
  29. public:
  30. int Id;
  31. std::string DeviceId;
  32. enum DeviceToken deviceToken;
  33. enum DeviceType deviceType;
  34. std::string UpdateTime;
  35. std::string ServerIp;
  36. std::string ServerCname;
  37. std::string ServerCpwd;
  38. public:
  39. ExpelDevice(){}
  40. ~ExpelDevice(){}
  41. /**
  42. * @description: json转换为对象
  43. * @param {string&} json
  44. * @return {*}
  45. */
  46. bool JsonToObject(std::string& json){
  47. rapidjson::Document doc;
  48. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  49. return false;
  50. }
  51. // get members
  52. const auto end = doc.MemberEnd();
  53. // json_type
  54. if(end == doc.FindMember("DeviceId") || !doc["DeviceId"].IsString()) {
  55. return false;
  56. }else{
  57. DeviceId = doc["DeviceId"].GetString();
  58. }
  59. // json_type
  60. if(end == doc.FindMember("DeviceToken") || !doc["DeviceToken"].IsString()) {
  61. return false;
  62. }else{
  63. deviceToken = (enum DeviceToken)atoi(doc["DeviceToken"].GetString());
  64. }
  65. if (end == doc.FindMember("DeviceType") || !doc["DeviceType"].IsString()) {
  66. return false;
  67. } else {
  68. deviceType = (enum DeviceType)atoi(doc["DeviceType"].GetString());
  69. }
  70. if (end == doc.FindMember("ServerIp") || !doc["ServerIp"].IsString()) {
  71. return false;
  72. } else {
  73. ServerIp = doc["ServerIp"].GetString();
  74. }
  75. if (end == doc.FindMember("ServerCname") || !doc["ServerCname"].IsString()) {
  76. return false;
  77. } else {
  78. ServerCname = doc["ServerCname"].GetString();
  79. }
  80. if (end == doc.FindMember("ServerCpwd") || !doc["ServerCpwd"].IsString()) {
  81. return false;
  82. } else {
  83. ServerCpwd = doc["ServerCpwd"].GetString();
  84. }
  85. return true;
  86. }
  87. /**
  88. * @description: object转换为sql
  89. * @param {*}
  90. * @return {*}
  91. */
  92. std::string ObjectToSql(){
  93. SqlStream sqlStream("insert into gsdDB.`DeviceList` (Id, DeviceId, DeviceToken, DeviceType, ServerIp, ServerCname, ServerCpwd, UpdateTime,ISDELETE)\
  94. values ('?','?','?','?','?','?','?','?','0')\
  95. on duplicate key update Id=VALUES(Id), DeviceId=VALUES(DeviceId),\
  96. DeviceToken=VALUES(DeviceToken), DeviceType=VALUES(DeviceType), \
  97. ServerIp=VALUES(ServerIp), ServerCname=VALUES(ServerCname), \
  98. ServerCpwd=VALUES(ServerCpwd), UpdateTime=VALUES(UpdateTime),\
  99. ISDELETE=VALUES(ISDELETE)");
  100. sqlStream << Id << DeviceId << deviceToken << deviceType << ServerIp << ServerCname << ServerCpwd << UpdateTime;
  101. return (std::string)sqlStream;
  102. }
  103. };
  104. #endif