-
Notifications
You must be signed in to change notification settings - Fork 0
/
flogger_landout.py
174 lines (153 loc) · 6.74 KB
/
flogger_landout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#
# This function determines if a flight has landed outside the airfield.
# The perimiter of the airfield is taken as circle based on the designated centre point
# of a specified radius. This is not the most accurate but will do to start with.
# If a flight lands outside the airfield then an SMS message with there landing coordinates
# is sent to a specified number.
import smtplib
import base64
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from __builtin__ import file
import settings
import os
import datetime
from geopy.distance import vincenty
from flogger_get_coords import get_coords
from google.directions import GoogleDirections
from LatLon import *
def landout_check(flight_reg, flight_no, af_centre, radius, landing_coords, mode):
#
# This function determines if a flight has landed within the vicinity of the airfield take off point.
# If not it sends an email to the designated address specifying the landing coordinates, these
# can then be used by the recovery team to locate the aircraft using appropriate mapping technology.
# The algorithm used is that of a circle of defined radius centred on the originating airfield.
# The function is intended to send the msg either by email or SMS but at the moment only email
# is supported. The SMS code is included, has not been tested and requires an account to be created
# and of course each SMS msg would be charged to that account. The provenance of the SMS code is
# included if this helps with subsequent development.
#
# Returns: True - landed out. False - landed inside airfield limits
#
print "landout_check called. Registration: ", flight_reg, " Start coords: ", af_centre, " End coords: ", landing_coords
landing_dist = vincenty(af_centre, landing_coords).meters
print "Landing distance is: %d metres from airfield centre" % landing_dist
if landing_dist <= radius:
landing_status = "landed"
result = False
print "Landed in airfield"
else:
landing_status = "landed out"
result = True
print "Flight landed out, send msg. Registration: ", flight_reg, " Flight No: ", flight_no
# Is an email or SMS of landing status requested?
if settings.FLOGGER_LANDING_EMAIL <> "Y" and settings.FLOGGER_LANDING_EMAIL <> "y":
# No email or SMS of landing status required, return landing status
return result
#
# Email or SMS of landed status to be sent
#
landing_point = LatLon(landing_coords[0], landing_coords[1]) # Decimal degrees to object
landing_coords = landing_point.to_string('d% %m% %S% %H') # Coordinates to degrees minutes seconds
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") # Date and time of event
if mode == "SMS":
#-----------------------------------
# Send SMS Text Message using Python
#
# Author : Matt Hawkins
# Site : http://www.raspberrypi-spy.co.uk/
# Date : 01/04/2016
#
# Requires account with TxtLocal
# http://www.txtlocal.co.uk/?tlrx=114032
#
#-----------------------------------
# Import required libraries
import urllib # URL functions
import urllib2 # URL functions
# Set YOUR TextLocal username
username = 'joebloggs@example.com'
# Set YOUR unique API hash
# It is available from the docs page
# https://control.txtlocal.co.uk/docs/
hash = '1234567890abcdefghijklmnopqrstuvwxyz1234'
# Set a sender name.
# Sender name must alphanumeric and
# between 3 and 11 characters in length.
sender = 'RPiSpy'
sender = settings.FLOGGER_YGC_ADMIN
# Set flag to 1 to simulate sending
# This saves your credits while you are
# testing your code.
# To send real message set this flag to 0
test_flag = 1
# Set the phone number you wish to send
# message to.
# The first 2 digits are the country code.
# 44 is the country code for the UK
# Multiple numbers can be specified if required
# e.g. numbers = ('447xxx123456','447xxx654321')
numbers = ('447xxx123456')
# Define your message
message = 'Test message sent from my Raspberry Pi'
#-----------------------------------------
# No need to edit anything below this line
#-----------------------------------------
values = {'test' : test_flag,
'uname' : username,
'hash' : hash,
'message' : message,
'from' : sender,
'selectednums' : numbers }
url = 'http://www.txtlocal.com/sendsmspost.php'
postdata = urllib.urlencode(values)
req = urllib2.Request(url, postdata)
print 'Attempt to send SMS ...'
try:
response = urllib2.urlopen(req)
response_url = response.geturl()
if response_url==url:
print 'SMS sent!'
return result
except urllib2.URLError, e:
print 'Send failed!'
print e.reason
return result
#
# Send landing status as email
#
if mode == "email":
print "Send %s email" % landing_status
fromaddr = settings.FLOGGER_SMTP_TX
toaddr = settings.FLOGGER_SMTP_RX
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
txt = "%s: %s %s at: %s. Time: %s" % (settings.APRS_USER, flight_reg, landing_status, landing_coords, now)
msg['Subject'] = txt
print "Email %s coordinates: %s" % (landing_status, txt)
body = txt + " Flight No: " + str(flight_no)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP(settings.FLOGGER_SMTP_SERVER_URL, settings.FLOGGER_SMTP_SERVER_PORT)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
return result
#
# Test call
#
#af_centre = get_coords(settings.FLOGGER_AIRFIELD_DETAILS)
#print "Airfield coords: ", af_centre
#radius = settings.FLOGGER_AIRFIELD_LIMIT
#radius = 44000
#mode = settings.FLOGGER_LANDOUT_MODE
#landing_coords = get_coords("Pocklington")
#print "Pocklington coords: ", landing_coords
#settings.FLOGGER_SMTP_SERVER_URL = "smtp.metronet.co.uk"
#settings.FLOGGER_SMTP_SERVER_PORT = 25
#settings.FLOGGER_SMTP_TX = "pjrobinson@metronet.co.uk"
#settings.FLOGGER_SMTP_RX = "pjrobinson@metronet.co.uk"
#landout_check("GB_JVC", af_centre, radius, landing_coords, mode)