Created
March 29, 2020 19:06
-
-
Save robinedwards/e9b51f04ca4efca541266a6a3bd81fea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require plot) | |
(require racket/gui) | |
(define temp-readings (make-hash)) | |
(define (list-thermal-zones) | |
(map | |
(lambda (tz) (car (regexp-match #px"\\d+$" tz))) | |
(filter (lambda (p) (string-prefix? p "thermal_zone")) | |
(map path->string (directory-list "/sys/class/thermal/"))))) | |
(define (fetch-readings) | |
(for/list ([zone (list-thermal-zones)]) | |
(let* ( | |
[reading (read-thermal-zone zone)] | |
[name (cdr reading)] | |
[temp (/ (car reading) 1000)] | |
[key (cons name zone)] | |
[ts-temp (list (current-seconds) temp)] | |
) | |
(if (hash-has-key? temp-readings key) | |
(hash-set! temp-readings key | |
(append (hash-ref temp-readings key) (list ts-temp))) | |
(hash-set! temp-readings key | |
(list ts-temp)))))) | |
(define (read-thermal-zone zone) | |
(let ([sys-file (format "/sys/class/thermal/thermal_zone~a/temp" zone)] | |
[type-file (format "/sys/class/thermal/thermal_zone~a/type" zone)]) | |
(if [file-exists? sys-file] | |
(cons (string->number (first (file->lines sys-file))) | |
(first (file->lines type-file))) | |
#f))) | |
(define (build-lines) | |
(hash-map temp-readings | |
(lambda (key reading-lst) | |
(lines | |
#:color (string->number (cdr key)) | |
#:width 2 | |
#:label (car key) reading-lst)))) | |
(define (plot-temperatures dc width height) | |
(parameterize ([plot-x-ticks (time-ticks)]) | |
(fetch-readings) | |
(plot/dc (build-lines) dc 0 0 width height | |
#:title "Temperature Sensors" | |
#:y-label "Degrees Celcius" | |
#:x-label "Time" | |
))) | |
(define frame (new frame% [label "System Temperature"])) | |
(define plot-canvas (new canvas% [parent frame] | |
[paint-callback (lambda (canvas dc) | |
(let-values ([(width height) (send canvas get-client-size)]) | |
(plot-temperatures dc width height)))])) | |
(new timer% [notify-callback (lambda () | |
(send plot-canvas refresh-now))] | |
[interval 10000]) | |
(send frame show #t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment