definition(
    name: "Send Streamie Event Notifications",
    namespace: "symphonicsys.com",
    author: "Curtis Jones",
    description: "Sends events to Streamie.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
)

preferences {
	section("Choose one or more, when..."){
		input "motion", "capability.motionSensor", title: "Motion Here", required: false, multiple: true
		input "contact", "capability.contactSensor", title: "Contact Opens", required: false, multiple: true
		input "contactClosed", "capability.contactSensor", title: "Contact Closes", required: false, multiple: true
		input "acceleration", "capability.accelerationSensor", title: "Acceleration Detected", required: false, multiple: true
		input "mySwitch", "capability.switch", title: "Switch Turned On", required: false, multiple: true
		input "mySwitchOff", "capability.switch", title: "Switch Turned Off", required: false, multiple: true
		input "arrivalPresence", "capability.presenceSensor", title: "Arrival Of", required: false, multiple: true
		input "departurePresence", "capability.presenceSensor", title: "Departure Of", required: false, multiple: true
		input "smoke", "capability.smokeDetector", title: "Smoke Detected", required: false, multiple: true
		input "water", "capability.waterSensor", title: "Water Sensor Wet", required: false, multiple: true
		input "lock", "capability.lock", title: "Lock Unlocked", required: false, multiple: true
		input "lockLocked", "capability.lock", title: "Lock Locked", required: false, multiple: true
		input "thermostat", "capability.thermostat", title: "Thermostat State", required: false, multiple: true
		input "musicPlayer", "capability.musicPlayer", title: "Music Player", required: false, multiple: true
        input "notification", "capability.notification", title: "Notification", required: false, multiple: true
        input "directional", "device.directionalSensor", title: "Directional", required: false, multiple: true
	}
//	section("Send this event notification to Streamie"){
//		input "StreamieCommand", "text", title: "Event notification to send", required: true
//	}
	section("Streamie Address #1"){
		input "server1", "text", title: "Server IP", description: "Your Streamie Server IP", required: true
		//input "port", "number", title: "Port", description: "Port Number", required: true
	}

	section("Streamie Address #2"){
		input "server2", "text", title: "Server IP", description: "Your Streamie Server IP", required: false
	}

	section("Streamie Address #3"){
		input "server3", "text", title: "Server IP", description: "Your Streamie Server IP", required: false
	}
//	section("Send Push Notification?") {
//      input "sendPush", "bool", required: false, title: "Send Push Notification?"
//  }
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribeToEvents()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribeToEvents()
}

def subscribeToEvents() {
	subscribe(contact, "contact.open", eventHandler)
	subscribe(contactClosed, "contact.closed", eventHandler)
	subscribe(acceleration, "acceleration.active", eventHandler)
	subscribe(motion, "motion.active", eventHandler)
	subscribe(mySwitch, "switch.on", eventHandler)
	subscribe(mySwitchOff, "switch.off", eventHandler)
	subscribe(arrivalPresence, "presence.present", eventHandler)
	subscribe(departurePresence, "presence.not present", eventHandler)
	subscribe(smoke, "smoke.detected", eventHandler)
	subscribe(smoke, "smoke.tested", eventHandler)
	subscribe(smoke, "carbonMonoxide.detected", eventHandler)
	subscribe(water, "water.wet", eventHandler)
	subscribe(lock, "lock.unlocked", eventHandler)
	subscribe(lockLocked, "lock.locked", eventHandler)
	subscribe(thermostat, "thermostat", eventHandler)
	subscribe(musicPlayer, "musicPlayer", eventHandler)
    subscribe(notification, "notification", eventHandler)
    subscribe(directional, "direction", directionalEventHandler)
}

def eventHandler(evt) {
	sendHttp3(evt)
}

def directionalEventHandler(evt) {
	sendHttp3(evt)
    
//  if (sendPush) {
//      sendPush("Driveway intrusion is ${evt.value}")
//  }
}

def sendHttp3(evt) {
    log.debug "***** servers ${settings.servers}"

	if (settings.server1) {
    	sendHttp4(evt, "${settings.server1}:10080")
    }


	if (settings.server2) {
    	sendHttp4(evt, "${settings.server2}:10080")
    }


	if (settings.server3) {
    	sendHttp4(evt, "${settings.server3}:10080")
    }

//  sendHttp4(evt, "${settings.server}:10080")
//  sendHttp4(evt, "${settings.server}:${settings.port}")
}

def sendHttp4(evt, ipAndPort) {
    def deviceNetworkId = "1234"
    def deviceId = java.net.URLEncoder.encode(evt.deviceId, "UTF-8")
    def deviceName = java.net.URLEncoder.encode(evt.displayName, "UTF-8")
    def deviceType = java.net.URLEncoder.encode(evt.getDevice().typeName, "UTF-8")
	def value = java.net.URLEncoder.encode(evt.value, "UTF-8")

    log.debug "***** The device ${deviceId} / ${deviceName} / ${displayName} / ${deviceType} is reporting ${value}"
    sendHubCommand(new physicalgraph.device.HubAction("""GET /smartthings?deviceId=${deviceId}&deviceName=${deviceName}&deviceType=${deviceType}&value=${value}&source=${evt.source} HTTP/1.1\r\nHOST: ${ipAndPort}\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
}


