142 lines
3.8 KiB
Python
142 lines
3.8 KiB
Python
#!/usr/bin/env python
|
|
import web
|
|
import os
|
|
|
|
fileData="data.txt"
|
|
|
|
|
|
class setCoord:
|
|
def GET(self, data):
|
|
|
|
fout= open(fileData,'a')
|
|
fout.write(data+"\n")
|
|
fout.close()
|
|
historico.Put(data)
|
|
ultimaPosicion.SetData(data)
|
|
return "setCoord OK"
|
|
|
|
class index:
|
|
def GET(self, data):
|
|
|
|
print("index: data: "+ data)
|
|
|
|
return "INDEX OK!"
|
|
|
|
class GetPos:
|
|
def GET(self):
|
|
"""
|
|
line=""
|
|
fout= open("D://desa//python//api_web//ultimaPos.txt",'r')
|
|
line=fout.readline(data)
|
|
fout.close()
|
|
ultimaPosicion=dataGloboSonda()
|
|
ultimaPosicion=SetData(line)
|
|
"""
|
|
return ultimaPosicion.GetPos()
|
|
|
|
class AppData:
|
|
def GET(self, data):
|
|
n=int(data.split("/")[0])
|
|
|
|
pos=historico.Get(n)
|
|
|
|
return pos.GetAPPData()
|
|
|
|
class dataGloboSonda:
|
|
def __init__(self):
|
|
self.lat = 0.0
|
|
self.lon = 0.0
|
|
self.alt = 0.0
|
|
self.fecha = "0000-00-00T00:00:00"
|
|
self.datosVisibles = ""
|
|
self.presion = ""
|
|
self.datosExt = ""
|
|
def GetPos(self):
|
|
res= "<!DOCTYPE html><html><body>";
|
|
res=res+"<h2>"+self.fecha+"</h2>"
|
|
res=res+"<p>Altura: "+str(self.alt)+"m</p>"
|
|
res=res+"<p>"+str(self.presion)+"</p>"
|
|
res=res+"<p>"+str(self.datosVisibles)+"</p>"
|
|
res=res+"<p>"+str(self.datosExt)+"</p>"
|
|
res=res+"<p><a href=\"https://www.google.com/maps/search/?api=1&query="+ str(self.lat)+","+str(self.lon)+"\">Ultima Posicion</a></p>"
|
|
res=res+ "</body></html>"
|
|
return res;
|
|
def GetAPPData(self):
|
|
res="AppData;"+self.fecha
|
|
res+=";"+str(self.lat)
|
|
res+=";"+str(self.lon)
|
|
res+=";"+str(self.alt)
|
|
res+=";"+self.datosVisibles
|
|
res+=";"+self.presion
|
|
res+=";"+self.datosExt
|
|
return res
|
|
def SetData(self, data):
|
|
dat=data.split("/")
|
|
if len(dat)<7:
|
|
return
|
|
self.fecha=dat[0]
|
|
self.lat=float(dat[1])
|
|
self.lon=float(dat[2])
|
|
self.alt=float(dat[3])
|
|
|
|
self.datosVisibles=dat[4]
|
|
self.presion=dat[5]
|
|
self.datosExt=dat[6]
|
|
|
|
|
|
class HistoricoGloboSonda:
|
|
def __init__(self):
|
|
self.m=110
|
|
self.data=[""]*self.m
|
|
|
|
self.ini=0
|
|
self.n=0
|
|
self.LeeData()
|
|
def Put(self, dat):
|
|
print("historicos put:"+ str(self.n)+" inicio: "+str(self.ini))
|
|
if self.n< self.m:
|
|
self.ini+self.n
|
|
self.data[self.ini+self.n]=dat
|
|
self.n+=1
|
|
return
|
|
self.data[self.ini]=dat
|
|
self.ini=(1+self.ini)%self.m
|
|
def LeeData(self):
|
|
if not os.path.exists(fileData):
|
|
fout= open(fileData,'w')
|
|
fout.close()
|
|
return
|
|
fout= open(fileData,'r')
|
|
for line in fout:
|
|
line.strip()
|
|
self.Put( line.strip())
|
|
fout.close()
|
|
print("historicos leidos:"+ str(self.n))
|
|
|
|
def Get(self, n):#0 es la ultima pos y m la primera
|
|
nn=self.n-n-1
|
|
print(str(n)+" getdato: "+ str(nn) +" dato real: " +str(self.ini+nn))
|
|
posicion=dataGloboSonda()
|
|
if nn>=0:
|
|
posicion.SetData(self.data[(self.ini+nn)%self.m])
|
|
print("Con DAto:"+ str((self.ini+nn)%self.m)+" "+self.data[(self.ini+nn)%self.m])
|
|
return posicion
|
|
|
|
|
|
|
|
urls = (
|
|
'/setcoor/(.*)', 'setCoord',
|
|
'/getpos', 'GetPos',
|
|
'/getappdata/(.*)', 'AppData',
|
|
'/(.*)', 'index'
|
|
)
|
|
|
|
ultimaPosicion=dataGloboSonda()
|
|
historico=HistoricoGloboSonda()
|
|
if __name__ == "__main__":
|
|
#historico.LeeData()
|
|
app = web.application(urls, globals())
|
|
#app.add_processor(add_global_hook())
|
|
app.run()
|
|
#6265
|