-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcore.cljs
More file actions
163 lines (145 loc) · 5.7 KB
/
Copy pathcore.cljs
File metadata and controls
163 lines (145 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(ns automation.core
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [reagent.core :as reagent :refer [atom cursor]]
[cljs-http.client :as http]
[cljs.core.async :refer [put! chan <! >! timeout close!]]))
(defn log [message]
(.log js/console message))
(defn error [message]
(.error js/console message))
(defn set-item [key value]
(.setItem js/localStorage key value))
(defn get-item [key]
(.getItem js/localStorage key))
(defonce app-state (atom {:password "placeholder"
:password-active false
:temperature 20
:mode "HEAT"
:fan-speed "AUTO"
:power-status "OFF"}))
;; Load the password from local storage
(swap! app-state assoc :password (get-item "password"))
(defn clj->json [msg]
(.stringify js/JSON (clj->js msg)))
(defn post-path [path data]
(http/post path {:with-credentials? false
:form-params data}))
(defn send-sqs-message [msg]
(go (let [response (<! (post-path "https://sqs.ap-northeast-1.amazonaws.com/183315676158/ir_commands"
{:Action "SendMessage"
:MessageBody (clj->json msg)}))]
(let [body (:body response)]
(log body)))))
(defn send-aircon-state []
(send-sqs-message {:device "air_conditioner"
:password (:password @app-state)
:data {:mode (:mode @app-state)
:temperature (:temperature @app-state)
:fan_speed (:fan-speed @app-state)
:power_status (:power-status @app-state)}}))
(defn toggle-aircon-power []
(do
(if (= (:power-status @app-state) "OFF")
(swap! app-state assoc :power-status "ON")
(swap! app-state assoc :power-status "OFF"))
(send-aircon-state)))
(defn password-input [value-atom]
[:input {:type "password"
:value @value-atom
:on-change #(do
(reset! value-atom (-> % .-target .-value))
(set-item "password" @value-atom))}])
(defn ceiling-light-control []
[:div {:id "clicker"}
[:img {:id "bulb"
:on-click #(do
(send-sqs-message {:device "ceiling_light"
:password (:password @app-state)
:data {}})
(.preventDefault %))
:src "images/light.png"}]])
(defn mode-circle [color value]
[:svg {:width 88
:height 75
:style {:cursor "pointer"}} ;; Necessary for mobile safari to register touch events
[:circle {:on-click #(do
(swap! app-state assoc :mode value)
(.preventDefault %))
:cx "50%"
:cy "50%"
:r 35
:fill "white"
:stroke-width 2
:stroke color}]
(if (= value (:mode @app-state))
[:circle {:cx "50%"
:cy "50%"
:r 22
:fill color}])])
(defn aircon-control []
(let [password-cursor (cursor app-state [:password])]
(fn []
[:div {:id "aircon"}
[:div {:id "temp-control"}
[:p.temperature (:temperature @app-state)]
[:div {:id "up-down"}
[:img.arrowUp {:src "images/chevron-up.svg"
:width 95
:on-click #(do
(swap! app-state update-in [:temperature] inc)
(send-aircon-state)
(.preventDefault %))}]
[:img.arrowDown {:src "images/chevron-down.svg"
:width 95
:on-click #(do
(swap! app-state update-in [:temperature] dec)
(send-aircon-state)
(.preventDefault %))}]]]
[:div {:id "mode"}
[mode-circle "#FF7D83" "HEAT"]
[mode-circle "#78D3FF" "COOL"]
[mode-circle "#F1F1BD" "DEHUMIDIFY"]]
[:div {:id "fan-speed"}
(if (:password-active @app-state)
[:div {:id "password-input"}
[password-input password-cursor]
[:img {:id "unlock"
:src "images/lock-open.svg"
:on-click #(do
(swap! app-state assoc :password-active false)
(.preventDefault %))
:width 30
:height 30}]]
[:div {:id "fan-speed"}
[:img {:id "power-icon"
:data-powered (:power-status @app-state)
:src "images/power.svg"
:on-click #(do
(toggle-aircon-power)
(.preventDefault %))
:width 75
:height 75}]
[:img {:src "images/fan.svg"
:on-click #(do
(send-aircon-state)
(.preventDefault %))
:width 75
:height 75}]
[:img {:id "lock"
:src "images/lock.svg"
:on-click #(do
(swap! app-state assoc :password-active true)
(.preventDefault %))
:width 30
:height 30}]])]])))
(defn page []
[:div
[ceiling-light-control]
[aircon-control]])
(reagent/render-component [page]
(. js/document (getElementById "app")))
(defn on-js-reload []
;; optionally touch your app-state to force rerendering depending on
;; your application
;; (swap! app-state update-in [:__figwheel_counter] inc)
)