summaryrefslogtreecommitdiff
path: root/ebus-racket/db.rkt
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2012-03-02 00:24:47 +0100
committerYves Fischer <yvesf-git@xapek.org>2012-03-02 00:24:47 +0100
commitd8baa50b97a98e57d817ee3935c0d203f4acdff1 (patch)
tree0c7f556cb3ad43ecf71384d1d3e8c99c5dc3076d /ebus-racket/db.rkt
parent2a72180fc6f381ce252b0083cb1ae000be7f12ea (diff)
downloadebus-alt-d8baa50b97a98e57d817ee3935c0d203f4acdff1.tar.gz
ebus-alt-d8baa50b97a98e57d817ee3935c0d203f4acdff1.zip
ebus-racket: rename db.plt->db.rkt
Diffstat (limited to 'ebus-racket/db.rkt')
-rw-r--r--ebus-racket/db.rkt37
1 files changed, 37 insertions, 0 deletions
diff --git a/ebus-racket/db.rkt b/ebus-racket/db.rkt
new file mode 100644
index 0000000..14817e5
--- /dev/null
+++ b/ebus-racket/db.rkt
@@ -0,0 +1,37 @@
+#lang racket/base
+(require (prefix-in db: (planet ryanc/db:1:5)))
+
+(define pgc
+ (db:postgresql-connect #:user "ebus"
+ #:database "ebus"
+ #:password "ebus"
+ #:server "10.2.2.26"))
+
+;; Query ID of sensor given by sensor-name
+;; Returns null if sensor is undefined
+(define (get-sensor-id sensor-name)
+ (with-handlers ([exn:fail? (lambda (exn) (void))])
+ (db:query-value pgc "SELECT id FROM sensor WHERE name = $1" sensor-name)))
+
+;; Create Sensor-ID with given name
+;; returns id
+(define (create-sensor-id sensor-name)
+ (db:query-exec "INSERT INTO sensor(name) VALUES ($1)" sensor-name)
+ (get-sensor-id sensor-name))
+
+;; Get ID of sensor given by sensor-name
+;; define sensor if needed
+(define (sensor-id sensor-name)
+ (let ([id (get-sensor-id sensor-name)])
+ (cond ((void? id) (create-sensor-id sensor-id))
+ (else id))))
+
+(define (insert sensor-name value-float value-int value-string)
+ (let ([sensor-id (sensor-id sensor-name)]
+ [type (cond ((not (void? value-string)) "string")
+ ((not (void? value-float)) "float")
+ ((not (void? value-int)) "int"))])
+ (db:query-exec (string-append "INSERT INTO value(timestamp, sensor_id, type, value_float, value_int, value_string) "
+ "VALUES (now(), $1, $2, $3, $4, $5)")
+ sensor-id type value-float value-int value-string)))
+