Make triggers more generic
This commit is contained in:
parent
27adf8569d
commit
8175f4f0d7
66
car.cgi.go
66
car.cgi.go
|
@ -1,66 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cgi"
|
||||
"os"
|
||||
"github.com/jsgoecke/tesla"
|
||||
"github.com/coduno/netrc"
|
||||
)
|
||||
|
||||
const authtok = "~!Jf5!uYFxhK"
|
||||
const clientId = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384"
|
||||
const clientSec = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3"
|
||||
|
||||
type Handler struct {
|
||||
cgi.Handler
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.FormValue("auth") != authtok {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintln(w, "NO: Invalid authtok")
|
||||
return
|
||||
}
|
||||
|
||||
os.Setenv("HOME", "/home/neale")
|
||||
n, _ := netrc.Parse()
|
||||
secrets := n["gitlab.com"] // Requiring a password is such bullshit.
|
||||
|
||||
auth := tesla.Auth{
|
||||
ClientID: clientId,
|
||||
ClientSecret: clientSec,
|
||||
Email: secrets.Login,
|
||||
Password: secrets.Password,
|
||||
}
|
||||
cli, err := tesla.NewClient(&auth)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintln(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
vehicles, err := cli.Vehicles()
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintln(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
vehicle := vehicles[0]
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintln(w, "OK")
|
||||
fmt.Fprintln(w, vehicle.StartAirConditioning())
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(0)
|
||||
log.SetPrefix("Status: 500 CGI Go Boom\nContent-type: text/plain\n\nERROR: ")
|
||||
h := Handler{}
|
||||
if err := cgi.Serve(h); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
3
install
3
install
|
@ -1,6 +1,7 @@
|
|||
#! /bin/sh
|
||||
|
||||
DESTDIR=${1:-/home/neale/public_html}
|
||||
GOPATH=$HOME/go export GOPATH
|
||||
|
||||
cd $(dirname $0)
|
||||
|
||||
|
@ -71,7 +72,7 @@ git ls-files | while read fn; do
|
|||
*/install)
|
||||
install $fn
|
||||
;;
|
||||
car.cgi.go)
|
||||
trigger.cgi.go)
|
||||
gc $fn
|
||||
setuid $fn
|
||||
;;
|
||||
|
|
|
@ -0,0 +1,101 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/coduno/netrc"
|
||||
"github.com/jsgoecke/tesla"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cgi"
|
||||
"net/smtp"
|
||||
"os"
|
||||
)
|
||||
|
||||
const authtok = "~!Jf5!uYFxhK"
|
||||
const clientId = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384"
|
||||
const clientSec = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3"
|
||||
|
||||
func Kersplode(w http.ResponseWriter, error string) {
|
||||
http.Error(w, error, 500)
|
||||
|
||||
// Send an email
|
||||
c, _ := smtp.Dial("localhost:25")
|
||||
defer c.Close()
|
||||
c.Mail("neale@woozle.org")
|
||||
c.Rcpt("dartcatcher@gmail.com")
|
||||
wc, _ := c.Data()
|
||||
defer wc.Close()
|
||||
fmt.Fprintln(wc, "From: IFTTT Trigger <neale@woozle.org>")
|
||||
fmt.Fprintln(wc, "To: Neale Pickett <dartcatcher@gmail.com>")
|
||||
fmt.Fprintln(wc, "Subject: IFTTT trigger error")
|
||||
fmt.Fprintln(wc, "")
|
||||
fmt.Fprintln(wc, "Hi, this is the IFTTT trigger at woozle.org.")
|
||||
fmt.Fprintln(wc, "I'm sorry to say that something went wrong with a recent request.")
|
||||
fmt.Fprintln(wc, "Here is the text of the error:")
|
||||
fmt.Fprintln(wc, "")
|
||||
fmt.Fprintln(wc, error)
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
cgi.Handler
|
||||
}
|
||||
|
||||
func (h Handler) TriggerHvac(w http.ResponseWriter, r *http.Request) {
|
||||
os.Setenv("HOME", "/home/neale")
|
||||
n, _ := netrc.Parse()
|
||||
secrets := n["gitosis.com"] // Requiring a password here is such bullshit.
|
||||
|
||||
auth := tesla.Auth{
|
||||
ClientID: clientId,
|
||||
ClientSecret: clientSec,
|
||||
Email: secrets.Login,
|
||||
Password: secrets.Password,
|
||||
}
|
||||
cli, err := tesla.NewClient(&auth)
|
||||
if err != nil {
|
||||
Kersplode(w, err.Error());
|
||||
return
|
||||
}
|
||||
|
||||
vehicles, err := cli.Vehicles()
|
||||
if err != nil {
|
||||
Kersplode(w, err.Error());
|
||||
return
|
||||
}
|
||||
|
||||
vehicle := vehicles[0]
|
||||
if _, err := vehicle.Wakeup(); err != nil {
|
||||
Kersplode(w, err.Error());
|
||||
return
|
||||
}
|
||||
if err := vehicle.StartAirConditioning(); err != nil {
|
||||
Kersplode(w, err.Error());
|
||||
return
|
||||
}
|
||||
http.Error(w, "OK", 200)
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.FormValue("auth") != authtok {
|
||||
http.Error(w, "Invalid authtok", 401)
|
||||
return
|
||||
}
|
||||
|
||||
switch trigger := r.FormValue("trigger"); trigger {
|
||||
case "hvac":
|
||||
h.TriggerHvac(w, r)
|
||||
default:
|
||||
http.Error(w, "Undefined trigger", 422)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(0)
|
||||
log.SetPrefix("Status: 500 CGI Go Boom\nContent-type: text/plain\n\nERROR: ")
|
||||
h := Handler{}
|
||||
if err := cgi.Serve(h); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue