meterComm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * MeterDriver.c
  3. *
  4. * Created on: 2021/6/4
  5. * Author: foluswen
  6. */
  7. #include "meterComm.h"
  8. #include "curlApi.h"
  9. #include "SystemLogMessage.h"
  10. #include "json-c/json.h"
  11. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  12. unsigned char meterModel = 0;
  13. char meterApiAddress[128];
  14. /**
  15. * Meter driver initialize
  16. * @param model
  17. */
  18. void meterInitialize(uint8_t model)
  19. {
  20. meterModel = model;
  21. switch(meterModel)
  22. {
  23. case METER_MODEL_LEM_L18005A:
  24. default:
  25. /*
  26. * TODO:
  27. * 1. Check meter actual API address.
  28. */
  29. //sprintf(meterApiAddress, "http://192.168.1.2/livemeasure");
  30. sprintf(meterApiAddress, "https://foluswen.com/tmate/livemeasure.php"); // Simulator by web server
  31. break;
  32. }
  33. }
  34. /**
  35. * Output voltage read
  36. * @param meter_info
  37. * @return
  38. */
  39. int readVoltage(Meter_Info *meter_info)
  40. {
  41. int result = FAIL;
  42. uint8_t data[4096];
  43. switch(meterModel)
  44. {
  45. case METER_MODEL_LEM_L18005A:
  46. default:
  47. if(httpGet((uint8_t*)meterApiAddress, data))
  48. {
  49. json_object *livemeasure;
  50. livemeasure = json_tokener_parse((char*)data);
  51. if(!is_error(livemeasure))
  52. {
  53. if(json_object_object_get(livemeasure, "voltage") != NULL)
  54. meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
  55. if(json_object_object_get(livemeasure, "current") != NULL)
  56. meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
  57. if(json_object_object_get(livemeasure, "power") != NULL)
  58. meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
  59. if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
  60. meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
  61. if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
  62. meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
  63. result = PASS;
  64. }
  65. json_object_put(livemeasure);
  66. }
  67. break;
  68. }
  69. return result;
  70. }
  71. /**
  72. * Output current read
  73. * @param meter_info
  74. * @return
  75. */
  76. int readCurrent(Meter_Info *meter_info)
  77. {
  78. int result = FAIL;
  79. uint8_t data[4096];
  80. switch(meterModel)
  81. {
  82. case METER_MODEL_LEM_L18005A:
  83. default:
  84. if(httpGet((uint8_t*)meterApiAddress, data))
  85. {
  86. json_object *livemeasure;
  87. livemeasure = json_tokener_parse((char*)data);
  88. if(!is_error(livemeasure))
  89. {
  90. if(json_object_object_get(livemeasure, "voltage") != NULL)
  91. meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
  92. if(json_object_object_get(livemeasure, "current") != NULL)
  93. meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
  94. if(json_object_object_get(livemeasure, "power") != NULL)
  95. meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
  96. if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
  97. meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
  98. if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
  99. meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
  100. result = PASS;
  101. }
  102. json_object_put(livemeasure);
  103. }
  104. break;
  105. }
  106. return result;
  107. }
  108. /**
  109. * Output power read
  110. * @param meter_info
  111. * @return
  112. */
  113. int readPower(Meter_Info *meter_info)
  114. {
  115. int result = FAIL;
  116. uint8_t data[4096];
  117. switch(meterModel)
  118. {
  119. case METER_MODEL_LEM_L18005A:
  120. default:
  121. if(httpGet((uint8_t*)meterApiAddress, data))
  122. {
  123. json_object *livemeasure;
  124. livemeasure = json_tokener_parse((char*)data);
  125. if(!is_error(livemeasure))
  126. {
  127. if(json_object_object_get(livemeasure, "voltage") != NULL)
  128. meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
  129. if(json_object_object_get(livemeasure, "current") != NULL)
  130. meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
  131. if(json_object_object_get(livemeasure, "power") != NULL)
  132. meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
  133. if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
  134. meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
  135. if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
  136. meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
  137. result = PASS;
  138. }
  139. json_object_put(livemeasure);
  140. }
  141. break;
  142. }
  143. return result;
  144. }
  145. /**
  146. * Export & import totalize energy read
  147. * @param meter_info
  148. * @return
  149. */
  150. int readEnergy(Meter_Info *meter_info)
  151. {
  152. int result = FAIL;
  153. uint8_t data[4096];
  154. switch(meterModel)
  155. {
  156. case METER_MODEL_LEM_L18005A:
  157. default:
  158. if(httpGet((uint8_t*)meterApiAddress, data))
  159. {
  160. json_object *livemeasure;
  161. livemeasure = json_tokener_parse((char*)data);
  162. if(!is_error(livemeasure))
  163. {
  164. if(json_object_object_get(livemeasure, "voltage") != NULL)
  165. meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
  166. if(json_object_object_get(livemeasure, "current") != NULL)
  167. meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
  168. if(json_object_object_get(livemeasure, "power") != NULL)
  169. meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
  170. if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
  171. meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
  172. if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
  173. meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
  174. result = PASS;
  175. }
  176. json_object_put(livemeasure);
  177. }
  178. break;
  179. }
  180. return result;
  181. }