forked from SwissKid/go-alexa
/
alexa-insteon.go
157 lines (148 loc) · 4.47 KB
/
alexa-insteon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
// "io"
"encoding/json"
"net/http"
//"net/url"
"fmt"
"io/ioutil"
"github.com/swisskid/go-insteon/insteon"
//"strings"
"strconv"
)
func main() {
insteon.PopulateAll()
http.HandleFunc("/", foo)
http.ListenAndServe(":9003", nil)
}
//Request Fields
type RequestMaster struct {
Version string `json:"version"`
Session Session `json:"session"`
Request Request `json:"request"`
}
type Session struct{
New bool `json:"new"`
SessionId string `json:"sessionId"`
Attributes map[string]string `json:"attributes"`
Application Application `json:"application"`
User User `json:"user"`
}
type Application struct {
ApplicationId string
}
type User struct{
UserId string
}
type Request struct { //I have to combine all the different requests into this. ugh.
Type string `json:"type"` //LaunchRequest,IntentRequest,SessionEndedRequest
Timestamp string `json:"date"`
RequestId string `json:"requestId"`
Intent Intent `json:"intent"`
Reason string `json:"reason"`
}
type Intent struct {
Name string `json:"name"`
Slots map[string]Slot `json:"slots"`
}
type Slot struct {
Name string `json:"name"`
Value string `json:"value"`
}
//Response Fields
type ResponseMaster struct {
Version string `json:"version"`
SessionAttributes map[string]interface{} `json:"sessionAttributes,omitempty"`
Response Response `json:"response"`
}
type Response struct {
OutputSpeech *OutputSpeech `json:"outputSpeech,omitempty"`
Card *Card `json:"card,omitempty"`
Reprompt *Reprompt `json:"reprompt,omitempty"`
ShouldEndSession bool `json:"shouldEndSession"`
}
type OutputSpeech struct {
Type string `json:"type,omitempty"` //must omit empty so it doesn't print the whole object
Text string `json:"text,omitempty"` //must omit empty so it doesn't print the whole object
}
type Card struct {
Type string `json:"type,omitempty"` //must omit empty so it doesn't print the whole object
Title string `json:"title,omitempty"`
Content string `json:"content,omitempty"`
}
type Reprompt struct {
OutputSpeech OutputSpeech `json:"outputSpeech,omitempty"`
}
func foo(w http.ResponseWriter, r *http.Request) {
var l RequestMaster
var j ResponseMaster
var k OutputSpeech
body, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(body[:]))
json.Unmarshal(body, &l)
// For all responses
j.Version = "1.0"
k.Type = "PlainText"
j.Response.ShouldEndSession = true
if l.Request.Intent.Name != "" {
fmt.Println("The name of the intent is " + l.Request.Intent.Name)
fmt.Println(l.Request.Intent.Slots)
for _, slot := range l.Request.Intent.Slots {
fmt.Println(slot.Name)
fmt.Println(slot.Value)
}
}
if l.Request.Intent.Name == "Lighting" { //Engage insteon subroutine
var direction string
var item string
var n insteon.Command
for _, slot := range l.Request.Intent.Slots {
if slot.Name == "Direction" {
direction = slot.Value
continue
}
if slot.Name == "Device" {
item = slot.Value
}
}
n.Command = direction
item_type, id, location := insteon.SearchString(item)
fmt.Println(item_type + " ID = " + strconv.Itoa(id) + " Location " + strconv.Itoa(location))
switch item_type{
case "device":
n.Device_Id = id
n.Level = insteon.DevList[location].DimLevel/254 * 100
k.Text = "Turning " + item + " " + direction
case "scene":
n.Scene_Id = id
k.Text = "Turning " + item + " " + direction
case "room":
fmt.Println("Rooms are a pain")
k.Text = "Rooms are a pain, do it yourself"
case "not_found":
k.Text = "The following device, scene, or room was not found: " + item
}
insteon.RunCommand(n)
} else {
k.Text = "I am a working response"
}
if l.Request.Intent.Name == "Activate" { //Activate a scene
var n insteon.Command
n.Command = "on"
scene_name := l.Request.Intent.Slots["Scene"].Value
ret_type, scene_id, _ := insteon.SearchString(scene_name)
if ret_type == "scene" {
n.Scene_Id = scene_id
insteon.RunCommand(n)
k.Text = "Turning the scene " + scene_name + " on"
} else {
k.Text = "Failed to find scene " + scene_name
}
}
j.Response.OutputSpeech = &k
b, _ := json.Marshal(j)
fmt.Println(string(b[:]))
w.Header().Set("Content-Type", "application/json")
w.Header().Set("charset", "UTF-8")
w.Write(b)
}