ExpelDevice.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-28 14:19:17
  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. DevicePlayerV5 = 6
  29. };
  30. class ExpelDevice
  31. {
  32. public:
  33. int Id;
  34. std::string DeviceId;
  35. enum ContorlGear::DeviceTools tool = ContorlGear::DeviceTools::serial;
  36. enum DeviceType deviceType;
  37. std::string UpdateTime;
  38. std::string ServerIp;
  39. std::string ServerCname;
  40. std::string ServerCpwd;
  41. public:
  42. ExpelDevice(){}
  43. ~ExpelDevice(){}
  44. /**
  45. * @description: json转换为对象
  46. * @param {string&} json
  47. * @return {*}
  48. */
  49. bool JsonToObject(std::string& json){
  50. rapidjson::Document doc;
  51. doc.Parse(json.c_str());
  52. if(!doc.IsObject()){
  53. return false;
  54. }
  55. // get members
  56. const auto end = doc.MemberEnd();
  57. // json_type
  58. if(end == doc.FindMember("DeviceId") || !doc["DeviceId"].IsString()) {
  59. return false;
  60. }else{
  61. DeviceId = doc["DeviceId"].GetString();
  62. }
  63. // json_type
  64. if(end == doc.FindMember("DeviceToken") || !doc["DeviceToken"].IsString()) {
  65. return false;
  66. }else{
  67. tool = (enum ContorlGear::DeviceTools)atoi(doc["DeviceToken"].GetString());
  68. }
  69. if (end == doc.FindMember("DeviceType") || !doc["DeviceType"].IsString()) {
  70. return false;
  71. } else {
  72. deviceType = (enum DeviceType)atoi(doc["DeviceType"].GetString());
  73. }
  74. if (end == doc.FindMember("ServerIp") || !doc["ServerIp"].IsString()) {
  75. return false;
  76. } else {
  77. ServerIp = doc["ServerIp"].GetString();
  78. }
  79. if (end == doc.FindMember("ServerCname") || !doc["ServerCname"].IsString()) {
  80. return false;
  81. } else {
  82. ServerCname = doc["ServerCname"].GetString();
  83. }
  84. if (end == doc.FindMember("ServerCpwd") || !doc["ServerCpwd"].IsString()) {
  85. return false;
  86. } else {
  87. ServerCpwd = doc["ServerCpwd"].GetString();
  88. }
  89. return true;
  90. }
  91. /**
  92. * @description: object转换为sql
  93. * @param {*}
  94. * @return {*}
  95. */
  96. std::string ObjectToSql(){
  97. SqlStream sqlStream("insert into gsdDB.`DeviceList` (Id, DeviceId, DeviceToken, DeviceType, ServerIp, ServerCname, ServerCpwd, UpdateTime,ISDELETE)\
  98. values ('?','?','?','?','?','?','?','?','0')\
  99. on duplicate key update Id=VALUES(Id), DeviceId=VALUES(DeviceId),\
  100. DeviceToken=VALUES(DeviceToken), DeviceType=VALUES(DeviceType), \
  101. ServerIp=VALUES(ServerIp), ServerCname=VALUES(ServerCname), \
  102. ServerCpwd=VALUES(ServerCpwd), UpdateTime=VALUES(UpdateTime),\
  103. ISDELETE=VALUES(ISDELETE)");
  104. sqlStream << Id << DeviceId << tool << deviceType << ServerIp << ServerCname << ServerCpwd << UpdateTime;
  105. return (std::string)sqlStream;
  106. }
  107. };
  108. #endif