From b03ba45a61bafc40dec0805be49672d2177ec1cd Mon Sep 17 00:00:00 2001 From: hexenmeister Date: Wed, 6 Aug 2014 13:45:40 +0000 Subject: [PATCH] power supply information readings (ac, usb, battery) added git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@6364 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/42_SYSMON.pm | 234 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/FHEM/42_SYSMON.pm b/FHEM/42_SYSMON.pm index b27f6ef39..5c42fed8e 100644 --- a/FHEM/42_SYSMON.pm +++ b/FHEM/42_SYSMON.pm @@ -169,6 +169,35 @@ SYSMON_updateCurrentReadingsMap($) { $rMap->{+CPU_TEMP} = "CPU temperature"; $rMap->{"cpu_temp_avg"} = "Average CPU temperature"; } + + if(SYSMON_isSysPowerAc($hash)) { + #$rMap->{"power_ac_online"} = "AC-Versorgung Status"; + #$rMap->{"power_ac_present"} = "AC-Versorgung vorhanden"; + #$rMap->{"power_ac_current"} = "AC-Versorgung Strom"; + #$rMap->{"power_ac_voltage"} = "AC-Versorgung Spannung"; + $rMap->{"power_ac_stat"} = "AC-Versorgung Info"; + $rMap->{"power_ac_text"} = "AC-Versorgung Info"; + } + + if(SYSMON_isSysPowerUsb($hash)) { + #$rMap->{"power_usb_online"} = "USB-Versorgung Status"; + #$rMap->{"power_usb_present"} = "USB-Versorgung vorhanden"; + #$rMap->{"power_usb_current"} = "USB-Versorgung Strom"; + #$rMap->{"power_usb_voltage"} = "USB-Versorgung Spannung"; + $rMap->{"power_usb_stat"} = "USB-Versorgung Info"; + $rMap->{"power_usb_text"} = "USB-Versorgung Info"; + } + + if(SYSMON_isSysPowerBat($hash)) { + #$rMap->{"power_battery_online"} = "Batterie-Versorgung Status"; + #$rMap->{"power_battery_present"} = "Batterie-Versorgung vorhanden"; + #$rMap->{"power_battery_current"} = "Batterie-Versorgung Strom"; + #$rMap->{"power_battery_voltage"} = "Batterie-Versorgung Spannung"; + $rMap->{"power_battery_stat"} = "Batterie-Versorgung Info"; + $rMap->{"power_battery_text"} = "Batterie-Versorgung Info"; + $rMap->{"power_battery_info"} = "Batterie-Versorgung Zusatzinfo"; + } + #$rMap->{"fhemuptime"} = "Betriebszeit FHEM"; #$rMap->{"fhemuptime_text"} = "Betriebszeit FHEM"; #$rMap->{"idletime"} = "Leerlaufzeit"; @@ -679,6 +708,17 @@ SYSMON_obtainParameters($$) $map = SYSMON_getLoadAvg($hash, $map); $map = SYSMON_getCPUProcStat($hash, $map); #$map = SYSMON_getDiskStat($hash, $map); + + # Power info (cubietruck) + if(SYSMON_isSysPowerAc($hash)) { + $map = SYSMON_PowerAcInfo($hash, $map); + } + if(SYSMON_isSysPowerUsb($hash)) { + $map = SYSMON_PowerUsbInfo($hash, $map); + } + if(SYSMON_isSysPowerBat($hash)) { + $map = SYSMON_PowerBatInfo($hash, $map); + } } } @@ -1853,6 +1893,130 @@ SYSMON_isFB($) { return $sys_fb; } +#-Power------- +my $sys_power_ac = undef; +sub +SYSMON_isSysPowerAc($) { + my ($hash) = @_; + if(!defined $sys_power_ac) { + $sys_power_ac = int(SYSMON_execute($hash, "[ -f /sys/class/power_supply/ac/online ] && echo 1 || echo 0")); + } + + return $sys_power_ac; +} + +my $sys_power_usb = undef; +sub +SYSMON_isSysPowerUsb($) { + my ($hash) = @_; + if(!defined $sys_power_usb) { + $sys_power_usb = int(SYSMON_execute($hash, "[ -f /sys/class/power_supply/usb/online ] && echo 1 || echo 0")); + } + + return $sys_power_usb; +} + +my $sys_power_bat = undef; +sub +SYSMON_isSysPowerBat($) { + my ($hash) = @_; + if(!defined $sys_power_bat) { + $sys_power_bat = int(SYSMON_execute($hash, "[ -f /sys/class/power_supply/battery/online ] && echo 1 || echo 0")); + } + + return $sys_power_bat; +} + +sub SYSMON_PowerAcInfo($$) +{ + #online, present, current_now (/1000 =>mA), voltage_now (/1000000 => V) + my ($hash, $map) = @_; + my $type="ac"; + my $base = "cat /sys/class/power_supply/".$type."/"; + + my $d_online = trim(SYSMON_execute($hash, $base."online")); + my $d_present = trim(SYSMON_execute($hash, $base."present")); + my $d_current = SYSMON_execute($hash, $base."current_now"); + if(defined $d_current) {$d_current/=1000;} + my $d_voltage = SYSMON_execute($hash, $base."voltage_now"); + if(defined $d_voltage) {$d_voltage/=1000000;} + + #$map->{"power_".$type."_online"}=$d_online; + #$map->{"power_".$type."_present"}=$d_present; + #$map->{"power_".$type."_current"}=$d_current; + #$map->{"power_".$type."_voltage"}=$d_voltage; + $map->{"power_".$type."_stat"}="$d_online $d_present $d_voltage $d_current"; + $map->{"power_".$type."_text"}=$type.": ".(($d_present eq "1") ? "present" : "absent")." / ".($d_online eq "1" ? "online" : "offline").", Voltage: ".$d_voltage." V, Current: ".$d_current." mA"; + + return $map; +} + +sub SYSMON_PowerUsbInfo($$) +{ + #online, present, current_now (/1000 =>mA), voltage_now (/1000000 => V) + my ($hash, $map) = @_; + my $type="usb"; + my $base = "cat /sys/class/power_supply/".$type."/"; + + my $d_online = trim(SYSMON_execute($hash, $base."online")); + my $d_present = trim(SYSMON_execute($hash, $base."present")); + my $d_current = SYSMON_execute($hash, $base."current_now"); + if(defined $d_current) {$d_current/=1000;} + my $d_voltage = SYSMON_execute($hash, $base."voltage_now"); + if(defined $d_voltage) {$d_voltage/=1000000;} + + #$map->{"power_".$type."_online"}=$d_online; + #$map->{"power_".$type."_present"}=$d_present; + #$map->{"power_".$type."_current"}=$d_current; + #$map->{"power_".$type."_voltage"}=$d_voltage; + $map->{"power_".$type."_stat"}="$d_online $d_present $d_voltage $d_current"; + $map->{"power_".$type."_text"}=$type.": ".(($d_present eq "1") ? "present" : "absent")." / ".($d_online eq "1" ? "online" : "offline").", Voltage: ".$d_voltage." V, Current: ".$d_current." mA"; + + return $map; +} + +sub SYSMON_PowerBatInfo($$) +{ + #online, present, current_now (/1000 =>mA), voltage_now (/1000000 => V) + my ($hash, $map) = @_; + my $type="battery"; + my $base = "cat /sys/class/power_supply/".$type."/"; + + my $d_online = trim(SYSMON_execute($hash, $base."online")); + my $d_present = trim(SYSMON_execute($hash, $base."present")); + my $d_current = SYSMON_execute($hash, $base."current_now"); + if(defined $d_current) {$d_current/=1000;} + my $d_voltage = SYSMON_execute($hash, $base."voltage_now"); + if(defined $d_voltage) {$d_voltage/=1000000;} + + #$map->{"power_".$type."_online"}=$d_online; + #$map->{"power_".$type."_present"}=$d_present; + #$map->{"power_".$type."_current"}=$d_current; + #$map->{"power_".$type."_voltage"}=$d_voltage; + $map->{"power_".$type."_stat"}="$d_online $d_present $d_voltage $d_current"; + $map->{"power_".$type."_text"}=$type.": ".(($d_present eq "1") ? "present" : "absent")." / ".($d_online eq "1" ? "online" : "offline").", Voltage: ".$d_voltage." V, Current: ".$d_current." mA"; + + # TODO + if($d_present eq "1") { + # Zusaetzlich: technology, capacity, status, health, temp (/10 => °C) + my $d_technology = trim(SYSMON_execute($hash, $base."technology")); + my $d_capacity = trim(SYSMON_execute($hash, $base."capacity")); + my $d_status = trim(SYSMON_execute($hash, $base."status")); + my $d_health = trim(SYSMON_execute($hash, $base."health")); + my $d_temp = trim(SYSMON_execute($hash, $base."temp")); + if(defined $d_temp) {$d_temp/=10;} + + $map->{"power_".$type."_info"}=$type." info: ".$d_technology." , capacity: ".$d_capacity." %, status: ".$d_status." , health: ".$d_health." , temperatur: ".$d_temp." C"; + + # ggf. noch irgendwann: model_name, voltage_max_design, voltage_min_design + } else { + $map->{"power_".$type."_info"}=$type." info: n/a , capacity: n/a %, status: n/a , health: n/a , temperatur: n/a C"; + } + + return $map; +} +#------------- + sub SYSMON_execute($$) { @@ -2051,6 +2215,41 @@ If one (or more) of the multiplier is set to zero, the corresponding readings is Information on the installed firmware version: