Skip to content

Instantly share code, notes, and snippets.

@rgchris rgchris/light-control.reb
Last active Feb 1, 2017

Embed
What would you like to do?
Early Pass at HTTP Driven API to Send Commands to a UDP Server
#!/usr/local/bin/ren-c
Rebol [
Title: "Turn Lights On and Off"
Date: 31-Jan-2017
Author: "Christopher Ross-Gill"
File: %light-control.reb
Version: 0.1.0
Rights: http://opensource.org/licenses/Apache-2.0
]
do %httpd.reb
lighting-url: udp://192.168.1.124:8899
light-on: #{470055}
light-off: #{480055}
turn-lights-at: func [url package][
probe package
; udp: open url
; udp/awake: func [evt][
; switch evt/type [
; lookup [open evt/port]
; connect [write evt/port package close evt/port break]
; ]
; ]
; wait udp
; close udp
]
server: open [
Scheme: 'httpd
Port-ID: 8080
Awake: function [
request [object!]
][
switch/default request/target [
"/api/on" [
Response: 200
turn-lights-at lighting-url light-on
]
"/api/off" [
Response: 200
turn-lights-at lighting-url light-off
]
][
Response: 404
]
make object! compose [
Status: response
Type: "text/plain"
Content: form response
]
]
]
; attempt [browse http://127.0.0.1:8080/api/on]
forever [
wait [server 10]
print "Do this periodically"
]
@rgchris

This comment has been minimized.

Copy link
Owner Author

commented Feb 1, 2017

Uses my experimental httpd scheme.

Discussed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.