commit ed7b486c01cd7b38a2f23fc495e0f9950973b338
parent d89e3281759c908677ac68b38058cc31af05342c
Author: rob <rob@tarina.org>
Date: Fri, 17 Mar 2023 14:54:05 +0200
search for cameras
Diffstat:
M | tarinactrl.py | | | 66 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
1 file changed, 60 insertions(+), 6 deletions(-)
diff --git a/tarinactrl.py b/tarinactrl.py
@@ -10,10 +10,22 @@ import config
import socket
from blessed import Terminal
from multiprocessing import Process, Queue
+import ifaddr
term = Terminal()
-cameras = config.cameras
+searchforcameras=True
+networks=[]
+adapters = ifaddr.get_adapters()
+for adapter in adapters:
+ print("IPs of network adapter " + adapter.nice_name)
+ for ip in adapter.ips:
+ if '::' not in ip.ip[0] and '127.0.0.1' != ip.ip:
+ print(ip.ip)
+ networks.append(ip.ip)
+
+network=networks[0]
+cameras = []
camerasoff =[]
port = config.port
nextstatus = ''
@@ -26,12 +38,32 @@ mastersound=0
recording=None
sleep=0.2
rectime=None
+pingip=0
+local_ip=''
##---------------Connection----------------------------------------------
+def pingtocamera(host, port, data):
+ print("Sending to "+host+" on port "+str(port)+" DATA:"+data)
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.settimeout(0.01)
+ try:
+ while True:
+ s.connect((host, port))
+ s.send(str.encode(data))
+ if host not in cameras and host not in networks:
+ cameras.append(host)
+ print("Found camera! "+host)
+ print("Sent to server..")
+ break
+ except:
+ ('did not connect')
+ s.close()
+
def sendtocamera(host, port, data):
print("Sending to "+host+" on port "+str(port)+" DATA:"+data)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.settimeout(0.1)
try:
while True:
s.connect((host, port))
@@ -54,8 +86,6 @@ def listenforclients(host, port, q):
c, addr = s.accept()
while True:
data = c.recv(1024)
- if addr not in cameras:
- cameras.append(addr)
if not data:
break
ctrltime=time.time()
@@ -69,9 +99,14 @@ def listenforclients(host, port, q):
q.put('')
def printmenu(recording,selected,cameras,camselected,serverstatus,rectime):
+
print(term.home+term.clear)
print(time.time())
print(rectime)
+ print('your ip: '+network)
+ print('-~-')
+ print('Tarina CTRL')
+ print('-~-')
if serverstatus != None:
try:
print(serverstatus[0])
@@ -86,6 +121,7 @@ def printmenu(recording,selected,cameras,camselected,serverstatus,rectime):
if recording != None:
print("Camera "+ str(recording)+" is recording! Shut up. And Action!")
a=0
+ print('-~-')
for i in menu:
if a != selected:
print(menu[a])
@@ -93,6 +129,7 @@ def printmenu(recording,selected,cameras,camselected,serverstatus,rectime):
print(menu[a]+'<---')
a=a+1
a=0
+ print('-~-')
for i in cameras:
if i in camerasoff:
if a != camselected:
@@ -105,14 +142,15 @@ def printmenu(recording,selected,cameras,camselected,serverstatus,rectime):
else:
print("["+str(a)+"] camera"+str(a+1)+" "+i+" <-----")
a=a+1
+ print('-~-')
-menu = ["Quit","Add CAMERA","New SCENE","Sync SCENE","Record"]
+menu = ["Quit","Add CAMERA","New SCENE","Sync SCENE","Record","Search","Snapshot"]
##----------------------Main loop starts-------------------------
def main(serverstatus):
- global nextstatus, selected, cameras, menu, camselected, recording, sleep, rectime
+ global nextstatus, selected, cameras, menu, camselected, recording, sleep, rectime, pingip,searchforcameras
newselected=camselected
with term.cbreak():
val=term.inkey(timeout=0)
@@ -126,6 +164,12 @@ def main(serverstatus):
if event == "KEY_ESCAPE":
nextstatus = ''
serverstatus = 'exit'
+ elif event == "KEY_ENTER" and menu[selected] == "Search":
+ print('Searching for cameras')
+ if searchforcameras == True:
+ searchforcameras = False
+ else:
+ searchforcameras = True
elif event == "KEY_ENTER" and menu[selected] == "Quit":
print('take care, bye, bye, then')
serverstatus = 'exit'
@@ -250,8 +294,18 @@ def main(serverstatus):
camselected=newselected
# ping ip every 10 sec while not recording to connect cameras
-
+ if searchforcameras == True:
+ if recording == None:
+ if pingip < 256:
+ pingip+=1
+ else:
+ pingip=0
+ searchforcameras=False
+ pingtocamera(network[:-3]+str(pingip),port,'PING')
printmenu(recording,selected,cameras,camselected,serverstatus,rectime)
+ if searchforcameras == True:
+ print('-~-')
+ print('pinging ip: '+network[:-3]+str(pingip))
return serverstatus
if __name__ == '__main__':