GPS Positioning
The M2000 gateway supports GPS positioning. Use Call the RESTful API to call the GPS APIs:
Read the GPS state: the
gpsfield in theGET /cassia/inforesponse, refer to Get/Set Gateway InfoEnable GPS at startup:
POST /cassia/infoto setdongle.gps_startupto"1"Request positioning:
GET /cassia/gps/request, returns after positioning succeeds
Note
The gateway must support the
gpsfeature (featuresin the/cassia/inforesponse containsgps)Right after the gateway boots, the GPS module is still initializing and the API may return
gps is startingWithout satellite reception,
/cassia/gps/requestcan block for a long time. Always set a timeout withasyncio.wait_for()
Example
1import asyncio
2import json
3
4import cassiablue
5
6REQUEST_TIMEOUT_S = 30
7
8
9def dms_to_deg(dms, sign):
10 d, m, s = str(dms).split(".")
11 deg = int(d) + int(m) / 60 + int(s) / 3600
12 return -deg if sign in ("S", "W") else deg
13
14
15def print_gps(gps):
16 if not gps:
17 print("no gps data")
18 return
19
20 lat, lon = gps.get("LAT"), gps.get("LON")
21 if lat and lon:
22 print("lat:", dms_to_deg(lat, gps.get("NS")))
23 print("lon:", dms_to_deg(lon, gps.get("WE")))
24 print("time(UTC):", gps.get("TIME"))
25 else:
26 print("no fix")
27
28 for i in (1, 2, 3):
29 sv = gps.get("SATELLITE%d" % i)
30 if sv:
31 print("satellite:", sv, "snr:", gps.get("SATELLITE%d_SNR" % i))
32
33
34async def read_gps_state():
35 ok, ret = await cassiablue.send_cmd("/cassia/info")
36 if not ok:
37 print("get info failed:", ret)
38 return
39 print_gps(json.loads(ret).get("gps"))
40
41
42async def request_gps():
43 try:
44 ok, ret = await asyncio.wait_for(cassiablue.send_cmd("/cassia/gps/request"), REQUEST_TIMEOUT_S)
45 except asyncio.TimeoutError:
46 print("gps request timeout, no fix yet")
47 return
48
49 if not ok:
50 print("gps request failed:", ret)
51 return
52 print_gps(json.loads(ret).get("gps"))
53
54
55async def main():
56 print("read cached gps state")
57 await read_gps_state()
58
59 print("request gps")
60 await request_gps()
61
62
63asyncio.run(main())
API
Request Positioning
ok, result = await asyncio.wait_for(cassiablue.send_cmd("/cassia/gps/request"), 30)
Successful response example:
{
"gps": {
"TIME": "07:40:09",
"LON": "117.14.18",
"LAT": "32.47.21",
"NS": "N",
"WE": "E",
"SATELLITE1": 8,
"SATELLITE1_SNR": 37,
"SATELLITE2": 26,
"SATELLITE2_SNR": 44,
"SATELLITE3": 31,
"SATELLITE3_SNR": 46
}
}
Field |
Type |
Description |
Example |
|---|---|---|---|
TIME |
str |
UTC time |
|
LON |
str |
Longitude (degrees.minutes.seconds) |
|
LAT |
str |
Latitude (degrees.minutes.seconds) |
|
NS |
str |
Hemisphere ( |
|
WE |
str |
Hemisphere ( |
|
SATELLITE1~3 |
int |
Satellite ID (0 means none) |
8 |
SATELLITE1~3_SNR |
int |
Satellite SNR |
37 |
Coordinate Format
LAT / LON use the degrees.minutes.seconds format, not decimal degrees. Conversion:
32.47.21 = 32 + 47/60 + 21/3600 = 32.789167
When NS is S or WE is W, the corresponding coordinate is negative.
Read the GPS State
The gps field in the GET /cassia/info response is the latest GPS state, with the same fields as the table above. Without a fix, LAT / LON are empty strings.
Enable GPS at Startup
body = json.dumps({"dongle": {"gps_startup": "1"}})
ok, result = await cassiablue.send_cmd("/cassia/info", "POST", None, body)
Refer to Get/Set Gateway Info for how to set it. After the configuration changes, the gateway reboots and GPS is enabled automatically after the reboot.