-
Notifications
You must be signed in to change notification settings - Fork 2
/
wwan_keep_alive.py
executable file
·145 lines (121 loc) · 4 KB
/
wwan_keep_alive.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3 -u
import subprocess
import logging
import threading
import time
import os
import serial
logging.basicConfig(
format="%(asctime)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S", level=logging.INFO
)
DEFAULT_SLEEP_SECONDS = 3
def sleep(seconds=DEFAULT_SLEEP_SECONDS):
time.sleep(seconds)
def system(command: str) -> bool:
return os.system(command) == 0
def system_and_sleep(command: str) -> bool:
logging.info(f"Running command: {command}")
code = system(command)
sleep()
return code
def write_reboot(name: str):
logging.info("Writing entry to reboots.log...")
with open("/home/pi/reboots.log", "a") as f:
now = int(time.time())
f.write(f"{now} - {str} restarted.")
def connect_data():
logging.info("Entering connect_data()...")
while True:
logging.info("Connecting Data...")
if not system_and_sleep(
"qmicli -d /dev/cdc-wdm0 --dms-set-operating-mode='online'"
):
continue
if not system_and_sleep("ip link set wwan0 down"):
continue
if not system_and_sleep("echo 'Y' | tee /sys/class/net/wwan0/qmi/raw_ip"):
continue
if not system_and_sleep("ip link set wwan0 up"):
continue
if not system_and_sleep(
"qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network='ip-type=4' --client-no-release-cid"
):
continue
if not system_and_sleep("timeout 20 udhcpc -i wwan0"):
continue
sleep(10)
if not system_and_sleep("ping 8.8.8.8 -I wwan0 -c1 -W2") and not system_and_sleep("ping 1.1.1.1 -I wwan0 -c1 -W2"):
continue
logging.info("Connected Data!")
break
def connect_gps():
logging.info("Entering connect_gps()...")
while True:
logging.info("Connecting GPS...")
logging.info(
"Checking if /dev/ttyUSB1 exists. If it doesn't, we need to reboot the pi."
)
if not os.path.exists("/dev/ttyUSB1"):
logging.info("/dev/ttyUSB1 does not exist. Rebooting the system")
write_reboot("system")
if not system_and_sleep("reboot"):
logging.info("Could not reboot the system!!!")
sleep()
else:
sleep()
if not system_and_sleep(
"stty -F /dev/ttyS0 115200 raw -echo -echoe -echok -echoctl -echoke"
):
continue
ser = serial.Serial("/dev/ttyUSB2", 115200)
ser.write(b"AT+CGPS=0\r")
sleep()
ser.write(b"AT+CGPSDEL\r")
sleep()
ser.write(b"AT+CGPSAUTO=1\r")
sleep()
ser.write(b"AT+CGPS=1\r")
sleep()
if not system_and_sleep("systemctl restart gpsd"):
continue
logging.info("Connected GPS!")
break
def handle_data():
connect_data()
while True:
count = 0
while count < 10:
sleep(1)
if system("ping -c 1 -I wwan0 1.1.1.1 > /dev/null 2>&1") or system("ping -c 1 -I wwan0 8.8.8.8 > /dev/null 2>&1"):
count = 0
else:
count += 1
# reconnect data
write_reboot("data")
connect_data()
def handle_gps():
connect_gps()
while True:
count = 0
while count < 5:
sleep(1)
try:
subprocess.check_output(["bash", "-c", "timeout 10 gpspipe -w"])
except Exception as e:
if "TPV" in e.output.decode():
count = 0
else:
count += 1
logging.info(f"gps count = {count}")
# reconnect gps
write_reboot("gps")
connect_gps()
def main():
sleep()
data_thread = threading.Thread(target=handle_data)
data_thread.start()
gps_thread = threading.Thread(target=handle_gps)
gps_thread.start()
data_thread.join()
gps_thread.join()
main()