123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /*
- * MeterDriver.c
- *
- * Created on: 2021/6/4
- * Author: foluswen
- */
- #include "meterComm.h"
- #include "curlApi.h"
- #include "SystemLogMessage.h"
- #include "json-c/json.h"
- #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
- unsigned char meterModel = 0;
- char meterApiAddress[128];
- /**
- * Meter model initialize
- * @param model
- */
- void meterInitialize(uint8_t model)
- {
- meterModel = model;
- }
- /**
- * Meter api address assign
- * @param meterIndex
- */
- void meterApiAssign(uint8_t meterIndex)
- {
- switch(meterModel)
- {
- case METER_MODEL_LEM_L18005A:
- default:
- /*
- * TODO:
- * 1. Check meter actual API address.
- */
- sprintf(meterApiAddress, "http://192.168.0.%d/v1/livemeasure", (31 + meterIndex));
- //sprintf(meterApiAddress, "http://192.168.0.31/livemeasure");
- //sprintf(meterApiAddress, "https://foluswen.com/tmate/livemeasure.php"); // Simulator by web server
- break;
- }
- }
- /**
- * Output voltage read
- * @param meter_info
- * @return
- */
- int readVoltage(Meter_Info *meter_info)
- {
- int result = FAIL;
- uint8_t data[4096];
- switch(meterModel)
- {
- case METER_MODEL_LEM_L18005A:
- default:
- if(httpGet((uint8_t*)meterApiAddress, data) != FAIL)
- {
- //printf("data: %s\n", data);
- //printf("meterApiAddress: %s\n", meterApiAddress);
- json_object *livemeasure;
- livemeasure = json_tokener_parse((char*)data);
- if(!is_error(livemeasure))
- {
- if(json_object_object_get(livemeasure, "voltage") != NULL)
- meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
- if(json_object_object_get(livemeasure, "current") != NULL)
- meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
- if(json_object_object_get(livemeasure, "power") != NULL)
- meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
- if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
- meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
- if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
- meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
- result = PASS;
- }
- json_object_put(livemeasure);
- }
- break;
- }
- return result;
- }
- /**
- * Output current read
- * @param meter_info
- * @return
- */
- int readCurrent(Meter_Info *meter_info)
- {
- int result = FAIL;
- uint8_t data[4096];
- switch(meterModel)
- {
- case METER_MODEL_LEM_L18005A:
- default:
- if(httpGet((uint8_t*)meterApiAddress, data) != FAIL)
- {
- json_object *livemeasure;
- livemeasure = json_tokener_parse((char*)data);
- if(!is_error(livemeasure))
- {
- if(json_object_object_get(livemeasure, "voltage") != NULL)
- meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
- if(json_object_object_get(livemeasure, "current") != NULL)
- meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
- if(json_object_object_get(livemeasure, "power") != NULL)
- meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
- if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
- meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
- if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
- meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
- result = PASS;
- }
- json_object_put(livemeasure);
- }
- break;
- }
- return result;
- }
- /**
- * Output power read
- * @param meter_info
- * @return
- */
- int readPower(Meter_Info *meter_info)
- {
- int result = FAIL;
- uint8_t data[4096];
- switch(meterModel)
- {
- case METER_MODEL_LEM_L18005A:
- default:
- if(httpGet((uint8_t*)meterApiAddress, data) != FAIL)
- {
- json_object *livemeasure;
- livemeasure = json_tokener_parse((char*)data);
- if(!is_error(livemeasure))
- {
- if(json_object_object_get(livemeasure, "voltage") != NULL)
- meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
- if(json_object_object_get(livemeasure, "current") != NULL)
- meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
- if(json_object_object_get(livemeasure, "power") != NULL)
- meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
- if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
- meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
- if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
- meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
- result = PASS;
- }
- json_object_put(livemeasure);
- }
- break;
- }
- return result;
- }
- /**
- * Export & import totalize energy read
- * @param meter_info
- * @return
- */
- int readEnergy(Meter_Info *meter_info)
- {
- int result = FAIL;
- uint8_t data[4096];
- switch(meterModel)
- {
- case METER_MODEL_LEM_L18005A:
- default:
- if(httpGet((uint8_t*)meterApiAddress, data) != FAIL)
- {
- json_object *livemeasure;
- livemeasure = json_tokener_parse((char*)data);
- if(!is_error(livemeasure))
- {
- if(json_object_object_get(livemeasure, "voltage") != NULL)
- meter_info->presetVoltage = json_object_get_double(json_object_object_get(livemeasure, "voltage"));
- if(json_object_object_get(livemeasure, "current") != NULL)
- meter_info->presentCurrent = json_object_get_double(json_object_object_get(livemeasure, "current"));
- if(json_object_object_get(livemeasure, "power") != NULL)
- meter_info->presentPower = json_object_get_double(json_object_object_get(livemeasure, "power"));
- if(json_object_object_get(livemeasure, "energyImportTotal") != NULL)
- meter_info->totlizeImportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyImportTotal"));
- if(json_object_object_get(livemeasure, "energyExportTotal") != NULL)
- meter_info->totlizeExportEnergy = json_object_get_double(json_object_object_get(livemeasure, "energyExportTotal"));
- result = PASS;
- }
- json_object_put(livemeasure);
- }
- break;
- }
- return result;
- }
|