summaryrefslogtreecommitdiff
path: root/packages/code/dfetch/app/Dfetch/Battery.hs
blob: 0315882c3f442ef3eb4b3372d42c31e675e5921e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Dfetch.Battery
  ( Battery(..)
  , allBatteries
  ) where

import           Data.List        (isPrefixOf)
import           System.Directory (listDirectory)
import           System.FilePath  ((</>))

basePath :: FilePath
basePath = "/sys/class/power_supply/"

data Battery = Battery
  { name       :: String
  , capacity   :: Int
  , energy     :: Double
  , energyFull :: Double
  } deriving (Show, Eq)

getCapacity :: String -> IO Battery
getCapacity bName = Battery
  <$> pure bName
  <*> (read <$> readFile (path </> "capacity"))
  <*> (read <$> readFile (path </> "energy_now"))
  <*> (read <$> readFile (path </> "energy_full"))
  where
    path = basePath </> bName

findBatteries :: IO [String]
findBatteries = filter ("BAT" `isPrefixOf`) <$> listDirectory basePath

-- | Fetch all batteries from the running system
allBatteries :: IO [Battery]
allBatteries = findBatteries >>= mapM getCapacity