Skip to content

Commit

Permalink
revert doorlock daemon to clj
Browse files Browse the repository at this point in the history
  • Loading branch information
akiroz committed Aug 15, 2016
1 parent ea5aa08 commit 5a66789
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

node_modules/
dist/
target/

.DS_Store
._*
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,21 @@ Diagrams are saved as `txt` files, you can import them under `File > Import From
**Build:**
```
[~/]$ git clone ...
[~/doorlock/embedded]$ lein cljsbuild once
[~/doorlock/embedded]$ lein uberjar
```
The compiled JS is now in `doorlock/embedded/dist/index.js`.

**Note:**
It is highly recommended that you build the JS file on a modern x86 computer.
Compilation takes around 30 secs on an i7 laptop.
The compiled JAR is now in `doorlock/embedded/target/doorlock-<version>-standalone.jar`.

### Deploy
**Embedded OS:** ArchLinux ARM

**Dependencies:**

* nodejs
* java
* wiringpi-git (AUR)

**Install as systemd service:**

1. copy the compiled JS to `/home/oursky/doorlock.js`
1. copy the compiled JAR to `/home/oursky/doorlock.jar`
2. copy `doorlock.service` to `/etc/systemd/system/`
3. enable and start the service:
```
Expand Down
2 changes: 1 addition & 1 deletion embedded/doorlock.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description=Doorlock Controller Daemon

[Service]
User=oursky
ExecStart=/usr/bin/node /home/oursky/doorlock.js
ExecStart=/usr/bin/java -jar /home/oursky/doorlock.jar

[Install]
WantedBy=multi-user.target
15 changes: 4 additions & 11 deletions embedded/project.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
(defproject com.oursky/doorlock "1.0.0-SNAPSHOT"
(defproject com.oursky/doorlock "1.0.0"
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.198"]
[org.clojure/core.async "0.2.385"]
[com.taoensso/timbre "4.7.0"]
[http-kit "2.2.0"]
]
:plugins [[lein-cljsbuild "1.1.3"]]
:clean-targets ^{:protect false} ["dist"]
:cljsbuild {:builds [{:source-paths ["src"]
:compiler {:main com.oursky.doorlock.core
:output-dir "dist"
:output-to "dist/index.js"
;:source-map "dist/index.js.map"
:target :nodejs
:optimizations :simple}}]})
:aot :all
:main com.oursky.doorlock.core)
50 changes: 50 additions & 0 deletions embedded/src/com/oursky/doorlock/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns com.oursky.doorlock.core
(:require [taoensso.timbre :as log]
[clojure.java.shell :refer [sh]]
[clojure.core.async :refer [<! >! >!! alts! go-loop chan timeout]]
[org.httpkit.server :refer [run-server]]
)
(:gen-class))

; unlock triggering channel
; identify the trigger source by sending {:source <source>}
(def unlock-chan (chan))

(defn -main [& args]
; setup GPIO via wiringpi CLI interface
; the clj-gpio library does not support internal pull-up
(sh "gpio" "mode" "0" "up")
(sh "gpio" "mode" "1" "out")

; button event listener
; hold down for atleast 200ms to trigger
; will emit event every 25000ms if held down
(go-loop []
(if (= 1 (read-string (:out (sh "gpio" "read" "0"))))
(sh "gpio" "wfi" "0" "falling")
(<! (timeout 2500)))
(<! (timeout 200))
(when (= 0 (read-string (:out (sh "gpio" "read" "0"))))
(>! unlock-chan {:source :button}))
(recur))

; listen on unlock-chan for unlock events
; if a new unlock event is revieved before the 3000ms timeout, the door is kept open.
(go-loop [unlock nil]
(when unlock
(sh "gpio" "write" "1" "1")
(loop [[trigger _] [unlock nil]]
(when trigger
(log/info (str "Unlock triggered by " (:source trigger)))
(recur (alts! [unlock-chan (timeout 3000)]))))
(sh "gpio" "write" "1" "0")
(log/info "Door Locked"))
(recur (<! unlock-chan)))

(run-server (fn [req]
(>!! unlock-chan {:source (or (get-in req [:headers "x-source"])
:network)})
{:status 200})
{:ip "0.0.0.0" :port 8090})

(log/info "=== Daemon Started ==="))
57 changes: 0 additions & 57 deletions embedded/src/com/oursky/doorlock/core.cljs

This file was deleted.

0 comments on commit 5a66789

Please sign in to comment.