tarinaserver.py (7464B)
1 #!/usr/bin/env python3 2 3 import web 4 import os 5 import socket 6 import ifaddr 7 import sys 8 import time 9 import random 10 import hashlib 11 12 # Get path of the current dir, then use it as working directory: 13 rundir = os.path.dirname(__file__) 14 if rundir != '': 15 os.chdir(rundir) 16 17 filmfolder = '/home/pi/Videos/' 18 19 basedir = os.path.dirname(os.path.realpath(__file__)) 20 sys.path.append(basedir) 21 22 # Link video directory to static dir 23 if os.path.isfile('static/Videos') == False: 24 os.system("ln -s -t static/ " + filmfolder) 25 26 films = [] 27 28 #NETWORKS 29 30 networks=[] 31 adapters = ifaddr.get_adapters() 32 for adapter in adapters: 33 print("IPs of network adapter " + adapter.nice_name) 34 for ip in adapter.ips: 35 if '::' not in ip.ip[0] and '127.0.0.1' != ip.ip: 36 print(ip.ip) 37 networks.append(ip.ip) 38 network=networks[0] 39 40 urls = ( 41 '/?', 'index', 42 '/f/(.*)?', 'films' 43 ) 44 45 app = web.application(urls, globals()) 46 render = web.template.render('templates/', base="base") 47 web.config.debug=False 48 os.system('rm '+basedir+'/sessions/*') 49 store = web.session.DiskStore(basedir + '/sessions/') 50 session = web.session.Session(app,store,initializer={'login': 0, 'user': '', 'backurl': '', 'bildsida': 0, 'cameras': [], 'reload': 0, 'randhash':''}) 51 52 port=55555 53 ip='0.0.0.0' 54 cameras=[] 55 56 session.randhash = hashlib.md5(str(random.getrandbits(256)).encode('utf-8')).hexdigest() 57 58 ##---------------Connection---------------------------------------------- 59 60 def pingtocamera(host, port, data): 61 print("Sending to "+host+" on port "+str(port)+" DATA:"+data) 62 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 63 s.settimeout(0.01) 64 try: 65 while True: 66 s.connect((host, port)) 67 s.send(str.encode(data)) 68 if host not in cameras and host not in networks: 69 session.cameras.append(host) 70 print("Found camera! "+host) 71 print("Sent to server..") 72 break 73 except: 74 ('did not connect') 75 s.close() 76 77 def sendtocamera(host, port, data): 78 print("Sending to "+host+" on port "+str(port)+" DATA:"+data) 79 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 80 s.settimeout(0.1) 81 try: 82 while True: 83 s.connect((host, port)) 84 s.send(str.encode(data)) 85 print("Sent to server..") 86 break 87 except: 88 ('did not connect') 89 s.close() 90 91 92 def getfilms(filmfolder): 93 #get a list of films, in order of settings.p file last modified 94 films_sorted = [] 95 films = next(os.walk(filmfolder))[1] 96 for i in films: 97 if os.path.isfile(filmfolder + i + '/settings.p') == True: 98 lastupdate = os.path.getmtime(filmfolder + i + '/' + 'settings.p') 99 films_sorted.append((i,lastupdate)) 100 else: 101 films_sorted.append((i,0)) 102 films_sorted = sorted(films_sorted, key=lambda tup: tup[1], reverse=True) 103 print(films_sorted) 104 return films_sorted 105 106 #------------Count scenes-------- 107 108 def countscenes(filmfolder, filmname): 109 scenes = 0 110 try: 111 allfiles = os.listdir(filmfolder + filmname) 112 except: 113 allfiles = [] 114 scenes = 0 115 for a in allfiles: 116 if 'scene' in a: 117 scenes = scenes + 1 118 return scenes 119 120 #------------Count shots-------- 121 122 def countshots(filmname, filmfolder, scene): 123 shots = 0 124 try: 125 allfiles = os.listdir(filmfolder + filmname + '/scene' + str(scene).zfill(3)) 126 except: 127 allfiles = [] 128 shots = 0 129 for a in allfiles: 130 if 'shot' in a: 131 shots = shots + 1 132 return shots 133 134 #------------Count takes-------- 135 136 def counttakes(filmname, filmfolder, scene, shot): 137 takes = 0 138 try: 139 allfiles = os.listdir(filmfolder + filmname + '/scene' + str(scene).zfill(3) + '/shot' + str(shot).zfill(3)) 140 except: 141 allfiles = [] 142 return takes 143 for a in allfiles: 144 if '.mp4' in a or '.h264' in a: 145 takes = takes + 1 146 return takes 147 148 class index: 149 def GET(self): 150 films = getfilms(filmfolder) 151 renderedfilms = [] 152 unrenderedfilms = [] 153 for f in films: 154 if os.path.isfile('static/Videos/' + f[0] + '/' + f[0] + '.mp4') == True: 155 renderedfilms.append(f[0]) 156 else: 157 unrenderedfilms.append(f[0]) 158 i=web.input(func=None,selected=None) 159 if i.selected != None: 160 sendtocamera(ip,port,'SELECTED:'+i.selected) 161 if i.func == 'search': 162 session.cameras=[] 163 # ping ip every 10 sec while not recording to connect cameras 164 pingip=0 165 while pingip < 255 : 166 pingip+=1 167 pingtocamera(network[:-3]+str(pingip),port,'PING') 168 elif i.func == 'record': 169 sendtocamera(ip,port,'REC') 170 elif i.func == 'retake': 171 sendtocamera(ip,port,'RETAKE') 172 elif i.func == 'up': 173 sendtocamera(ip,port,'UP') 174 elif i.func == 'down': 175 sendtocamera(ip,port,'DOWN') 176 elif i.func == 'left': 177 sendtocamera(ip,port,'LEFT') 178 elif i.func == 'right': 179 sendtocamera(ip,port,'RIGHT') 180 elif i.func == 'view': 181 sendtocamera(ip,port,'VIEW') 182 elif i.func == 'middle': 183 sendtocamera(ip,port,'MIDDLE') 184 elif i.func == 'delete': 185 sendtocamera(ip,port,'DELETE') 186 elif i.func == 'picture': 187 sendtocamera(ip,port,'PICTURE') 188 session.randhash = hashlib.md5(str(random.getrandbits(256)).encode('utf-8')).hexdigest() 189 session.reload = 1 190 if i.func != None: 191 session.reload = 1 192 raise web.seeother('/') 193 time.sleep(1) 194 interface=open('/dev/shm/interface','r') 195 vumeter=open('/dev/shm/vumeter','r') 196 menu=interface.readlines() 197 vumetermessage=vumeter.readlines()[0].rstrip('\n') 198 try: 199 selected=int(menu[0]) 200 except: 201 selected=0 202 try: 203 name=menu[3].split(':')[1] 204 name=name.rstrip('\n') 205 except: 206 name='' 207 try: 208 scene=menu[4].split(':')[1].split('/')[0] 209 except: 210 scene=1 211 try: 212 shot=menu[5].split(':')[1].split('/')[0] 213 except: 214 shot=1 215 try: 216 take=menu[6].split(':')[1].split('/')[0] 217 except: 218 take=1 219 session.reload = 0 220 thumb="/static/Videos/"+name+"/scene"+str(scene).zfill(3)+"/shot"+str(shot).zfill(3)+"/picture"+str(take).zfill(3)+".jpeg" 221 print(thumb) 222 if os.path.isfile(basedir+thumb) == False: 223 print(basedir+thumb) 224 thumb='' 225 return render.index(renderedfilms, unrenderedfilms, session.cameras, menu, selected,name,scene,shot,take,str,session.randhash,thumb,vumetermessage,i.func) 226 227 class films: 228 def GET(self, film): 229 shots = 0 230 takes = 0 231 i = web.input(page=None, scene=None, shot=None, take=None) 232 if i.scene != None: 233 shots = countshots(film, filmfolder, i.scene) 234 takes = counttakes(film, filmfolder, i.scene, i.shot) 235 if i.scene != None and i.shot != None: 236 shots = countshots(film, filmfolder, i.scene) 237 scenes = countscenes(filmfolder, film) 238 return render.filmpage(film, scenes, str, filmfolder, counttakes, countshots, shots, i.scene, takes, i.shot, i.take) 239 240 application = app.wsgifunc() 241