Created
December 17, 2014 06:22
-
-
Save miyamoto-daisuke/fd626ecf8a5d2e2737b8 to your computer and use it in GitHub Desktop.
BBWB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("start BiriBiriWorkerBox"); | |
// ==== config | |
var accountId = "123456789012"; | |
var cognitoRoleArn = "arn:aws:iam::123456789012:role/Cognito_WorkerBoxUnauth_DefaultRole"; | |
var cognitoIdentityPoolId = "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; | |
var queueUrl = "https://sqs.ap-northeast-1.amazonaws.com/123456789012/workerbox"; | |
// ==== mraa | |
var mraa = require('mraa'); | |
console.log('MRAA Version: ' + mraa.getVersion()); | |
var shockPin = new mraa.Gpio(5); | |
shockPin.dir(mraa.DIR_OUT); | |
var ledPin = new mraa.Gpio(6); | |
ledPin.dir(mraa.DIR_OUT); | |
var buzzerPin = new mraa.Gpio(7); | |
buzzerPin.dir(mraa.DIR_OUT); | |
var onBoardLedPin = new mraa.Gpio(13); | |
onBoardLedPin.dir(mraa.DIR_OUT); | |
onBoardLedPin.write(0); | |
function outputPinLow(pin) { | |
if (pin === null) return; | |
pin.write(0); | |
} | |
function outputPinHigh(pin) { | |
if (pin === null) return; | |
pin.write(1); | |
} | |
function pinHighTimeout(pin, duration) { | |
outputPinHigh(pin); | |
setTimeout(function() { outputPinLow(pin); }, duration); | |
} | |
function errorHandled(dataHandler) { | |
return function(err, data) { | |
if (err) { | |
onBoardLedPin.write(1); | |
console.log(err, err.stack); | |
setTimeout(function(){onBoardLedPin.write(0);}, 2000); | |
} else if (typeof dataHandler === 'function') { | |
dataHandler(data); | |
} else { | |
console.log("unknown handler: " + dataHandler); | |
} | |
}; | |
} | |
// ==== AWS | |
var AWS = require('aws-sdk'); | |
var awsRegion = "us-east-1"; | |
var cognitoParams = { | |
AccountId: accountId, | |
RoleArn: cognitoRoleArn, | |
IdentityPoolId: cognitoIdentityPoolId | |
}; | |
AWS.config.region = awsRegion; | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams); | |
AWS.config.credentials.get(errorHandled(function() { | |
console.log("Cognito Identity Id: " + AWS.config.credentials.identityId); | |
})); | |
var sqs = new AWS.SQS({region: 'ap-northeast-1'}); | |
function readMessage() { | |
console.log("readMessage"); | |
sqs.receiveMessage({ | |
QueueUrl: queueUrl, | |
MaxNumberOfMessages: 1, | |
VisibilityTimeout: 30, | |
WaitTimeSeconds: 20 | |
}, errorHandled(function(data) { | |
if (data && data.Messages && typeof data.Messages[0] !== 'undefined' && typeof data.Messages[0].Body !== 'undefined') { | |
var message = data.Messages[0]; | |
console.log("message = " + message.Body); | |
if (message.Body.indexOf('led') > -1) { | |
pinHighTimeout(ledPin, 2000); | |
} | |
if (message.Body.indexOf('buzzer') > -1) { | |
pinHighTimeout(buzzerPin, 2000); | |
} | |
if (message.Body.indexOf('shock') > -1) { | |
pinHighTimeout(shockPin, 1000); | |
} | |
sqs.deleteMessage({ | |
QueueUrl : queueUrl, | |
ReceiptHandle: message.ReceiptHandle | |
}, errorHandled(function(){})); | |
} | |
readMessage(); | |
})); | |
} | |
readMessage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment