summaryrefslogtreecommitdiff
path: root/ebus-racket/inserter.rkt
diff options
context:
space:
mode:
authorEbus-at-dockstar <ebus@dockstar>2014-07-25 22:14:40 +0200
committerEbus-at-dockstar <ebus@dockstar>2014-07-25 22:14:40 +0200
commit3f5b5ff5e71cf078c2c04906bffa06f3a1050085 (patch)
tree9c2b892b5f79dc10489fd275e5cf4faf82b9af44 /ebus-racket/inserter.rkt
parentf6675ccdd7a5997def3c4656f0e2c5dbbbed1fc8 (diff)
downloadebus-alt-3f5b5ff5e71cf078c2c04906bffa06f3a1050085.tar.gz
ebus-alt-3f5b5ff5e71cf078c2c04906bffa06f3a1050085.zip
add tcp repl to inserter
Diffstat (limited to 'ebus-racket/inserter.rkt')
-rwxr-xr-xebus-racket/inserter.rkt22
1 files changed, 15 insertions, 7 deletions
diff --git a/ebus-racket/inserter.rkt b/ebus-racket/inserter.rkt
index a5a7ac8..cd05ad9 100755
--- a/ebus-racket/inserter.rkt
+++ b/ebus-racket/inserter.rkt
@@ -1,10 +1,11 @@
#! /usr/bin/env racket
#lang racket/base
(require racket/cmdline
- racket/stream
racket/tcp
+ racket/stream
net/url
"ebus/layer7.rkt"
+ "util/tcp-repl.rkt"
"util/json.rkt")
(define logger (make-logger 'ebus-inserter (current-logger)))
@@ -12,7 +13,7 @@
(define connect-host? (make-parameter null))
(define connect-port? (make-parameter null))
(define insert? (make-parameter #f))
-(define baseurl? (make-parameter "http://localhost:8080/api"))
+(define baseurl? (make-parameter "http://localhost:8000/sensor"))
;; Send field and value to database server
(define (insert-field sensor-name datatype offset value)
@@ -20,14 +21,19 @@
((member datatype (list "bit" "byte" "data1b" "word" "bcd")) "int")
((member datatype (list "byteEnum")) "string")))
(set! value (cond ((string=? type "float") (real->decimal-string value))
- ((string=? type "int") (number->string value))
(else value)))
- (put-pure-port
- (string->url (format "~a/value/~a" (baseurl?) sensor-name))
- (string->bytes/utf-8 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-message logger 'debug (format "Successful insert: type=~a value=~a"
+ type value) #t))
+ (else (log-message logger 'error (format "Error: type=~a value=~a ERROR:~a"
+ type value response) #t))))
(define (handle-packet packet)
-
(for ([field packet])
(log-message logger 'info (format "Field: ~a" field) #t)
(when (insert?)
@@ -48,6 +54,8 @@
[("-c" "--connect") host port "Connect to server <host> <port>"
(connect-host? host)
(connect-port? (string->number port))]
+ ["--tcp-repl" port "Open REPL on TCP <port>"
+ (tcp-repl-run (namespace-anchor->namespace repl-ns-anchor) (string->number port))]
["--insert" "Do Insert into Database"
(insert? #t)]
["--baseurl" url "Database server http url"