ping pong loop nearly working
This commit is contained in:
@@ -1,16 +1,41 @@
|
||||
from crypt_random import secure_random
|
||||
from lora_handler import LoRaHandler, LoRaMessage
|
||||
|
||||
|
||||
import time
|
||||
import uasyncio as asyncio
|
||||
|
||||
|
||||
class Connection:
|
||||
def load_public_constants(self):
|
||||
with open("public_constants.txt", "r") as f:
|
||||
self.P = int(f.readline(), 16)
|
||||
self.G = int(f.readline(), 16)
|
||||
|
||||
def __init__(self):
|
||||
self.load_public_constants()
|
||||
|
||||
self.private_key = int.from_bytes(secure_random(1024), "big")
|
||||
self.public_key = pow(self.G, self.private_key) % self.P
|
||||
# generate diffie-hellman keys
|
||||
print("Generating keys...")
|
||||
print(" - Generating private key...")
|
||||
self.private_key = int.from_bytes(secure_random(32), "big") # 32 bytes = 256 bits
|
||||
print(" - Generating public key...")
|
||||
self.public_key = pow(self.G, self.private_key, self.P)
|
||||
|
||||
# start lora handler
|
||||
self.lora = LoRaHandler()
|
||||
|
||||
async def advertising_loop(self):
|
||||
while True:
|
||||
self.lora.advertise()
|
||||
await asyncio.sleep(5)
|
||||
|
||||
async def start(self):
|
||||
print("Starting connection...")
|
||||
asyncio.create_task(self.advertising_loop())
|
||||
|
||||
while True:
|
||||
msg: LoRaMessage = await self.lora.message_queue.get()
|
||||
|
||||
print(f"Received message from {msg.sender_id}: {msg.payload}")
|
||||
print("Sending response...")
|
||||
self.lora.send(b"Pong!", msg.sender_id)
|
||||
|
||||
def load_public_constants(self):
|
||||
with open("public_constants.txt", "r") as f:
|
||||
self.P = int(f.readline(), 16)
|
||||
self.G = int(f.readline(), 16)
|
||||
Reference in New Issue
Block a user