meterComm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. #define _DEBUG_ 0
  13. #if _DEBUG_
  14. #define PR(...) printf(__VA_ARGS__)
  15. #else
  16. #define PR(...)
  17. #endif
  18. unsigned char meterModel = 0;
  19. char meterApiAddress[128];
  20. /**
  21. * Meter model initialize
  22. * @param model
  23. */
  24. void meterInitialize(uint8_t model)
  25. {
  26. meterModel = model;
  27. }
  28. /**
  29. * Output voltage read
  30. * @param meter_info
  31. * @return
  32. */
  33. int readMeterLivemeasure(uint8_t meterIndex, Meter_Info *meter_info)
  34. {
  35. int result = FAIL;
  36. char host[128]="";
  37. uint8_t data[4096];
  38. sprintf(host, "http://192.168.0.%d/v1/livemeasure", (31 + meterIndex));
  39. switch(meterModel)
  40. {
  41. case METER_MODEL_LEM_L18005A:
  42. default:
  43. if(httpGet((uint8_t*)host, data) != FAIL)
  44. {
  45. //PR("data: %s\n", data);
  46. //PR("meterApiAddress: %s\n", meterApiAddress);
  47. json_object *livemeasure;
  48. livemeasure = json_tokener_parse((char*)data);
  49. if(!is_error(livemeasure))
  50. {
  51. if(json_object_object_get(livemeasure, "voltage") != NULL)
  52. meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
  53. PR("Output voltage %d : %.3f V\n", meter_info->presetVoltage);
  54. if(json_object_object_get(livemeasure, "current") != NULL)
  55. meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
  56. PR("Output current %d : %.3f V\n", meter_info->presentCurrent);
  57. if(json_object_object_get(livemeasure, "power") != NULL)
  58. meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
  59. PR("Output power %d : %.3f V\n", meter_info->presentPower);
  60. if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
  61. meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
  62. PR("Totalize import energy %d : %.3f V\n", meter_info->totlizeImportEnergy);
  63. if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
  64. meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
  65. PR("Totalize export energy %d : %.3f V\n", meter_info->totlizeExportEnergy);
  66. if(json_object_object_get(livemeasure, "timestamp") != NULL){
  67. sprintf(&meter_info->timestamp, "%s", json_object_get_string(json_object_object_get(livemeasure, "timestamp")));
  68. PR("timestamp : %s\n",meter_info->timestamp);
  69. }
  70. PR("\n\n\n\n\n\n\n\n");
  71. result = PASS;
  72. }
  73. json_object_put(livemeasure);
  74. }
  75. break;
  76. }
  77. return result;
  78. }
  79. /**
  80. * Export & import totalize energy read
  81. * @param meter_info
  82. * @return
  83. */
  84. int readMeterStatusInfo(uint8_t meterIndex, Meter_Status_Info *meter_status_info)
  85. {
  86. int result = FAIL;
  87. char host[128]="";
  88. uint8_t data[4096];
  89. sprintf(host, "http://192.168.0.%d/v1/status", (31 + meterIndex));
  90. switch(meterModel)
  91. {
  92. case METER_MODEL_LEM_L18005A:
  93. default:
  94. //if(httpGet((uint8_t*)meterApiAddress, data) != FAIL)
  95. if(httpGet((uint8_t*)host,data)!= FAIL)
  96. {
  97. json_object *livemeasure;
  98. livemeasure = json_tokener_parse((char*)data);
  99. //PR("Get %s\n",data);
  100. if(!is_error(livemeasure))
  101. {
  102. if(json_object_object_get(json_object_object_get(livemeasure, "status"), "value") != NULL)
  103. {
  104. meter_status_info->StatusValue = json_object_get_int(json_object_object_get(json_object_object_get(livemeasure, "status"), "value"));
  105. PR("status-value : %d \n",meter_status_info->StatusValue);
  106. }
  107. //ststus bit
  108. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "suLinkStatusIsOk") != NULL)
  109. {
  110. meter_status_info->MeterStatusFlag.bits.suLinkStatusIsOk = json_object_get_boolean(json_object_object_get(json_object_object_get
  111. (json_object_object_get(livemeasure, "status"), "bits"), "suLinkStatusIsOk"));
  112. PR("bits-suLinkStatusIsOk : %d \n",meter_status_info->MeterStatusFlag.bits.suLinkStatusIsOk);
  113. }
  114. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "muFatalErrorOccured") != NULL)
  115. {
  116. meter_status_info->MeterStatusFlag.bits.muFatalErrorOccured = json_object_get_boolean(json_object_object_get(json_object_object_get
  117. (json_object_object_get(livemeasure, "status"), "bits"), "muFatalErrorOccured"));
  118. PR("bits-muFatalErrorOccured : %d \n",meter_status_info->MeterStatusFlag.bits.muFatalErrorOccured);
  119. }
  120. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "transactionIsOnGoing") != NULL)
  121. {
  122. meter_status_info->MeterStatusFlag.bits.transactionIsOnGoing = json_object_get_boolean(json_object_object_get(json_object_object_get
  123. (json_object_object_get(livemeasure, "status"), "bits"), "transactionIsOnGoing"));
  124. PR("bits-transactionIsOnGoing : %d \n",meter_status_info->MeterStatusFlag.bits.transactionIsOnGoing);
  125. }
  126. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "tamperingIsDetected") != NULL)
  127. {
  128. meter_status_info->MeterStatusFlag.bits.tamperingIsDetected = json_object_get_boolean(json_object_object_get(json_object_object_get
  129. (json_object_object_get(livemeasure, "status"), "bits"), "tamperingIsDetected"));
  130. PR("bits-tamperingIsDetected : %d \n",meter_status_info->MeterStatusFlag.bits.tamperingIsDetected);
  131. }
  132. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "timeSyncStatusIsOk") != NULL)
  133. {
  134. meter_status_info->MeterStatusFlag.bits.timeSyncStatusIsOk = json_object_get_boolean(json_object_object_get(json_object_object_get
  135. (json_object_object_get(livemeasure, "status"), "bits"), "timeSyncStatusIsOk"));
  136. PR("bits-timeSyncStatusIsOk : %d \n",meter_status_info->MeterStatusFlag.bits.timeSyncStatusIsOk);
  137. }
  138. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "overTemperatureIsDetected") != NULL)
  139. {
  140. meter_status_info->MeterStatusFlag.bits.overTemperatureIsDetected = json_object_get_boolean(json_object_object_get(json_object_object_get
  141. (json_object_object_get(livemeasure, "status"), "bits"), "overTemperatureIsDetected"));
  142. PR("bits-overTemperatureIsDetected : %d \n",meter_status_info->MeterStatusFlag.bits.overTemperatureIsDetected);
  143. }
  144. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "reversedVoltage") != NULL)
  145. {
  146. meter_status_info->MeterStatusFlag.bits.reversedVoltage = json_object_get_boolean(json_object_object_get(json_object_object_get
  147. (json_object_object_get(livemeasure, "status"), "bits"), "reversedVoltage"));
  148. PR("bits-reversedVoltage : %d \n",meter_status_info->MeterStatusFlag.bits.reversedVoltage);
  149. }
  150. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "status"), "bits"), "suMeasureFailureOccurred") != NULL)
  151. {
  152. meter_status_info->MeterStatusFlag.bits.suMeasureFailureOccurred = json_object_get_boolean(json_object_object_get(json_object_object_get
  153. (json_object_object_get(livemeasure, "status"), "bits"), "suMeasureFailureOccurred"));
  154. PR("bits-suMeasureFailureOccurred : %d \n",meter_status_info->MeterStatusFlag.bits.suMeasureFailureOccurred);
  155. }
  156. if(json_object_object_get(json_object_object_get(livemeasure, "version"), "applicationFirmwareVersion") != NULL)
  157. {
  158. sprintf(&meter_status_info->applicationFirmwareVersion, "%s", json_object_get_string(json_object_object_get
  159. (json_object_object_get(livemeasure, "version"), "applicationFirmwareVersion")));
  160. PR("applicationFirmwareVersion : %s\n",meter_status_info->applicationFirmwareVersion);
  161. }
  162. if(json_object_object_get(json_object_object_get(livemeasure, "version"), "applicationFirmwareAuthTag") != NULL)
  163. {
  164. sprintf(&meter_status_info->applicationFirmwareAuthTag, "%s", json_object_get_string(json_object_object_get
  165. (json_object_object_get(livemeasure, "version"), "applicationFirmwareAuthTag")));
  166. PR("applicationFirmwareAuthTag : %s\n",meter_status_info->applicationFirmwareAuthTag);
  167. }
  168. if(json_object_object_get(json_object_object_get(livemeasure, "version"), "legalFirmwareVersion") != NULL)
  169. {
  170. sprintf(&meter_status_info->legalFirmwareVersion, "%s", json_object_get_string(json_object_object_get
  171. (json_object_object_get(livemeasure, "version"), "legalFirmwareVersion")));
  172. PR("applicationFirmwareAuthTag : %s\n",meter_status_info->legalFirmwareVersion);
  173. }
  174. if(json_object_object_get(json_object_object_get(livemeasure, "version"), "legalFirmwareAuthTag") != NULL)
  175. {
  176. sprintf(&meter_status_info->legalFirmwareAuthTag, "%s", json_object_get_string(json_object_object_get
  177. (json_object_object_get(livemeasure, "version"), "legalFirmwareAuthTag")));
  178. PR("applicationFirmwareAuthTag : %s\n",meter_status_info->legalFirmwareAuthTag);
  179. }
  180. if(json_object_object_get(json_object_object_get(livemeasure, "version"), "sensorFirmwareVersion") != NULL)
  181. {
  182. sprintf(&meter_status_info->sensorFirmwareVersion, "%s", json_object_get_string(json_object_object_get
  183. (json_object_object_get(livemeasure, "version"), "sensorFirmwareVersion")));
  184. PR("applicationFirmwareAuthTag : %s\n",meter_status_info->sensorFirmwareVersion);
  185. }
  186. if(json_object_object_get(json_object_object_get(livemeasure, "version"), "sensorFirmwareCrc") != NULL)
  187. {
  188. sprintf(&meter_status_info->sensorFirmwareCrc, "%s", json_object_get_string(json_object_object_get
  189. (json_object_object_get(livemeasure, "version"), "sensorFirmwareCrc")));
  190. PR("applicationFirmwareAuthTag : %s\n",meter_status_info->sensorFirmwareCrc);
  191. }
  192. if(json_object_object_get(livemeasure, "ipAddress") != NULL)
  193. {
  194. sprintf(&meter_status_info->ipAddress, "%s", json_object_get_string(json_object_object_get(livemeasure, "ipAddress")));
  195. PR("ipAddress : %s\n",meter_status_info->ipAddress);
  196. }
  197. if(json_object_object_get(livemeasure, "meterId") != NULL)
  198. {
  199. sprintf(&meter_status_info->meterId, "%s", json_object_get_string(json_object_object_get(livemeasure, "meterId")));
  200. PR("meterId : %s\n",meter_status_info->meterId);
  201. }
  202. //error bit
  203. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "muInitIsFailed") != NULL)
  204. {
  205. meter_status_info->MeterErrorFlag.bits.muInitIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  206. (json_object_object_get(livemeasure, "errors"), "bits"), "muInitIsFailed"));
  207. PR("bits-muInitIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.muInitIsFailed);
  208. }
  209. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "suStateIsInvalid") != NULL)
  210. {
  211. meter_status_info->MeterErrorFlag.bits.suStateIsInvalid = json_object_get_boolean(json_object_object_get(json_object_object_get
  212. (json_object_object_get(livemeasure, "errors"), "bits"), "suStateIsInvalid"));
  213. PR("bits-suStateIsInvalid : %d \n",meter_status_info->MeterErrorFlag.bits.suStateIsInvalid);
  214. }
  215. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "versionCheckIsFailed") != NULL)
  216. {
  217. meter_status_info->MeterErrorFlag.bits.versionCheckIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  218. (json_object_object_get(livemeasure, "errors"), "bits"), "versionCheckIsFailed"));
  219. PR("bits-versionCheckIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.versionCheckIsFailed);
  220. }
  221. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "muRngInitIsFailed") != NULL)
  222. {
  223. meter_status_info->MeterErrorFlag.bits.muRngInitIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  224. (json_object_object_get(livemeasure, "errors"), "bits"), "muRngInitIsFailed"));
  225. PR("bits-muRngInitIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.muRngInitIsFailed);
  226. }
  227. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "muDataIntegrityIsFailed") != NULL)
  228. {
  229. meter_status_info->MeterErrorFlag.bits.muDataIntegrityIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  230. (json_object_object_get(livemeasure, "errors"), "bits"), "muDataIntegrityIsFailed"));
  231. PR("bits-muDataIntegrityIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.muDataIntegrityIsFailed);
  232. }
  233. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "muFwIntegrityIsFailed") != NULL)
  234. {
  235. meter_status_info->MeterErrorFlag.bits.muFwIntegrityIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  236. (json_object_object_get(livemeasure, "errors"), "bits"), "muFwIntegrityIsFailed"));
  237. PR("bits-muFwIntegrityIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.muFwIntegrityIsFailed);
  238. }
  239. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "suIntegrityIsFailed") != NULL)
  240. {
  241. meter_status_info->MeterErrorFlag.bits.suIntegrityIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  242. (json_object_object_get(livemeasure, "errors"), "bits"), "suIntegrityIsFailed"));
  243. PR("bits-suIntegrityIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.suIntegrityIsFailed);
  244. }
  245. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "logbookIntegrityIsFailed") != NULL)
  246. {
  247. meter_status_info->MeterErrorFlag.bits.logbookIntegrityIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  248. (json_object_object_get(livemeasure, "errors"), "bits"), "logbookIntegrityIsFailed"));
  249. PR("bits-logbookIntegrityIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.logbookIntegrityIsFailed);
  250. }
  251. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "logbookIsFull") != NULL)
  252. {
  253. meter_status_info->MeterErrorFlag.bits.logbookIsFull = json_object_get_boolean(json_object_object_get(json_object_object_get
  254. (json_object_object_get(livemeasure, "errors"), "bits"), "logbookIsFull"));
  255. PR("bits-logbookIsFull : %d \n",meter_status_info->MeterErrorFlag.bits.logbookIsFull);
  256. }
  257. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "memoryAccessIsFailed") != NULL)
  258. {
  259. meter_status_info->MeterErrorFlag.bits.memoryAccessIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  260. (json_object_object_get(livemeasure, "errors"), "bits"), "memoryAccessIsFailed"));
  261. PR("bits-memoryAccessIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.memoryAccessIsFailed);
  262. }
  263. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "errors"), "bits"), "muStateIsFailed") != NULL)
  264. {
  265. meter_status_info->MeterErrorFlag.bits.muStateIsFailed = json_object_get_boolean(json_object_object_get(json_object_object_get
  266. (json_object_object_get(livemeasure, "errors"), "bits"), "muStateIsFailed"));
  267. PR("bits-muStateIsFailed : %d \n",meter_status_info->MeterErrorFlag.bits.muStateIsFailed);
  268. }
  269. if(json_object_object_get(livemeasure, "publicKey") != NULL)
  270. {
  271. sprintf(&meter_status_info->publicKey, "%s", json_object_get_string(json_object_object_get(livemeasure, "publicKey")));
  272. PR("publicKey : %s\n",meter_status_info->publicKey);
  273. }
  274. if(json_object_object_get(livemeasure, "publicKeyOcmf") != NULL)
  275. {
  276. sprintf(&meter_status_info->publicKeyOcmf, "%s", json_object_get_string(json_object_object_get(livemeasure, "publicKeyOcmf")));
  277. PR("publicKeyOcmf : %s\n",meter_status_info->publicKeyOcmf);
  278. }
  279. if(json_object_object_get(livemeasure, "indexOfLastTransaction") != NULL)
  280. {
  281. meter_status_info->indexOfLastTransaction = json_object_get_int(json_object_object_get(livemeasure, "indexOfLastTransaction"));
  282. PR("indexOfLastTransaction : %d \n",meter_status_info->indexOfLastTransaction);
  283. }
  284. if(json_object_object_get(livemeasure, "numberOfStoredTransactions") != NULL)
  285. {
  286. meter_status_info->numberOfStoredTransactions = json_object_get_int(json_object_object_get(livemeasure, "numberOfStoredTransactions"));
  287. PR("numberOfStoredTransactions : %d \n",meter_status_info->numberOfStoredTransactions);
  288. }
  289. PR("\n\n\n\n\n\n\n\n");
  290. result = PASS;
  291. }
  292. json_object_put(livemeasure);
  293. }
  294. break;
  295. }
  296. return result;
  297. }
  298. int startMeterTransaction(uint8_t meterIndex, Meter_transaction_Action *meter_transaction_Action)
  299. {
  300. int result = FAIL;
  301. char host[128]="";
  302. uint8_t data[4096];
  303. json_object *TransactionStartData = json_object_new_object();
  304. json_object_object_add(TransactionStartData, "evseId", json_object_new_string((char*)meter_transaction_Action->head.evseId));
  305. json_object_object_add(TransactionStartData, "transactionId", json_object_new_string((char*)meter_transaction_Action->head.transactionId));
  306. json_object_object_add(TransactionStartData, "clientId", json_object_new_string((char*)meter_transaction_Action->head.clientId));
  307. json_object_object_add(TransactionStartData, "tariffId", json_object_new_int((char*)meter_transaction_Action->head.tariffId));
  308. json_object_object_add(TransactionStartData, "cableId", json_object_new_int((char*)meter_transaction_Action->head.cableId));
  309. json_object_object_add(TransactionStartData, "userData", json_object_new_string((char*)meter_transaction_Action->head.userData));
  310. TransactionStartData = json_object_to_json_string(TransactionStartData);
  311. sprintf(host, "http://192.168.0.%d/v1/legal", (31 + meterIndex));
  312. switch(meterModel)
  313. {
  314. case METER_MODEL_LEM_L18005A:
  315. default:
  316. if(httpPost((uint8_t*)host,TransactionStartData,data)!= FAIL)
  317. {
  318. json_object *livemeasure;
  319. livemeasure = json_tokener_parse((char*)data);
  320. if(!is_error(livemeasure))
  321. {
  322. PR("PostStartGet %s\n",data);
  323. result = PASS;
  324. }
  325. json_object_put(livemeasure);
  326. json_object_put(TransactionStartData);
  327. }
  328. break;
  329. }
  330. return result;
  331. }
  332. int stopMeterTransaction(uint8_t meterIndex, Meter_transaction_Action *meter_transaction_Action,Meter_transaction_Result *meter_transaction_Result)
  333. {
  334. int result = FAIL;
  335. char host[128]="";
  336. uint8_t data[4096];
  337. sprintf(host, "http://192.168.0.%d/v1/legal?transactionId=%s",(31 + meterIndex), meter_transaction_Action->head.transactionId);
  338. json_object *TransactionStopData = json_object_new_object();
  339. json_object_object_add(TransactionStopData, "running", json_object_new_boolean(0));
  340. TransactionStopData = (uint8_t *) json_object_to_json_string(TransactionStopData);
  341. switch(meterModel)
  342. {
  343. case METER_MODEL_LEM_L18005A:
  344. default:
  345. if(httpPut((uint8_t*)host,TransactionStopData,data)!= FAIL)
  346. //if(httpGet(host,data)!= FAIL)
  347. {
  348. json_object *livemeasure;
  349. livemeasure = json_tokener_parse((char*)data);
  350. if(!is_error(livemeasure))
  351. {
  352. PR("PutStopGet %s\n",data);
  353. if(json_object_object_get(livemeasure, "paginationCounter") != NULL)
  354. {
  355. meter_transaction_Result->paginationCounter = json_object_get_int(json_object_object_get(livemeasure, "paginationCounter"));
  356. PR("paginationCounter : %d\n",meter_transaction_Result->paginationCounter);
  357. }
  358. if(json_object_object_get(livemeasure, "transactionId") != NULL)
  359. {
  360. sprintf(&meter_transaction_Result->head.transactionId, "%s", json_object_get_string(json_object_object_get(livemeasure, "transactionId")));
  361. PR("transactionId : %s\n",meter_transaction_Result->head.transactionId);
  362. }
  363. if(json_object_object_get(livemeasure, "evseId") != NULL)
  364. {
  365. sprintf(&meter_transaction_Result->head.evseId, "%s", json_object_get_string(json_object_object_get(livemeasure, "evseId")));
  366. PR("evseId : %s\n",meter_transaction_Result->head.evseId);
  367. }
  368. if(json_object_object_get(livemeasure, "clientId") != NULL)
  369. {
  370. sprintf(&meter_transaction_Result->head.clientId, "%s", json_object_get_string(json_object_object_get(livemeasure, "clientId")));
  371. PR("clientId : %s\n",meter_transaction_Result->head.clientId);
  372. }
  373. if(json_object_object_get(livemeasure, "tariffId") != NULL)
  374. {
  375. meter_transaction_Result->head.tariffId = json_object_get_int(json_object_object_get(livemeasure, "tariffId"));
  376. PR("tariffId : %d\n",meter_transaction_Result->head.tariffId);
  377. }
  378. if(json_object_object_get(livemeasure, "userData") != NULL)
  379. {
  380. sprintf(&meter_transaction_Result->head.userData, "%s", json_object_get_string(json_object_object_get(livemeasure, "userData")));
  381. PR("clientId : %s\n",meter_transaction_Result->head.userData);
  382. }
  383. if(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "timestampStart") != NULL)
  384. {
  385. sprintf(&meter_transaction_Result->timestampStart, "%s", json_object_get_string(json_object_object_get
  386. (json_object_object_get(livemeasure, "meterValue"), "timestampStart")));
  387. PR("timestampStart : %s\n",meter_transaction_Result->timestampStart);
  388. }
  389. if(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "timestampStop") != NULL)
  390. {
  391. sprintf(&meter_transaction_Result->timestampStop, "%s", json_object_get_string(json_object_object_get
  392. (json_object_object_get(livemeasure, "meterValue"), "timestampStop")));
  393. PR("timestampStop : %s\n",meter_transaction_Result->timestampStop);
  394. }
  395. if(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "transactionDuration") != NULL)
  396. {
  397. meter_transaction_Result->transactionDuration = json_object_get_int(json_object_object_get
  398. (json_object_object_get(livemeasure, "meterValue"), "transactionDuration"));
  399. PR("transactionDuration : %d\n",meter_transaction_Result->transactionDuration);
  400. }
  401. if(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "transactionStatus") != NULL)
  402. {
  403. meter_transaction_Result->transactionStatus = json_object_get_int(json_object_object_get
  404. (json_object_object_get(livemeasure, "meterValue"), "transactionStatus"));
  405. PR("transactionStatus : %d\n",meter_transaction_Result->transactionStatus);
  406. }
  407. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyUnit") != NULL)
  408. {
  409. strcpy(meter_transaction_Result->energyUnit, json_object_get_string(json_object_object_get(json_object_object_get(json_object_object_get
  410. (livemeasure, "meterValue"), "sampleValue"), "energyUnit")));
  411. PR("energyUnit : %s\n",meter_transaction_Result->energyUnit);
  412. }
  413. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyImport") != NULL)
  414. {
  415. meter_transaction_Result->energyImport = json_object_get_double(json_object_object_get(json_object_object_get(json_object_object_get
  416. (livemeasure, "meterValue"), "sampleValue"), "energyImport"));
  417. PR("energyImport : %.3f\n",meter_transaction_Result->energyImport);
  418. }
  419. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyImportTotalStart") != NULL)
  420. {
  421. meter_transaction_Result->energyImportTotalStart = json_object_get_double(json_object_object_get(json_object_object_get(json_object_object_get
  422. (livemeasure, "meterValue"), "sampleValue"), "energyImportTotalStart"));
  423. PR("energyImportTotalStart : %.3f\n",meter_transaction_Result->energyImportTotalStart);
  424. }
  425. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyImportTotalStop") != NULL)
  426. {
  427. meter_transaction_Result->energyImportTotalStop = json_object_get_double(json_object_object_get(json_object_object_get(json_object_object_get
  428. (livemeasure, "meterValue"), "sampleValue"), "energyImportTotalStop"));
  429. PR("energyImportTotalStop : %.3f\n",meter_transaction_Result->energyImportTotalStop);
  430. }
  431. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyExport") != NULL)
  432. {
  433. meter_transaction_Result->energyExport = json_object_get_double(json_object_object_get(json_object_object_get(json_object_object_get
  434. (livemeasure, "meterValue"), "sampleValue"), "energyExport"));
  435. PR("energyExport : %.3f\n",meter_transaction_Result->energyExport);
  436. }
  437. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyExportTotalStart") != NULL)
  438. {
  439. meter_transaction_Result->energyExportTotalStart = json_object_get_double(json_object_object_get(json_object_object_get(json_object_object_get
  440. (livemeasure, "meterValue"), "sampleValue"), "energyExportTotalStart"));
  441. PR("energyExportTotalStart : %.3f\n",meter_transaction_Result->energyExportTotalStart);
  442. }
  443. if(json_object_object_get(json_object_object_get(json_object_object_get(livemeasure, "meterValue"), "sampleValue"), "energyExportTotalStop") != NULL)
  444. {
  445. meter_transaction_Result->energyExportTotalStop = json_object_get_double(json_object_object_get(json_object_object_get(json_object_object_get
  446. (livemeasure, "meterValue"), "sampleValue"), "energyExportTotalStop"));
  447. PR("energyExportTotalStop : %.3f\n",meter_transaction_Result->energyExportTotalStop);
  448. }
  449. if(json_object_object_get(livemeasure, "signature") != NULL)
  450. {
  451. sprintf(&meter_transaction_Result->signature, "%s", json_object_get_string(json_object_object_get(livemeasure, "signature")));
  452. PR("signature : %s\n",meter_transaction_Result->signature);
  453. }
  454. PR("\n\n\n\n\n\n\n\n");
  455. result = PASS;
  456. }
  457. json_object_put(livemeasure);
  458. json_object_put(TransactionStopData);
  459. }
  460. break;
  461. }
  462. return result;
  463. }
  464. int readMeterTransactionOCMF(uint8_t meterIndex, Meter_transaction_Action *meter_transaction_Action)
  465. {
  466. int result = FAIL;
  467. char host[128]="";
  468. uint8_t data[4096];
  469. sprintf(host, "http://192.168.0.%d/v1/ocmf?transactionId=%s",(31 + meterIndex),meter_transaction_Action->head.transactionId);
  470. //printf ("host : %s\n",host);
  471. switch(meterModel)
  472. {
  473. case METER_MODEL_LEM_L18005A:
  474. default:
  475. if(httpGet((uint8_t*)host,data)!= FAIL)
  476. {
  477. PR("Get %s\n",data);
  478. sprintf(&meter_transaction_Action->transactionOCMF, "%s", data);
  479. //printf("GETOCMF %s\n",meter_transaction_Result->transactionOCMF);
  480. json_object *livemeasure;
  481. livemeasure = json_tokener_parse((char*)data);
  482. if(!is_error(livemeasure))
  483. {
  484. result = PASS;
  485. }
  486. json_object_put(livemeasure);
  487. }
  488. break;
  489. }
  490. return result;
  491. }
  492. int readMeterLegal(uint8_t meterIndex, Meter_transaction_Action *meter_transaction_Action)
  493. {
  494. int result = FAIL;
  495. char host[128]="";
  496. uint8_t data[4096];
  497. sprintf(host, "http://192.168.0.%d/v1/legal",(31 + meterIndex));
  498. //printf ("host : %s\n",host);
  499. switch(meterModel)
  500. {
  501. case METER_MODEL_LEM_L18005A:
  502. default:
  503. if(httpGet((uint8_t*)host,data)!= FAIL)
  504. {
  505. json_object *livemeasure;
  506. livemeasure = json_tokener_parse((char*)data);
  507. if(!is_error(livemeasure))
  508. {
  509. if(json_object_object_get(livemeasure, "transactionId") != NULL)
  510. {
  511. sprintf(&meter_transaction_Action->head.transactionId, "%s", json_object_get_string(json_object_object_get(livemeasure, "transactionId")));
  512. PR("transactionId : %s\n",meter_transaction_Action->head.transactionId);
  513. }
  514. result = PASS;
  515. }
  516. json_object_put(livemeasure);
  517. }
  518. break;
  519. }
  520. return result;
  521. }
  522. int configure_meter_time(uint8_t meterIndex)
  523. {
  524. int result = FAIL;
  525. char host[256]="";
  526. uint8_t postdata[256];
  527. uint8_t data[4096];
  528. time_t tmi;
  529. struct tm* utcTime;
  530. time(&tmi);
  531. utcTime = gmtime(&tmi);
  532. //{"time": { "utc":"2022-06-29T13:09:26Z" }}
  533. sprintf(host,"http://192.168.0.%d/v1/settings",(31 + meterIndex));
  534. memset(postdata, 0, sizeof(postdata));
  535. sprintf(postdata,"{\"time\": { \"utc\":\"%d-%d-%dT%02d:%02d:%02dZ\" }}", (utcTime->tm_year+1900), (utcTime->tm_mon+1), (utcTime->tm_mday), (utcTime->tm_hour) % 24, utcTime->tm_min, utcTime->tm_sec);
  536. //sprintf(postdata,"{\"time\": { \"utc\":\"%d-%d-%dT%02d:%02d:%02dZ\" }}", 2021, (utcTime->tm_mon+1), (utcTime->tm_mday), (utcTime->tm_hour) % 24, utcTime->tm_min, utcTime->tm_sec);
  537. PR("Local time and date: %s\n", postdata);
  538. switch(meterModel)
  539. {
  540. case METER_MODEL_LEM_L18005A:
  541. default:
  542. if(httpPut((uint8_t*)host,postdata, data)!= FAIL)
  543. {
  544. json_object *livemeasure;
  545. livemeasure = json_tokener_parse((char*)data);
  546. if(!is_error(livemeasure))
  547. {
  548. PR("PutConfingTimeGet %s\n",data);
  549. PR("\n\n\n\n\n\n\n\n");
  550. if(json_object_object_get(livemeasure, "result") != NULL)
  551. {
  552. PR("configure_meter_time resul: %d \n",json_object_get_int(json_object_object_get(livemeasure, "result")));
  553. }
  554. result = PASS;
  555. }
  556. json_object_put(livemeasure);
  557. }
  558. break;
  559. }
  560. return result;
  561. }