-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_main.py
95 lines (73 loc) · 3.08 KB
/
lambda_main.py
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
"""
.. module:: lambda_main
:synopsis: entry point for lambda handler
"""
# Borrow a lot from:
# https://github.com/alexa/alexa-smarthome/tree/master/sample_lambda/python
# -*- coding: utf-8 -*-
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License"). You may not use this file except in
# compliance with the License. A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import logging
import time
import json
import uuid
import sys
sys.path.append('src/')
sys.path.append('validator/')
import utils
import lambda_master_handler
import sys
import os
from validation import validate_message
from AWSIoTPythonSDK.exception.AWSIoTExceptions import disconnectTimeoutException, connectTimeoutException
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(req, context):
"""
This is where the program begins and where the program will end, its return value will be forwarded to Alexa
:param req: the message from Alexa
:param context: execution context of the message, not used
:return: json dict that if valid, will be turned into Alexa speech response to user
"""
try:
logger.info("Directive:")
logger.info(json.dumps(req, indent=4, sort_keys=True))
version = utils.get_directive_version(req)
if version == "3":
try:
masterHandler = lambda_master_handler.MasterHandler()
except connectTimeoutException:
logger.warning("Connection timeout")
logger.info("Received v3 directive!")
if req["directive"]["header"]["name"] == "Discover":
# discovery is used to help Alexa knows what devices exist as well as what can be done with them
response = masterHandler.handleDiscoveryV3(req)
else:
response = masterHandler.handleNonDiscoveryV3(req, context)
# logger.info("Resp:")
# logger.info(json.dumps(response, indent=4, sort_keys=True))
# check the response against amazon json schema
validate_message(req, response)
# disconnect from mqtt server
try:
while False == masterHandler._mqttManager._myAWSIoTMQTTClient.disconnect():
pass
except disconnectTimeoutException:
logger.warning("Disconnect timed out")
else:
# we shouldn't be receiving v2 directive for the devices that are being used
logger.info("Received v2 directive!")
response = masterHandler.handleErrorResponse(
req, "Unsupported Directive Version", "V2 directive Received")
return response
except ValueError as error:
logger.error(error)
raise