-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhue.rkt
More file actions
97 lines (79 loc) · 2.78 KB
/
Copy pathhue.rkt
File metadata and controls
97 lines (79 loc) · 2.78 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
#lang racket/base
(require net/url)
(require net/http-client)
(require json)
(provide light-set-bri-hue! ; id bri hue username -> pure-port
light-set-bri-ct! ; id bri ct username -> pure-port
light-on? ; id username -> Bool
light-get ; id username -> jsexpr
MAX-BRI
RED GREEN BLUE)
;; Limits
(define MAX-HUE 65535)
(define MAX-BRI 254)
(define MAX-SAT 254)
;; Colours
;; Range of "colour temperature"
;; The hue colour can be set as a temperature in "mireds"
;; https://en.wikipedia.org/wiki/Mired
(define MIN-CT 153) ; 6500 K
(define MAX-CT 500) ; 2000 K
;; Range of "hue"
(define RED 0)
(define GREEN 25500)
(define BLUE 46920)
;; Net
(define BASE-URL
(make-url "http" ; scheme
#f ; user
"philips-hue.home" ; host
#f ; port
#t ; path-absolute?
null ; path
null ; query
#f)) ; fragment
;; Is the light on?
(define (light-on? id username)
(hash-ref
(hash-ref (light-get id username) 'state #f)
'on #f))
;; Get JSON of light status
(define (light-get id username)
(define in (get-pure-port (light-get-url id username)))
(read-json in))
(define (light-get-url id username)
(make-hue-url (list "api" username "lights" (number->string id))))
;; Set light with id to given brightness and colour, given username
;; brightness, and hue using Hue scale
(define (light-set-bri-hue! id brightness hue username)
(put-pure-port
(light-put-url id username)
(jsexpr->bytes
(hasheq 'bri brightness
'hue hue
'sat MAX-SAT))))
;; Set light to given brightness and colour temp
(define (light-set-bri-ct! id brightness ct username)
(put-pure-port
(light-put-url id username)
(jsexpr->bytes
(hasheq 'bri brightness
'ct ct))))
(define (light-put-url id username)
(make-hue-url (list "api" username "lights" (number->string id) "state")))
(define (light-put-body brightness hue)
(jsexpr->bytes
(hasheq 'bri brightness 'hue hue 'sat MAX-SAT)))
;; List-of String? - > URL
(define (make-hue-url paths)
(make-url "http" ; scheme
#f ; user
"philips-hue.home" ; host
#f ; port
#t ; path-absolute?
;; path
(map (λ (s) (make-path/param s null))
paths)
null ; query
#f ; fragment
))