Skip to content

Instantly share code, notes, and snippets.

@marcsello
Last active October 16, 2023 11:24
Show Gist options
  • Save marcsello/1f708a9cfd3c38ae4edd29991e225f18 to your computer and use it in GitHub Desktop.
Save marcsello/1f708a9cfd3c38ae4edd29991e225f18 to your computer and use it in GitHub Desktop.
Expression 2 code for my extra-input project
@name E2 Extra Input v1 by Marcsello
@inputs
@outputs A B C D E F Connections ConnectionQuality EventCounter EventsSkipped
@persist Last
@trigger
@strict
# Designed to work with https://github.com/marcsello/e2-extra-input-server
# I've also written a blog post about all this: https://blog.marcsello.com/posts/2023/wiremod-input-from-the-real-world/
if (first() || duped()) {
runOnHTTP(1)
Last = 0
Connections = 0
A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
ConnectionQuality = 7
EventsSkipped = 0
EventCounter = 0
timer("connect", 500) # start the first connection after 500ms
}
if (clk("connect")) {
if (Connections < 3) {
if (httpCanRequest()) {
httpRequest("https://[YOUR DOMAIN]/sub?key=[YOUR READ_KEY]&last=" + Last)
Connections++
if (Connections < 3) {
timer("connect", 15050) # 15 sec required after a new connection
}
} else {
timer("connect", 500) # retry connection if couldn't make it
}
}
}
if (httpClk()) {
Connections--
Success = 0
# Parse response
if (httpSuccess()) {
Data = jsonDecode(httpData())
if (jsonError() == "") {
A = Data["A", normal]
B = Data["B", normal]
C = Data["C", normal]
D = Data["D", normal]
E = Data["E", normal]
F = Data["F", normal]
ID = Data["id", normal]
if (ID > Last) {
if (EventCounter > 0) { # do not count for the first connection
EventsSkipped += (ID-Last-1)
}
Last = ID
}
Success = 1
EventCounter++
}
}
# update connection quality indicator
if (Success) {
if (ConnectionQuality < 10) {
ConnectionQuality++
}
} else {
if (ConnectionQuality > 0) {
ConnectionQuality--
}
}
# start timer for a new connection
if (Connections < 3) {
timer("connect", 3050) # 3 sec required after a closed connection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment