BigW Consortium Gitlab

Commit 21aaaab9 by Zahid Chowdhury

Added dhub config of combain api key if not set from config, default periodic…

Added dhub config of combain api key if not set from config, default periodic sensor for location to enabled & 30 sec., got rid of exits, tried adding wlan caching (revisit)
parent f94500f6
......@@ -46,14 +46,6 @@ static size_t WriteMemCallback(void *contents, size_t size, size_t nmemb, void *
void *CombainHttpThreadFunc(void *context)
{
le_cfg_ConnectService();
char combainApiKey[32];
const le_result_t cfgRes = le_cfg_QuickGetString(
"/ApiKey", combainApiKey, sizeof(combainApiKey) - 1, "");
LE_FATAL_IF(
cfgRes != LE_OK || combainApiKey[0] == '\0',
"Failed to read Combain API Key from config tree");
char combainUrl[128] = "https://cps.combain.com?key=";
strncat(combainUrl, combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl)));
do {
......
......@@ -12,4 +12,8 @@ void CombainHttpInit(
void CombainHttpDeinit(void);
void *CombainHttpThreadFunc(void *context);
#define MAX_LEN_API_KEY 32
extern char combainApiKey[MAX_LEN_API_KEY];
#endif // COMBAIN_HTTP_H
......@@ -29,6 +29,7 @@ requires:
{
api:
{
dhubIO = io.api
le_cfg.api
}
......
......@@ -12,6 +12,11 @@
#include "CombainHttp.h"
#include "ThreadSafeQueue.h"
#define RES_PATH_API_KEY "ApiKey/value"
static bool combainApiKeySet = false;
char combainApiKey[MAX_LEN_API_KEY];
struct RequestRecord
{
......@@ -126,6 +131,11 @@ le_result_t ma_combainLocation_SubmitLocationRequest
void *context
)
{
/* WLAN caching has so much variance, ssids coming & going, ordered list, ...
* Need to rethink the algorithm for caching
* Lets cache the value so that we don't make multiple Combain requests
static std::string cached_requestBody; */
RequestRecord *requestRecord = GetRequestRecordFromHandle(handle, true);
if (!requestRecord)
{
......@@ -139,7 +149,16 @@ le_result_t ma_combainLocation_SubmitLocationRequest
}
std::string requestBody = requestRecord->request->generateRequestBody();
LE_DEBUG("Submitting request: %s", requestBody.c_str());
/* if (!cached_requestBody.empty()) {
LE_INFO("Submitting request: %s", requestBody.c_str());
LE_INFO("Cached request: %s", cached_requestBody.c_str());
if (cached_requestBody.compare(requestBody) == 0) {
LE_INFO("cached_requestBody same as requestBody");
return LE_DUPLICATE;
}
} */
{
FILE* f = fopen("request.txt", "w");
LE_ASSERT(f != NULL);
......@@ -147,6 +166,8 @@ le_result_t ma_combainLocation_SubmitLocationRequest
fclose(f);
}
requestRecord->responseHandler = responseHandler;
/* cached_requestBody = requestBody; */
requestRecord->responseHandlerContext = context;
// NULL out the request generator since we're done with it
requestRecord->request.reset();
......@@ -440,6 +461,22 @@ static bool TryParseAsSuccess(json_t *responseJson, std::shared_ptr<CombainResul
return parseSuccess;
}
static void SetApiKey (double timestamp, const char *api_key, void *contextPtr)
{
LE_INFO("SetApiKey called");
if (strlen(api_key) > (MAX_LEN_API_KEY - 1))
{
LE_INFO("To large an ApiKey!!");
return;
}
strncpy(combainApiKey, api_key, MAX_LEN_API_KEY - 1);
combainApiKeySet = true;
LE_INFO("combainApiKey set from dhub starting http thread");
CombainHttpInit(&RequestJson, &ResponseJson, ResponseAvailableEvent);
le_thread_Ref_t httpThread = le_thread_Create("CombainHttp", CombainHttpThreadFunc, NULL);
le_thread_Start(httpThread);
}
COMPONENT_INIT
{
ResponseAvailableEvent = le_event_CreateId("CombainResponseAvailable", 0);
......@@ -449,7 +486,24 @@ COMPONENT_INIT
le_msg_AddServiceCloseHandler(
ma_combainLocation_GetServiceRef(), ClientSessionClosedHandler, NULL);
// Let's either get the API key from the config tree or wait for it from dhub
le_cfg_ConnectService();
const le_result_t cfgRes = le_cfg_QuickGetString(
"/ApiKey", combainApiKey, sizeof(combainApiKey) - 1, "");
if (cfgRes == LE_OK && combainApiKey[0] != '\0')
{
combainApiKeySet = true;
CombainHttpInit(&RequestJson, &ResponseJson, ResponseAvailableEvent);
le_thread_Ref_t httpThread = le_thread_Create("CombainHttp", CombainHttpThreadFunc, NULL);
le_thread_Start(httpThread);
}
// Let's wait for dhub to set the combainApiKey
else
{
LE_INFO("Wait for dhub to set the combainApiKey");
LE_ASSERT(LE_OK == dhubIO_CreateOutput(RES_PATH_API_KEY, DHUBIO_DATA_TYPE_STRING, ""));
dhubIO_AddStringPushHandler(RES_PATH_API_KEY, SetApiKey, NULL);
dhubIO_MarkOptional(RES_PATH_API_KEY);
}
}
......@@ -11,7 +11,7 @@ requires:
positioning/le_posCtrl.api
positioning/le_pos.api
dhubIO = io.api
${CURDIR}/../../CombainLocation/ma_combainLocation.api
${CURDIR}/../ma_combainLocation.api
wifi/le_wifiClient.api
//modemServices/le_mrc.api
}
......
......@@ -13,7 +13,9 @@
#include <time.h>
#define HACCURACY_GOOD 50
#define HACCURACY_GREY_ZONE_UPPER 500
#define PSENSOR_ENABLE "coordinates/enable"
#define PSENSOR_PERIOD "coordinates/period"
#define DEFAULT_PERIOD 30
typedef enum {GPS, WIFI} Loc_t;
......@@ -181,7 +183,6 @@ static void LocationResultHandler(
if (res != LE_OK)
{
LE_INFO("Received result notification of type success response, but couldn't fetch the result\n");
exit(1);
}
else
{
......@@ -217,7 +218,6 @@ static void LocationResultHandler(
if (res != LE_OK)
{
LE_INFO("Received result notification of type success response, but couldn't fetch the result\n");
exit(1);
}
else
{
......@@ -227,7 +227,6 @@ static void LocationResultHandler(
LE_INFO(" firstMessage: %s\n", firstMessage);
LE_INFO(" code: %d\n", code);
LE_INFO(" message: %s\n", message);
exit(1);
}
break;
......@@ -241,24 +240,20 @@ static void LocationResultHandler(
if (res != LE_OK)
{
LE_INFO("Received result notification of type success response, but couldn't fetch the result\n");
exit(1);
}
else
{
LE_INFO("Received a result which couldn't be parsed \"%s\"\n", rawResult);
exit(1);
}
break;
}
case MA_COMBAINLOCATION_RESULT_COMMUNICATION_FAILURE:
LE_INFO("Couldn't communicate with Combain server\n");
exit(1);
break;
default:
LE_INFO("Received unhandled result type (%d)\n", result);
exit(1);
}
}
......@@ -269,13 +264,21 @@ static bool TrySubmitRequest(void)
LE_INFO("Attempting to submit location request");
const le_result_t res = ma_combainLocation_SubmitLocationRequest(
State.combainHandle, LocationResultHandler, NULL);
if (res != LE_OK)
LE_FATAL("Failed to submit location request\n");
LE_INFO("Submitted request handle: %d", (uint32_t) State.combainHandle);
if (res == LE_DUPLICATE) {
ma_combainLocation_DestroyLocationRequest(State.combainHandle);
return true;
}
if (res == LE_OK) {
LE_INFO("Submitted request handle: %d", (uint32_t) State.combainHandle);
return true;
}
LE_FATAL("Failed to submit location request\n");
return false;
}
LE_INFO("Cannot submit WIFI location request to Combain as previous in transit");
return false;
}
......@@ -303,14 +306,12 @@ static void WifiEventHandler(le_wifiClient_Event_t event, void *context)
if (res != LE_OK)
{
LE_INFO("Failed while fetching WiFi SSID\n");
exit(1);
}
res = le_wifiClient_GetBssid(ap, bssid, sizeof(bssid) - 1);
if (res != LE_OK)
{
LE_INFO("Failed while fetching WiFi BSSID\n");
exit(1);
}
// TODO: LE-10254 notes that an incorrect error code of 0xFFFF is mentioned in the
......@@ -319,13 +320,11 @@ static void WifiEventHandler(le_wifiClient_Event_t event, void *context)
if (signalStrength == 0xFFF)
{
LE_INFO("Failed while fetching WiFi signal strength\n");
exit(1);
}
if (!MacAddrStringToBinary(bssid, bssidBytes))
{
LE_INFO("WiFi scan contained invalid bssid=\"%s\"\n", bssid);
exit(1);
}
res = ma_combainLocation_AppendWifiAccessPoint(
......@@ -347,7 +346,6 @@ static void WifiEventHandler(le_wifiClient_Event_t event, void *context)
case LE_WIFICLIENT_EVENT_SCAN_FAILED:
LE_INFO("WiFi scan failed\n");
exit(1);
break;
default:
......@@ -409,14 +407,14 @@ static void Sample
startRes = le_wifiClient_Start();
State.combainHandle = ma_combainLocation_CreateLocationRequest();
LE_INFO("Create request handle: %d", (uint32_t) State.combainHandle);
if (startRes != LE_OK && startRes != LE_BUSY) {
LE_FATAL("Couldn't start the WiFi service error code: %s", LE_RESULT_TXT(startRes));
exit(1);
}
le_wifiClient_Scan();
State.combainHandle = ma_combainLocation_CreateLocationRequest();
LE_INFO("Create request handle: %d", (uint32_t) State.combainHandle);
saved_ref = ref;
State.waitingForWifiResults = true;
}
......@@ -430,7 +428,7 @@ static void Sample
PackJson(GPS, &scan, json, sizeof(json));
psensor_PushJson(ref, 0 /* now */, json);
// Kill any errant WIFI sacn requests as we got GPS
// Kill any errant WIFI scan requests as we got GPS
if (State.waitingForWifiResults)
State.waitingForWifiResults = false;
}
......@@ -453,6 +451,10 @@ COMPONENT_INIT
// interface. We'll provide samples as JSON structures.
psensor_Create("coordinates", DHUBIO_DATA_TYPE_JSON, "", Sample);
// Make sure the periodic sensor is enabled and defaults to 30 secs.
dhubIO_SetBooleanDefault(PSENSOR_ENABLE, true);
dhubIO_SetNumericDefault(PSENSOR_PERIOD, DEFAULT_PERIOD);
dhubIO_SetJsonExample("coordinates/value", "{\"lat\":0.1,\"lon\":0.2,"
"\"alt\":0.3,\"hAcc\":0.4,\"vAcc\":0.5,\"fixType\":\"GNSS\"}");
}
......
File mode changed from 100755 to 100644
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment