-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (87 loc) · 3.03 KB
/
main.py
File metadata and controls
114 lines (87 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
import random
from paho.mqtt.client import *
from dotenv import load_dotenv
from src.gps.GPSDataReader import read_satellite_data
import os
import ssl
UPDATE_INTERVAL = 60
satellites = {}
def update_position(msg):
current_prns = set()
for sat in msg.get("satellites", []):
prn = sat.get("PRN")
if prn is None:
continue
current_prns.add(prn)
azimuth = sat.get("az", random.uniform(0, 360))
elevation = max(0, min(90, sat.get("el", random.uniform(0, 10))))
if prn in satellites:
satellites[prn]["azimuth"] = azimuth % 360
satellites[prn]["elevation"] = elevation
satellites[prn]["signal"] = sat.get("ss", 0)
satellites[prn]["used"] = sat.get("used", False)
satellites[prn]["fading"] = False
else:
satellites[prn] = {
"id": prn,
"azimuth": azimuth % 360,
"elevation": elevation,
"signal": sat.get("ss", 0),
"used": sat.get("used", False),
"fading": False,
}
for prn in list(satellites.keys()):
if prn not in current_prns:
satellites[prn]["fading"] = True
def simulate_movement():
to_remove = []
for prn, sat in satellites.items():
if sat["fading"]:
sat["elevation"] -= 2
if sat["elevation"] <= 0:
to_remove.append(prn)
else:
sat["azimuth"] = (sat["azimuth"] + random.uniform(-3, 3)) % 360
sat["elevation"] = max(0, min(90, sat["elevation"] + random.uniform(-1, 1)))
for prn in to_remove:
del satellites[prn]
def get_public_payload():
return {
"visibleCount": len(satellites),
"usedCount": sum(1 for sat in satellites.values() if sat.get("used", False)),
"hdop": round(random.uniform(0.8, 2.5), 2),
"satellites": list(satellites.values()),
}
def main():
last_update = time.time()
print("Starting simulation...")
load_dotenv()
user = os.getenv("MQTT_USER")
password = os.getenv("MQTT_PASSWORD")
host = os.getenv("MQTT_HOST")
port = os.getenv("MQTT_PORT", "8883")
if not host:
raise ValueError("MQTT_HOST is required")
client = Client(client_id="", userdata=None, protocol=MQTTv5)
client.tls_set(tls_version=ssl.PROTOCOL_TLS_CLIENT)
client.username_pw_set(user, password)
client.connect(host, int(port))
client.loop_start()
try:
for msg in read_satellite_data():
if msg is None:
continue
update_position(msg)
now = time.time()
if now - last_update >= UPDATE_INTERVAL:
simulate_movement()
payload = get_public_payload()
client.publish("gps/satellites", json.dumps(payload))
last_update = now
except KeyboardInterrupt:
print("Stopped by user.")
client.loop_stop()
client.disconnect()
if __name__ == "__main__":
main()