Closed juppdes closed 6 years ago
Hi juppdes, I'm implementing a very similar scenario like yours. I have problem making mqtt connecting with my aws-iot thing. Can you please help me ?
My "handler function" gives me error in the "event.session.new" . This is the error and below there is the code and after the handler function :
START RequestId: b58fff63-418c-11e8-998d-5d6c78662e64 Version: $LATEST
2018-04-16T15:42:04.588Z b58fff63-418c-11e8-998d-5d6c78662e64 {"errorMessage":"Cannot read property 'new' of undefined","errorType":"TypeError","stackTrace":["exports.handler.err (/var/task/index.js:283:26)"]}
2018-04-16T15:42:04.829Z b58fff63-418c-11e8-998d-5d6c78662e64 connect
END RequestId: b58fff63-418c-11e8-998d-5d6c78662e64
Code
exports.handler = (event, context, callback) => {
try {
console.log(`event.session.application.applicationId=${event.session.application.applicationId}`);
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
/*
if (event.session.application.applicationId !== 'amzn1.echo-sdk-ams.app.[unique-value-here]') {
callback('Invalid Application ID');
}
*/
/*client = awsIot.device(mqtt_config);
client.on("connect",function(){ console.log("Connected to AWS IoT"); }); */
if (event.session.new) {
onSessionStarted({ requestId: event.request.requestId }, event.session);
}
if (event.request.type === 'LaunchRequest') {
onLaunch(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === 'IntentRequest') {
onIntent(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === 'SessionEndedRequest') {
onSessionEnded(event.request, event.session);
callback();
}
} catch (err) {
callback(err);
}
};
@Leobute unfortunately I have not been able to solve the problem yet. In case I get any light, I warn you.
Thanks a lot !!!!!!!!!!!!!!!!!!!!!
Are you trying to access local fire disk in web environment? Can you try using websocket connections to see if the problem still exists?
@fengsongAWS other websocket (client) work perfectly. this error occurs already in the connection attempt. Just by importing the "aws-iot-device-skd" library, the compiler already has errors in not locating "fs" and "tls". To resolve this, I have to declare "config.node = {fs: 'empty', tls: 'empty'}" I believe it is some mismatch between Nuxt and the aws library.
HI all. Thanks a lot anyway. My lambda function works answering my invocation and intents. Second step should be, using mqtt to communicaste with my raspberriPi3.
Below my lambda function that doesn't work when connecting to iot raspberry
`'use strict';
const awsIot = require("aws-iot-device-sdk");
const host = "xxxxxxxxxxxxx.iot.us-west-2.amazonaws.com"; const topic = "$aws/things/RaspberryPi3/shadow/update"; const app_id = "amzn1.ask.skill.5924be59-72e1-4c9b-83d0-bc3d2b197dea"; const deviceName = "RaspberryPi3"; var intent = ""
var mqtt_config = {
"host": "xxxxxxxxxxxxxxxxxxxxxx.iot.us-west-2.amazonaws.com",
"port": 8883,
"clientId": "RaspberryPi3",
"thingName": "RaspberryPi3",
"caCert": "certs/root-CA.crt",
"clientCert": "certs/RaspberryPi3.cert.pem",
"privateKey": "certs/RaspberryPi3.private.key",
"region": "us-west-2",
};
var client; var connected = false
function mqttPublish(intent, sessionAttributes, cardTitle, speechOutput, repromptText, shouldEndSession) { const strIntent = JSON.stringify(intent); console.log("mqttPublish: INTENT text = " + strIntent);
client.on('connect', () => {
client.subscribe('GroupLeonardo/connected')
})
client.on('message', (topic, message) => {
if(topic === '$aws/things/RaspberryPi3/shadow/update') {
connected = (message.toString() === 'true');
}
})
context.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
}
// --------------- Helpers that build all of the responses -----------------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: 'PlainText',
text: output,
},
card: {
type: 'Simple',
title: SessionSpeechlet - ${title}
,
content: SessionSpeechlet - ${output}
,
},
reprompt: {
outputSpeech: {
type: 'PlainText',
text: repromptText,
},
},
shouldEndSession,
};
}
function buildResponse(sessionAttributes, speechletResponse) { return { version: '1.0', sessionAttributes, response: speechletResponse, }; }
// --------------- Functions that control the skill's behavior -----------------------
function getWelcomeResponse(callback) { // If we wanted to initialize the session to have some attributes we could add those here. const sessionAttributes = {}; const cardTitle = 'Welcome'; const speechOutput = 'Welcome to Leonardo Desktop' + 'Please tell me what do you want me to do';
const repromptText = 'Please tell me what do you want me to do' +
'Manage my room';
const shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function handleSessionEndRequest(callback) { const cardTitle = 'Session Ended'; const speechOutput = 'Thank you for using your studyroom management. If you need me again, just ask!'; // Setting this to true ends the session and exits the skill. const shouldEndSession = true;
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}
function switchmybedroom(intent, session, callback) {
var repromptText = null;
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
repromptText = "Tell me how you want the light to be switched. ";
var state = intent.slots.state.value;
const validstates = [ "on", "off" ];
if (validstates.indexOf(state) == -1)
{
speechOutput = "I couldn't understand the state of the lights. ";
context.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
}
else
{
var cardTitle = "OK, Bedroom lighs turning " + state;
speechOutput = "OK, Turning lights of your bedroom " + state;
callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
client = awsIot.device(mqtt_config);
mqttPublish(intent, sessionAttributes, cardTitle, speechOutput, repromptText, shouldEndSession);
}
}
function switchmytable(intent, session, callback) {
var repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
let speechOutput = "";
repromptText = "Tell me how you want the light on the table to be switched.";
var state = intent.slots.state.value;
var validstates = [ "on", "off" ];
if (validstates.indexOf(state) == -1)
{
speechOutput = "I couldn't understand the state of the lights. ";
context.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
}
else
{
var cardTitle = "OK, Table light turning " + state;
speechOutput = "OK, Turning your table light " + state;
callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
client = awsIot.device(mqtt_config);
mqttPublish(intent, sessionAttributes, cardTitle, speechOutput, repromptText, shouldEndSession);
}
}
function switchmytv(intent, session, callback) {
var repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
let speechOutput = "";
repromptText = "Tell me how you want the tv be switched.";
var state = intent.slots.state.value;
var validstates = [ "on", "off" ];
if (validstates.indexOf(state) == -1)
{
speechOutput = "I couldn't understand the state of the tv. ";
context.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
}
else
{
var cardTitle = "OK, Tv turning " + state;
speechOutput = "Ok, now Tv is " + state;
callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
client = awsIot.device(mqtt_config);
mqttPublish(intent, sessionAttributes, cardTitle, speechOutput, repromptText, shouldEndSession);
}
}
// --------------- Events -----------------------
/**
onSessionStarted requestId=${sessionStartedRequest.requestId}, sessionId=${session.sessionId}
);
}/**
Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
console.log(onLaunch requestId=${launchRequest.requestId}, sessionId=${session.sessionId}
);
// Dispatch to your skill's launch. getWelcomeResponse(callback); }
/**
Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log(onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}
);
var intent = intentRequest.intent; var intentName = intentRequest.intent.name;
console.log("REQUEST to string =" + JSON.stringify(intentRequest)); console.log("Intentname = " + intentName); // Dispatch to your skill's intent handlers
if (intentName == "switchmybedroom") { switchmybedroom(intent, session, callback); } else if (intentName == "switchmytable") { switchmytable(intent, session, callback); } else if (intentName == "switchmytv") { switchmytv(intent, session, callback); } if (intentName == "AMAZON.HelpIntent") { getWelcomeResponse(callback); } else if (intentName == "AMAZON.StopIntent" || intentName == "AMAZON.CancelIntent") { handleSessionEndRequest(callback); } else { throw new Error('Invalid intent'); } }
/**
onSessionEnded requestId=${sessionEndedRequest.requestId}, sessionId=${session.sessionId}
);
// Add cleanup logic here
}// --------------- Main handler -----------------------
// Route the incoming request based on type (LaunchRequest, IntentRequest, // etc.) The JSON body of the request is provided in the event parameter. exports.handler = (event, context, callback) => { try {
console.log(event.session.application.applicationId);
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
/*
if (event.session.application.applicationId !== 'amzn1.ask.skill.5924be59-72e1-4c9b-83d0-bc3d2b197dea') {
callback('Invalid Application ID');
}
*/
if (event.session.new) {
onSessionStarted({ requestId: event.request.requestId }, event.session);
}
if (event.request.type === 'LaunchRequest') {
onLaunch(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === 'IntentRequest') {
onIntent(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === 'SessionEndedRequest') {
onSessionEnded(event.request, event.session);
callback();
}
} catch (err) {
callback(err);
}
}; `
I thinks there is something wrong with my "function mqttPublish"
Hi @juppdes , The web browser environment has some compatibility issues with tls and fs library. This should not happen in Node.js engine though. This is same for use with browserify and webpack library also. By defining them as empty is a workaround on that.
Hello @fengsongAWS .. yes, but solving this issue of FS and TLS, the other problem occurs which is the error message:
C:\Sistemas\AM\nodemodules\aws-iot-device-sdk\common\lib\tls-reader.js:89 Uncaught TypeError: filesys.existsSync is not a function at webpackJsonp../nodemodules/aws-iot-device-sdk/common/lib/tls-reader.js.module.exports
it displays this message, but it works normally. I would like to resolve this issue, as I will use this functionality a lot.
I think this is probably due to missing fs module which webpack does not recognize but since you are not using this module, it will not affect your functionality. This is pretty much a compatibility issue and a lot of different web framework has. To resolve this issue, you probably need to modify source code to remove reference of fs modules such as existsSync.
@fengsongAWS Ok.. I believe that, regardless of my situation, it would be interesting to review this library, as the use of Next / Nuxt is increasing, and this problem will be frequent.
Where could I download the unminimized source code so I can make the adjustments manually?
I am not familiar with Nuxt plug in. Can you pull down from Github and load it?
@fengsongAWS Sure. Thank you !
Hi guys, i solved and now it works perfect. Thanks anyway. If you need help/questions, ask. Bye
Leobute how do you did it?
@Leobute 2 years later and I ran into this issue. how did you solve this?
ERROR in ./node_modules/aws-iot-device-sdk/common/lib/tls-reader.js
Module not found: Error: Can't resolve 'fs' in .../node_modules/aws-iot-device-sdk/common/lib'
ERROR in ./node_modules/aws-iot-device-sdk/device/index.js
Module not found: Error: Can't resolve 'fs' in.../node_modules/aws-iot-device-sdk/device'
ERROR in ./node_modules/aws-iot-device-sdk/device/lib/tls.js
Module not found: Error: Can't resolve 'tls' in .../node_modules/aws-iot-device-sdk/device/lib'
I create a plugin like this:
var awsIot = require('aws-iot-device-sdk') var device = awsIot.device({ keyPath: 'xxxxxx, certPath: 'xxxxxx, caPath: 'xxxxxx', clientId: 'xxxxx', region: 'xxxxxx', host: 'xxxxxxxxxx.amazonaws.com' })
Work´s, but display a error message:
C:\Sistemas\AM\nodemodules\aws-iot-device-sdk\common\lib\tls-reader.js:89 Uncaught TypeError: filesys.existsSync is not a function at webpackJsonp../nodemodules/aws-iot-device-sdk/common/lib/tls-reader.js.module.exports
Ps. In Config.nuxt.js, i include this in Build: config.node = { fs: 'empty', tls: 'empty' } to fix error: fd tls not found. Please install it.
Maybe this cause the error here...
How can i fix it ?