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