Skip to content
Snippets Groups Projects
Verified Commit 66302d90 authored by Legy (Beini)'s avatar Legy (Beini)
Browse files

Add new battery logic for kernels, that expose other endpoints

parent f4d6d11b
No related branches found
No related tags found
No related merge requests found
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
...@@ -13,7 +14,6 @@ import qualified Data.Text.Lazy.IO as TIO ...@@ -13,7 +14,6 @@ import qualified Data.Text.Lazy.IO as TIO
import System.Directory import System.Directory
import Data.Maybe import Data.Maybe
import Text.Read (readMaybe)
import Control.Lens import Control.Lens
...@@ -31,23 +31,51 @@ data BatteryState = BatteryState ...@@ -31,23 +31,51 @@ data BatteryState = BatteryState
getBatteryState :: FilePath -> IO (Maybe BatteryState) getBatteryState :: FilePath -> IO (Maybe BatteryState)
getBatteryState path = tryMaybe $ do getBatteryState path = maybe getBatteryStateCharge (return . Just) =<< getBatteryStateEnergy
status' <- TIO.readFile (path <> "/status")
powerNow' <- tryMaybe $ TIO.readFile (path <> "/power_now")
energyNow' <- readIO =<< readFile (path <> "/energy_now")
energyFull' <- readIO =<< readFile (path <> "/energy_full")
return BatteryState
{ _status = batteryStatus . T.strip $ status'
, _powerNow = readMaybe . T.unpack =<< powerNow'
, _energyNow = energyNow'
, _energyFull = energyFull'
}
where where
batteryStatus :: T.Text -> BatteryStatus getVoltage :: IO Double
batteryStatus statusText getVoltage = readIO =<< readFile (path <> "/voltage_now")
| statusText == "Charging" = BatteryCharging getBatteryStateEnergy :: IO (Maybe BatteryState)
| statusText == "Discharging" = BatteryDischarging getBatteryStateEnergy = tryMaybe $ do
| otherwise = BatteryOther status' <- batteryStatus
energyNow' <- readIO =<< readFile (path <> "/energy_now")
energyFull' <- readIO =<< readFile (path <> "/energy_full")
powerNow' <- batteryPower getVoltage
return BatteryState
{ _status = status'
, _powerNow = powerNow'
, _energyNow = energyNow'
, _energyFull = energyFull'
}
getBatteryStateCharge :: IO (Maybe BatteryState)
getBatteryStateCharge = tryMaybe $ do
status' <- batteryStatus
voltageNow' <- getVoltage
powerNow' <- batteryPower (return voltageNow')
chargeNow' <- readIO =<< readFile (path <> "/charge_now")
chargeFull' <- readIO =<< readFile (path <> "/charge_full")
return BatteryState
{ _status = status'
, _powerNow = powerNow'
, _energyNow = round $ voltageNow' * chargeNow' / 1000000
, _energyFull = round $ voltageNow' * chargeFull' / 1000000
}
batteryPower :: IO Double -> IO (Maybe Int)
batteryPower getVoltage' = do
power' <- tryMaybe $ readIO =<< readFile (path <> "/power_now")
case power' of
power@(Just _) -> return power
Nothing -> tryMaybe $ do
current <- readIO =<< readFile (path <> "/current_now")
voltage <- getVoltage'
return $ round $ voltage * current / 1000000
batteryStatus :: IO BatteryStatus
batteryStatus = do
statusText <- tryMaybe $ T.strip <$> TIO.readFile (path <> "/status")
return $ if
| statusText == Just "Charging" -> BatteryCharging
| statusText == Just "Discharging" -> BatteryDischarging
| otherwise -> BatteryOther
batteryBlock :: PullBlock batteryBlock :: PullBlock
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment