From 35714ccbb7b64f28ba38d67dca9cfdffde2d21f3 Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 9 Feb 2010 11:41:07 +0100 Subject: Batt: use sysfs to retrieve information Ignore-this: 06b7e637c2898c3abcc0959a077b8e418c0c15d4 darcs-hash:20100209104107-d6583-4cbd13d8e7194ce28831e2a106e0bd7078481f63.gz --- Plugins/Monitors/Batt.hs | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 78d9eae..93fddf9 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -18,37 +18,36 @@ import qualified Data.ByteString.Lazy.Char8 as B import Plugins.Monitors.Common import System.Posix.Files (fileExist) -data Batt = Batt Float +data Batt = Batt Float String | NA battConfig :: IO MConfig battConfig = mkMConfig "Batt: " -- template - ["left"] -- available replacements + ["left","status"] -- available replacements -file2batfile :: String -> (String, String) -file2batfile s = ("/proc/acpi/battery/"++ s ++ "/info", "/proc/acpi/battery/"++ s ++ "/state") +file2batfile :: String -> (String, String, String) +file2batfile s = ( s' ++ "/charge_full" + , s' ++ "/charge_now" + , s' ++ "/status") + where s' = "/sys/class/power_supply/" ++ s -readFileBatt :: (String, String) -> IO (B.ByteString, B.ByteString) -readFileBatt (i,s) = - do a <- rf i - b <- rf s - return (a,b) +readFileBatt :: (String, String, String) -> IO (String, String, String) +readFileBatt (f,n,s) = + do a <- rf f + b <- rf n + c <- rf s + return (a,b,c) where rf file = do - f <- fileExist file - if f then catRead file else return B.empty + fe <- fileExist file + if fe then B.unpack `fmap` catRead file else return [] -parseBATT :: [(String, String)] -> IO Batt +parseBATT :: [(String, String, String)] -> IO Batt parseBATT bfs = - do [(a0,b0),(a1,b1),(a2,b2)] <- mapM readFileBatt (take 3 $ bfs ++ repeat ("","")) - let sp p s = case stringParser p s of - [] -> 0 - x -> read x - (f0, p0) = (sp (3,2) a0, sp (2,4) b0) - (f1, p1) = (sp (3,2) a1, sp (2,4) b1) - (f2, p2) = (sp (3,2) a2, sp (2,4) b2) - left = (p0 + p1 + p2) / (f0 + f1 + f2) --present / full - return $ if isNaN left then NA else Batt left + do [(a0,b0,c0),(a1,b1,_),(a2,b2,_)] <- mapM readFileBatt (take 3 $ bfs ++ repeat ("","","")) + let read' s = if s == [] then 0 else read s + left = (read' b0 + read' b1 + read' b2) / (read' a0 + read' a1 + read' a2) --present / full + return $ if isNaN left then NA else Batt left c0 formatBatt :: Float -> Monitor [String] formatBatt x = @@ -63,6 +62,6 @@ runBatt' :: [String] -> [String] -> Monitor String runBatt' bfs _ = do c <- io $ parseBATT (map file2batfile bfs) case c of - Batt x -> do l <- formatBatt x - parseTemplate l + Batt x s -> do l <- formatBatt x + parseTemplate (l ++ [s]) NA -> return "N/A" -- cgit v1.2.3