1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2024-11-24 08:12:13 +02:00

SmartHome listening

This commit is contained in:
MarkParker5 2021-04-17 16:18:46 +03:00
parent f43de2d45d
commit 64b2aadb82
5 changed files with 58 additions and 21 deletions

View File

@ -39,8 +39,7 @@ class RThread(Thread):
self._return = None
def run(self):
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)
if self._target: self._return = self._target(*self._args, **self._kwargs)
def join(self, *args, **kwargs):
super().join(*args, **kwargs)

View File

@ -1,36 +1,71 @@
from Command import Command # import parent class
import time
# for nrf24l01
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
from .lib_nrf24 import NRF24
from lib_nrf24 import NRF24
import spidev
import time
import json as JSON
from Command import Command
from threading import Thread
pipe = [0xf0, 0xf0, 0xf0, 0xf0, 0xe1]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
time.sleep(1)
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()
radio.openWritingPipe(pipe)
radio.openReadingPipe(1, pipe)
radio.openWritingPipe([0xf0, 0xf0, 0xf0, 0xf0, 0xe1])
radio.startListening()
radio.stopListening()
radio.startListening()
class SmartHome(Command):
radio = radio
send_queue = []
def start(this, string): # main method
pass
@staticmethod
def send(data):
string = '{'
string += ','.join([f'"{key}": "{value}"' for key, value in data.items()])
string += '}\n'
for char in string:
radio.write(char)
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)
if name := data.get('name'):
params = data.get('params') or {}
if cmd := Command.getCommand(name):
try: cmd.start(params)
except: pass
json = ''
receiveAndTransmitThread = Thread(target=SmartHome.receiveAndTransmit)
receiveAndTransmitThread.start()

View File

@ -7,7 +7,7 @@ def method(params):
'target': 'window',
'cmd': 'window_open',
})
voice = text = 'Поднимаю роллеты'
voice = text = ''
return Response(text = text, voice = voice)
keywords = {}
@ -22,7 +22,7 @@ def method(params):
'target': 'window',
'cmd': 'window_close',
})
voice = text = 'Опускаю роллеты'
voice = text = ''
return Response(text = text, voice = voice)
keywords = {}

View File

@ -23,5 +23,5 @@ def command(request):
if not cmd: return HttpResponse('')
try: response = cmd.start(params)
except: return HttpResponse('')
if not cmd: return HttpResponse('')
return HttpResponse('')
json_string = json.dumps(response)
return HttpResponse(json_string)

View File

@ -2,6 +2,8 @@
import os
import config
os.system('git pull')
modules = {
'Voice Assistant': 'voice_assistant',
'Telegram bot': 'telegram_bot',
@ -10,9 +12,10 @@ modules = {
for name, module in modules.items():
try:
print(f'launching the {name}')
os.system(f'lxterminal --command="python3.8 {config.path}/{module}.py"')
os.system(f'lxterminal --command="python3.8 {config.path}/{module}.py & read"')
except:
print(f'[error]\t{name} launch failed')
os.system(f'lxterminal --command="python3.8 {config.path}/manage.py runserver 192.168.0.129:8000"')
print('Running server...')
os.system(f'lxterminal --command="python3.8 {config.path}/manage.py runserver 192.168.0.129:8000 & read"')
os.system(f'lxterminal --command="vlc"')