From 8f08fd044e505d63837fc747c31b2dbb1ab4688d Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Sat, 27 Jul 2024 21:05:18 +0200 Subject: WTF was that. --- manifest.scm | 10 +++++----- src/smart-relay.rkt | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/smart-relay.rtk | 48 ------------------------------------------------ src/smart-relay.sh | 2 +- 4 files changed, 54 insertions(+), 54 deletions(-) create mode 100644 src/smart-relay.rkt delete mode 100644 src/smart-relay.rtk diff --git a/manifest.scm b/manifest.scm index 91100f3..53544d4 100644 --- a/manifest.scm +++ b/manifest.scm @@ -66,8 +66,8 @@ (lambda* (#:key outputs #:allow-other-keys) (let ((out (assoc-ref outputs "out"))) (substitute* "smart-relay.sh" - (("smart-relay.rtk") - (string-append out "/share/smart-relay.rtk")))))) + (("smart-relay.rkt") + (string-append out "/share/smart-relay.rkt")))))) (add-before 'install 'move-files (lambda* (#:key inputs outputs #:allow-other-keys) (let* ((out (assoc-ref outputs "out")) @@ -78,10 +78,10 @@ (chmod "smart-relay.sh" #o555) (copy-recursively "smart-relay.sh" (string-append bin "smart-relay")) - (copy-recursively "smart-relay.rtk" - (string-append share "smart-relay.rtk")) + (copy-recursively "smart-relay.rkt" + (string-append share "smart-relay.rkt")) (delete-file-recursively "smart-relay.sh") - (delete-file-recursively "smart-relay.rtk"))))))) + (delete-file-recursively "smart-relay.rkt"))))))) (synopsis "Lisp program to control usb relay over mqtt") (description "Lisp program to control usb relay over mqtt") (home-page "https://git.jdlugosz.com/hsp/smart-relay") diff --git a/src/smart-relay.rkt b/src/smart-relay.rkt new file mode 100644 index 0000000..270780a --- /dev/null +++ b/src/smart-relay.rkt @@ -0,0 +1,48 @@ +(module smart-relay racket/base + (require mqtt-client) + (provide main) + + (define (make-send-to-dev data) + (lambda (dev) + (with-output-to-file dev + #:exists 'truncate + #:mode 'binary + (lambda () + (write-bytes data))))) + + (define send-message-on + (make-send-to-dev (bytes #xA0 #x01 #x01 #xA2))) + (define send-message-off + (make-send-to-dev (bytes #xA0 #x01 #x00 #xA1))) + + (define (make-toggle-relay dev) + (let ((turn-on? #t)) + (lambda () + (displayln (format "Relay on ~a toggled" dev)) + (if turn-on? + (send-message-on dev) + (send-message-off dev)) + (set! turn-on? (not turn-on?))))) + + (define (main #:host [host "localhost"] + #:username [username #f] + #:password [password #f] + #:client-name [client-name "smart-relay"] + #:topic-name [topic-name "hsp/bobma/smart-relay"] + #:relay-dev [relay-dev "/dev/ttyUSB0"]) + (define toggle-relay (make-toggle-relay relay-dev)) + (mqtt/with-client (host client-name) + (mqtt/with-connection (#:keep-alive-interval 20 + #:clean-session #t + #:username username + #:password password) + + (mqtt/subscribe topic-name) + (let loop () + (mqtt/with-message-recv (topic payload) + (displayln (format "Message \"~a\" recieved on topic \"~a\"" payload topic)) + (cond + ((string=? (bytes->string/utf-8 payload) + "toggle") + (toggle-relay)))) + (loop)))))) diff --git a/src/smart-relay.rtk b/src/smart-relay.rtk deleted file mode 100644 index 270780a..0000000 --- a/src/smart-relay.rtk +++ /dev/null @@ -1,48 +0,0 @@ -(module smart-relay racket/base - (require mqtt-client) - (provide main) - - (define (make-send-to-dev data) - (lambda (dev) - (with-output-to-file dev - #:exists 'truncate - #:mode 'binary - (lambda () - (write-bytes data))))) - - (define send-message-on - (make-send-to-dev (bytes #xA0 #x01 #x01 #xA2))) - (define send-message-off - (make-send-to-dev (bytes #xA0 #x01 #x00 #xA1))) - - (define (make-toggle-relay dev) - (let ((turn-on? #t)) - (lambda () - (displayln (format "Relay on ~a toggled" dev)) - (if turn-on? - (send-message-on dev) - (send-message-off dev)) - (set! turn-on? (not turn-on?))))) - - (define (main #:host [host "localhost"] - #:username [username #f] - #:password [password #f] - #:client-name [client-name "smart-relay"] - #:topic-name [topic-name "hsp/bobma/smart-relay"] - #:relay-dev [relay-dev "/dev/ttyUSB0"]) - (define toggle-relay (make-toggle-relay relay-dev)) - (mqtt/with-client (host client-name) - (mqtt/with-connection (#:keep-alive-interval 20 - #:clean-session #t - #:username username - #:password password) - - (mqtt/subscribe topic-name) - (let loop () - (mqtt/with-message-recv (topic payload) - (displayln (format "Message \"~a\" recieved on topic \"~a\"" payload topic)) - (cond - ((string=? (bytes->string/utf-8 payload) - "toggle") - (toggle-relay)))) - (loop)))))) diff --git a/src/smart-relay.sh b/src/smart-relay.sh index af5fa26..d14eaac 100755 --- a/src/smart-relay.sh +++ b/src/smart-relay.sh @@ -1,5 +1,5 @@ #!/bin/sh raco pkg install mqtt-client -racket -e "(begin (require (file \"smart-relay.rtk\")) (main $@))" +racket -e "(begin (require (file \"smart-relay.rkt\")) (main $@))" -- cgit v1.2.3