summaryrefslogtreecommitdiff
path: root/ebus-racket
diff options
context:
space:
mode:
authorEbus-at-dockstar <ebus@dockstar>2014-07-27 17:36:09 +0200
committerEbus-at-dockstar <ebus@dockstar>2014-07-27 17:36:09 +0200
commitf7f2f4a611f15d98d8e33241dc65d5cbfe7ff03b (patch)
treeb27e814cf49c6ddc92340066a9aef00f81dc3b49 /ebus-racket
parentb5f713a888d1fc90c3c3664a5197a4cfcb969750 (diff)
downloadebus-alt-f7f2f4a611f15d98d8e33241dc65d5cbfe7ff03b.tar.gz
ebus-alt-f7f2f4a611f15d98d8e33241dc65d5cbfe7ff03b.zip
improve logging, close input ports after usage
Diffstat (limited to 'ebus-racket')
-rw-r--r--ebus-racket/ebus/layer7.rkt13
-rwxr-xr-xebus-racket/inserter.rkt35
2 files changed, 25 insertions, 23 deletions
diff --git a/ebus-racket/ebus/layer7.rkt b/ebus-racket/ebus/layer7.rkt
index 76ea6ae..b3caf0b 100644
--- a/ebus-racket/ebus/layer7.rkt
+++ b/ebus-racket/ebus/layer7.rkt
@@ -9,9 +9,12 @@
(define-logger ebus7)
(define definition
- (parameterize ([collapse-whitespace #t]
- [xexpr-drop-empty-attributes #t])
- (xml->xexpr (document-element (read-xml (open-input-file "../ebus-xml/ebus.xml"))))))
+ (let* ([in (open-input-file "../ebus-xml/ebus.xml")]
+ [xexpr (parameterize ([collapse-whitespace #t]
+ [xexpr-drop-empty-attributes #t])
+ (xml->xexpr (document-element (read-xml in))))])
+ (close-input-port in)
+ xexpr))
(define (paket ebus-paket)
(define primaryCommand (number->string (layer2-ebus-paket-primaryCommand ebus-paket)))
@@ -70,13 +73,13 @@
(list 'data1c (lambda (name field offset payload)
(list name 'data1b (field-decoder-data1c (list-ref payload offset)))))
(list 'byte (lambda (name field offset payload)
- (list name 'byte offset (list-ref payload offset))))
+ (list name 'byte (list-ref payload offset))))
(list 'data2b (lambda (name field offset payload)
(list name 'data2b (field-decoder-data2b
(list-ref payload offset)
(list-ref payload (+ offset 1))))))
(list 'data2c (lambda (name field offset payload)
- (list name 'data2c offset
+ (list name 'data2c
(field-decoder-data2c (list-ref payload offset) (list-ref payload (+ offset 1))))))
(list 'word (lambda (name field offset payload)
(list name 'word (field-decoder-word (list-ref payload offset) (list-ref payload (+ offset 1))))))
diff --git a/ebus-racket/inserter.rkt b/ebus-racket/inserter.rkt
index ab97ecf..d72d2ea 100755
--- a/ebus-racket/inserter.rkt
+++ b/ebus-racket/inserter.rkt
@@ -3,6 +3,7 @@
(require racket/cmdline
racket/tcp
racket/stream
+ racket/string
net/url
"ebus/layer7.rkt"
"util/tcp-repl.rkt"
@@ -13,29 +14,27 @@
(define connect-host? (make-parameter null))
(define connect-port? (make-parameter null))
(define insert? (make-parameter #f))
-(define baseurl? (make-parameter "http://localhost:8000/sensor"))
+(define baseurl? (make-parameter "http://localhost:8080/api/value"))
+
;; Send field and value to database server
-(define (insert-field sensor-name datatype offset value)
- (define type (cond ((member datatype (list "data1c" "data2b" "data2c")) "float")
- ((member datatype (list "bit" "byte" "data1b" "word" "bcd")) "int")
- ((member datatype (list "byteEnum")) "string")))
- (set! value (cond ((string=? type "float") (real->decimal-string value))
- (else value)))
- (define response
- (read-line (put-pure-port
- (string->url (format "~a/~a" (baseurl?) sensor-name))
- (string->bytes/utf-8 (format "value=~a&type=~a" value type)))))
- (define responseJson (string->jsexpr response))
- (cond ((eq? (json-null) (hash-ref responseJson 'error))
- (log-inserter-debug "Successful insert: type=~a value=~a"
- type value))
- (else (log-inserter-error "Error: type=~a value=~a ERROR:~a"
- type value response))))
+(define (insert-field sensor-name datatype value)
+ (define url (format "~a/~a" (baseurl?) sensor-name))
+ (define raw-value
+ (cond ((member datatype '(data1c data2b data2c)) (real->decimal-string value))
+ ((member datatype '(bit byte data1b word bcd)) (format "~s" value))
+ ((member datatype '(byteEnum)) value)
+ (else (error 'invalid-datatype))))
+ (define input-port (put-impure-port (string->url url) (string->bytes/utf-8 raw-value)))
+ (define server-response (read-line input-port))
+ (close-input-port input-port)
+ (cond ((string=? (list-ref (string-split server-response) 1) "200")
+ (log-inserter-info "OK: ~a : ~a" url raw-value))
+ (else
+ (error "server-error" url raw-value server-response))))
(define (handle-packet packet)
(for ([field packet])
- (log-inserter-info "Field: ~a" field)
(when (insert?)
(with-handlers ([exn:fail? (lambda (exn)
(log-inserter-error "Failed to insert ~a: ~a" field exn))]