@bobbyD
I just updated to 2.4.3.137
and lost callbacks into parse, but I had a hunch
changed the rawData to true
in my initialize()
interfaces.eventStream.connect(url, [
pingInterval: 10,
readTimeout: 60,
headers:["Accept": "text/event-stream"],
rawData: true
])
// data from eventStream
void parse(String response) {
log.debug("parse(): ${response}")
}
and sure enough, parse() now gets called
when I was at 2.4.3.131 & rawData: false, parse() string would be:
parse(): { "upTime": 24915162, "freeHeap": 116376, "minHeap": 45084, "wifiRSSI": "-39 dBm, Channel 6" }
now its like the backend isnt preparsing message data
here is the result when rawData set to true:
parse():
parse(): data: { "upTime": 25795256, "freeHeap": 113324, "minHeap": 45084, "wifiRSSI": "-40 dBm, Channel 6" }
parse(): event: message
so get
- event:message
- data: { "upTime": 25795256, "freeHeap": 113324, "minHeap": 45084, "wifiRSSI": "-40 dBm, Channel 6" }
- empty string
rawData:false is what i want to use, as the JSON at that point ready to parse
sse.dev
is a good site to test against this
i have written a driver that does and only when rawData: true
does it call parse()
/*
*
* interfaces.eventStream tester
*
*/
import groovy.transform.Field
@Field static def shouldReconnect = false
metadata {
definition(name: "EventStream-Tester", namespace: "MJS Gadgets", author: "MitchJS", importUrl: "") {
capability "Initialize"
command "disconnect"
attribute "eventStreamStatus", "string"
attribute "networkStatus", "enum", ["offline","online"]
}
}
preferences {
}
def initialize() {
log.debug("initialize() called")
// temporally prevent reconnection
if (device.currentValue("eventStreamStatus") == "Connected") {
shouldReconnect = false
}
sendEvent(name:"networkStatus", value: "offline")
sendEvent(name: "eventStreamStatus", value: "Connecting", descriptionText:"${device.displayName} eventStreamStatus is Connecting")
log.debug("initialize() called, request subscribe to SSE")
try {
// SSE test url
url = "https://sse.dev/test?interval=5"
log.debug("initialize() subscribing to events via interfaces.eventStream.connect(${url})")
// connect to SSE
interfaces.eventStream.connect(url, [
pingInterval: 10,
readTimeout: 60,
headers:["Accept": "text/event-stream"],
rawData: false
])
}
catch (Exception e) {
log.error "initialize() Execption: ${e}"
runIn(10, initialize)
}
}
def disconnect() {
log.debug("disconnect() called")
if (device.currentValue("eventStreamStatus") == "Connected") {
shouldReconnect = false;
interfaces.eventStream.close()
}
}
// data from eventStream
void parse(String response) {
log.debug("parse() ${response}")
}
def eventStreamStatus(String message) {
log.debug("eventStreamStatus() ${message}")
if (message.startsWith("START:")) {
sendEvent(name:"networkStatus", value: "online")
sendEvent(name: "eventStreamStatus", value: "Connected", descriptionText:"${device.displayName} eventStreamStatus is Connected")
shouldReconnect = true;
}
else if (message.startsWith("STOP:")) {
sendEvent(name:"networkStatus", value: "offline")
sendEvent(name: "eventStreamStatus", value: "Disconnected", descriptionText:"${device.displayName} eventStreamStatus is Disconnected")
// try to reconnect
if (shouldReconnect == true) {
log.debug("eventStreamStatus() will re-init")
runIn(5, "initialize")
}
}
else if (message.startsWith("ERROR:")) {
sendEvent(name:"networkStatus", value: "offline")
sendEvent(name: "eventStreamStatus", value: "Disconnected", descriptionText:"${device.displayName} eventStreamStatus is Disconnected")
if (message.contains("SocketTimeoutException")) {
// try to reconnect
log.debug("eventStreamStatus() will re-init")
runIn(5, "initialize")
}
}
}