/
handlershelpers.go
executable file
·133 lines (122 loc) · 4.44 KB
/
handlershelpers.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
package alexaconnector
import (
"context"
"encoding/json"
"fmt"
"github.com/function61/hautomo/pkg/adapters/alexaadapter/aamessages"
"github.com/function61/hautomo/pkg/alexatypes"
)
func (h *Handlers) Handle(ctx context.Context, input []byte) (*alexatypes.AlexaResponse, error) {
directiveMsg, payloadGeneric, err := unmarshalDirective(input)
if err != nil {
return nil, err
}
switch payload := payloadGeneric.(type) {
case *alexatypes.AlexaAuthorizationAcceptGrantInput:
return h.AlexaAuthorizationAcceptGrantInput(ctx, directiveMsg, payload)
case *alexatypes.AlexaDiscoveryDiscoverInput:
return h.AlexaDiscoveryDiscoverInput(ctx, directiveMsg, payload)
case *alexatypes.AlexaReportStateInput:
return h.AlexaReportStateInput(ctx, directiveMsg, payload)
case *alexatypes.AlexaPowerControllerTurnOn:
return h.AlexaPowerControllerTurnOn(ctx, directiveMsg, payload)
case *alexatypes.AlexaPowerControllerTurnOff:
return h.AlexaPowerControllerTurnOff(ctx, directiveMsg, payload)
case *alexatypes.AlexaBrightnessControllerSetBrightness:
return h.AlexaBrightnessControllerSetBrightness(ctx, directiveMsg, payload)
case *alexatypes.AlexaColorTemperatureControllerSetColorTemperature:
return h.AlexaColorTemperatureControllerSetColorTemperature(ctx, directiveMsg, payload)
case *alexatypes.AlexaColorControllerSetColor:
return h.AlexaColorControllerSetColor(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerPlay:
return h.AlexaPlaybackControllerPlay(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerPause:
return h.AlexaPlaybackControllerPause(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerStop:
return h.AlexaPlaybackControllerStop(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerStartOver:
return h.AlexaPlaybackControllerStartOver(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerPrevious:
return h.AlexaPlaybackControllerPrevious(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerNext:
return h.AlexaPlaybackControllerNext(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerRewind:
return h.AlexaPlaybackControllerRewind(ctx, directiveMsg, payload)
case *alexatypes.AlexaPlaybackControllerFastForward:
return h.AlexaPlaybackControllerFastForward(ctx, directiveMsg, payload)
case *alexatypes.AlexaPercentageControllerSetPercentage:
return h.AlexaPercentageControllerSetPercentage(ctx, directiveMsg, payload)
default:
return nil, fmt.Errorf(
"unknown payload type: %s/%s",
directiveMsg.Directive.Header.Namespace,
directiveMsg.Directive.Header.Name)
}
}
func (h *Handlers) sendCommand(
ctx context.Context,
directiveMsg *alexatypes.DirectiveInput,
command aamessages.Message,
) error {
queue, found := directiveMsg.Directive.Endpoint.Cookie["queue"]
if !found {
return fmt.Errorf(
"sendCommand: queue not specified for %s",
directiveMsg.Directive.Endpoint.EndpointId)
}
return h.extSystems.SendCommand(ctx, queue, command)
}
func (h *Handlers) mkProperty(
namespace string,
propertyName string,
propertyValue interface{},
) alexatypes.AlexaProperty {
return alexatypes.AlexaProperty{
Namespace: namespace,
Name: propertyName,
Value: propertyValue,
TimeOfSample: h.now(),
UncertaintyInMilliseconds: 0,
}
}
func (h *Handlers) propertyResponse(
name string,
directiveMsg *alexatypes.DirectiveInput,
properties ...alexatypes.AlexaProperty,
) *alexatypes.AlexaResponse {
noPayload := json.RawMessage("{}")
return &alexatypes.AlexaResponse{
Context: &alexatypes.AlexaResponseContext{
Properties: properties,
},
Event: alexatypes.AlexaEvent{
Header: alexatypes.AlexaEventHeader{
Namespace: "Alexa",
Name: name,
PayloadVersion: "3",
MessageId: h.msgIdGenerator(),
CorrelationToken: directiveMsg.Directive.Header.CorrelationToken,
},
Endpoint: directiveMsg.Directive.Endpoint,
Payload: &noPayload,
},
}
}
// no context, correlation token, endpoint or payload
func (h *Handlers) basicResponse(
namespace string,
name string,
) *alexatypes.AlexaResponse {
noPayload := json.RawMessage("{}")
return &alexatypes.AlexaResponse{
Event: alexatypes.AlexaEvent{
Header: alexatypes.AlexaEventHeader{
Namespace: namespace,
Name: name,
PayloadVersion: "3",
MessageId: h.msgIdGenerator(),
},
Payload: &noPayload,
},
}
}