Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Last active December 17, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjaap/5542983 to your computer and use it in GitHub Desktop.
Save peterjaap/5542983 to your computer and use it in GitHub Desktop.
Notifier class for Raspberry notifications. Replace values on line 13, 34, 35, 46, 47, 108, 117 and 118 to your context. Current values line 46 and 47 are defaults for Ziggo modems (Dutch ISP). You'll also need: - A Raspberry Pi ;-) - Simple HTML DOM class; http://simplehtmldom.sourceforge.net/ - Pushover account (http://www.pushover.net) - Push…
<?php
chdir(dirname(__FILE__));
class Notifier {
public function __construct() {
require_once 'simple_html_dom.php';
}
public function checkNewDownloads() {
$blackListed = array('Thumbs.db');
@$lastDownloads = unserialize(file_get_contents('lastDownloads.dat'));
if(!$lastDownloads) $lastDownloads = array();
$currentDownloads = array_slice(scandir('PUT_YOUR_DOWNLOAD_DIR_HERE'),2);
file_put_contents('lastDownloads.dat',serialize($currentDownloads));
$diff = array_diff($currentDownloads,$lastDownloads);
$newDownloads = array();
foreach($diff as $new) {
if(in_array($new,$blackListed)) continue;
$new = str_ireplace(".mp4","",$new);
$new = str_replace("."," ",$new);
$newDownloads[] = $new;
}
if(count($newDownloads)>0) {
$message = implode($newDownloads,"\n");
if(strlen($message) > (512-32)) {
$message = substr($message,0,(512-32)).'.. and more';
}
$this->sendPushover('You have new downloads',$message);
}
}
public function checkOnline() {
$lastResult = file_get_contents('lastResult.dat');
$gateway = 'ROUTER_GATEWAY';
$iPhoneMACAddress = 'PLACE_YOUR_MAC_ADDRESS_HERE';
$acceptableLossPercentage = 0.2;
$pingRuns = 5;
$ch = curl_init('http://' . $gateway . '/goform/login');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$params = array(
'loginUsername'=>'ziggo',
'loginPassword'=>'draadloos',
'submit'=>'Inloggen',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
if(curl_exec($ch) === false) {
echo 'Curl error: ' . curl_error($ch);
exit;
}
$ch = curl_init('http://' . $gateway . '/RgDhcp.asp');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
$found = false;
$ip = false;
$ipNext = false;
$dom = str_get_html($contents);
foreach($dom->find('form[name="Dhcp"] table > table tbody tr td') as $tdkey=>$td) {
if($ipNext) {
$ip = $td->innertext;
$ipParts = explode(".",$ip);
foreach($ipParts as &$part) {
$part = ltrim($part,'0');
}
$ip = implode(".",$ipParts);
break;
}
if($td->innertext == $iPhoneMACAddress) {
$ipNext = true;
}
}
curl_close($ch);
if($ip) {
$success = 0;
exec('ping '.$ip . ' -c '.$pingRuns,$resultLines);
foreach($resultLines as $line) {
if(stripos($line,'bytes from ' . $ip)!==false) {
$success++;
}
}
if(($success / $pingRuns) < (1-$acceptableLossPercentage)) {
$online = '0';
//echo 'Too many lost packages';
} else {
$online = '1';
//echo 'Still online!';
}
} else {
$online = '0';
//echo 'No lease found.';
}
file_put_contents('lastResult.dat',$online);
$this->online = $online;
$this->lastResult = $lastResult;
}
public function warnBridge() {
$go = false; if(strtotime('now') > strtotime('today 8:50am') && strtotime('now') < strtotime('today 9:05am')) $go = true;
if($go) {
$this->checkOnline();
if(!$this->online && $this->lastResult) $this->sendPushover('Niet over de Europabrug','i.v.m. openstaande brug');
}
}
public function sendPushover($title,$message,$device=null) {
if(empty($title) || empty($message)) return false;
$params = array(
'user' => 'YOUR_PUSHOVER_USER_TOKEN',
'token' => 'YOUR_PUSHOVER_APP_TOKEN',
'title' => $title,
'message' => $message
);
if($device != null) {
$params['device'] = $device;
}
$this->doCurl('https://api.pushover.net/1/messages.json',$params);
}
public function doCurl($url,$params) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
return curl_exec($ch);
}
}
$notifier = new Notifier();
$notifier->warnBridge();
$notifier->checkNewDownloads();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment