Hello,
While I’m not deeply familiar with this particular use case in Python, perhaps someone else in the user community may be able to share additional insights or more advanced solutions.
However, I can suggest a general approach for reference that could help you get started with building such a solution:
- Authentication: First, you’ll need to authenticate and obtain a bearer token to make authorized API requests. Instructions on how to do this can typically be found here:
- API calls to retrieve Required Data: Once authenticated, you can send API requests to gather the needed modem and GPS data.
For modem status, you can run a query like this:
api get /modems/status | jsonfilter \
-e 'LAC=@.http_body.data[0].lac' \
-e 'MMC=@.http_body.data[0].cell_info[0].mcc' \
-e 'MNC=@.http_body.data[0].cell_info[0].mnc' \
-e 'TAC=@.http_body.data[0].tac' \
-e 'RSRP=@.http_body.data[0].rsrp' \
-e 'RSRQ=@.http_body.data[0].rsrq' \
-e 'CELL_ID=@.http_body.data[0].cellid' \
-e 'OPERATOR=@.http_body.data[0].operator'
For GPS position status:
api get /gps/position/status | jsonfilter \
-e 'SPEED=@.http_body.data.speed' \
-e 'COURSE=@.http_body.data.angle' \
-e 'ALTITUDE=@.http_body.data.altitude' \
-e 'GPS_TIME=@.http_body.data.utc_timestamp' \
-e 'LATITUDE=@.http_body.data.latitude' \
-e 'LONGITUDE=@.http_body.data.longitude' \
-e 'FIX_STATUS=@.http_body.data.fix_status' \
-e 'SATELLITES=@.http_body.data.satellites'
- Export to CSV file:
Once you’ve received the data in Python, you can structure the values into rows and columns using standard libraries like csv or pandas, and write this information to a CSV file.
For reference, leaving a Python script written with chatgpt:
Click to expand
import requests
import csv
import datetime
import os
# Configuration
API_BASE_URL = "https://your-api-server.com/api" # Change to your base URL
DEVICE_IDS = ["device1", "device2"] # List of your device IDs
AUTH_TOKEN = "your_auth_token_here" # If authentication is needed
CSV_FILE = "device_monitoring_data.csv"
HEADERS = {
"Authorization": f"Bearer {AUTH_TOKEN}",
"Content-Type": "application/json"
}
# Ensure CSV exists with headers
def initialize_csv(file_path):
if not os.path.exists(file_path):
with open(file_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow([
"Timestamp", "DeviceID",
"LAC", "MMC", "MNC", "TAC", "RSCP", "RSRQ", "CELL_ID", "OPERATOR",
"SPEED", "COURSE", "ALTITUDE", "GPS_TIME", "LATITUDE", "LONGITUDE", "FIX_STATUS", "SATELLITES"
])
# Get /modem/status data
def get_modem_status(device_id):
response = requests.get(f"{API_BASE_URL}/devices/{device_id}/modem/status", headers=HEADERS)
data = response.json().get("http_body", {}).get("data", {})
return {
"LAC": data.get("lac", "N/A"),
"MMC": data.get("mcc", "N/A"),
"MNC": data.get("mnc", "N/A"),
"TAC": data.get("tac", "N/A"),
"RSCP": data.get("rscp", "N/A"), # May not be available in LTE mode
"RSRQ": data.get("rsrq", "N/A"),
"CELL_ID": data.get("cellid", "N/A"),
"OPERATOR": data.get("operator", "N/A")
}
# Get /gps/position/status data
def get_gps_status(device_id):
response = requests.get(f"{API_BASE_URL}/devices/{device_id}/gps/position/status", headers=HEADERS)
data = response.json().get("http_body", {}).get("data", {})
return {
"SPEED": data.get("speed", "N/A"),
"COURSE": data.get("angle", "N/A"),
"ALTITUDE": data.get("altitude", "N/A"),
"GPS_TIME": data.get("utc_timestamp", "N/A"),
"LATITUDE": data.get("latitude", "N/A"),
"LONGITUDE": data.get("longitude", "N/A"),
"FIX_STATUS": data.get("fix_status", "N/A"),
"SATELLITES": data.get("satellites", "N/A")
}
# Combine and write to CSV
def fetch_and_store_data():
initialize_csv(CSV_FILE)
timestamp = datetime.datetime.utcnow().isoformat()
for device_id in DEVICE_IDS:
try:
modem_data = get_modem_status(device_id)
gps_data = get_gps_status(device_id)
with open(CSV_FILE, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([
timestamp, device_id,
modem_data["LAC"], modem_data["MMC"], modem_data["MNC"], modem_data["TAC"],
modem_data["RSCP"], modem_data["RSRQ"], modem_data["CELL_ID"], modem_data["OPERATOR"],
gps_data["SPEED"], gps_data["COURSE"], gps_data["ALTITUDE"], gps_data["GPS_TIME"],
gps_data["LATITUDE"], gps_data["LONGITUDE"], gps_data["FIX_STATUS"], gps_data["SATELLITES"]
])
print(f"[✓] Data recorded for {device_id}")
except Exception as e:
print(f"[!] Failed for {device_id}: {e}")
# Run
if __name__ == "__main__":
fetch_and_store_data()
This structure could be implemented with a scheduled script (e.g., via crontabs) that retrieves and stores the data each day. More information on crontabs can be found here:
I hope this brings you any of useful insights or hints.
Best regards,