Skip to content

Commit

Permalink
Fix for LoRa sensor data 500 error
Browse files Browse the repository at this point in the history
  • Loading branch information
pvgennip committed Feb 21, 2019
1 parent e0dca82 commit 2ff4186
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions app/Http/Controllers/Api/SensorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private function last_sensor_increment_values($sensor, $data_array=null)

for ($i=0; $i < $limit; $i++)
{
if (isset($output[$i]))
if (isset($output[$i]) && gettype($output[$i]) == 'array')
{
foreach ($output[$i] as $key => $val)
{
Expand Down Expand Up @@ -422,7 +422,7 @@ private function storeMeasurements($data_array)
{
if (!in_array('key', array_keys($data_array)) || $data_array['key'] == '' || $data_array['key'] == null)
{
Storage::disk('local')->put('sensors/sensor_no_key.log', json_encode($sensor));
Storage::disk('local')->put('sensors/sensor_no_key.log', json_encode($data_array));
return Response::json('No key provided', 400);
}

Expand All @@ -431,7 +431,7 @@ private function storeMeasurements($data_array)
$sensor = Sensor::where('key', $sensor_key)->first();
if(!$sensor)
{
Storage::disk('local')->put('sensors/sensor_invalid_key.log', json_encode($sensor));
Storage::disk('local')->put('sensors/sensor_invalid_key.log', json_encode($data_array));
return Response::json('No valid key provided', 401);
}

Expand Down Expand Up @@ -723,9 +723,20 @@ public function lora_sensors(Request $request)
//die(print_r($data_obj));

if ($request->has('LrnDevEui'))
$data_array['key'] = $request->input('LrnDevEui');
else if (isset($data_obj['LrnDevEui'])) // KPN Simpoint msg
$data_array['key'] = $data_obj['LrnDevEui'];
if (Sensor::where('key', $request->input('LrnDevEui'))->count() > 0)
$data_array['key'] = $request->input('LrnDevEui');

if (isset($data_obj['LrnDevEui'])) // KPN Simpoint msg
if (Sensor::where('key', $data_obj['LrnDevEui'])->count() > 0)
$data_array['key'] = $data_obj['LrnDevEui'];

if (isset($data_obj['DevEUI_uplink']['DevEUI'])) // KPN Simpoint msg
if (Sensor::where('key', $data_obj['DevEUI_uplink']['DevEUI'])->count() > 0)
$data_array['key'] = $data_obj['DevEUI_uplink']['DevEUI'];

if (isset($data_obj['DevEUI_location']['DevEUI'])) // KPN Simpoint msg
if (Sensor::where('key', $data_obj['DevEUI_location']['DevEUI'])->count() > 0)
$data_array['key'] = $data_obj['DevEUI_location']['DevEUI'];

if (isset($data_obj['DevEUI_uplink']['LrrRSSI']))
$data_array['rssi'] = $data_obj['DevEUI_uplink']['LrrRSSI'];
Expand Down Expand Up @@ -757,7 +768,7 @@ public function lora_sensors(Request $request)

//die(print_r($data_array));
$logFileName = isset($data_array['key']) ? 'lora_sensor_'.$data_array['key'].'.json' : 'lora_sensor_no_key.json';
Storage::disk('local')->put('sensors/'.$logFileName, '["lora_object":'.json_encode($data_obj).',"data_array":'.json_encode($data_array).']');
Storage::disk('local')->put('sensors/'.$logFileName, '[{"lora_object":'.json_encode($data_obj).'},{"data_array":'.json_encode($data_array).'}]');

return $this->storeMeasurements($data_array);
}
Expand Down

0 comments on commit 2ff4186

Please sign in to comment.