From: Ralph Ronnquist Date: Sat, 1 Jan 2022 11:00:52 +0000 (+1100) Subject: add net option and minor code polishing X-Git-Tag: 0.1~38 X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=e952e4ea6aa01a7787597fe9fe363cae79e555e5;p=rrq%2Fhourglass.git add net option and minor code polishing --- diff --git a/control-logic.lsp b/control-logic.lsp index cd9a830..c842c06 100644 --- a/control-logic.lsp +++ b/control-logic.lsp @@ -16,36 +16,57 @@ (date-list (+ NOW (* 60 (now 0 -2))))) ;(println (list YEAR MONTH DATE HOUR MINUTE SECOND DOY DOW)) -# Load "control.dat" -# ( (control "file") (gap minutes) ( weekday start limit stop ) ... ) -(setf CONTROL (read-expr (read-file "control.dat"))) -(map set '(dow MODE START LIMIT END) - (or (assoc DOW CONTROL) (list DOW (6 30) 120 (20 0)))) (setf DAY (list YEAR MONTH DATE) HM (list HOUR MINUTE) TOTAL '() + CONTROL.DAT "control.dat" + EXTRA.DAT "control-extra.dat" + USAGE.DAT "usage.dat" + USAGE.TMP ".usage.dat" + ) + +# Load CONTROL.DAT +# ( (control "file") (gap minutes) ( weekday start limit stop ) ... ) +(setf CONTROL (read-expr (or (read-file CONTROL.DAT) + (die "** Missing " CONTROL.DAT " ** Exiting.")) + )) +(map set '(dow MODE START LIMIT END) + (or (assoc DOW CONTROL) + (assoc 'policy CONTROL) + (list DOW (6 30) 120 (20 0))) + ) + +(setf GAP (or (lookup 'gap CONTROL) 15) CLIP (or (lookup 'clip CONTROL) 1000) + NET (or (lookup 'net CONTROL) "10.0.0.0/8") ) -# Load control mechanism +# Load the configured control mechanism (if (lookup 'control CONTROL) (load $it) (die "** Unknown control mechanism. Exiting!!")) (unless control - (die "** Unknown control action. Exiting!!")) + (die "** Control function (control cmd reason) not defined. Exiting!!")) +;; Apply control command with reason, then exit (define (do-control x r) (control x r) (exit 0)) -(when (file? "control-extra.dat") - (let ((f (file-info "control-extra.dat" 6)) - (x (regex "([0-9]+) ([0-9]+)" (read-file "control-extra.dat") 0))) - (when (and f x (<= NOW (+ f (* 3600 (int $1 0 10)) (* 60 (int $2 0 10))))) - (setf OVERRIDE true)))) +;; Utility: Combine an (hour minutes) pair into total minutes +(define (minutes x) (+ (* (x 0) 60) (x 1))) + +;; Utility: Combine hours and minutes into total seconds +(define (seconds H M) (+ (* 3600 H) (* 60 M))) +# Apply EXTRA.DAT. This is a pair of hours and minutes to force open, +# relative to the modification time of the file. +(setf OVERRIDE + (when (regex "([0-9]+) ([0-9]+)" (or (read-file EXTRA.DAT) "") 0) + (<= NOW (+ (file-info EXTRA.DAT 6) (seconds (int $1) (int $2)))))) + +;;==== Utilities for activity data # Activity is lines of timestamps. Collect TOTAL as list of unique # time values (H M) within the start-end time span. - (define (log-name-fmt t) (format "%d%02d%02d-.*\\.dat" (0 3 (date-list t)))) @@ -65,6 +86,7 @@ (on (if (regex "^[0-9]+ ([0-9]+)$" x 0) (> (int $1 0 10) CLIP) 1)) ) (and on (= (0 3 d) DAY) tm)))) ; (>= tm START) (< tm END) tm)))) +;; ====== # Collect all mentioned minutes from the activity logs (setf TOTAL @@ -75,7 +97,6 @@ # Add all mentioned minutes, and fill in any time periods of less than # the configured GAP minutes between them. -(define (minutes x) (+ (* (x 0) 60) (x 1))) (setf SUM 0) (when TOTAL (setf LAST (minutes (pop TOTAL) SUM 1)) @@ -88,15 +109,17 @@ # Rework SUM into (h m) format (setf SUM (letn ((h (/ SUM 60)) (m (- SUM (* 60 h)))) (list h m))) -(write-file ".usage.dat" (string SUM)) -(rename-file ".usage.dat" "usage.dat") +# Write out current usage with atomic update (jic) +(write-file USAGE.TMP (string SUM)) +(rename-file USAGE.TMP USAGE.DAT) -# Close host outside start-end times +# Apply current policy setting +; ** Note that do-control exits +(when OVERRIDE (do-control "open" "override")) (case MODE (closed (do-control "close" "closed")) (opened (do-control "open" "open")) - (timed - (when OVERRIDE (do-control "open" "override")) + (timed ;; Close host outside start-end times (when (< HM START) (do-control "close" "early")) (when (>= HM END) (do-control "close" "late")) (when (> SUM LIMIT) (do-control "close" "usage"))