diff --git a/GhinkAPI.py b/GhinkAPI.py deleted file mode 100644 index e69de29..0000000 diff --git a/Server.py b/Server.py new file mode 100644 index 0000000..57c19df --- /dev/null +++ b/Server.py @@ -0,0 +1,109 @@ +import socket,threading,os,time,math,json,sys,urllib,requests +sys.path.append('libraries\\') +from Astronomy import * +#-------------------------------------# +#Define Area +class APICore(object): + HTTPHead={ + '200':'HTTP/1.1 200 OK\r\nServer: SparkE/Alpha1.0\r\nContent-Type: application/json; charset=utf-8\r\n\r\n', + '400':'HTTP/1.1 400 BAD REQUEST\r\nServer: SparkE/Alpha1.0\r\nContent-Type: application/json; charset=utf-8\r\n\r\n', + '404':'HTTP/1.1 404 NOT FOUND\r\nServer: SparkE/Alpha1.0\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' + } + def __init__(self): + self.__Log=[] + def Log(self,IP,Port,Type,Text): + Time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) + self.__Log.append((Time,IP,Port,Type,Text)) + print("["+Type+"]["+Time+"]["+IP+"]["+str(Port)+"]"+str(Text)) + def IsJson(self,text): + try: + json.loads(text) + return True + except: + return False + def Error400(self,sock,addr): + sock.send(self.HTTPHead['400'].encode("utf-8")) + Result=json.dumps({"info":"bad request","code":400}) + self.Log(addr[0],addr[1],"SEND",Result) + sock.send(str(Result).encode("utf-8")) + def SunAzEl(self,sock,addr): + sock.send(self.HTTPHead['200'].encode("utf-8")) + Result=json.dumps(Astronomy().calcSunAzEl()) + self.Log(addr[0],addr[1],"SEND",Result) + sock.send(str(Result).encode("utf-8")) +#-------------------------------------# +API=APICore() +#-------------------------------------# +RequestRecord={} +server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) +server.bind(("0.0.0.0",8000)) +server.listen() +def socket(sock,addr): + while True: + try: + data = sock.recv(1024) + API.Log(addr[0],addr[1],"RECV","Received a request.") + #print(data.decode("utf-8")) + if("HTTP/" in data.decode("utf-8")): + #HTTP Request. + HTTPRequestHead=data.decode("utf-8").split("\r\n")[0].split(" ")[1].split("/") + del HTTPRequestHead[0] + for i in range(0,len(HTTPRequestHead)): + HTTPRequestHead[i]=urllib.parse.unquote(HTTPRequestHead[i]) + API.Log(addr[0],addr[1],"RECV",HTTPRequestHead) + if(" / " in data.decode("utf-8")): + sock.send(API.HTTPHead['200'].encode("utf-8")) + Result=json.dumps({"wikis":"https://gitee.com/ghink/api/wikis"}) + API.Log(addr[0],addr[1],"SEND",Result) + sock.send(str(Result).encode("utf8")) + break + elif(" /SunAzEl" in data.decode("utf-8")): + if("GET " in data.decode("utf-8")): + try: + HTTPRequestHead[1] + if(API.IsJson(HTTPRequestHead[1])): + API.SunAzEl(sock,addr) + else: + API.Error400(sock,addr) + except: + API.SunAzEl(sock,addr) + elif("POST " in data.decode("utf-8")): + try: + HTTPRequestHead[1] + API.Error400(sock,addr) + except: + API.SunAzEl(sock,addr) + break + else: + sock.send(API.HTTPHead['404'].encode("utf8")) + Result=json.dumps({"info":"not found","code":404}) + API.Log(addr[0],addr[1],"SEND",Result) + sock.send(Result.encode("utf-8")) + break + else: + #TCP Request. + if(data.decode("utf-8")=="Help" or data.decode("utf-8")=="HELP" or data.decode("utf-8")=="help" or data.decode("utf-8")=="WIKI" or data.decode("utf-8")=="wiki" or data.decode("utf-8")=="wikis" or data.decode("utf-8")=="WIKIS" or data.decode("utf-8")=="?"): + Result=json.dumps({"wikis":"https://gitee.com/ghink/api/wikis"}) + API.Log(addr[0],addr[1],"SEND",Result) + sock.send(str(Result).encode("utf8")) + elif(data.decode("utf-8")=="SunAzEl"): + Result=json.dumps(Astronomy().calcSunAzEl()) + API.Log(addr[0],addr[1],"SEND",Result) + sock.send(str(Result).encode("utf-8")) + else: + Result=json.dumps({"info":"not found","code":404}) + API.Log(addr[0],addr[1],"SEND",Result) + sock.send(Result.encode("utf-8")) + except (ConnectionResetError,ConnectionAbortedError): + API.Log(addr[0],addr[1],"STATUS","Lost connection.") + break + except UnicodeDecodeError: + API.Log(addr[0],addr[1],"STATUS","Wrong code mode,must be utf-8.") + API.Log(addr[0],addr[1],"STATUS","Lost connection.") + break +def main(): + sock,addr=server.accept() + client_thread=threading.Thread(target=socket,args=(sock,addr)) + client_thread.start() +while True: + main() diff --git a/TCPServer.py b/TCPServer.py deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/Astronomy.py b/libraries/Astronomy.py new file mode 100644 index 0000000..7ba8bc6 --- /dev/null +++ b/libraries/Astronomy.py @@ -0,0 +1,161 @@ +import os,time,math +#-------------------------------------# +class Astronomy(object): + def calcSunDeclination(self,Stamp=None,TimeZone=8): + MonthList = [ + {"name": 'January', "numdays": 31}, + {"name": 'February', "numdays": 28}, + {"name": 'March', "numdays": 31}, + {"name": 'April', "numdays": 30}, + {"name": 'May', "numdays": 31}, + {"name": 'June', "numdays": 30}, + {"name": 'July', "numdays": 31}, + {"name": 'August', "numdays": 31}, + {"name": 'September', "numdays": 30}, + {"name": 'October', "numdays": 31}, + {"name": 'November', "numdays": 30}, + {"name": 'December', "numdays": 31}, + ] + if(Stamp==None): + TimeStamp = time.time() + else: + TimeStamp = Stamp + Year=int(time.strftime("%Y",time.localtime(TimeStamp))) + Month=int(time.strftime("%m",time.localtime(TimeStamp))) + Day=int(time.strftime("%d",time.localtime(TimeStamp))) + Hour=int(time.strftime("%H",time.localtime(TimeStamp))) + Minute=int(time.strftime("%M",time.localtime(TimeStamp))) + Second=int(time.strftime("%S",time.localtime(TimeStamp))) + if(((Year % 4 == 0 and Year % 100 != 0) or Year % 400 == 0) and (Month == 2)): + if(Day > 29): + Day=29 + else: + if(Day > MonthList[Month-1]['numdays']): + Day = MonthList[Month-1]['numdays'] + if(Month <= 2): + Year -= 1 + Month += 12 + L0 = 280.46646 + ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (36000.76983 + ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (0.0003032)) + while(L0 > 360.0): + L0 -= 360.0 + while(L0 < 0.0): + L0 += 360.0 + return (math.degrees(math.asin(math.sin(math.radians(23 + (26 + ((21.448 - ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (46.8150 + ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (0.00059 - ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (0.001813)))) / 60.0)) / 60.0 + 0.00256 * math.cos(math.radians(1254 - 1934.136 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525))))) * math.sin(math.radians(L0 + math.sin(math.radians(357.52911 + ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (359995029 - 0.0001537 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525)))) * (1.914602 - ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (0.004817 + 0.000014 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525))) + math.sin(math.radians(357.52911 + ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (359995029 - 0.0001537 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525))) * 2) * (0.019993 - 0.000101 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525)) + math.sin(math.radians(357.52911 + ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525) * (359995029 - 0.0001537 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525))) * 3) * 0.000289 - 0.00569 - 0.00478 * math.sin(math.radians(1254 - 1934.136 * ((math.floor(365.25 * (Year + 4716)) + math.floor(30.6001 * (Month + 1)) + Day + 2 - math.floor(Year / 100) + math.floor(math.floor(Year / 100) / 4) - 1524.5 + (Hour * 60 + Minute + Second/60.0) / 1440.0 - TimeZone / 24 - 2451545) / 36525))))))) * 100 + 0.5) / 100.0 + def calcHourAngle(self,Stamp=None,Lon=120,TimeZone=8): + MonthList = [ + {"name": 'January', "numdays": 31}, + {"name": 'February', "numdays": 28}, + {"name": 'March', "numdays": 31}, + {"name": 'April', "numdays": 30}, + {"name": 'May', "numdays": 31}, + {"name": 'June', "numdays": 30}, + {"name": 'July', "numdays": 31}, + {"name": 'August', "numdays": 31}, + {"name": 'September', "numdays": 30}, + {"name": 'October', "numdays": 31}, + {"name": 'November', "numdays": 30}, + {"name": 'December', "numdays": 31}, + ] + if(Stamp==None): + TimeStamp = time.time() + else: + TimeStamp = Stamp + Year=int(time.strftime("%Y",time.localtime(TimeStamp))) + Month=int(time.strftime("%m",time.localtime(TimeStamp))) + Day=int(time.strftime("%d",time.localtime(TimeStamp))) + Hour=int(time.strftime("%H",time.localtime(TimeStamp))) + Minute=int(time.strftime("%M",time.localtime(TimeStamp))) + Second=int(time.strftime("%S",time.localtime(TimeStamp))) + if(((Year % 4 == 0 and Year % 100 != 0) or Year % 400 == 0) and (Month == 2)): + if(Day > 29): + Day=29 + else: + if(Day > MonthList[Month-1]['numdays']): + Day = MonthList[Month-1]['numdays'] + if(Month <= 2): + Year -= 1 + Month += 12 + TimeJulianCent = (math.floor(365.25*(Year + 4716)) + math.floor(30.6001*(Month+1)) + Day + 2 - math.floor(Year/100) + math.floor(math.floor(Year/100)/4) - 1524.5 + Hour * 60 + Minute + Second/60.0/1440.0 - TimeZone/24 - 2451545)/36525 + L0 = 280.46646 + TimeJulianCent * (36000.76983 + TimeJulianCent *(0.0003032)) + while(L0 > 360.0): + L0 -= 360.0 + while(L0 < 0.0): + L0 += 360.0 + TrueSolarTime = (Hour * 60 + Minute + Second / 60) + ((4 * math.degrees((math.tan(math.radians((23 + (26 + ((21.448 - TimeJulianCent * (46.8150 + TimeJulianCent * (0.00059 - TimeJulianCent * (0.001813)))) / 60.0)) / 60.0 + 0.00256 * math.cos(math.radians(1254 - 1934.136 * TimeJulianCent))))/2) ** 2) * math.sin(2 * math.radians(L0)) - 2 * (0.016708634 - TimeJulianCent * (0.000042037 + 0.0000001267 * TimeJulianCent)) * math.sin(math.radians(357.52911 + TimeJulianCent * (359995029 - 0.0001537 * TimeJulianCent))) + 4 * (0.016708634 - TimeJulianCent * (0.000042037 + 0.0000001267 * TimeJulianCent)) * (math.tan(math.radians((23 + (26 + ((21.448 - TimeJulianCent * (46.8150 + TimeJulianCent * (0.00059 - TimeJulianCent * (0.001813)))) / 60.0)) / 60.0 + 0.00256 * math.cos(math.radians(1254 - 1934.136 * TimeJulianCent)))) / 2) ** 2) * math.sin(math.radians(357.52911 + TimeJulianCent * (359995029 - 0.0001537 * TimeJulianCent))) * math.cos(2 * math.radians(L0)) - 0.5 * (math.tan(math.radians((23 + (26 + ((21.448 - TimeJulianCent * (46.8150 + TimeJulianCent * (0.00059 - TimeJulianCent * (0.001813)))) / 60.0)) / 60.0 + 0.00256 * math.cos(math.radians(1254 - 1934.136 * TimeJulianCent))))/2) ** 2) * (math.tan(math.radians((23 + (26 + ((21.448 - TimeJulianCent * (46.8150 + TimeJulianCent * (0.00059 - TimeJulianCent * (0.001813)))) / 60.0)) / 60.0 + 0.00256 * math.cos(math.radians(1254 - 1934.136 * TimeJulianCent))))/2) ** 2) * math.sin(4 * math.radians(L0)) - 1.25 * ((0.016708634 - TimeJulianCent * (0.000042037 + 0.0000001267 * TimeJulianCent)) ** 2) * math.sin(2 * math.radians(math.radians(357.52911 + TimeJulianCent * (359995029 - 0.0001537 * TimeJulianCent)))))) + 4 * Lon - 60.0 * TimeZone) + while (TrueSolarTime > 1440): + TrueSolarTime -= 1440 + HourAngle = TrueSolarTime / 4 - 180.0 + if (HourAngle < -180): + HourAngle += 360.0 + return HourAngle + def calcSunAzEl(self,Stamp=None,Lon=120,Lat=30,TimeZone=8,ZeroAzimuth="North"): + MonthList = [ + {"name": 'January', "numdays": 31}, + {"name": 'February', "numdays": 28}, + {"name": 'March', "numdays": 31}, + {"name": 'April', "numdays": 30}, + {"name": 'May', "numdays": 31}, + {"name": 'June', "numdays": 30}, + {"name": 'July', "numdays": 31}, + {"name": 'August', "numdays": 31}, + {"name": 'September', "numdays": 30}, + {"name": 'October', "numdays": 31}, + {"name": 'November', "numdays": 30}, + {"name": 'December', "numdays": 31}, + ] + if(Stamp==None): + TimeStamp = time.time() + else: + TimeStamp = Stamp + Year=int(time.strftime("%Y",time.localtime(TimeStamp))) + Month=int(time.strftime("%m",time.localtime(TimeStamp))) + Day=int(time.strftime("%d",time.localtime(TimeStamp))) + Hour=int(time.strftime("%H",time.localtime(TimeStamp))) + Minute=int(time.strftime("%M",time.localtime(TimeStamp))) + Second=int(time.strftime("%S",time.localtime(TimeStamp))) + if(((Year % 4 == 0 and Year % 100 != 0) or Year % 400 == 0) and (Month == 2)): + if(Day > 29): + Day=29 + else: + if(Day > MonthList[Month-1]['numdays']): + Day = MonthList[Month-1]['numdays'] + if(Month <= 2): + Year -= 1 + Month += 12 + ZeroAzimuth=180 if(ZeroAzimuth=="South") else 0 + csz = math.sin(math.radians(Lat)) * math.sin(math.radians(self.calcSunDeclination(Stamp,TimeZone))) + math.cos(math.radians(Lat)) * math.cos(math.radians(self.calcSunDeclination(Stamp,TimeZone))) * math.cos(math.radians(self.calcHourAngle(Stamp,Lon,TimeZone))) + if(csz > 1): + csz = 1 + elif(csz < -1): + csz = -1 + if(abs((math.cos(math.radians(Lat)) * math.sin(math.radians(math.degrees(math.acos(csz)))))) > 0.001): + azRad = ((math.sin(math.radians(Lat)) * math.cos(math.radians(math.degrees(math.acos(csz))))) - math.sin(math.radians(self.calcSunDeclination(Stamp,TimeZone)))) / (math.cos(math.radians(Lat)) * math.sin(math.radians(math.degrees(math.acos(csz))))) + if(abs(azRad) > 1.0): + if(azRad < 0): + azRad = -1.0 + else: + azRad = 1.0 + azimuth = 180.0 - math.degrees(math.acos(azRad)) + if(self.calcHourAngle(Stamp,Lon,TimeZone) > 0.0): + azimuth = -azimuth + else: + if(Lat > 0.0): + azimuth = 180.0 + else: + azimuth = 0.0 + if(azimuth < 0.0): + azimuth += 360.0 + exoatmElevation = (90.0 - math.degrees(math.acos(csz))) + if (exoatmElevation > 85.0): + refractionCorrection = 0.0 + else: + te = math.tan (math.radians(exoatmElevation)) + if (exoatmElevation > 5.0): + refractionCorrection = 58.1 / te - 0.07 / (te*te*te) + 0.000086 / (te*te*te*te*te) + elif (exoatmElevation > -0.575): + refractionCorrection = 1735.0 + exoatmElevation * (-518.2 + exoatmElevation * (103.4 + exoatmElevation * (-12.79 + exoatmElevation * 0.711) ) ) + else: + refractionCorrection = -20.774 / te + refractionCorrection = refractionCorrection / 3600.0 + solarZen = math.degrees(math.acos(csz)) - refractionCorrection + return {"az":((azimuth*100 +0.5) - (ZeroAzimuth*100))/100.0,"el":((90.0-solarZen)*100+0.5)/100.0} \ No newline at end of file diff --git a/libraries/Calendar.py b/libraries/Calendar.py deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/__pycache__/Astronomy.cpython-36.pyc b/libraries/__pycache__/Astronomy.cpython-36.pyc new file mode 100644 index 0000000..41491c9 Binary files /dev/null and b/libraries/__pycache__/Astronomy.cpython-36.pyc differ