Guest User

elk - m1 - perl script - data logging

a guest
Apr 28th, 2015
238
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. use ElkM1::Control;
  4. use Switch;
  5. use strict;
  6.  
  7. # to trap ctrl-c and exit gracefully closing the connection to elk
  8. $SIG{'INT'} = 'cleanup';
  9. $SIG{'TERM'} = 'cleanup';
  10. $SIG{'KILL'} = 'cleanup';
  11.  
  12. my %elkcodes = (
  13.     AS => { name => 'Arming Status Report Data' },
  14.     AP => { name => 'ASCII String To IP Address' },
  15.     AR => { name => 'Ethernet Central Station - Alarm Reporting' },
  16.     AT => { name => 'Ethernet Central Station - Alarm Reporting Test' },
  17.     XK => { name => 'Ethernet Central Station - Ethernet Module Test' },
  18.     AZ => { name => 'Alarm By Zone Report Data' },
  19.     CA => { name => 'Touchscreen Audio Data' },
  20.     CC => { name => 'Output Change Update' },
  21.     CD => { name => 'Outgoing Audio Command' },
  22.     CS => { name => 'Control Output Status Report Data' },
  23.     CR => { name => 'Custom Value Data' },
  24.     CU => { name => 'Change User Code' },
  25.     CV => { name => 'Counter Value Format Data' },
  26.     DS => { name => 'Lighting Device Status Data' },
  27.     EE => { name => 'Entry/Exit Data' },
  28.     EM => { name => 'Email Trigger Data' },
  29.     IC => { name => 'Valid or Invalid User Code Format' },
  30.     IE => { name => 'Installer Mode Exited' },
  31.     SD => { name => 'Insteon Lighting - ASCII String Text Descriptions' },
  32.     IR => { name => 'Insteon Lighting - Read Insteon Lighting Device Data' },
  33.     IP => { name => 'Insteon Lighting - Programming of Insteon Device Data' },
  34.     KA => { name => 'Keypad Areas Data' },
  35.     KF => { name => 'Keypad Function Key Press' },
  36.     LW => { name => 'Zone/Keypad Temperature Data' },
  37.     PC => { name => 'PLC - Power Line Carrier - Change Update Data' },
  38.     PS => { name => 'PLC - Power Line Carrier - Status Data' },
  39.     RE => { name => 'Reset Ethernet IP Address' },
  40.     RP => { name => 'ElkRP Connected' },
  41.     RR => { name => 'Real Time Clock Data' },
  42.     SD => { name => 'ASCII String Text Descriptions Data' },
  43.     SS => { name => 'System Trouble Status' },
  44.     ST => { name => 'Requested Temperature Data' },
  45.     TC => { name => 'Task Change Update' },
  46.     TR => { name => 'Thermostat Control - Thermostat Data' },
  47.     T2 => { name => 'M1 to PC with Omnistat 2 ASCII Hex Data' },
  48.     VN => { name => 'M1 Version Number Data' },
  49.     UA => { name => 'Valid User Code Areas Data' },
  50.     ZC => { name => 'Zone Change Update' },
  51.     ZB => { name => 'Zone Status - Bypassed Zone State' },
  52.     ZP => { name => 'Zone Status - Zone Partition Report' },
  53.     ZS => { name => 'Zone Status - Zone Status Report' },
  54.     ZD => { name => 'Zone Definition Data' },
  55.     ZV => { name => 'Zone Analog Voltage Data' },
  56.     );
  57.  
  58. my %zonelist = (
  59.     1 => { name => 'Den Back Door' },
  60.     2 => { name => 'Den Bath Window' },
  61.     3 => { name => 'Kitchen Door Win' },
  62.     4 => { name => 'DenGar Front Win' },
  63.     5 => { name => 'Kitchen Glass' },
  64.     6 => { name => 'Front Door' },
  65.     7 => { name => 'Garage Doors Win' },
  66.     8 => { name => 'Outside Wires' },
  67.     9 => { name => 'Zone 009' },
  68.     10 => { name => 'Crawl Space Temp' },
  69.     11 => { name => 'Den Glass' },
  70.     12 => { name => 'Living Rm Motion' },
  71.     13 => { name => 'Water Kitchen' },
  72.     14 => { name => 'Water Basement' },
  73.     15 => { name => 'Attic Temp' },
  74.     16 => { name => 'Fire Alarm' }
  75.     );
  76.  
  77. my %keypads = (
  78.     1 => { name => 'Upstairs Hall Keypad' },
  79.     2 => { name => 'Den Keypad' }
  80.     );
  81.  
  82. my %areas = (
  83.     1 => { name => 'Klemens Home' }
  84.     );
  85.        
  86. my $elk = ElkM1::Control->new(host => '192.168.1.22', port => 2101, 'debug' => 0);
  87. my $msg;
  88.     while (1) {  # Loop forever
  89.         $msg = $elk->readMessage(); # Read the M1's messages
  90.        
  91.         if(!defined($msg)) {
  92.             sleep 1;
  93.         } else {
  94.             my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
  95.             my $time = sprintf '%d-%02d-%02d %02d:%02d:%02d', 1900+$year, 1+$mon, $mday, $hour, $min, $sec;
  96.             open (my $fh, '>>', '/tmp/elklog.log');
  97.            
  98.             switch(ref($msg)) {
  99.                 case 'ElkM1::Control::Message::EthernetModuleTest' {
  100.                     my $eth_hour = $msg->getHour;
  101.                     my $eth_min = $msg->getMinutes;
  102.                     my $eth_sec = $msg->getSeconds;
  103.                     print $fh "$time - ETHERNET TEST - $eth_hour:$eth_min:$eth_sec\n";  # Ethernet Test
  104.                 }
  105.                
  106.                 case 'ElkM1::Control::Message::ZoneChangeUpdateReport' {
  107.                             my $zonenumber = $msg->getZone;
  108.                             my $zonename = $zonelist{$zonenumber}{'name'};
  109.                             my $zonestate = $msg->getState;
  110.                     my $zone_status = ($msg->isOpen) ? 'open' : 'closed';
  111.            
  112.                     print $fh "$time - ZONE CHANGE - $zonename - ZONE $zonenumber IS NOW $zonestate\n";  # Print zone change
  113.                     }
  114.                
  115.                     case 'ElkM1::Control::Message::TemperatureReply' {
  116.                             my $groupname = $msg->getGroupName;
  117.                             my $groupnumber = $msg->getGroup;
  118.                             my $groupdevice = $msg->getDevice;
  119.                             my $temperature = $msg->getTemperature;
  120.                     my $fixedgroupname = "";
  121.  
  122.                     if ($groupname eq "keypad") {
  123.                         $fixedgroupname = $keypads{$groupdevice}{'name'};
  124.                     } else {
  125.                         $fixedgroupname = $zonelist{$groupdevice}{'name'};
  126.                     }
  127.  
  128.                     print $fh "$time - TEMPERATURE DATA - $fixedgroupname : $temperature F\n";  # Print temp data
  129.                     }
  130.                
  131.                     case 'ElkM1::Control::Message::ArmingStatusReport' {
  132.                     my $area_number = 1;
  133.                     my $area_name = $areas{$area_number}{'name'};
  134.                     my $area_status = $msg->getArmedStatusName($area_number);
  135.                     my $area_alarm = $msg->getAlarmStatusName($area_number);
  136.            
  137.                     print $fh "$time - ARMING STATUS - AREA $area_name - $area_number IS $area_status - $area_alarm\n";  # Print everything else
  138.                 }
  139.  
  140.                 case 'ElkM1::Control::Message::KeypadKeyChangeUpdate' {
  141.                     my $keypress = $msg->getKeyName;
  142.                     my $keypad = $keypads{$msg->getKeypadNumber}{'name'};
  143.  
  144.                     print $fh "$time - KEYPAD PRESS - KEYPAD:$keypad - KEY:$keypress\n"; # Print keypand press
  145.                 }
  146.  
  147.                 case 'ElkM1::Control::Message::ThermostatDataReply' {
  148.                     my $thermo = $msg->toString;
  149.  
  150.                     print $fh "$time - THERMOSTAT DATA - $thermo\n"; # Print thermostat data
  151.                 }
  152.            
  153.                 case 'ElkM1::Control::Message' {
  154.                     my $elkmessage = $msg->message;
  155.                     my $elkmsgcode = substr $elkmessage, 2, 2;
  156.                     my $elkmsgtype = $elkcodes{$elkmsgcode}{'name'};
  157.                    
  158.                     print $fh "$time - ELK MESSAGE - $elkmsgtype - $elkmessage\n"; # Print message
  159.                 }
  160.  
  161.                 else {
  162.                     print $fh "$time - UNKNOWN DATA - $msg\n";  # Print everything else
  163.                 }
  164.             }
  165.             close $fh;
  166.         }
  167.     }
  168. $elk->disconnect;
  169. exit;
  170.  
  171. sub cleanup {
  172.     $elk->disconnect;
  173.     die 'stopped';
  174.     exit;
  175. }
RAW Paste Data