summaryrefslogtreecommitdiff
path: root/ebus-racket
diff options
context:
space:
mode:
Diffstat (limited to 'ebus-racket')
-rw-r--r--ebus-racket/ebus/layer7.rkt81
-rwxr-xr-xebus-racket/inserter.rkt29
2 files changed, 67 insertions, 43 deletions
diff --git a/ebus-racket/ebus/layer7.rkt b/ebus-racket/ebus/layer7.rkt
index b3caf0b..b43a58a 100644
--- a/ebus-racket/ebus/layer7.rkt
+++ b/ebus-racket/ebus/layer7.rkt
@@ -8,6 +8,11 @@
(define-logger ebus7)
+(define (ersatzwert)
+ 'ersatzwert)
+(define (ersatzwert? v)
+ (eq? (ersatzwert) v))
+
(define definition
(let* ([in (open-input-file "../ebus-xml/ebus.xml")]
[xexpr (parameterize ([collapse-whitespace #t]
@@ -94,19 +99,25 @@
;; type data1b
(define (field-decoder-data1b value)
- value)
+ (if (= value #x80)
+ (ersatzwert)
+ value))
;; type data1c
(define (field-decoder-data1c value)
- (/ value 2.0))
+ (if (= value #xff)
+ (ersatzwert)
+ (/ value 2.0)))
;; type data2b
(define (field-decoder-data2b lowByte highByte)
- (if (= (bitwise-and highByte 128) 128)
- (* -1
- (+ (+ 256 (bitwise-not highByte))
- (/ (+ 256 (bitwise-not (+ lowByte 1))) 256.0)))
- (+ highByte (/ lowByte 256.0))))
+ (if (and (= highByte 128) (= lowByte 0))
+ (ersatzwert)
+ (if (= (bitwise-and highByte 128) 128)
+ (* -1
+ (+ (+ 256 (bitwise-not highByte))
+ (/ (+ 256 (bitwise-not (+ lowByte 1))) 256.0)))
+ (+ highByte (/ lowByte 256.0)))))
;; type data2c
;; Beispiel für die Berechnung:
@@ -118,22 +129,25 @@
;; + dez(Low_ Nibble (Low Byte (x))) / 16
(define (field-decoder-data2c lowByte highByte)
(define (lowNibble v)
- (bitwise-and v #x0f))
+ (bitwise-and v #x0f))
(define (highNibble v)
- (arithmetic-shift v -4))
+ (arithmetic-shift v -4))
(define (u-not v)
- (+ 256 (bitwise-not v)))
- (if (= (bitwise-and highByte 128) 128)
- (* -1
- (+ (arithmetic-shift (u-not highByte) 4)
- (highNibble (u-not lowByte))
- (/ (+ (lowNibble (u-not lowByte)) 1)
- 16.0)))
- (+ (arithmetic-shift highByte 4)
- (highNibble lowByte)
- (/
- (lowNibble lowByte)
- 16))))
+ (+ 256 (bitwise-not v)))
+
+ (if (and (= highByte 128) (= lowByte 0))
+ (ersatzwert)
+ (if (= (bitwise-and highByte 128) 128)
+ (* -1
+ (+ (arithmetic-shift (u-not highByte) 4)
+ (highNibble (u-not lowByte))
+ (/ (+ (lowNibble (u-not lowByte)) 1)
+ 16.0)))
+ (+ (arithmetic-shift highByte 4)
+ (highNibble lowByte)
+ (/
+ (lowNibble lowByte)
+ 16)))))
;; type byteEnum
(define (field-decoder-byteEnum value field-definition)
@@ -149,13 +163,18 @@
;; type word
(define (field-decoder-word lowByte highByte)
- (+ lowByte
- (arithmetic-shift highByte 8)))
+ (define value
+ (+ lowByte (arithmetic-shift highByte 8)))
+ (if (= value #xffff)
+ (ersatzwert)
+ value))
;; type bcd
(define (field-decoder-bcd value)
- (+ (bitwise-and value #x0f)
- (arithmetic-shift value -4)))
+ (if (= value #xff)
+ (ersatzwert)
+ (+ (bitwise-and value #x0f)
+ (arithmetic-shift value -4))))
;; read one ebus-paket or eof from input-port
;; or return #<eof>
@@ -165,10 +184,12 @@
(paket-parse paket))
(else paket)))
-(provide (prefix-out layer7- paket)
- (prefix-out layer7- paket-parse)
- (prefix-out layer7- device)
- (prefix-out layer7- device-name)
- (prefix-out layer7- definition)
+(provide (prefix-out layer7- ersatzwert)
+ (prefix-out layer7- ersatzwert?)
+ (prefix-out layer7- paket)
+ (prefix-out layer7- paket-parse)
+ (prefix-out layer7- device)
+ (prefix-out layer7- device-name)
+ (prefix-out layer7- definition)
;; read ebus from port an return fields from next paket
(prefix-out layer7- read-ebus))
diff --git a/ebus-racket/inserter.rkt b/ebus-racket/inserter.rkt
index d72d2ea..b234ad9 100755
--- a/ebus-racket/inserter.rkt
+++ b/ebus-racket/inserter.rkt
@@ -19,19 +19,22 @@
;; Send field and value to database server
(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))))
+ (if (layer7-ersatzwert? value)
+ (log-inserter-info "Skipping Ersatzwert for ~a/~a" sensor-name datatype)
+ (let ()
+ (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])