tarinactrl.py (11442B)
1 #!/usr/bin/env/ python 2 # -*- coding: utf-8 -*- 3 4 #code by rbckman 5 #freesoftware, copy,share&remix=care 6 7 import os 8 import time 9 import config 10 import socket 11 from blessed import Terminal 12 from multiprocessing import Process, Queue 13 import ifaddr 14 15 term = Terminal() 16 17 searchforcameras=True 18 networks=[] 19 adapters = ifaddr.get_adapters() 20 for adapter in adapters: 21 print("IPs of network adapter " + adapter.nice_name) 22 for ip in adapter.ips: 23 if '::' not in ip.ip[0] and '127.0.0.1' != ip.ip: 24 print(ip.ip) 25 networks.append(ip.ip) 26 27 network=networks[0] 28 29 ##--Cameras--- 30 31 cameras = [] 32 camerasoff =[] 33 port = config.port 34 nextstatus = '' 35 serverstatus = None 36 os.system('clear') 37 selected = 1 38 camselected=0 39 newselected=0 40 mastersound=None 41 camera_recording=None 42 sleep=0.2 43 rectime=None 44 pingip=0 45 local_ip='' 46 47 ##---------------Connection---------------------------------------------- 48 49 def pingtocamera(host, port, data): 50 print("Sending to "+host+" on port "+str(port)+" DATA:"+data) 51 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 52 s.settimeout(0.1) 53 newcamera='' 54 try: 55 while True: 56 s.connect((host, port)) 57 s.send(str.encode(data)) 58 newcamera=host 59 print("Sent to server..") 60 break 61 except: 62 print('did not connect') 63 s.close() 64 return newcamera 65 66 def sendtocamera(host, port, data): 67 print("Sending to "+host+" on port "+str(port)+" DATA:"+data) 68 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 69 #s.settimeout(5) 70 try: 71 while True: 72 s.connect((host, port)) 73 s.send(str.encode(data)) 74 print("Sent to server..") 75 break 76 except: 77 print('did not connect') 78 s.close() 79 80 ##--------------Listen for Clients----------------------- 81 82 def listenforclients(host, port, q): 83 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 84 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 85 s.bind((host,port)) 86 try: 87 s.listen(5) 88 c, addr = s.accept() 89 while True: 90 data = c.recv(1024) 91 if not data: 92 break 93 ctrltime=time.time() 94 tarinadata=data.decode() 95 #lag=ctrltime-float(tarinatime) 96 #status = addr[0], str(lag*1000), tarinatime 97 status = addr[0], tarinadata 98 c.close() 99 q.put(status) 100 break 101 except: 102 q.put('') 103 104 def printmenu(camera_recording,selected,cameras,camselected,serverstatus,rectime): 105 print(term.home+term.clear) 106 print(time.time()) 107 print(rectime) 108 print('your ip: '+network) 109 print('-~-') 110 print('Tarina CTRL') 111 print('-~-') 112 if serverstatus != None: 113 try: 114 print(serverstatus[0]) 115 print(serverstatus[1]) 116 except: 117 print('') 118 if camera_recording != None: 119 print("Camera "+ str(camera_recording)+" is camera_recording! Shut up. And Action!") 120 a=0 121 print('-~-') 122 for i in menu: 123 if a != selected: 124 print(menu[a]) 125 else: 126 print(menu[a]+'<---') 127 a=a+1 128 a=0 129 print('-~-') 130 for i in cameras: 131 if i in camerasoff: 132 if a != camselected: 133 print(term.grey("["+str(a)+"] camera"+str(a+1)+" "+i)) 134 else: 135 print(term.grey("["+str(a)+"] camera"+str(a+1)+" "+i+" <-----")) 136 else: 137 if a != camselected: 138 print("["+str(a)+"] camera"+str(a+1)+" "+i) 139 else: 140 print("["+str(a)+"] camera"+str(a+1)+" "+i+" <-----") 141 a=a+1 142 print('-~-') 143 144 ##----------------------Main loop starts------------------------- 145 146 def main(serverstatus): 147 global nextstatus, selected, cameras, menu, camselected, camera_recording, sleep, rectime, pingip,searchforcameras 148 if camera_recording != None: 149 menu = ["Quit","New FILM","Sync FILM","New SCENE","Sync SCENE","Stop","Retake","Search","Snapshot"] 150 else: 151 menu = ["Quit","New FILM","Sync FILM","New SCENE","Sync SCENE","Record","Retake","Search","Snapshot"] 152 newselected=camselected 153 with term.cbreak(): 154 val=term.inkey(timeout=0) 155 if val.is_sequence: 156 event=val.name 157 elif val: 158 event=val 159 else: 160 event='' 161 print(event) 162 if event == "KEY_ESCAPE": 163 nextstatus = '' 164 serverstatus = 'exit' 165 elif event == "KEY_ENTER" and menu[selected] == "Search": 166 print('Searching for cameras') 167 if searchforcameras == True: 168 searchforcameras = False 169 else: 170 searchforcameras = True 171 elif event == "KEY_ENTER" and menu[selected] == "Quit": 172 print('take care, bye, bye, then') 173 serverstatus = 'exit' 174 elif event == "KEY_ENTER" and menu[selected] == "New FILM": 175 newfilmname=input("new film name:") 176 a=0 177 for i in cameras: 178 if i not in camerasoff: 179 sendtocamera(i,port,'NEWFILM:'+newfilmname) 180 a=a+1 181 elif event == "KEY_UP": 182 if selected > 0: 183 selected = selected - 1 184 elif event == "KEY_DOWN": 185 if selected < len(menu): 186 selected = selected + 1 187 elif event == "R": 188 a=0 189 for i in cameras: 190 if i not in camerasoff: 191 if a == camselected: 192 if camera_recording == camselected: 193 sendtocamera(i,port,'STOPRETAKE') 194 camera_recording=None 195 else: 196 sendtocamera(i,port,'RETAKE') 197 camera_recording=camselected 198 else: 199 if camera_recording != None: 200 sendtocamera(i,port,'PLACEHOLDER') 201 a=a+1 202 elif event == "KEY_ENTER" and menu[selected] == 'Sync SCENE': 203 #for p in cameras[1:]: 204 # if p not in camerasoff: 205 # if camera_recording == None: 206 # sendtocamera(cameras[0],port,'SYNCIP:'+p) 207 if camselected != 0: 208 sendtocamera(cameras[0],port,'SYNCIP:'+cameras[camselected]) 209 elif event == "KEY_ENTER" and menu[selected]=='New SCENE': 210 a=0 211 for i in cameras: 212 if i not in camerasoff: 213 sendtocamera(i,port,'NEWSCENE') 214 a=a+1 215 elif event == "KEY_ENTER" and menu[selected]=='Ping': 216 a=0 217 for i in cameras: 218 if i not in camerasoff: 219 sendtocamera(i,port,'PING') 220 a=a+1 221 elif event == "KEY_PGUP" and camera_recording == None or event=="KEY_ENTER" and menu[selected]=='Record': 222 a=0 223 for i in cameras: 224 if i not in camerasoff: 225 if a == camselected: 226 sendtocamera(i,port,'REC') 227 camera_recording=camselected 228 rectime=time.time() 229 #elif a == mastersound: 230 # if camera_recording == a: 231 # sendtocamera(i,port,'STOP') 232 # else: 233 # sendtocamera(i,port,'RECSOUND') 234 # if camera_recording != mastersound: 235 # time.sleep(sleep) 236 # sendtocamera(i,port,'RECSOUND') 237 else: 238 sendtocamera(i,port,'PLACEHOLDER') 239 a=a+1 240 elif event == "KEY_PGUP" and camera_recording or event=="KEY_ENTER" and menu[selected]=='Stop': 241 a=0 242 for i in cameras: 243 if i not in camerasoff: 244 if a == camselected: 245 if camera_recording == camselected: 246 sendtocamera(i,port,'STOP') 247 camera_recording=None 248 elif a == mastersound: 249 if camera_recording == a: 250 sendtocamera(i,port,'STOP') 251 else: 252 sendtocamera(i,port,'RECSOUND') 253 #if camera_recording != mastersound: 254 # time.sleep(sleep) 255 # sendtocamera(i,port,'RECSOUND') 256 #else: 257 #sendtocamera(i,port,'PLACEHOLDER') 258 a=a+1 259 260 elif event == "0": 261 newselected = 0 262 elif event == "1": 263 newselected = 1 264 elif event == "2": 265 newselected = 2 266 elif event == "3": 267 newselected = 3 268 elif event == "4": 269 newselected = 4 270 elif event == "-": 271 if cameras[camselected] not in camerasoff: 272 camerasoff.append(cameras[camselected]) 273 elif event == "+": 274 if cameras[camselected] in camerasoff: 275 camerasoff.remove(cameras[camselected]) 276 if camselected != newselected: 277 if camera_recording != None: 278 #change camera 279 a=0 280 for c in cameras: 281 if c not in camerasoff: 282 print(c) 283 if a == camselected: 284 if a == mastersound: 285 sendtocamera(c,port,'STOP') 286 time.sleep(sleep) 287 sendtocamera(c,port,'RECSOUND') 288 else: 289 sendtocamera(c,port,'STOP') 290 time.sleep(sleep) 291 sendtocamera(c,port,'PLACEHOLDER') 292 elif a == newselected: 293 if a == mastersound: 294 sendtocamera(c,port,'STOP') 295 time.sleep(sleep) 296 sendtocamera(c,port,'REC') 297 camera_recording=newselected 298 rectime=time.time() 299 else: 300 if a == mastersound: 301 sendtocamera(c,port,'STOP') 302 time.sleep(sleep) 303 sendtocamera(c,port,'RECSOUND') 304 else: 305 sendtocamera(c,port,'STOP') 306 time.sleep(sleep) 307 sendtocamera(c,port,'PLACEHOLDER') 308 #time.sleep(2) 309 a=a+1 310 camselected=newselected 311 312 # ping ip every 10 sec while not camera_recording to connect cameras 313 if searchforcameras == True: 314 if camera_recording == None: 315 if pingip < 256: 316 pingip+=1 317 else: 318 pingip=0 319 searchforcameras=False 320 newcamera=pingtocamera(network[:-3]+str(pingip),port,'PING') 321 if newcamera != '': 322 if newcamera not in cameras and newcamera not in networks: 323 cameras.append(newcamera) 324 print("Found camera! "+newcamera) 325 printmenu(camera_recording,selected,cameras,camselected,serverstatus,rectime) 326 if searchforcameras == True: 327 print('-~-') 328 print('pinging ip: '+network[:-3]+str(pingip)) 329 return serverstatus 330 331 if __name__ == '__main__': 332 serverstatus=None 333 q = Queue() 334 p = Process(target=listenforclients, args=('0.0.0.0', port, q)) 335 p.start() 336 while serverstatus != 'exit': 337 serverstatus = main(serverstatus) 338 if p.is_alive() == False: 339 serverstatus = q.get() 340 p = Process(target=listenforclients, args=('0.0.0.0', port, q)) 341 p.start() 342 time.sleep(0.02) 343 p.terminate() 344 quit()