1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2025-07-02 22:36:54 +02:00
Files
STARK/SmartHome/SmartHome.py

75 lines
2.0 KiB
Python
Raw Normal View History

2020-11-13 23:27:41 +02:00
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
2021-04-17 16:37:11 +03:00
from .lib_nrf24 import NRF24
2020-11-13 23:27:41 +02:00
import spidev
2021-04-17 16:18:46 +03:00
import time
import json as JSON
from Command import Command
from threading import Thread
pipe = [0xf0, 0xf0, 0xf0, 0xf0, 0xe1]
2020-11-13 23:27:41 +02:00
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setRetries(15,15)
radio.setPayloadSize(32)
radio.setChannel(0x60)
radio.setDataRate(NRF24.BR_250KBPS)
radio.setPALevel(NRF24.PA_HIGH)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
2021-04-17 16:18:46 +03:00
radio.openWritingPipe(pipe)
radio.openReadingPipe(1, pipe)
radio.startListening()
radio.stopListening()
2020-11-13 23:27:41 +02:00
2021-04-17 16:18:46 +03:00
radio.startListening()
2020-11-13 23:27:41 +02:00
class SmartHome(Command):
radio = radio
2021-04-17 16:18:46 +03:00
send_queue = []
2020-11-13 23:27:41 +02:00
def start(this, string): # main method
pass
@staticmethod
def send(data):
2021-04-17 16:18:46 +03:00
SmartHome.send_queue.append(data)
@staticmethod
def _send(data):
while radioIsBusy: tile.sleep(0.1)
string = JSON.dumps(data)
for char in string: radio.write(char)
@staticmethod
def receiveAndTransmit():
json = ''
while True:
for command in SmartHome.send_queue: _send(command)
# listening radio
while not radio.available(): time.sleep(0.01)
recv_buffer = []
radio.read(recv_buffer, radio.getDynamicPayloadSize())
if recv_buffer[0] != 10:
json += chr(recv_buffer[0])
continue
print(json)
# parsing of received data
data = JSON.loads(json)
2021-04-17 23:11:40 +03:00
if data.get('target') != 'hub':
json = ''
continue
if name := data.get('command'):
2021-04-17 16:18:46 +03:00
params = data.get('params') or {}
if cmd := Command.getCommand(name):
try: cmd.start(params)
except: pass
json = ''
receiveAndTransmitThread = Thread(target=SmartHome.receiveAndTransmit)
receiveAndTransmitThread.start()