SORACOM LTE-M Buttonを買って遊んでみました
単純にボタンを押したらメール送信ならほぼスマホアプリのAWS 1-Clickだけで実現できました
ただ最初はボタンの1回押し、2回押し、長押しを認識してくれるか情報がなかったので、ネットで検索してAWS Enterprise IoT Buttonのサンプルコードを参考にてコードを書き加えてみました
ボタンを押した状態は「clickType」で取得できるみたいです
ボタンを押した状態は「clickType」で取得できるみたいです
追加でボタンを押した時間を含めたかったんだけど、「reportTime」に入ってる時間は標準時間なので+9時間して使ってます
本当はボタンの残クリック回数を取得したかったのですが、「remainingLife」は残クリック回数か残日数の小さい方を示しているらしいのでif文で分岐することにしました
当分は残日数の方が表示され続けますね...
当分は残日数の方が表示され続けますね...
メールは確認用に2箇所に送信するためにボタンのイベントに入ってる「to_address」ではなく「clickType」の分岐の中で配列を作り直してます
配列を作り直したので、最後の「ses.send_email」の中の'ToAddresses'には「to_address」を直接入れてます
配列を作り直したので、最後の「ses.send_email」の中の'ToAddresses'には「to_address」を直接入れてます
from __future__ import print_function
import boto3
import json
import logging
import dateutil.parser
from datetime import datetime, timedelta
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ses = boto3.client('ses')
def check_email(email):
result = ses.get_identity_verification_attributes(Identities=[email])
attr = result['VerificationAttributes']
if (email not in attr or attr[email]['VerificationStatus'] != 'Success'):
logging.info('Verification email sent. Please verify it.')
ses.verify_email_identity(EmailAddress=email)
return False
return True
def lambda_handler(event, context):
logging.info('Received event: ' + json.dumps(event))
clicktype = event['deviceEvent']['buttonClicked']['clickType']
attributes = event['placementInfo']['attributes']
from_address = attributes['email']
to_address = attributes['email']
if not check_email(from_address):
logging.error('From email is not verified')
return
if not check_email(to_address):
logging.error('To email is not verified')
return
dsn = event['deviceInfo']['deviceId']
remainLife = event['deviceInfo']['remainingLife']
lifeBatttery = int(remainLife) * 15
lifeDays = int(remainLife) * 3.65
if lifeBatttery <= lifeDays:
life = str(int(round(lifeBatttery,0))) + '回'
else:
life = str(int(round(lifeDays,0))) + '日'
click_type = event['deviceEvent']['buttonClicked']['clickType']
reportTime = event['deviceEvent']['buttonClicked']['reportedTime']
ts = datetime.strptime(reportTime[:19], "%Y-%m-%dT%H:%M:%S")
tdelta = timedelta(hours=9)
ts += tdelta
if (clicktype == "SINGLE"):
to_address = []
to_address.append("test1@example")
to_address.append("test2@example")
subject = 'SINGLE'
body ='SINGLE\n' + str(ts) + '\n' + life
elif (clicktype == "DOUBLE"):
to_address = []
to_address.append("test1@example")
to_address.append("test2@example")
subject = 'DOUBLE'
body ='DOUBLE\n' + str(ts) + '\n' + life
elif (clicktype == "LONG"):
to_address = []
to_address.append("test1@example")
to_address.append("test2@example")
subject = 'LONG'
body ='LONG\n' + str(ts) + '\n' + life
ses.send_email(Source=from_address,
Destination={'ToAddresses': to_address},
Message={'Subject': {'Data': subject}, 'Body': {'Text': {'Data': body}}})
LONGイベントには何を設定しようかな?