2 * Copyright 2015 SmartThings
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11 * for the specific language governing permissions and limitations under the License.
14 import com.sun.corba.se.spi.activation._ServerImplBase
19 * Author: Steve Vlaminck
25 name: "SmartBlock Manager",
26 namespace: "vlaminck/Minecraft",
27 author: "SmartThings",
28 description: "A SmartApp for managing SmartBlocks",
29 iconUrl: "http://f.cl.ly/items/0p2c222z0p2K0y3y3w2M/SmartApp-icon.png",
30 iconX2Url: "http://f.cl.ly/items/0p2c222z0p2K0y3y3w2M/SmartApp-icon.png",
31 oauth: [displayName: "SmartBlock Manager", displayLink: ""]
36 page(name: "listPage")
37 page(name: "serverPage")
38 page(name: "blockPage")
39 page(name: "linkerPage")
40 page(name: "notifierPage")
41 page(name: "chatPage")
43 section("SmartBlock Manager") {
44 input name: "explanation1", title: "Every time you place a SmartBlock in Minecraft, a new SmartThings Device will be created. These Devices can be used in various SmartApps like \"Minecraft Notifier\", or \"Switch State Matcher\"", description: "", type: "paragraph", element: "paragraph", required: false
45 input name: "explanation2", title: "In order for SmartThings to send commands back to your Minecraft SmartBlocks, you will have to enter your Server Address via the SmartThings iOS or Android app", description: "", type: "paragraph", element: "paragraph", required: false
53 def linkerApps = findAllChildAppsByName("SmartBlock Linker")
54 def linkerState = linkerApps ? "complete" : ""
55 def linkerDescription = linkerApps.collect { it.label ?: it.name }.sort().join("\n")
57 def notifierApps = findAllChildAppsByName("SmartBlock Notifier")
58 def notifierState = notifierApps ? "complete" : ""
59 def notifierDescription = notifierApps.collect { it.label ?: it.name }.sort().join("\n")
61 def chatApps = findAllChildAppsByName("SmartBlock Chat Sender")
62 def chatState = chatApps ? "complete" : ""
63 def chatDescription = chatApps.collect { it.label ?: it.name }.sort().join("\n")
65 return dynamicPage(name: "listPage", title: "Configure Your SmartBlocks", install: true, uninstall: true) {
69 title: "Link SmartBlocks To Switches",
70 description: linkerDescription,
75 name: "toNotifierPage",
76 title: "Get Notified When a SmartBlock updates",
77 description: notifierDescription,
83 title: "Send Notifications into Minecraft",
84 description: chatDescription,
93 title: "In order for SmartThings to send commands back to the SmartBlocks on your Minecraft server, you will have to enter your Server Address",
102 log.debug "serverPage"
103 dynamicPage(name: "serverPage", title: "Connect SmartThings To Your Minecraft Server") {
107 title: "In order for SmartThings to send commands back to the SmartBlocks on your Minecraft server, you will have to enter your Server Address",
117 dynamicPage(name: "linkerPage", title: "Link SmartBlocks To Switches") {
120 title: "Link a SmartBlock to a switch",
121 name: "blockLinker-new",
122 namespace: "vlaminck/Minecraft",
123 appName: "SmartBlock Linker",
126 params: ["blocks": getChildDevices()]
134 return dynamicPage(name: "notifierPage", title: "Get Notified When a SmartBlock is updated") {
137 title: "Get Notified",
138 name: "blockNotifier-new",
139 namespace: "vlaminck/Minecraft",
140 appName: "SmartBlock Notifier",
148 return dynamicPage(name: "chatPage", title: "Send Notifications into Minecraft") {
151 title: "Send Notifications",
152 name: "chatSender-new",
153 namespace: "vlaminck/Minecraft",
154 appName: "SmartBlock Chat Sender",
162 path("/block") { // any need for GET?
165 POST : "createBlock",
167 DELETE: "deleteBlock"
179 def data = request.JSON
180 def blockCoordinates = blockCoordinates(data)
181 def blockDNI = blockDNI(data)
182 def block = block(data)
185 log.debug "Block ${block?.label} with id $blockDNI already exists"
187 block = addChildDevice("vlaminck/Minecraft", "Smart Block", blockDNI, null, [name: "SmartBlock", label: "SmartBlock $blockCoordinates"])
190 block?.setCoordinates(data.x, data.y, data.z)
191 block?.setDestroyed(false)
192 block?.setWorldSeed(data?.worldSeed)
193 block?.setDimensionName(data?.dimensionName)
194 block?.setPlacedBy(data?.placedBy)
197 block.setServerIp(serverIp)
200 log.debug "created ${block?.label} with id $blockDNI"
204 log.debug "ack params : $params"
205 log.debug "ack JSON : ${request.JSON}"
207 sendDataToBlock(request?.JSON, false)
211 sendDataToBlock(request?.JSON, true)
214 def sendDataToBlock(data, isStateChange) {
216 def blockCoordinates = blockCoordinates(data)
217 def blockDNI = blockDNI(data)
218 def block = block(data)
219 log.debug "updating Block ${block?.label} with id $blockDNI"
221 block?.neighborBlockChange(data)
223 if (data.worldSeed) {
224 block.setWorldSeed(data.worldSeed)
227 if (data.dimensionName) {
228 block.setDimensionName(data.dimensionName)
232 block.setPlacedBy(data.placedBy)
235 block.setServerIp(serverIp)
240 def data = request.JSON
241 def blockDNI = blockDNI(data)
242 def block = block(data)
244 block?.setDestroyed(true)
248 log.debug "attempting to delete Block ${block?.label} with id $blockDNI"
249 deleteChildDevice(blockDNI)
252 private blockCoordinates(data) {
253 return "(${data?.x},${data?.y},${data?.z})"
256 private blockDNI(data) {
257 "${data.worldSeed}|${data.dimensionName}|${blockCoordinates(data)}".encodeAsMD5()
260 private block(data) {
261 return getChildDevice(blockDNI(data))
265 log.debug "Installed with settings: ${settings}"
271 log.debug "Updated with settings: ${settings}"
278 // update all children with serverIp.
280 getChildDevices().each { block ->
281 block.setServerIp(serverIp)
287 public getServerURL() {
288 return "http://${serverIp}:3333"