BigW Consortium Gitlab

Commit 5e4bb54a by Zahid Chowdhury

Change GPS/Wifi algorithm, add locationServices to sdef, fix bug in not killing…

Change GPS/Wifi algorithm, add locationServices to sdef, fix bug in not killing location request, streamline processing
parent 87cc7b88
......@@ -12,7 +12,8 @@
#include <stdio.h>
#include <time.h>
#define HACCURACY_GOOD 50
#define HACCURACY_GOOD_GPS 75
#define HACCURACY_WIFI_ONLY 500
#define PSENSOR_ENABLE "coordinates/enable"
#define PSENSOR_PERIOD "coordinates/period"
#define DEFAULT_PERIOD 30
......@@ -35,10 +36,15 @@ static struct
{
ma_combainLocation_LocReqHandleRef_t combainHandle;
bool waitingForWifiResults;
bool waitingForCombainResults;
le_wifiClient_NewEventHandlerRef_t wifiHandler;
} State;
static psensor_Ref_t saved_ref= NULL;
// > HACCURACY_GOOD_GPS - save the last GPS scan to send if Wifi scan fails
static Scan_t SavedGpsScan;
static Scan_t *GpsScan = NULL;
static psensor_Ref_t saved_ref = NULL;
uint64_t GetCurrentTimestamp(void)
{
......@@ -166,15 +172,36 @@ static void PackJson
LE_FATAL("JSON string (len %d) is longer than buffer (size %zu).", len, jsonl);
}
static void UseGpsScan(void)
{
char json[256];
if (GpsScan != NULL)
{
PackJson(GPS, &SavedGpsScan, json, sizeof(json));
LE_INFO("Sending dhub json: %s", json);
psensor_PushJson(saved_ref, 0 /* now */, json);
GpsScan = NULL;
}
}
static void LocationResultHandler(
ma_combainLocation_LocReqHandleRef_t handle, ma_combainLocation_Result_t result, void *context)
{
char json[256];
if (State.waitingForCombainResults != true)
{
LE_FATAL("Serious error from Combain service");
exit(1);
}
State.waitingForCombainResults = false;
switch (result)
{
case MA_COMBAINLOCATION_RESULT_SUCCESS:
{
Scan_t scan;
char json[256];
const le_result_t res = ma_combainLocation_GetSuccessResponse(
......@@ -189,11 +216,11 @@ static void LocationResultHandler(
LE_INFO("Location: latitude=%f, longitude=%f, accuracy=%f meters\n",
scan.lat, scan.lon, scan.hAccuracy);
PackJson(WIFI, &scan, json, sizeof(json));
LE_INFO("Sent dhub json: %s", json);
LE_INFO("Sending dhub json: %s", json);
psensor_PushJson(saved_ref, 0 /* now */, json);
saved_ref = NULL;
}
break;
return;
}
case MA_COMBAINLOCATION_RESULT_ERROR:
......@@ -257,6 +284,8 @@ static void LocationResultHandler(
default:
LE_INFO("Received unhandled result type (%d)\n", result);
}
UseGpsScan();
}
static bool TrySubmitRequest(void)
......@@ -273,6 +302,7 @@ static bool TrySubmitRequest(void)
}
if (res == LE_OK) {
State.waitingForCombainResults = true;
LE_INFO("Submitted request handle: %d", (uint32_t) State.combainHandle);
return true;
}
......@@ -347,14 +377,15 @@ static void WifiEventHandler(le_wifiClient_Event_t event, void *context)
}
break;
case LE_WIFICLIENT_EVENT_SCAN_FAILED:
LE_INFO("WiFi scan failed\n");
break;
default:
// Do nothing - don't care about connect/disconnect events
{
LE_INFO("WiFi scan failed\n");
ma_combainLocation_DestroyLocationRequest(State.combainHandle);
State.waitingForWifiResults = false;
UseGpsScan();
break;
}
}
}
static void Sample
......@@ -374,8 +405,11 @@ static void Sample
le_result_t posRes = le_pos_Get3DLocation(&lat, &lon, &hAccuracy, &alt, &vAccuracy);
/* LE_INFO("le_pos_Get3DLocation returned: lat = %d lon: %d hAccuracy: %d alt: %d vAccuracy: %d",
lat, lon, hAccuracy, alt, vAccuracy); */
if (posRes == LE_OK)
LE_INFO("le_pos_Get3DLocation returned: lat = %d lon: %d hAccuracy: %d alt: %d vAccuracy: %d",
lat, lon, hAccuracy, alt, vAccuracy);
else
LE_INFO("le_pos_Get3DLocation FAILED: %s", LE_RESULT_TXT(posRes));
scan.lat = (double) lat / 1000000.0 ;
scan.lon = (double) lon / 1000000.0;
......@@ -387,9 +421,15 @@ static void Sample
scan.lat, scan.lon, scan.hAccuracy, scan.alt, scan.vAccuracy); */
// No GPS or low accuracy GPS try Wifi Scan & Combain translation
if (posRes != LE_OK || scan.hAccuracy > HACCURACY_GOOD) {
if (posRes != LE_OK || scan.hAccuracy > HACCURACY_GOOD_GPS) {
le_result_t startRes;
if (scan.hAccuracy < HACCURACY_WIFI_ONLY)
{
SavedGpsScan = scan;
GpsScan = &SavedGpsScan;
}
if (!State.waitingForWifiResults) {
/* Legato WIFI is broken so we need to create a fake access point to do a scan */
const char *ssidPtr = "mangOH";
......@@ -429,19 +469,23 @@ static void Sample
}
// Good GPS
else if (posRes == LE_OK && scan.hAccuracy <= HACCURACY_GOOD)
else if (posRes == LE_OK && scan.hAccuracy <= HACCURACY_GOOD_GPS)
{
PackJson(GPS, &scan, json, sizeof(json));
LE_INFO("Sending dhub json: %s", json);
psensor_PushJson(ref, 0 /* now */, json);
GpsScan = NULL;
// Kill any errant WIFI sacn requests as we got GPS
if (State.waitingForWifiResults)
// Kill any errant WIFI scan or Combain requests as we got GPS
if (State.waitingForWifiResults || State.waitingForCombainResults)
{
ma_combainLocation_DestroyLocationRequest(State.combainHandle);
State.waitingForWifiResults = false;
}
}
else
LE_ERROR("SHOULD NOT REACH HERE");
}
......
......@@ -67,6 +67,8 @@ apps:
#endif // MANGOH_BOARD
$MANGOH_ROOT/apps/locationService/combainLocation
$MANGOH_ROOT/apps/locationService/location
$MANGOH_ROOT/apps/MqttClient/mqttClient
$MANGOH_ROOT/apps/DataRouter/dataRouter
$MANGOH_ROOT/apps/DataRouter/drTool/drTool
......@@ -101,6 +103,7 @@ interfaceSearch:
#elif ${MANGOH_BOARD} = yellow
$LEGATO_ROOT/apps/sample/dataHub
$MANGOH_ROOT/apps/Bme680EnvironmentalSensor
$LEGATO_ROOT/apps/sample/dataHub/components/periodicSensor
#endif // MANGOH_BOARD
......@@ -124,6 +127,7 @@ componentSearch:
${CURDIR}/apps/GpioExpander/gpioExpanderService
#elif ${MANGOH_BOARD} = yellow
${CURDIR}/apps/YellowSensorToCloud/interfaces
$LEGATO_ROOT/apps/sample/dataHub/components
#endif
}
......
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