-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi7600.py
531 lines (472 loc) · 17.6 KB
/
pi7600.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import asyncio
import sys
import time
import serial
BUFFER_CHAR_LIMIT = 559 # "AT" prefix does not count
EXIT_SUCCESS_CODE = 0
EXIT_FAILURE_CODE = 1
TIMEOUT = 3
SMS_SEND_TIMEOUT = 15
BUFFER_WAIT_TIME = 0.01
GPS_TIMEOUT = 1
GPS_RETRY = 10
PHONE_TIMEOUT = 1
BAUDRATE = 115200
COM = "/dev/ttyUSB2"
WATCHER_COM = "/dev/ttyUSB3"
POLL = 5
def py_version_check() -> bool:
"""
Python minimum version check (3.10)
:return: bool
"""
try:
if float(sys.version[: sys.version[2:].find(".") + 2]) < 3.10:
print("Python version must be 3.10 or greater")
print("\nExiting...")
return False
except:
user_input = input(
"Python version check failed. Depends on 3.10 or greater, continue anyways(y/N?"
).lower()
if user_input in ["", "n"]:
print("\nExiting...")
return False
return True
class SingletonMeta(type):
"""
This metaclass ensures that only one instance of any class using it exists.
"""
_instances = {} # Dictionary to hold single instances
def __call__(cls, *args, **kwargs):
"""
If an instance of cls doesn't exist, create one and store it in _instances.
Otherwise, return the existing instance.
"""
if cls not in cls._instances:
# print(f"Creating new instance of {cls.__name__}")
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
else:
# print(f"Using existing instance of {cls.__name__}")
pass
return cls._instances[cls]
class AT:
def __init__(self, com: str, baudrate: int) -> None:
self.com = com
self.baudrate = baudrate
self.ser = self.init_serial(baudrate, com)
self.rec_buff = ""
self.write_queue = asyncio.Queue() # Queue for managing outgoing commands
self.task = None # Task for processing the write queue
def init_serial(self, baud, com):
ser = serial.Serial(com, baud, rtscts=True, timeout=0.5)
return ser
async def send_at(self, command: str, back: str, timeout: int) -> str:
"""
Send an AT command and wait for the expected response.
:param command: str - AT command to be sent
:param back: str - Expected response to check
:param timeout: int - Timeout to wait for a response
:return: str - The response from the device, or an error message.
"""
if self.task is None or self.task.done():
self.task = asyncio.create_task(self.process_write_queue())
self.clear_buffer()
self.ser.write((command + "\r\n").encode())
start_time = time.time()
while True:
if self.ser.in_waiting > 0:
self.rec_buff += self.ser.read(self.ser.in_waiting).decode(
errors="ignore"
)
if back in self.rec_buff:
response = self.rec_buff
self.clear_buffer()
return response
if time.time() - start_time > timeout:
self.clear_buffer()
return f"ERROR: Timeout while waiting for response to '{command}'"
# Yield control to other tasks and wait a bit before checking again
await asyncio.sleep(0.1)
async def process_write_queue(self):
"""
Asynchronously process the write queue and send commands.
"""
while True:
command, back, timeout, repeat = await self.write_queue.get()
response = await self.send_at(command, back, timeout)
if "ERROR" in response:
print(f"Failed to execute: {command}, Error: {response}")
else:
print(f"Successfully executed: {command}, Response: {response}")
# Re-enqueue if repeat is True
if repeat:
await asyncio.sleep(POLL)
await self.write_queue.put((command, back, timeout, repeat))
# Mark task as done if not repeating
if not repeat:
self.write_queue.task_done()
def close_serial(self) -> None:
try:
self.ser.close()
except:
print("Failed to close serial: Already closed or inaccessible")
def clear_buffer(self) -> None:
if self.ser.in_waiting:
self.ser.flush()
self.rec_buff = ""
class Settings(metaclass=SingletonMeta):
def __init__(self, com=COM, baudrate=BAUDRATE) -> None:
"""
Initializes Settings class
:param port: str
:param baudrate: int
"""
self.at = AT(com=com, baudrate=baudrate)
self.first_run = True
def __getattr__(self, name):
try:
return getattr(self.at, name)
except AttributeError:
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)
async def perform_initial_checks(self) -> None:
"""
Initial environment checks asynchronously.
"""
checks = {
"Python version requirements not met": lambda: py_version_check(),
"SIM device not ready": lambda: self.sim_ready_check(),
}
check_failed = False
for check, result_function in checks.items():
result = await result_function() # Await the result from the function
if result is False:
check_failed = True
print(check)
if check_failed:
sys.exit(EXIT_FAILURE_CODE)
else:
self.first_run = False
async def enable_verbose_logging(self) -> bool:
if await self.send_at("AT+CMEE=2", "OK", TIMEOUT):
return True
return False
async def sim_ready_check(self) -> bool:
if await self.send_at("AT+CPIN?", "READY", TIMEOUT):
return True
return False
async def get_config(self) -> str | bool:
if await self.send_at("AT&V", "OK", TIMEOUT):
return True
return False
async def set_usb_os(self, os: str) -> bool:
"""
USB setting for RNDIS, OS specific. "WIN" or "UNIX".
:param os: str
:return: bool
"""
if os == "WIN":
await self.send_at("AT+CUSBPIDSWITCH=9001,1,1", "OK", TIMEOUT)
elif os == "UNIX":
await self.send_at("AT+CUSBPIDSWITCH=9011,1,1", "OK", TIMEOUT)
for _ in range(6): # Wait up to 3 mins for reboot
time.sleep(30)
try:
self.init_serial(BAUDRATE, COM)
if await self.send_at("AT", "OK", TIMEOUT):
print(f"Set usb for {os}")
return True
except:
print("Waiting for device to reboot...")
print("Failed to set USB mode.")
return False
async def set_sms_storage(self, mode: str) -> bool:
"""
Set SMS storage location
:param mode: str
:return: bool
"""
if await self.send_at(
f'AT+CPMS="{mode}","{mode}","{mode}"', "OK", TIMEOUT
): # Store messages on SIM(SM), "ME"/"MT" is flash
return True
return False
async def set_data_mode(self, mode: int) -> None:
"""
HEX is automatically used if there is data issues, such as low signal quality.
:param mode: int
:return: None
"""
if mode == 1:
await self.send_at("AT+CMGF=1", "OK", TIMEOUT) # Set to text mode
if mode == 0:
await self.send_at("AT+CMGF=0", "OK", TIMEOUT) # Set to hex mode
async def set_encoding_mode(self, mode: int) -> None:
"""
Set encoding mode. 0=IRA, 1=GSM, 2=UCS2
:param mode: int
:return: None
"""
if mode == 2:
await self.send_at('AT+CSCS="UCS2"', "OK", TIMEOUT)
if mode == 1:
await self.send_at('AT+CSCS="GSM"', "OK", TIMEOUT)
if mode == 0:
await self.send_at('AT+CSCS="IRA"', "OK", TIMEOUT)
class GPS:
"""
Initialize the GPS class.
"""
def __init__(self):
self.settings = Settings()
self.loc = ""
self.is_running = False # Initialized to False; actual status will be checked asynchronously later
def __getattr__(self, name):
try:
return getattr(self.settings, name)
except AttributeError:
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)
async def session_check(self):
check = await self.send_at("AT+CGPS?", "+CGPS", TIMEOUT)
self.is_running = True if "+CGPS: 1,1" in check else False
return self.is_running
async def gps_session(self, start: bool) -> bool:
"""
True to start session. False to close session.
:param start: bool
:return: bool
"""
await self.session_check()
if start:
print("Starting GPS session...")
if await self.send_at(
"AT+CGPS=0,1", "OK", GPS_TIMEOUT
) and await self.send_at("AT+CGPS=1,1", "OK", GPS_TIMEOUT):
print("Started successfully")
await asyncio.sleep(2)
self.is_running = True
return True
else:
print("Closing GPS session...")
self.rec_buff = ""
if await self.send_at("AT+CGPS=0,1", "OK", GPS_TIMEOUT):
self.is_running = False
return True
else:
print("Error closing GPS, is it open?")
return False
async def get_gps_position(self, retries: int = GPS_RETRY) -> str | bool:
await self.session_check() # Ensure session status is checked asynchronously
if self.is_running:
for _ in range(retries):
answer = await self.send_at("AT+CGPSINFO", "+CGPSINFO: ", GPS_TIMEOUT)
if answer and ",,,,,," not in answer:
return answer
elif ",,,,,," in answer:
return "GPS is active but no signal was found"
else:
print("Error accessing GPS, attempting to close session")
if not await self.gps_session(False): # Await the async method
print("GPS was not found or did not close correctly")
else:
print("Done")
return False
print("Retry limit reached; GPS signal not found...")
return False
else:
print(
"Attempting to get location without an open GPS session, trying to open one now..."
)
await self.gps_session(True) # Await the async method
return await self.get_gps_position() # Await the async recursive call
def parse_sms(sms_buffer: str) -> list:
"""
Parses the modem sms buffer into a list of dictionaries
:param sms_buffer: str
:return: list<dict>
"""
read_messages = sms_buffer.split("\r\n")
read_messages = read_messages[
1:-3
] # first and last few values are just cmd and resp code
message_list = []
for i, v in enumerate(read_messages):
if not i & 1: # Even idx has msg info, odd is msg content for preceding idx
message = v.replace('"', "", 9).split(",")
message_list.append(
{
"message_index": message[0][message[0].rfind(" ") + 1 :],
"message_type": message[1],
"message_originating_address": message[2],
"message_destination_address": message[3],
"message_date": message[4][1:],
"message_time": message[5][:-1],
"message_contents": read_messages[
i + 1
], # idx + 1 is always message content
}
)
return message_list
class Phone:
"""
Initialize the Phone class.
"""
def __init__(self):
self.settings = Settings()
def __getattr__(self, name):
try:
return getattr(self.settings, name)
except AttributeError:
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)
async def hangup_call(self) -> bool:
if await self.send_at("AT+CHUP", "OK", PHONE_TIMEOUT):
return True
return False
def call_incoming(self):
# call_incoming(): Check for incoming calls
# I will come back to this after I determine concurrency/interrupts/chaining AT commands
pass
async def active_calls(self) -> str | bool:
"""
Returns information on any active calls
:return: str || bool
"""
if await self.send_at("AT+CLCC?", "OK", PHONE_TIMEOUT):
return True
return False
async def call(self, contact_number: str, retry: int = 0) -> bool:
"""
Start outgoing call.
:param contact_number: str
:param retry: int
:return: bool
"""
# A True return does not mean call was connected, simply means the attempt was valid without errors.
attempt = 1
try:
while True:
print(
f"Attempting to call {contact_number}; Attempt: {attempt}; Retry: {retry}"
)
# IF ATD returns an error then determine source of error.
if await self.send_at(
"ATD" + contact_number + ";", "OK", PHONE_TIMEOUT
):
input("Call connected!\nPress enter to end call")
# self.ser.write('AT+CHUP\r\n'.encode()) # Hangup code, why is serial used vs send_at()?
self.hangup_call()
print("Call disconnected")
return True
elif retry == 0 or attempt == retry:
return True
elif retry != 0:
print(
f"Retrying call to {contact_number}; Attempt: {attempt}/{retry}"
)
attempt += 1
except:
return False
def closed_call(self) -> str:
# This will display information about calls that have been made/attempted
# Call time, connection status, error outputs, etc
pass
class SMS:
"""
Initialize the SMS class.
"""
def __init__(self):
self.settings = Settings()
def __getattr__(self, name):
try:
return getattr(self.settings, name)
except AttributeError:
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)
async def receive_message(self, message_type: str) -> list:
"""
Sends SMS command to AT
:param message_type: str
:return: list<dict>
"""
# self.set_data_mode(1)
answer = await self.send_at(f'AT+CMGL="{message_type}"', "OK", TIMEOUT)
if answer:
if message_type != "ALL" and message_type in answer:
answer = parse_sms(answer)
return answer
elif message_type == "ALL":
answer = parse_sms(answer)
return answer
else:
print(f"AT command failed, returned the following:\n{answer}")
def read_message(self, message_type: str) -> list:
"""
Reads message from specified message type.
:param message_type: str
:return: list<dict>
"""
try:
buffer = self.receive_message(message_type)
return buffer
except Exception as e:
print("Error:", e)
if self.ser is not None:
self.ser.close()
def loop_for_messages(self, message_type: str) -> list:
"""
Starts a loop that reads message(s) from specified message type.
:param message_type: str
:return: str
"""
while True:
try:
buffer = self.receive_message(message_type)
return buffer
except Exception as e:
print(f"Unhandled error: {e}")
if self.ser is not None:
self.ser.close()
async def send_message(self, phone_number: str, text_message: str) -> bool:
answer = await self.send_at('AT+CMGS="' + phone_number + '"', ">", TIMEOUT)
if answer:
self.ser.write(text_message.encode())
self.ser.write(b"\x1a")
# 'OK' here means the message sent?
answer = await self.send_at("", "OK", SMS_SEND_TIMEOUT)
if answer:
print(
f"Number: {phone_number}\n"
f"Message: {text_message}\n"
f"Message sent!"
)
return True
else:
print(
f"Error sending message...\n"
f"phone_number: {phone_number}\n"
f"text_message: {text_message}\n"
f"Not sent!"
)
return False
else:
print(f"error: {answer}")
return False
async def delete_message(self, msg_idx: int) -> dict:
"""delete message by index
Args:
msg_idx (int): message to delete
Returns:
dict: {"response": "Success" | False}
"""
resp = await self.send_at(f"AT+CMGD={msg_idx}", "OK", TIMEOUT)
if resp:
return {"response": "Success"}
else:
return {"response": False}