Last active
February 1, 2017 04:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses my experimental httpd scheme.
Discussed here.