Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1.  
  2. 'use strict';
  3.  
  4. const AWS = require('aws-sdk');
  5. const moment = require('moment');
  6. AWS.config.region = 'us-east-1';
  7. var sns = new AWS.SNS();
  8. var oldTime;
  9. var nomAlert = 'none';
  10. var mysql = require('mysql');
  11. var connection = mysql.createConnection({
  12. host : 'au-cdbr-sl-syd-01.cleardb.net',
  13. user : 'b07de0797f0dff',
  14. password : 'f769813c',
  15. database : 'ibmsl_4e7e44c949329d11f050'
  16. });
  17. const singleClick = 'nhap 1 cai';
  18. const doubleClick = 'nhap 2 cai';
  19. const longClick = 'nhap giu';
  20. const fiveSClick ='click sau 5 s';
  21. // console.log((+moment("2018-05-18 18:11:05").format("ss")- +moment("2018-05-18 18:11:00").format("ss")))
  22.  
  23.  
  24.  
  25. function sendSMS2(text,callback){
  26.  
  27.  
  28. var params = {
  29. Message: text,
  30. MessageStructure: 'string',
  31. TopicArn: 'arn:aws:sns:us-east-1:410564622077:MyIotButtonSNSTopic'
  32. };
  33.  
  34. sns.publish(params, function(err, data) {
  35. if (err) console.log(err, err.stack); // an error occurred
  36. else console.log(data); // successful response
  37. });
  38. }
  39. exports.handler = (event, context, callback) => {
  40.  
  41. // !!CHANGE THIS!! Your text to display goes in the single quotation marks below
  42.  
  43.  
  44. if(event.clickType=="SINGLE"){
  45. connection.connect((err)=>{
  46. if(err) console.log(err)
  47. connection.query(`select * from iot`, function (error, results, fields) {
  48. if (error) throw error;
  49.  
  50. if(results.length>0&&(+moment(new Date()).format("ss")- +moment(results[0].datetime).format("ss"))>=5 &&
  51. (+moment(new Date()).format("ss")- +moment(results[0].datetime).format("ss"))<=20)
  52. // console.log('The solution is: ', results[0].MaKH);
  53. {
  54. nomAlert=fiveSClick;
  55.  
  56. }
  57. else nomAlert=singleClick;
  58.  
  59. sendSMS2(nomAlert);
  60. connection.query(`update iot set datetime= "${moment(new Date()).format('YYYY-MM-DD hh:mm:ss')}"`, function(err,results){
  61. console.log("ok")
  62. })
  63. });
  64. });
  65.  
  66. // else if((new Date()).getTime()-oldTime.getTime()>=3000){
  67. // nomAlert=fiveSClick
  68. // }
  69. }
  70.  
  71.  
  72. if(event.clickType == "DOUBLE"){
  73. nomAlert = doubleClick;
  74. sendSMS2(nomAlert);
  75. }
  76.  
  77. if(event.clickType == "LONG"){
  78. nomAlert = longClick;
  79. sendSMS2(nomAlert);
  80. }
  81.  
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement