Working on XML notifications.

pull/263/head
Joe Prochazka 2016-09-23 15:15:54 -04:00
rodzic e0469d7669
commit 93bcb8b808
6 zmienionych plików z 150 dodań i 29 usunięć

Wyświetl plik

@ -90,9 +90,10 @@
// Add flight if not saved already.
if (!in_array($flight, $savedFlights)) {
$dbh = $common->pdoOpen();
$sql = "INSERT INTO ".$settings::db_prefix."notifications (flight) VALUES (:flight)";
$sql = "INSERT INTO ".$settings::db_prefix."notifications (flight, lastMessageCount) VALUES (:flight, :lastMessageCount)";
$sth = $dbh->prepare($sql);
$sth->bindParam(':flight', $flight, PDO::PARAM_STR, 10);
$sth->bindParam(':lastMessageCount', 0, PDO::PARAM_INT);
$sth->execute();
$sth = NULL;
$dbh = NULL;
@ -101,10 +102,6 @@
}
// Set TRUE or FALSE for checkbox items.
$enableNotifications = FALSE;
if (isset($_POST['enableNotifications']) && $_POST['enableNotifications'] == "TRUE")
$enableNotifications = TRUE;
$enableFlights = FALSE;
if (isset($_POST['enableFlights']) && $_POST['enableFlights'] == "TRUE")
$enableFlights = TRUE;
@ -153,6 +150,14 @@
if (isset($_POST['useDump1090FaMap']) && $_POST['useDump1090FaMap'] == "TRUE")
$useDump1090FaMap = TRUE;
$enableWebNotifications = FALSE;
if (isset($_POST['enableWebNotifications']) && $_POST['enableWebNotifications'] == "TRUE")
$enableWebNotifications = TRUE;
$enableEmailNotifications = FALSE;
if (isset($_POST['enableEmailNotifications']) && $_POST['enableEmailNotifications'] == "TRUE")
$enableEmailNotifications = TRUE;
$enableTwitterNotifications = FALSE;
if (isset($_POST['enableTwitterNotifications']) && $_POST['enableTwitterNotifications'] == "TRUE")
$enableTwitterNotifications = TRUE;
@ -162,7 +167,6 @@
$common->updateSetting("template", $_POST['template']);
$common->updateSetting("defaultPage", $_POST['defaultPage']);
$common->updateSetting("dateFormat", $_POST['dateFormat']);
$common->updateSetting("enableNotifications", $enableNotifications);
$common->updateSetting("enableFlights", $enableFlights);
$common->updateSetting("enableBlog", $enableBlog);
$common->updateSetting("enableInfo", $enableInfo);
@ -184,7 +188,10 @@
$common->updateSetting("networkInterface", $_POST['networkInterface']);
$common->updateSetting("timeZone", $_POST['timeZone']);
$common->updateSetting("useDump1090FaMap", $useDump1090FaMap);
$common->updateSetting("enableWebNotifications", $enableWebNotifications);
$common->updateSetting("enableEmailNotifications", $enableEmailNotifications);
$common->updateSetting("enableTwitterNotifications", $enableTwitterNotifications);
$common->updateSetting("emailNotificationAddresses", $_POST['emailNotificationAddresses']);
$common->updateSetting("twitterUserName", $_POST['twitterUserName']);
$common->updateSetting("twitterConsumerKey", $_POST['twitterConsumerKey']);
$common->updateSetting("twitterConsumerSecret", $_POST['twitterConsumerSecret']);
@ -228,7 +235,8 @@
$notifications = ltrim($notifications.",".$savedFlight['flight'], ',');
}
}
$enableNotifications = $common->getSetting("enableNotifications");
$enableWebNotifications = $common->getSetting("enableWebNotifications");
$enableEmailNotifications = $common->getSetting("enableEmailNotifications");
$enableTwitterNotifications = $common->getSetting("enableTwitterNotifications");
$twitterUserName = $common->getSetting("twitterUserName");
$twitterConsumerKey = $common->getSetting("twitterConsumerKey");
@ -386,14 +394,23 @@
<div class="panel panel-default">
<div class="panel-heading">Flight Notifications</div>
<div class="panel-body">
<div class="form-group">
<label for="notifications"">Flight names. (coma delimited)</label>
<input type="text" class="form-control" id="notifications" name="notifications" value="<?php echo $notifications; ?>">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="enableNotifications" value="TRUE"<?php ($enableNotifications == 1 ? print ' checked' : ''); ?>> Enable flight notifications.
<input type="checkbox" name="enableWebNotifications" value="TRUE"<?php ($enableWebNotifications == 1 ? print ' checked' : ''); ?>> Enable web based flight notifications.
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="enableEmailNotifications" value="TRUE"<?php ($enableEmailNotifications == 1 ? print ' checked' : ''); ?>> Enable email flight notifications.
</label>
</div>
<div class="form-group">
<label for="notifications"">Flight Notifications (coma delimited)</label>
<input type="text" class="form-control" id="notifications" name="notifications" value="<?php echo $notifications; ?>">
<label for="notificationEmailAddresses"">Email addresses to be notified. (coma delimited)</label>
<input type="text" class="form-control" id="notificationEmailAddresses" name="notificationEmailAddresses" value="<?php echo $notificationEmailAddresses; ?>">
</div>
<div class="checkbox">
<label>

Wyświetl plik

@ -28,13 +28,15 @@
// SOFTWARE. //
/////////////////////////////////////////////////////////////////////////////////////
$possibleActions = array("flights");
$possibleActions = array("flights", "update");
if (isset($_GET['type']) && in_array($_GET["type"], $possibleActions)) {
switch ($_GET["type"]) {
case "flights":
$queryArray = getVisibleFlights();
break;
case "update":
$queryArray = updateVisibleFlights();
}
exit(json_encode($queryArray));
} else {
@ -80,6 +82,7 @@
}
$foundFlights = array();
$foundFlights['tracking'] = '';
foreach ($lookingFor as $flight) {
if(strpos($flight[0], "%") !== false) {
$searchFor = str_replace("%", "", $flight[0]);
@ -101,4 +104,9 @@
return json_decode(json_encode((array)$foundFlights), true);
}
function updateVisibleFlights() {
$success = TRUE;
return json_decode(json_encode((array)$success), true);
}
?>

Wyświetl plik

@ -396,12 +396,14 @@ EOF;
$common->addSetting('measurementTemperature', 'imperial');
$common->addSetting('measurementBandwidth', 'kbps');
$common->addSetting('networkInterface', 'eth0');
$common->addSetting('enableNotifications', FALSE);
$common->addSetting('emailFrom', 'noreply@adsbreceiver.net');
$common->addSetting('emailReplyTo', 'noreply@adsbreceiver.net');
$common->addSetting('timeZone', $_POST['timeZone']);
$common->addSetting('useDump1090FaMap', FALSE);
$common->addSetting('enableWebNotifications', FALSE);
$common->addSetting('enableEmailNotifications', FALSE);
$common->addSetting('enableTwitterNotifications', FALSE);
$common->addSetting('emailNotificationAddresses', '');
$common->addSetting('twitterUserName', '');
$common->addSetting('twitterConsumerKey', '');
$common->addSetting('twitterConsumerSecret', '');

Wyświetl plik

@ -152,13 +152,17 @@ EOF;
$dbh = NULL;
}
// Rename the enableFlightNotifications to enableNotifications.
$enableNotifications = $common->getSetting('enableFlightNotifications')
$common->addSetting('enableNotifications', $enableNotifications);
// Rename the enableFlightNotifications to enableWebNotifications.
$enableWebNotifications = $common->getSetting('enableFlightNotifications')
$common->addSetting('enableWebNotifications', $enableWebNotifications);
$common->deleteSetting('enableFlightNotifications');
// Add Twitter API settings.
// Add new flight notification settings.
$common->addSetting('enableEmailNotifications', FALSE);
$common->addSetting('enableTwitterNotifications', FALSE);
$common->addSetting('emailNotificationAddresses', '');
// Add Twitter API settings.
$common->addSetting('twitterUserName', '');
$common->addSetting('twitterConsumerKey', '');
$common->addSetting('twitterConsumerSecret', '');

Wyświetl plik

@ -29,7 +29,7 @@
<link rel="stylesheet" href="/templates/{setting:template}/assets/css/bootstrap.min.css">
<link rel="stylesheet" href="/templates/{setting:template}/assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="/templates/{setting:template}/assets/css/portal.css">
{if setting:enableNotifications eq TRUE}<link rel="stylesheet" href="/templates/{setting:template}/assets/css/notifications.css">{/if}
{if setting:enableWebNotifications eq TRUE}<link rel="stylesheet" href="/templates/{setting:template}/assets/css/notifications.css">{/if}
{area:head}
</head>
<body>
@ -66,7 +66,7 @@
</div>
</div>
</nav>
{if setting:enableNotifications eq TRUE}
{if setting:enableWebNotifications eq TRUE}
<div id="flight-notifications" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
@ -97,7 +97,7 @@
</footer>
<script src="/templates/{setting:template}/assets/js/jquery-2.2.1.min.js"></script>
<script src="/templates/{setting:template}/assets/js/bootstrap.min.js"></script>
{if setting:enableNotifications eq TRUE}
{if setting:enableWebNotifications eq TRUE}
<script src="/templates/{setting:template}/assets/js/notifications.js"></script>
{/if}
<script type="text/javascript">

Wyświetl plik

@ -33,17 +33,29 @@
# TODO: #
#------------------------------------------------------------#
# When no flights are seen a JSON error is encountered. #
# Send an email to the administrators when a flight is seen. #
# Send a tweet to Twitter when a flight is seen. #
# Add pip install python-twitter to the install script. #
# Update the lastMessageCount using the API. #
# Add support for MySQL. #
# Add support for SQLite. #
##############################################################
import json
import urllib
import urllib2
import socket
import smtplib
import twitter
from xml.dom import minidom
import xml.etree.ElementTree as ET
# Temporary variables for testing.
portal_url = "http://172.17.2.148"
debug_script = "true"
send_email = "false"
send_tweet = "false"
###############################################
@ -51,12 +63,37 @@ from xml.dom import minidom
# Get the portal settings from the settings.xml file.
doc = minidom.parse("/var/www/html/data/settings.xml")
settings = doc.getElementsByTagName("setting")
config_tree = ET.parse("/var/www/html/data/settings.xml")
config_root = config_tree.getroot()
for setting in config_root.findall('setting'):
if setting.find('name').text == "enableEmailNotifications":
setting_enable_email_notifications = setting.find('value').text
if setting.find('name').text == "emailNotificationAddresses":
setting_enable_email_notifications = setting.find('value').text
if setting.find('name').text == "enableTwitterNotifications":
setting_enable_twitter_notifications = setting.find('value').text
if setting.find('name').text == "twitterUserName":
setting_twitter_user_name = setting.find('value').text
if setting.find('name').text == "twitterConsumerKey":
setting_twitter_cosumer_key = setting.find('value').text
if setting.find('name').text == "twitterConsumerSecret":
setting_twitter_consumer_secret = setting.find('value').text
if setting.find('name').text == "twitterAccessToken":
setting_twitter_access_token = setting.find('value').text
if setting.find('name').text == "twitterAccessTokenSecret":
setting_twitter_access_token_secret = setting.find('value').text
# Get the portal administrators from the administrators.xml file.
doc = minidom.parse("/var/www/html/data/administrators.xml")
administrators = doc.getElementsByTagName("administrator")
# Display setting variables if script debuging is turned on.
if debug_script == "true":
print "setting_enable_email_notifications: ",setting_enable_email_notifications
print "setting_email_notification_addressees: ",setting_email_notification_addressees
print "setting_enable_twitter_notifications: ",setting_enable_twitter_notifications
print "setting_twitter_user_name: ",setting_twitter_user_name
print "setting_twitter_cosumer_key: ",setting_twitter_cosumer_key
print "setting_twitter_consumer_secret: ",setting_twitter_consumer_secret
print "setting_twitter_access_token: ",setting_twitter_access_token
print "setting_twitter_access_token_secret: ",setting_twitter_access_token_secret
print "--------------------------------------------------"
# Get flights to send notifications for from the notifications.xml file.
doc = minidom.parse("/var/www/html/data/notifications.xml")
@ -68,7 +105,7 @@ flights = doc.getElementsByTagName("flight")
# Get notification JSON from the portal.
response = urllib2.urlopen("http://localhost/api/notifications.php?type=flights")
response = urllib2.urlopen(portal_url,"/api/notifications.php?type=flights")
flights_seen = json.load(response)
for flight in flights:
@ -76,6 +113,59 @@ for flight in flights:
lastMessageCount = flight.getElementsByTagName("lastMessageCount")[0]
for i in flights_seen['tracking']:
if name.firstChild.data.strip() == i['flight'] and lastMessageCount.firstChild.data.strip() < i['lastMessageCount']:
print "Send emails..."
print "Send tweet..."
if name.firstChild.data.strip() == i['flight'] and lastMessageCount.firstChild.data.strip() > i['lastMessageCount']:
if send_emails == "true":
# Send emails to the administrators telling them this flight is being tracked.
sender = 'noreply@adsbreceiver.net'
receivers = ['joe@swiftbyte.com']
message = """From: From ADS-B Receiver <noreply@adsbreceiver.net>
To: To Administrator <joe@swiftbyte.com>
MIME-Version: 1.0
Content-type: text/html
Subject: ADS-B Receiver Flight Notification
<h1>ADS-B Receiver Flight Notification</h1>
<b>The following flight is currently being tracked.</b>
<p>{flight}</p>
<a href="http://{ip_address}/dump1090.php">Click here</a> to view this and any other flights currently being tracked by this receiver.
""".format(flight=name.firstChild.data.strip(), ip_address=socket.gethostbyname(socket.gethostname()))
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
if debug_script == "true":
### ADD EMAIL TO THIS LINE
print "Successfully sent a notification for flight",name.firstChild.data.strip()," to email address --- ADD EMAIL ADDRESS HERE ---."
except SMTPException:
print "Error: unable to send email"
if send_tweet == "true":
# Send a tweet to Twitter saying this flight is being tracked.
# https://github.com/bear/python-twitter
message = "The following flight is currently being tracked: {flight}".format(flight=name.firstChild.data.strip())
api = twitter.Api(consumer_key=setting_twitter_cosumer_key, consumer_secret=setting_twitter_consumer_secret, access_token_key=setting_twitter_access_token, access_token_secret=setting_twitter_access_token_secret)
try:
status = api.PostUpdate(message)
print "Successfully sent tweet"
except UnicodeDecodeError:
print "Error: unable to send tweet"
###############################################
## UPDATE THE FLIGHTS LASTMESSAGECOUNT
if name.firstChild.data.strip() == i['flight']:
update_data = [('flight',i['flight']),('messages','0')]
update_data = urllib.urlencode(update_data)
request = urllib2.Request(portal_url,"/api/notifications.php?type=update", update_data)
request.add_header("Content-type", "application/x-www-form-urlencoded")
response = urllib2.urlopen(request)
json_response = json.load(response)
### READ JSON FOR RESPONSE
if debug_script == "true":
print "lastMessage count for ",i['flight']," updated to ",i['lastMessageCount'],"."