#!/usr/bin/perl #use strict; #require ('../libs/perl/x10.pl'); #script by Alejandro Gonzalez Sole # Capstone Project - Group SANDLS #Script based on: http://doleans.net/priscilla/demo/modules/daemons/x10/ use DBI; use vars qw($VERSION $DEBUG); $VERSION = '0.01'; $DEBUG = 1; my $debugmode = 0; my $nodbupdate = 0; my $showupdates = 0; my $noadd = 0; my $dbname = "default"; my $dbuser = "root"; my $dbpass = "root"; my $dbhost = "localhost"; my $dbport = "3306"; my $dbtable = "devices"; my $skip = 0; foreach $num (0 .. $#ARGV) { if ($ARGV[$num] eq "-debug"){ $debugmode = 1; }elsif ($ARGV[$num] eq "-nodbupdate"){ $nodbupdate = 1; }elsif ($ARGV[$num] eq "-showupdates"){ $showupdates = 1; }elsif ($ARGV[$num] eq "-d"){ $skip=1; $dbname = $ARGV[$num+1]; }elsif ($ARGV[$num] eq "-u"){ $skip=1; $dbuser = $ARGV[$num+1]; }elsif ($ARGV[$num] eq "-p"){ $skip=1; $dbpass = $ARGV[$num+1]; }elsif ($ARGV[$num] eq "-h"){ $skip=1; $dbhost = $ARGV[$num+1]; }elsif ($ARGV[$num] eq "-p"){ $skip=1; $dbport = $ARGV[$num+1]; }elsif ($ARGV[$num] eq "-t"){ $skip=1; $dbtable = $ARGV[$num+1]; }elsif ($ARGV[$num] eq "-nadd"){ $noadd = 1; }elsif ($ARGV[$num] eq "-?" || $ARGV[$num] eq "-help" || $ARGV[$num] eq "--help"){ print "\nUsage: perl x10bindDB.pl [arguments]\n"; print " -debug\t\tstarts the X10 binder in debug mode.\n"; print " -nodbupdate\t\tdisables database updates.\n"; print " -showupdates\t\tdisplays information when updating database.\n"; print " -d [database]\tsets the database name. default is 'default'\n"; print " -u [username]\tsets the username of the datebase. default 'root'.\n"; print " -p [password]\tsets the password for the username. default 'root'.\n"; print " -h [host_name]\tsets the host of the database. default 'localhost'.\n"; print " -p [port_number]\tsets the port number of the host. default '3306'.\n"; print " -t [table_name]\tsets the database table to be used. default 'devices'.\n"; print " -?, --help, -help\tDisplays this help menu.\n"; print "\n\n"; exit; }else{ if ($skip==0){ print "invalid argument: '".$ARGV[$num]."'\n"; print "Try `chmod --help' for more information.\n"; exit; }else{ $skip =0; } } } print "\nStarting X10 binder\n"; if ($debugmode == 1){print "Debug mode enabled\n";} if ($nodbupdate == 1){ print "Skipping database updating.\n"; }else{ print "Using "; print "database:'$dbname' "; print "host:'$dbhost:$dbport' "; print "user:'$dbuser' "; print "table:'$dbtable'\n"; } if ($noadd == 1){ print "Skipping add-to-table on noexist.\n";} if ($showupdates == 1){ print "Displaying updates to database.\n";} sysopen( CM15A, "/dev/cm15a0", O_RDWR |O_NOCTTY | O_NONBLOCK ) || die "Device Failed\n Error: Cannot open device: /dev/cm15a0\n"; print "Device Ok\n"; select(undef, undef, undef, 0.70); my $serial_port = \*CM15A; my $oldunpack = ""; print "Device ready for input\n"; while (true) { my $nb = 0; my $unpack = 0; my $nb = sysread($serial_port,$enr,100); my $unpack = unpack('H*', $enr); my $hex2bin = hex2bin($unpack); if (!($unpack eq $oldunpack)) { if (substr($unpack, 0, 4) eq "5d29"){ if ($debugmode==1){ print "RF command $unpack recieved:\n"; }else{print "RF command recieved \n";} $devid = bin2dec(substr($hex2bin, 15, 8).substr($hex2bin, 47, 8)); print " DEVICE ID: ".$devid." - STATE: "; $stbin = substr($hex2bin, 31, 8); $opnstate = substr($stbin,1,1); $minstate = substr($stbin,6,1); $batstate = substr($stbin,7,1); if ($opnstate==0){ print "OPEN "; }else{ print "CLOSE "; } if ($maxstate==0){ print "(MAX DELAY) "; }else{ print "(MIN DELAY) "; } if ($batstate==1){ print "*LOW BATTERY*"; } print "\n"; if ($nodbupdate == 0){ updateDataBase($devid, $opnstate, $minstate, $batstate); } }else{ if ($debugmode==1){ print "Unknown command $unpack recieved.\n"; } } $oldunpack = $unpack } select(undef, undef, undef, 0.01); } close( CM15A ); sub updateDataBase { my $devid = shift; my $devstate = shift; my $devmax = shift; my $devbat = shift; $dbh = DBI->connect("dbi:mysql:database=$dbname;host=$dbhost:$dbport;user=$dbuser;password=$dbpass") or die "Couldn't connect to database.\n$DBI::errstr\n"; $query_select = "SELECT * FROM $dbtable WHERE device = '$devid'"; $sth = $dbh->prepare($query_select); $sth->execute; if ($sth->rows > 0){ $query_select = "UPDATE $dbtable SET status='$devstate', delay_min='$devmax', low_battery='$devbat' WHERE device ='$devid'"; $sth = $dbh->prepare($query_select); $sth->execute; if ($debugmode==1 || $showupdates==1){ print "Device updated in database.\n";} }else{ $query_select = "INSERT INTO $dbtable (device, status, delay_min, low_battery) VALUES ('$devid', '$devstate', '$devmax', '$devbat')"; $sth = $dbh->prepare($query_select); $sth->execute; if ($debugmode==1 || $showupdates==1){ print "Device added to the database.\n";} } $dbh->disconnect; } sub hex2bin { my $h = shift; my $hlen = length($h); my $blen = $hlen * 4; return unpack("B$blen", pack("H$hlen", $h)); } sub bin2dec { my $b = shift; my $blen = length($b); return unpack("N", pack("B32", substr("0" x 32 . $b, -32))); }