summaryrefslogtreecommitdiffstats
path: root/app/Sensor.hs
blob: b6cfba500bebc9c950b197de241b8ca9c2358d3b (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}

module Sensor
  ( cpu,
    io,
    net,
    mem,
    bat,
    Sensor.snd,
    disk,
    date,
    thermal,
    time,
    wmName,
    weatherForecast,
    wmWorkspaces,
  )
where

import Control.Arrow
import Control.DeepSeq
import Control.Monad
import Control.Monad.Reader
import Data.Aeson
import Data.Char
import Data.Dynamic
import Data.Functor.WithIndex
import Data.List
import Data.Maybe
import Data.Sensor qualified as S
import Data.Set qualified as S
import Data.String
import Data.Time.Clock
import Data.Time.Format
import Data.Time.LocalTime
import GHC.Generics (Generic)
import Graphics.X11 qualified as X
import Graphics.X11.Xlib.Extras qualified as X
import Network.HTTP.Simple
import Pretty qualified as P
import Process.Shell (sh)
import Safe
import System.Directory
import System.FilePath
import System.IO (hGetContents)
import System.Posix.StatVFS
import System.Posix.Types (Fd (..))
import Text.Printf
import UnliftIO
import UnliftIO.Concurrent
import Witherable (ifilter)
import Prelude hiding (readFile)

data CpuStatData = CpuStatData
  { used :: Int,
    total :: Int
  }
  deriving (Eq, Show, Generic, NFData)

data CpuStat = CpuStat deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m CpuStat CpuStatData where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = readFile "/proc/stat" $ \stat -> do
        case filter (["cpu"] `isPrefixOf`) (words <$> lines stat) of
          [] -> error "/proc/stat: no cpu line"
          ((drop 1 . fmap read -> (xs@(_ : _ : _ : idle : _))) : _) -> do
            let total = sum xs
                used = total - idle
            pure (CpuStatData used total)
          _ -> error "/proc/stat: unexpected cpu line"

readFile :: (MonadUnliftIO m, NFData a) => FilePath -> (String -> m a) -> m a
readFile fp f =
  withFile fp ReadMode $ \h ->
    evaluate =<< f =<< (liftIO (hGetContents h))

sleep :: (MonadIO m) => S.AggregateT s m ()
sleep = threadDelay (5 * 10 ^ 5)

cpuStat :: (S.MonadSensor m) => S.Sensor m () CpuStatData
cpuStat = S.sensor CpuStat

cpu :: (S.MonadSensor m) => S.Sensor m () (P.Diagram [Float])
cpu = step <$$> diagram 3 cpuStat
  where
    step xs =
      zipWith
        ( \next prev ->
            let CpuStatData {used, total} =
                  CpuStatData (next.used - prev.used) (next.total - prev.total)
             in if total == 0 then 0 else fromIntegral used / fromIntegral total
        )
        (fromMaybe [] (tailMay xs))
        (fromMaybe [] (initMay xs))

diagram :: (Monad m) => Int -> S.Sensor m () a -> S.Sensor m () (P.Diagram [a])
diagram n sf = S.feedbackS [] $ proc ((), xs) -> do
  x <- sf -< ()
  returnA -< (P.diagram n (reverse (x : xs)), take (2 * (n - 1) + 1) (x : xs))

data Thermal = Thermal deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m Thermal Int where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = liftIO do
        is <- listDirectory "/sys/class/thermal"
        maximum <$> mapM (parse1 . ("/sys/class/thermal" </>)) is
      parse1 fp = do
        (choice 0 [readFile (fp </> "temp") (pure . read . strip)])

thermal :: (S.MonadSensor m) => S.Sensor m () String
thermal = do
  temp <- S.sensor Thermal
  pure (printf "%.1f" (value temp))
  where
    value :: Int -> Float
    value temp = fromIntegral temp / 1000.0

data IoStat = IoStat deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m IoStat Int where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = liftIO do
        fmap sum . mapM (parse1 . ("/sys/block" </>))
          =<< listDirectory "/sys/block"
      parse1 :: FilePath -> IO Int
      parse1 fp = do
        readFile (fp </> "stat") $ \stat -> do
          case words stat of
            [_, _, read', _, _, _, write, _, _, _, _, _, _, _, _, _, _] ->
              pure (read read' + read write)
            _ -> error (printf "%s/stat: malformed" fp)

ioStat :: (S.MonadSensor m) => S.Sensor m () Int
ioStat = S.sensor IoStat

io :: (S.MonadSensor m) => S.Sensor m () (P.Diagram [Float])
io = diagram 3 (maxS (rate ioStat))

maxS :: (Monad m, Num a, Ord a, Integral a) => S.Sensor m () a -> S.Sensor m () Float
maxS sf = S.feedbackS 0 $ proc ((), xMax) -> do
  x <- fi <$> sf -< ()
  let xMax' = max xMax x
  returnA -< (if xMax <= 0 then 0 else x / xMax', xMax')

rate :: (Monad m, Num a) => S.Sensor m () a -> S.Sensor m () a
rate sf = S.feedbackS Nothing $ proc ((), x') -> do
  x <- sf -< ()
  returnA -< (maybe 0 ((-) x) x', Just x)

data NetStat = NetStat deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m NetStat Int where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = liftIO do
        is <- listDirectory "/sys/class/net"
        sum <$> mapM (parse1 . ("/sys/class/net" </>)) is
      parse1 :: FilePath -> IO Int
      parse1 fp = do
        (+)
          <$> (readFile (fp </> "statistics/rx_bytes") (pure . read))
          <*> (readFile (fp </> "statistics/tx_bytes") (pure . read))

netStat :: (S.MonadSensor m) => S.Sensor m () Int
netStat = S.sensor NetStat

net :: (S.MonadSensor m) => S.Sensor m () (P.Diagram [Float])
net = diagram 3 (maxS (rate netStat))

data MemStat = MemStat deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m MemStat Float where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = readFile "/proc/meminfo" $ \meminfo -> do
        case foldl
          ( \(total, avail) xs ->
              case xs of
                ["MemTotal:", v, "kB"] -> (Just (read v), avail)
                ["MemAvailable:", v, "kB"] -> (total, Just (read v))
                _ -> (total, avail)
          )
          (Nothing, Nothing)
          (map words (lines meminfo)) of
          (Just total, Just avail) -> pure (1 - avail / total)
          (Nothing, _) -> error "/proc/meminfo: MemTotal missing"
          (_, Nothing) -> error "/proc/meminfo: MemAvail missing"

memStat :: (S.MonadSensor m) => S.Sensor m () Float
memStat = S.sensor MemStat

mem :: (S.MonadSensor m) => S.Sensor m () (P.Diagram [Float])
mem = diagram 3 memStat

data BatStat = BatStat deriving (Show)

data BatState
  = NotCharging
  | Charging
  | Discharging
  deriving (Generic, Eq, NFData, Show)

data BatType
  = Mains
  | Bat
  | USB
  deriving (Generic, Eq, NFData)

instance (S.MonadSensor m) => S.Aggregate m BatStat [(Float, BatState, BatType)] where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = liftIO do
        mapM (parse1 . ("/sys/class/power_supply" </>))
          =<< listDirectory "/sys/class/power_supply"
      parse1 :: FilePath -> IO (Float, BatState, BatType)
      parse1 fp =
        liftM3
          (,,)
          ( choice 1 $
              [ (/)
                  <$> (readFile (fp </> "charge_now") (pure . read))
                  <*> (readFile (fp </> "charge_full") (pure . read)),
                (/)
                  <$> (readFile (fp </> "energy_now") (pure . read))
                  <*> (readFile (fp </> "energy_full") (pure . read))
              ]
          )
          (choice NotCharging [readFile (fp </> "status") (pure . parseBatState . map toLower)])
          (choice Mains [readFile (fp </> "type") (pure . parseBatType . map toLower)])

parseBatState :: String -> BatState
parseBatState string =
  case strip string of
    "not charging" -> NotCharging
    "charging" -> Charging
    "discharging" -> Discharging
    _ -> error ("invalid bat state: " ++ string)

parseBatType :: String -> BatType
parseBatType string =
  case strip string of
    "mains" -> Mains
    "battery" -> Bat
    "usb" -> USB
    _ -> error ("invalid bat type: " ++ string)

bat :: (S.MonadSensor m) => S.Sensor m () P.Doc
bat = do
  bats <- S.sensor BatStat
  fmap mconcat $ sequence $ intersperse (pure (P.pretty " ")) $ map printBat $ filter isBat bats
  where
    isBat (_, _, batType) =
      batType == Bat
    printBat :: (S.MonadSensor m) => (Float, BatState, BatType) -> S.Sensor m () P.Doc
    printBat (value, batState, _) =
      case batState of
        NotCharging ->
          pure (printBatNotCharging value)
        Charging ->
          pure (printBatCharging value)
        Discharging ->
          printBatDischarging value

printBatNotCharging :: Float -> P.Doc
printBatNotCharging value =
  if
    | value >= 0.95 -> P.pretty "\xf0079"
    | value >= 0.9 -> P.pretty "\xf0082"
    | value >= 0.8 -> P.pretty "\xf0081"
    | value >= 0.7 -> P.pretty "\xf0080"
    | value >= 0.6 -> P.pretty "\xf007f"
    | value >= 0.5 -> P.pretty "\xf007e"
    | value >= 0.4 -> P.pretty "\xf007d"
    | value >= 0.3 -> P.pretty "\xf007c"
    | value >= 0.2 -> P.pretty "\xf007b"
    | value >= 0.1 -> P.pretty "\xf007a"
    | otherwise -> P.pretty "\xf008e"

printBatCharging :: Float -> P.Doc
printBatCharging value =
  if
    | value >= 0.9 -> P.pretty "\xf0085"
    | value >= 0.8 -> P.pretty "\xf008b"
    | value >= 0.7 -> P.pretty "\xf008a"
    | value >= 0.6 -> P.pretty "\xf089e"
    | value >= 0.5 -> P.pretty "\xf0089"
    | value >= 0.4 -> P.pretty "\xf089d"
    | value >= 0.3 -> P.pretty "\xf0088"
    | value >= 0.2 -> P.pretty "\xf0087"
    | value >= 0.1 -> P.pretty "\xf0086"
    | otherwise -> P.pretty "\xf089c"

printBatDischarging :: (S.MonadSensor m) => Float -> S.Sensor m () P.Doc
printBatDischarging value =
  if value < 0.1
    then
      ( \b ->
          if b
            then mconcat [P.pretty batValue, P.pretty "\xf433"]
            else (P.color P.Red) (mconcat [P.pretty "\xf0083", P.pretty "\xf433"])
      )
        <$> blink
    else pure (mconcat [P.pretty batValue, P.pretty "\xf433"])
  where
    batValue =
      if
        | value >= 0.95 -> "\xf0079"
        | value >= 0.9 -> "\xf0082"
        | value >= 0.8 -> "\xf0081"
        | value >= 0.7 -> "\xf0080"
        | value >= 0.6 -> "\xf007f"
        | value >= 0.5 -> "\xf007e"
        | value >= 0.4 -> "\xf007d"
        | value >= 0.3 -> "\xf007c"
        | value >= 0.2 -> "\xf007b"
        | value >= 0.1 -> "\xf007a"
        | otherwise -> "\xf008e"

data Blink = Blink deriving (Show)

blink :: (S.MonadSensor m) => S.Sensor m () Bool
blink = S.sensor Blink

instance (S.MonadSensor m) => S.Aggregate m Blink Bool where
  aggregate _ = loop True
    where
      loop b = S.yield b >> sleep >> loop (not b)

choice :: a -> [IO a] -> IO a
choice def [] = pure def
choice def (x : xs) = x `catch` (\(_ :: SomeException) -> choice def xs)

data Snd = Snd deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m Snd Float where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = liftIO do (/ 153) . read <$> [sh|pamixer --get-volume|]

snd :: (S.MonadSensor m) => S.Sensor m () String
snd = do
  value <- S.sensor Snd
  return
    ( if
        | 0.4 < value -> "\xf028"
        | 0.0 < value -> "\xf027"
        | otherwise -> "\xf026"
    )

data DiskStat = DiskStat deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m DiskStat Float where
  aggregate _ = forever do S.yield =<< parse <* sleep
    where
      parse = liftIO do
        stat <- statVFS "/"
        pure $
          (fi stat.statVFS_bfree)
            / fi (stat.statVFS_bfree + stat.statVFS_bavail)

diskStat :: (S.MonadSensor m) => S.Sensor m () Float
diskStat = S.sensor DiskStat

disk :: (S.MonadSensor m) => S.Sensor m () (P.Diagram [Float])
disk = diagram 1 diskStat

data WeatherForecast = WeatherForecast LocationData deriving (Show)

data Forecast = Forecast
  { properties :: ForecastProperties
  }
  deriving (Generic)

instance FromJSON Forecast

data ForecastProperties = ForecastProperties
  { timeseries :: [ForecastTimeserie]
  }
  deriving (Generic)

instance FromJSON ForecastProperties

data ForecastTimeserie = ForecastTimeserie
  { data_ :: ForecastData
  }
  deriving (Generic)

data ForecastData = ForecastData
  { next_6_hours :: Maybe Next6Hours,
    instant :: Instant
  }
  deriving (Generic)

instance FromJSON ForecastData

data Next6Hours = Next6Hours
  { summary :: Summary,
    details :: Details
  }
  deriving (Generic)

instance FromJSON Next6Hours

data Summary = Summary
  { symbol_code :: String
  }
  deriving (Generic)

instance FromJSON Summary

data Details = Details
  { air_temperature_max :: Float,
    air_temperature_min :: Float,
    precipitation_amount :: Float
  }
  deriving (Generic)

instance FromJSON Details

data Instant = Instant
  { details :: InstantDetails
  }
  deriving (Generic)

instance FromJSON Instant

data InstantDetails = InstantDetails
  { air_temperature :: Float
  }
  deriving (Generic)

instance FromJSON InstantDetails

instance FromJSON ForecastTimeserie where
  parseJSON value =
    fmap ForecastTimeserie (withObject "ForecastTimeserie" (\v -> v .: fromString "data") value)

instance (S.MonadSensor m) => S.Aggregate m WeatherForecast ForecastData where
  aggregate (WeatherForecast location) = forever do
    forecast <- liftIO (getForecast location)
    S.yield (head forecast.properties.timeseries).data_
    threadDelay ((5 * 60) * 10 ^ 6)

getForecast :: LocationData -> IO Forecast
getForecast LocationData {..} = do
  fmap getResponseBody . httpJSON . addUserAgent . fromString $
    printf "https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=%f&lon=%f" latitude longitude
  where
    addUserAgent req = addRequestHeader (fromString "user-agent") (fromString "astatusbar") req

weatherForecast :: (S.MonadSensor m) => S.Sensor m () P.Doc
weatherForecast = do
  forecastData <- S.sensor . WeatherForecast =<< location
  let airTemperature = P.pretty (show forecastData.instant.details.air_temperature)
  pure
    ( case forecastData.next_6_hours of
        Nothing ->
          airTemperature
        Just (Next6Hours {summary, details}) ->
          mconcat $ intersperse (P.pretty " ") [mconcat [symbol, P.pretty " "], tempMax, tempMin, airTemperature]
          where
            symbol =
              P.pretty
                ( case summary.symbol_code of
                    -- sunny
                    "clearsky_day" -> nfMdWeatherSunny
                    "clearsky_night" -> nfMdWeatherSunny
                    "clearsky_polartwilight" -> nfMdWeatherSunny
                    -- cloudy
                    "cloudy" -> nfMdWeatherCloudy
                    -- partly cloudy
                    "fair_day" -> nfMdWeatherPartlyCloudy
                    "fair_night" -> nfMdWeatherPartlyCloudy
                    "fair_polartwilight" -> nfMdWeatherPartlyCloudy
                    "partlycloudy_day" -> nfMdWeatherPartlyCloudy
                    "partlycloudy_night" -> nfMdWeatherPartlyCloudy
                    "partlycloudy_polartwilight" -> nfMdWeatherPartlyCloudy
                    -- fog
                    "fog" -> nfMdWeatherFog
                    -- rain
                    "rain" -> nfMdWeatherRainy
                    "lightrain" -> nfMdWeatherRainy
                    "heavyrain" -> nfMdWeatherPouring
                    -- snow
                    "snow" -> nfMdWeatherSnowy
                    "lightsnow" -> nfMdWeatherSnowy
                    "heavysnow" -> nfMdWeatherSnowyHeavy
                    -- sleet
                    "sleet" -> nfMdWeatherHail
                    "lightsleet" -> nfMdWeatherSnowyRainy
                    "heavysleet" -> nfMdWeatherHail
                    -- thunder
                    -- rain + thunder
                    "rainandthunder" -> nfMdWeatherLightning
                    "lightrainandthunder" -> nfMdWeatherLightning
                    "heavyrainandthunder" -> nfMdWeatherLightning
                    -- sleet + thunder
                    "sleetandthunder" -> nfMdWeatherLightning
                    "lightsleetandthunder" -> nfMdWeatherLightning
                    "heavysleetandthunder" -> nfMdWeatherLightning
                    -- snow + thunder
                    "snowandthunder" -> nfMdWeatherLightning
                    "lightsnowandthunder" -> nfMdWeatherLightning
                    "heavysnowandthunder" -> nfMdWeatherLightning
                    -- SHOWERS, map to "partly"
                    -- rain
                    "rainshowers_day" -> nfMdWeatherPartlyRainy
                    "rainshowers_night" -> nfMdWeatherPartlyRainy
                    "rainshowers_polartwilight" -> nfMdWeatherPartlyRainy
                    "lightrainshowers_day" -> nfMdWeatherPartlyRainy
                    "lightrainshowers_night" -> nfMdWeatherPartlyRainy
                    "lightrainshowers_polartwilight" -> nfMdWeatherPartlyRainy
                    "heavyrainshowers_day" -> nfMdWeatherPartlyRainy
                    "heavyrainshowers_night" -> nfMdWeatherPartlyRainy
                    "heavyrainshowers_polartwilight" -> nfMdWeatherPartlyRainy
                    -- snow
                    "snowshowers_day" -> nfMdWeatherPartlySnowy
                    "snowshowers_night" -> nfMdWeatherPartlySnowy
                    "snowshowers_polartwilight" -> nfMdWeatherPartlySnowy
                    "lightsnowshowers_day" -> nfMdWeatherPartlySnowy
                    "lightsnowshowers_night" -> nfMdWeatherPartlySnowy
                    "lightsnowshowers_polartwilight" -> nfMdWeatherPartlySnowy
                    "heavysnowshowers_day" -> nfMdWeatherPartlySnowy
                    "heavysnowshowers_night" -> nfMdWeatherPartlySnowy
                    "heavysnowshowers_polartwilight" -> nfMdWeatherPartlySnowy
                    -- sleet
                    "sleetshowers_day" -> nfMdWeatherPartlySnowyRainy
                    "sleetshowers_night" -> nfMdWeatherPartlySnowyRainy
                    "sleetshowers_polartwilight" -> nfMdWeatherPartlySnowyRainy
                    "heavysleetshowers_day" -> nfMdWeatherPartlySnowyRainy
                    "heavysleetshowers_night" -> nfMdWeatherPartlySnowyRainy
                    "heavysleetshowers_polartwilight" -> nfMdWeatherPartlySnowyRainy
                    "lightsleetshowers_day" -> nfMdWeatherPartlySnowyRainy
                    "lightsleetshowers_night" -> nfMdWeatherPartlySnowyRainy
                    "lightsleetshowers_polartwilight" -> nfMdWeatherPartlySnowyRainy
                    -- thunder
                    -- rain + thunder
                    "rainshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "rainshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "rainshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    "lightrainshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "lightrainshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "lightrainshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    "heavyrainshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "heavyrainshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "heavyrainshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    -- snow + thunder
                    "snowshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "snowshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "snowshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    "lightssnowshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "lightssnowshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "lightssnowshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    "heavysnowshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "heavysnowshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "heavysnowshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    -- sleet + thunder
                    "sleetshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "sleetshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "sleetshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    "lightssleetshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "lightssleetshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "lightssleetshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    "heavysleetshowersandthunder_day" -> nfMdWeatherPartlyLightning
                    "heavysleetshowersandthunder_night" -> nfMdWeatherPartlyLightning
                    "heavysleetshowersandthunder_polartwilight" -> nfMdWeatherPartlyLightning
                    _ -> summary.symbol_code
                )
            tempMax = P.color P.Red (P.pretty (show details.air_temperature_max))
            tempMin = P.colorDull P.Green (P.pretty (show details.air_temperature_min))

            nfMdWeatherCloudy = "\xf0590"
            nfMdWeatherFog = "\xf0591"
            nfMdWeatherHail = "\xf0592"
            nfMdWeatherLightning = "\xf0593"
            nfMdWeatherPartlyCloudy = "\xf0595"
            nfMdWeatherPartlyLightning = "\xf0f32"
            nfMdWeatherPartlyRainy = "\xf0f33"
            nfMdWeatherPartlySnowyRainy = "\xf0f35"
            nfMdWeatherPartlySnowy = "\xf0f34"
            nfMdWeatherPouring = "\xf0596"
            nfMdWeatherRainy = "\xf0597"
            nfMdWeatherSnowyHeavy = "\xf0f36"
            nfMdWeatherSnowyRainy = "\xf067f"
            nfMdWeatherSnowy = "\xf0598"
            nfMdWeatherSunny = "\xf0599"
    )

data Location = Location deriving (Show)

location :: (S.MonadSensor m) => S.Sensor m () LocationData
location = S.sensor Location

data LocationData = LocationData
  { latitude :: Float,
    longitude :: Float
  }
  deriving (Generic, Show)

instance FromJSON LocationData

instance (S.MonadSensor m) => S.Aggregate m Location LocationData where
  aggregate _ = forever do
    S.yield . getResponseBody =<< liftIO (httpJSON (fromString "https://reallyfreegeoip.org/json/?fields=lat,lon"))
    threadDelay (5 * 60 * 10 ^ 6)

data CurrentTime = CurrentTime deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m CurrentTime UTCTime where
  aggregate _ = forever do
    x <- liftIO getCurrentTime
    S.yield =<< liftIO getCurrentTime
    threadDelay
      (((10 ^ 12) - (fromEnum (utctDayTime x) `mod` (10 ^ 12))) `div` (10 ^ 6))

data CurrentTimeZone = CurrentTimeZone deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m CurrentTimeZone TimeZone where
  aggregate _ = forever do
    S.yield =<< liftIO getCurrentTimeZone
    sleep

currentTimeZone :: (S.MonadSensor m) => S.Sensor m () TimeZone
currentTimeZone = S.sensor CurrentTimeZone

currentTime :: (S.MonadSensor m) => S.Sensor m () UTCTime
currentTime = S.sensor CurrentTime

date :: (S.MonadSensor m) => S.Sensor m () String
date =
  ((formatTime defaultTimeLocale "%b %e" .) . utcToLocalTime)
    <$> currentTimeZone
    <*> currentTime

time :: (S.MonadSensor m) => S.Sensor m () String
time =
  ((formatTime defaultTimeLocale "%R" .) . utcToLocalTime)
    <$> currentTimeZone
    <*> currentTime

wmName :: (S.MonadSensor m) => S.Sensor m () String
wmName = activeWindow >>= maybe (pure "") wmNameOf

data ActiveWindow = ActiveWindow deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m ActiveWindow (Maybe X.Window) where
  aggregate _ = do
    bracket
      ( liftIO do
          dpy <- X.openDisplay ""
          let root = X.rootWindowOfScreen (X.defaultScreenOfDisplay dpy)
          X.selectInput dpy root X.propertyChangeMask
          X.sync dpy False
          pure (dpy, root)
      )
      (\(dpy, _) -> liftIO (X.closeDisplay dpy))
      ( \(dpy, root) -> do
          netActiveWindow <- liftIO (X.internAtom dpy "_NET_ACTIVE_WINDOW" False)
          let waitForEvent = do
                liftIO (X.pending dpy) >>= \case
                  0 -> do
                    liftIO do threadWaitRead (Fd (X.connectionNumber dpy))
                    waitForEvent
                  _ -> do
                    e <-
                      liftIO
                        ( X.allocaXEvent $ \ev -> do
                            X.nextEvent dpy ev
                            X.getEvent ev
                        )
                    when (X.ev_atom e /= netActiveWindow) waitForEvent

              readActiveWindow = do
                liftIO (X.getWindowProperty32 dpy netActiveWindow root) >>= \case
                  Just (0 : _) -> S.yield Nothing
                  Just (win : _) -> S.yield (Just (fi win))
                  _ -> S.yield Nothing
          forever do
            readActiveWindow
            waitForEvent
      )

activeWindow :: (S.MonadSensor m) => S.Sensor m () (Maybe X.Window)
activeWindow = S.sensor ActiveWindow

data WmNameOf = WmNameOf X.Window deriving (Show)

instance (S.MonadSensor m) => S.Aggregate m WmNameOf String where
  aggregate (WmNameOf win) = do
    bracket
      ( liftIO do
          dpy <- X.openDisplay ""
          X.selectInput dpy win X.propertyChangeMask
          X.sync dpy False
          pure dpy
      )
      (liftIO . X.closeDisplay)
      ( \dpy -> do
          netWmName <- liftIO (X.internAtom dpy "_NET_WM_NAME" False)
          let waitForEvent = do
                liftIO (X.pending dpy) >>= \case
                  0 -> do
                    liftIO do threadWaitRead (Fd (X.connectionNumber dpy))
                    waitForEvent
                  _ -> do
                    e <-
                      liftIO
                        ( X.allocaXEvent $ \ev -> do
                            X.nextEvent dpy ev
                            X.getEvent ev
                        )
                    when (X.ev_atom e /= netWmName) waitForEvent

              readWmName = do
                S.yield
                  =<< fmap (strip . head) . liftIO . X.wcTextPropertyToTextList dpy
                  =<< liftIO (X.getTextProperty dpy win netWmName)
          forever do
            readWmName
            waitForEvent
      )

wmNameOf :: (S.MonadSensor m) => X.Window -> S.Sensor m () String
wmNameOf = S.sensor . WmNameOf

data WmWorkspaces = WmWorkspaces deriving (Show)

data Workspace
  = Active String
  | Inactive String
  deriving (Eq, Typeable, Show)

instance (S.MonadSensor m) => S.Aggregate m WmWorkspaces [Workspace] where
  aggregate WmWorkspaces = do
    bracket
      ( liftIO do
          dpy <- X.openDisplay ""
          let root = X.rootWindowOfScreen (X.defaultScreenOfDisplay dpy)
          X.selectInput dpy root X.propertyChangeMask
          X.sync dpy False
          pure (dpy, root)
      )
      (\(dpy, _) -> liftIO (X.closeDisplay dpy))
      ( \(dpy, root) -> do
          netClientList <- liftIO $ X.internAtom dpy "_NET_CLIENT_LIST" False
          netCurrentDesktop <- liftIO $ X.internAtom dpy "_NET_CURRENT_DESKTOP" False
          netDesktopNames <- liftIO $ X.internAtom dpy "_NET_DESKTOP_NAMES" False
          netWmDesktop <- liftIO $ X.internAtom dpy "_NET_WM_DESKTOP" False
          let waitForEvent = do
                liftIO (X.pending dpy) >>= \case
                  0 -> do
                    liftIO (threadWaitRead (Fd (X.connectionNumber dpy)))
                    waitForEvent
                  _ -> do
                    e <-
                      liftIO
                        ( X.allocaXEvent $ \ev -> do
                            X.nextEvent dpy ev
                            X.getEvent ev
                        )
                    when
                      ( ((not .) . elem)
                          (X.ev_atom e)
                          [ netClientList,
                            netCurrentDesktop,
                            netDesktopNames,
                            netWmDesktop
                          ]
                      )
                      waitForEvent

              readWmWorkspaces = do
                S.yield
                  =<< liftIO do
                    currentDesktop <-
                      fmap (fi . head)
                        <$> X.getWindowProperty32 dpy netCurrentDesktop root
                    occupiedDesktops <-
                      fmap (S.unions . catMaybes)
                        . mapM
                          ( \win -> do
                              fmap (S.singleton . fi . head)
                                <$> X.getWindowProperty32 dpy netWmDesktop win
                          )
                        . map fi
                        . fromMaybe []
                        =<< X.getWindowProperty32 dpy netClientList root
                    fmap
                      ( ifilter
                          ( \i _ ->
                              Just i == currentDesktop
                                || i `S.member` occupiedDesktops
                          )
                          . imap
                            ( \i ->
                                if Just i == currentDesktop
                                  then Active
                                  else Inactive
                            )
                      )
                      . X.wcTextPropertyToTextList dpy
                      =<< X.getTextProperty dpy root netDesktopNames
          forever do
            readWmWorkspaces
            waitForEvent
      )

wmWorkspaces :: (S.MonadSensor m) => S.Sensor m () [Workspace]
wmWorkspaces = S.sensor WmWorkspaces

instance P.Pretty [Workspace] where
  pretty =
    P.Col . intersperse (P.pretty " ") . map P.pretty

instance P.Pretty Workspace where
  pretty (Active s) = P.color P.White (P.pretty s)
  pretty (Inactive s) = P.color P.Cyan (P.pretty s)

(<$$>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
f <$$> x = fmap f <$> x

infixl 4 <$$>

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral

strip :: String -> String
strip =
  reverse . dropWhile isSpace . reverse . dropWhile isSpace