Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active February 1, 2017 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgchris/ab7a935190cf0c4a85b8bb599ce3ab26 to your computer and use it in GitHub Desktop.
Save rgchris/ab7a935190cf0c4a85b8bb599ce3ab26 to your computer and use it in GitHub Desktop.
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
Copy link
Author

rgchris 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