tarinactrl

git clone https://git.tarina.org/tarinactrl
Log | Files | Refs

commit a957febfe927e79943f8040b04f3832f86ffcec1
Author: rob <rob@tarina.org>
Date:   Sat, 10 Dec 2022 18:33:10 +0200

first

Diffstat:
Atarinactrl.py | 167+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 167 insertions(+), 0 deletions(-)

diff --git a/tarinactrl.py b/tarinactrl.py @@ -0,0 +1,167 @@ +#!/usr/bin/env/ python +# -*- coding: utf-8 -*- + +#code by rbckman +#freesoftware, copy,share&remix=care + +import os +import time +import config +import socket +from blessed import Terminal +from multiprocessing import Process, Queue + +term = Terminal() + +server = config.server +port = config.port +clientpath = config.clientpath +clientup = "" +event = "" +nextstatus = '' +serverstatus = '' +os.system('clear') +selected = 1 +camselected = 0 +newselected=None +recording=None + +port = 55555 +cameras = ["192.168.75.212"] +menu = ["","ADD CAMERA","SYNC",""] + +##---------------Connection---------------------------------------------- + +def sendtocamera(host, port, data): + print("Sending to "+host+" on port "+str(port)+" DATA:"+data) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + while True: + try: + s.connect((host, port)) + s.send(str.encode(data)) + break + except: + ('trying again...') + print("Sent to server..") + s.close() + +##--------------Listen for Clients----------------------- + +def listenforclients(host, port, q): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind((host,port)) + try: + s.listen(1) + c, addr = s.accept() + while True: + data = c.recv(1024) + if not data: + break + status = data + c.close() + q.put(status) + break + except: + q.put('') + +##----------------------Main loop starts------------------------- + +def main(serverstatus): + global nextstatus, selected, cameras, menu, camselected, recording + newselected=camselected + print(term.home+term.on_red+term.clear) + if recording != None: + print("Camera "+ str(recording)+" is recording! Shut up. And Action!") + a=0 + for i in menu: + if a != selected: + print(menu[a]) + else: + print(menu[a]+'<---') + a=a+1 + a=0 + for i in cameras: + if a != camselected: + print("["+str(a)+"] camera"+str(a+1)+" "+i) + else: + print("["+str(a)+"] camera"+str(a+1)+" "+i+" <-----") + a=a+1 + with term.cbreak(): + val=term.inkey(timeout=0) + if val.is_sequence: + event=val.name + elif val: + event=val + else: + event='' + print(event) + if event == "KEY_ESCAPE": + nextstatus = '' + serverstatus = 'exit' + if event == "KEY_ENTER" and menu[selected] == "ADD CAMERA": + cam=str(len(cameras)+1) + if event == 'KEY_ENTER': + ip=input("ip address of tarina camera"+cam+"?:") + cameras.append(ip) + elif event == "KEY_UP": + if selected > 0: + selected = selected - 1 + elif event == "KEY_DOWN": + if selected < 2: + selected = selected + 1 + elif event == "R": + a=0 + for i in cameras: + if a == camselected: + sendtocamera(cameras[camselected],port,'RETAKE') + recording=camselected + #else: + # sendtocamera(cameras[camselected],port,'PLACEHOLDER') + elif event == "KEY_PGUP": + a=0 + for i in cameras: + if a == camselected: + sendtocamera(cameras[camselected],port,'REC') + if recording == camselected: + recording=None + else: + recording=camselected + else: + sendtocamera(cameras[camselected],port,'PLACEHOLDER') + elif event == "0": + newselected = 0 + elif event == "1": + newselected = 1 + elif event == "2": + newselected = 2 + elif event == "3": + newselected = 3 + elif event == "4": + newselected = 4 + if camselected != newselected: + #change camera + a=0 + for c in cameras: + if a == camselected: + sendtocamera(cameras[camselected],port,'STOP') + if a == newselected: + sendtocamera(cameras[newselected],port,'REC') + camselected=newselected + else: + return serverstatus + +if __name__ == '__main__': + serverstatus='' + q = Queue() + p = Process(target=listenforclients, args=('0.0.0.0', port, q)) + p.start() + while serverstatus != 'exit': + serverstatus = main(serverstatus) + if p.is_alive() == False: + serverstatus = q.get() + p = Process(target=listenforclients, args=('0.0.0.0', port, q)) + p.start() + time.sleep(0.1) + p.terminate() + quit()