Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #APRS Weather BOT
- #By CR7ALW
- #Uses the module aprslib
- #Uses http://openweathermap.org API
- #Imports
- import agwlib
- import aprslib
- from parsebody import parse_body
- import socket
- import urllib
- import json #To understand the data
- import time
- callsign=input('Please enter your callsign/SSID; ex. CQ0PVI-3.\n')
- via=input('Please enter the VIA path. (max 7, separated by commas); ex. WIDE1*,WIDE2*\n')
- via=via.split(',')
- TCP_IP = '127.0.0.1'
- TCP_PORT = 8000
- BUFFER_SIZE = 1024 #Should be more than enough
- APPID='xxxxxxxxxxxxxxxxxxxxxxxxxxxx' #THIS IS MINE, so I deleted my real key on this paste
- #Open socket comunications to AGPWPE
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((TCP_IP, TCP_PORT))
- #Basic proceidure
- #Add a handler to automatically decode the packets
- def parse_packet(unpacked):
- """Remember the handlers the unpacked dict to the function. Example:
- {'datalen': 1, 'DataKind': 'X', 'CallFrom': 'a', 'PID': 0, 'Port': 0, 'data': '\x01', 'CallTo': ''}"""
- parsed=parse_body(unpacked['data'])
- unpacked.update(parsed) #Adds the data from the dictionary given by parse_body to the unpacked stuff
- return unpacked
- for kind in 'ISUT':
- agwlib.add_handler(parse_packet,kind) #This applies to either of ISUT kinds, so solve them at once
- #Add a handler for the "register callsign" data
- def process_X(unpacked):
- if unpacked['data']!='\x01': raise Exception('Could not register callsign')
- #TODO: maybe do this a little better
- agwlib.add_handler(process_X,'X')
- #Register callsign and grab response
- s.send(agwlib.send_X(callsign)) #Beaware that send_# functions only generate the data. You still need the socket send function
- answer=agwlib.process(s.recv(BUFFER_SIZE)) #Process automatically sees what data we're having and calls the accordingly handler
- #Now enable packet monitorings
- s.send(agwlib.send_m()) #That's it
- #Now prepare the openweathermap API
- url='http://api.openweathermap.org/data/2.5/weather'
- def get_weather_data(lat,lon):
- values={'lat':lat, 'lon':lon, 'APPID':APPID}
- url_values = urllib.parse.urlencode(values)
- full_url = url + '?' + url_values
- data = urllib.request.urlopen(full_url)
- data=data.read().decode(data.headers.get_content_charset())
- data=json.loads(data)
- return data
- def weather_text(data):
- """Takes weather data, makes a nice text"""
- tempK=data['main']['temp']
- tempC=tempK-273.15
- desc=data['weather'][0]['description']
- main=data['weather'][0]['main']
- country=data['sys']['country']
- name=data['name']
- string= "Weather for %s, %s: %s (%s). Temperature: %.1fK (%.1fC)." % (name, country, main, desc, tempK, tempC)
- #string="Weather for "+name+', '+country+': '+main+' ('+desc+'). Temperature: '+str(tempK)+'.'
- return string
- def custom_text(string, callsign):
- """Takes the weasther string and adds a signature"""
- #TODO: something here
- return string
- #Get on an infinite loop
- while 1:
- packet = s.recv(BUFFER_SIZE) #This gets stuck in here until received
- processed=agwlib.process(packet) #Process it
- #Now send the weather data, case the user asked for it
- dic=processed[1] #Forget the packet type
- print(dic)
- if 'comment' in dic.keys():
- if 'CQWeather' in dic['comment']:
- if 'latitude' in dic.keys() and 'longitude' in dic.keys():
- weather_data=get_weather_data(dic['latitude'],dic['longitude'])
- weather=weather_text(weather_data)
- print(weather)
- #TODO: maybe add a custom text
- #And now send the package back with the data
- to_send=agwlib.send_V(1, callsign, dic['CallFrom'], via, weather)
- time.sleep(0.5)
- s.send(to_send)
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement