-
Notifications
You must be signed in to change notification settings - Fork 0
/
m8rten-se.groovy
111 lines (96 loc) · 4.29 KB
/
m8rten-se.groovy
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
@GrabResolver(name="netty snapshots", root="http://clinker.netty.io/nexus/content/repositories/snapshots")
@GrabResolver(name="OJO", root="https://oss.jfrog.org/artifactory/repo")
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
@Grab("io.ratpack:ratpack-groovy:0.9.13")
@Grab("io.ratpack:ratpack-jackson:0.9.13")
import static ratpack.groovy.Groovy.ratpack
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.time.TimeCategory
import ratpack.jackson.JacksonModule
import static ratpack.jackson.Jackson.jsonNode
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.JSON
/*
* Loads configuration
*/
def mongolabApiKey = System.getenv('MONGOLAB_API_KEY')
def vaxthusetAdminKey = System.getenv('VAXTHUSET_ADMIN_KEY')
/*
* Updates image every 60 s
*/
def imageUpdater = Thread.start {
while(true){
def url = new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/bilder?q={"_id":"foto"}&apiKey=$mongolabApiKey""")
new File('public/vaxthuset/img/foto.jpg').bytes = new JsonSlurper().parseText(url.text)[0].base64.decodeBase64()
sleep(60000)
}
}
/*
* Updates image every 60 s
*/
def gifUpdater = Thread.start {
while(true){
def url = new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/bilder?q={"_id":"gif"}&apiKey=$mongolabApiKey""")
new File('public/vaxthuset/img/animation.gif').bytes = new JsonSlurper().parseText(url.text)[0].base64.decodeBase64()
sleep(3600000)
}
}
/*
* Defines handlers
*/
ratpack {
bindings {
add new JacksonModule()
}
handlers {
/*
* Api calls
*/
get("vaxthuset/api/status-hourly") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&s={_id:-1}&q={"minute":0}&apiKey=$mongolabApiKey""").text
}
get("vaxthuset/api/status-historic") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&s={_id:-1}&q={\$and:[{date:{\$gt:"2015-05-11"}},{minute:0}]}&apiKey=54SF3Z7w9BVVSE7w8C0lNCuzNnMtoPPl""").text
}
get("vaxthuset/api/status-latest") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&s={_id:-1}}&l=1&apiKey=$mongolabApiKey""").text
}
get("vaxthuset/api/status-latest-5") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&s={_id:-1}}&l=360&apiKey=$mongolabApiKey""").text
}
get("vaxthuset/api/status-latest-1") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&s={_id:-1}}&l=60&apiKey=$mongolabApiKey""").text
}
get("vaxthuset/api/highest-temperature") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&l=1&s={temperature:-1}&q={date:{\$gt:"${oneDayAgo()}"}}&apiKey=$mongolabApiKey""").text
}
get("vaxthuset/api/lowest-temperature") {
response.send new URL("""https://api.mongolab.com/api/1/databases/vaxthuset/collections/status?f={_id:0}&l=1&s={temperature:1}&q={date:{\$gt:"${oneDayAgo()}"}}&apiKey=$mongolabApiKey""").text
}
post("vaxthuset/api/controller") {
def controller = parse jsonNode()
if (controller.key.toString() == "\"$vaxthusetAdminKey\"") {
def http = new HTTPBuilder("http://83.68.246.58:50508/api/controller")
http.post(body: [key: controller.key.toString(),
time: controller.time.toString().toInteger()],
requestContentType: JSON ) { resp ->
render resp.status
}
} else {
render "fail"
}
}
/*
* Static stuff
*/
assets "public"
}
}
def oneDayAgo() {
def currentDate = new Date()
use(TimeCategory) {
currentDate = currentDate - 1.day
}
currentDate.format("yyyy-MM-dd")
}