Skip to content

Instantly share code, notes, and snippets.

@InFog
Last active August 29, 2015 14:24
Show Gist options
  • Save InFog/ace7b8fedd5311c23456 to your computer and use it in GitHub Desktop.
Save InFog/ace7b8fedd5311c23456 to your computer and use it in GitHub Desktop.
Sonic Boom
<?php
// Download medoo.php (http://medoo.in) and place it here:
require "/usr/local/lib/php/medoo.php";
function writeLog($message)
{
// If you don't want logs, use false
if (true) {
$message = date('Y-m-d H:i:s') . " " . $message . "\n";
file_put_contents(
'/var/log/automation/feeding.log',
$message,
FILE_APPEND
);
}
}
writeLog('Starting new run');
$db = new medoo([
'database_type' => 'mysql',
'database_name' => 'myautomation',
'server' => 'localhost',
'username' => 'dbuser',
'password' => 'secure',
'charset' => 'utf8',
]);
foreach ($db->select('modulos', '*') as $module) {
writeLog("Running update for module ip {$module['IP']}");
try {
$status = str_split(file_get_contents("http://{$module['IP']}/?READSTATUS"));
$db->insert('status', [
'ID' => $module['ID'],
'LAMP1' => $status[0],
'LAMP2' => $status[1],
'LAMP3' => $status[2],
'LAMP4' => $status[3],
'LAMP5' => $status[4],
'LAMP6' => $status[5],
'LAMP7' => $status[6],
'LAMP8' => $status[7],
]);
writeLog("Sucessfully run update for module ip {$module['IP']}");
} catch (Exception $e) {
writeLog("Error updating module {$module['IP']}. {$e->getMessage()}");
}
}
writeLog('Finished run');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment