Skip to content

Commit

Permalink
refactor test rockblock web server to take in both data and error code (
Browse files Browse the repository at this point in the history
#450)

* refactor test rockblock server to intake waypoint data and error code

* add documentation for refactor of rockblock web server

* fix for linter
  • Loading branch information
samdai01 authored Oct 31, 2024
1 parent 8c9ea21 commit a98da3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ TEST_F(TestRemoteTransceiver, rockblockWebServerExample)
curl = curl_easy_init();

if (curl != nullptr) {
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8100/?data=B&imei=300434065264590&username=myuser");
curl_easy_setopt(
curl, CURLOPT_URL, "http://localhost:8100/?data=thisistestdata&ec=B&imei=300434065264590&username=myuser");

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");

Expand Down
11 changes: 8 additions & 3 deletions src/network_systems/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ This web server is a virtual implementation of the Rockblock Web Servers used to
[here](https://docs.groundcontrol.com/iot/rockblock/web-services/sending-mt-message).

This virtual server follows this specification with one notable difference, it will return the specific error code that
is contained in the `data` section of the request sent to it.
is contained in the `ec` section of the request sent to it.

So a request to this url: <http://localhost:8100/?data=B&imei=300434065264590&username=myuser> will return
`FAILED,11, No RockBLOCK with this IMEI found on your account` since `data=B` and B is hex for 11 which is the
So a request to this url: <http://localhost:8100/?data=thisistestdata&ec=B&imei=300434065264590&username=myuser> will
return `FAILED,11, No RockBLOCK with this IMEI found on your account` since `ec=B` and B is hex for 11 which is the
corresponding error code as mentioned above.

Continuing on, the data parameter now implements the functionality for any form of data to be handled by the server.
This data will be written to `TEMP_FILE_PATH` so that any tests can properly verify that the rockblock web server will
have received the correct data. So as above, if all goes well, `thisistestdata` should be written to the file located
at `TEMP_FILE_PATH`.
32 changes: 19 additions & 13 deletions src/network_systems/scripts/rockblock_web_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import http.server
import logging
import os
import random
import signal
import socketserver
Expand All @@ -24,29 +25,32 @@
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)

TEMP_FILE_PATH = "/tmp/remote_transceiver/downstream_test.log"


# Custom HTTP request handler
class MyHandler(http.server.BaseHTTPRequestHandler):

def do_POST(self):
logger.debug("Received POST request.")
# Extract data parameter from the request
data = self.get_data()
data, error_code = self.get_data()
logger.debug(f"Extracted data: {data}")

# Check if data is not empty
if data:
try:
# Decode the hex data
error_code = int(data, 16)
logger.debug(f"Decoded error code: {error_code}")
except ValueError:
# If decoding fails, return error code 99
logger.error("Failed to decode data. Using error code 99.")
error_code = 99
else:
# Default error code if data is empty
logger.warning("No data found. Using error code 99.")
os.makedirs(os.path.dirname(TEMP_FILE_PATH), exist_ok=True)
with open(TEMP_FILE_PATH, "w") as f:
f.write(data)
logger.info(f"Data written to {TEMP_FILE_PATH}")
logger.info(f"Data written is: {data}")
except Exception as e:
logger.error(f"Failed to write data to file: {e}")

try:
error_code = int(error_code, 16) if error_code else 99
except ValueError:
logger.error("Failed to decode ec parameter. Using error code 99.")
error_code = 99

# Handle error codes and special case for error_code 0
Expand Down Expand Up @@ -75,7 +79,9 @@ def get_data(self):
"""Extracts the data parameter from the URL query string (e.g., ?data=48656C6C6F)"""
query = parse.urlsplit(self.path).query
params = dict(parse.parse_qsl(query))
return params.get("data", "")
data = params.get("data", "")
error_code = params.get("ec", "")
return data, error_code


# Set up the server
Expand Down

0 comments on commit a98da3d

Please sign in to comment.