1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2025-07-12 22:50:22 +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 self._return = None
def run(self): def run(self):
if self._target is not None: if self._target: self._return = self._target(*self._args, **self._kwargs)
self._return = self._target(*self._args, **self._kwargs)
def join(self, *args, **kwargs): def join(self, *args, **kwargs):
super().join(*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 import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
from .lib_nrf24 import NRF24 from lib_nrf24 import NRF24
import spidev 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 = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17) radio.begin(0, 17)
time.sleep(1)
radio.setRetries(15,15) radio.setRetries(15,15)
radio.setPayloadSize(32) radio.setPayloadSize(32)
radio.setChannel(0x60) radio.setChannel(0x60)
radio.setDataRate(NRF24.BR_250KBPS) radio.setDataRate(NRF24.BR_250KBPS)
radio.setPALevel(NRF24.PA_HIGH) radio.setPALevel(NRF24.PA_HIGH)
radio.setAutoAck(True) radio.setAutoAck(True)
radio.enableDynamicPayloads() radio.enableDynamicPayloads()
radio.enableAckPayload() 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): class SmartHome(Command):
radio = radio radio = radio
send_queue = []
def start(this, string): # main method def start(this, string): # main method
pass pass
@staticmethod @staticmethod
def send(data): def send(data):
string = '{' SmartHome.send_queue.append(data)
string += ','.join([f'"{key}": "{value}"' for key, value in data.items()])
string += '}\n' @staticmethod
for char in string: def _send(data):
radio.write(char) 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', 'target': 'window',
'cmd': 'window_open', 'cmd': 'window_open',
}) })
voice = text = 'Поднимаю роллеты' voice = text = ''
return Response(text = text, voice = voice) return Response(text = text, voice = voice)
keywords = {} keywords = {}
@ -22,7 +22,7 @@ def method(params):
'target': 'window', 'target': 'window',
'cmd': 'window_close', 'cmd': 'window_close',
}) })
voice = text = 'Опускаю роллеты' voice = text = ''
return Response(text = text, voice = voice) return Response(text = text, voice = voice)
keywords = {} keywords = {}

View File

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

View File

@ -2,6 +2,8 @@
import os import os
import config import config
os.system('git pull')
modules = { modules = {
'Voice Assistant': 'voice_assistant', 'Voice Assistant': 'voice_assistant',
'Telegram bot': 'telegram_bot', 'Telegram bot': 'telegram_bot',
@ -10,9 +12,10 @@ modules = {
for name, module in modules.items(): for name, module in modules.items():
try: try:
print(f'launching the {name}') 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: except:
print(f'[error]\t{name} launch failed') 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"') os.system(f'lxterminal --command="vlc"')