ExpelDevice.h 3.5 KB

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