-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.rb
188 lines (175 loc) · 4.79 KB
/
lambda_function.rb
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require 'aws-sdk'
require 'securerandom'
IOT_THING_REGION = '<your-iot-region>'
IOT_THING_REST_ENDPOINT = 'https://<your-iot-rest-endpoint>.iot.<your-region>.amazonaws.com'
# If you use GARAGE_DOOR, then you have to manually activate voice control
# in the Alexa app and voice control requires you say a pin. It's more
# secure, but less convenient
DISPLAY_CATEGORY = 'SWITCH'
def lambda_handler(event:, context:)
puts JSON.pretty_generate(event), "\n"
response = case event.dig('directive', 'header', 'name')
when 'Discover'
discovery()
when 'TurnOn', 'TurnOff'
toggle(event: event)
else
error(message: 'unsupported directive')
end
puts JSON.pretty_generate(response), "\n"
response
end
def toggle event:
puts "Toggling garage door\n"
instance = event.dig('directive', 'header', 'instance')
endpointId = event.dig('directive', 'endpoint', 'endpointId')
correlationToken = event.dig('directive', 'header', 'correlationToken')
client = Aws::IoTDataPlane::Client.new(
region: IOT_THING_REGION,
endpoint: IOT_THING_REST_ENDPOINT
)
side = endpointId[/(left|right)/]
client.publish({
topic: "garage/toggle/#{side}",
qos: 0,
payload: 'ON'
})
{
event: {
header: {
namespace: "Alexa",
name: "Response",
messageId: SecureRandom.uuid,
correlationToken: correlationToken,
payloadVersion: "3"
},
endpoint: {
endpointId: endpointId
},
payload: {}
},
context: {
properties: [
{
namespace: "Alexa.ToggleController",
instance: instance,
name: "toggleState",
value: "ON"
}
]
}
}
end
def discovery
puts "Sending discovery event"
{
event: {
header: {
payloadVersion: '3',
name: 'Discover.Response',
namespace: 'Alexa.Discovery',
messageId: SecureRandom.uuid
},
payload: {
endpoints: ['left', 'right'].map do |side|
{
endpointId: "garage-#{side}",
manufacturerName: 'kylekyle',
description: 'Hacked Garage Door Opener',
friendlyName: "#{side.capitalize} Garage Door",
displayCategories: [DISPLAY_CATEGORY],
cookie: {},
connections: [],
relationships: {},
additionalAttributes: {},
capabilities: [
{
type: 'AlexaInterface',
interface: 'Alexa.ToggleController',
instance: side,
version: '3',
properties: {
supported: [
{
name: 'toggleState'
}
],
proactivelyReported: false,
retrievable: false
},
capabilityResources: {
friendlyNames: [
{
'@type': 'text',
value: {
text: "#{side} garage door",
locale: 'en-US'
}
}
]
},
semantics: {
actionMappings: [
{
'@type': 'ActionsToDirective',
actions: ['Alexa.Actions.Close'],
directive: {
name: 'TurnOff',
payload: {}
}
},
{
'@type': 'ActionsToDirective',
actions: ['Alexa.Actions.Open'],
directive: {
name: 'TurnOn',
payload: {}
}
}
],
stateMappings: [
{
'@type': 'StatesToValue',
states: ['Alexa.States.Closed'],
value: 'OFF'
},
{
'@type': 'StatesToValue',
states: ['Alexa.States.Open'],
value: 'ON'
}
]
}
},
{
type: 'AlexaInterface',
interface: 'Alexa',
version: '3'
}
]
}
end
}
}
}
end
def error message:
puts 'ERROR: Unsupported directive'
{
event: {
header: {
namespace: "Alexa",
payloadVersion: "3",
name: "ErrorResponse",
messageId: SecureRandom.uuid
},
endpoint: {
endpointId: "garage"
},
payload: {
type: "INTERNAL_ERROR",
message: message
}
}
}
end