ExpelDevice.h 3.4 KB

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