#!/usr/bin/env python """ Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import datetime as dt import rospy import boto3 from std_msgs.msg import String from std_msgs.msg import Int64MultiArray DETECTIONS_PREFIX = 'detections' # sns = boto3.client('sns', aws_region) # Disable SNS for now, may enable it later if needed class Notifier(): def __init__(self): rospy.loginfo('(Notifier): initializing') self.object_sub = rospy.Subscriber('/detected_objects', String, self.notify) self.pub = rospy.Publisher('/led_cmds', Int64MultiArray, queue_size=1) self.bucket = rospy.get_param('~/notifier/s3_bucket_name') self.search_object = rospy.get_param('~/notifier/search_object') self.article = 'an' if self.search_object.lower()[0] in ['a', 'e', 'i', 'o', 'u'] else 'a' rospy.loginfo('(Notifier) Bucket: {}'.format(self.bucket)) self.s3 = boto3.client('s3') # This will store the details of detected search object self.search_detect_key = '{}/{}.txt'.format(DETECTIONS_PREFIX, self.search_object) # This will store all the most-recent detected objecs self.all_detect_key = '{}/objects.txt'.format(DETECTIONS_PREFIX) self.log_to_s3(all_detected_objects='', search_detected_msg='Setting up') self.started = False def log_to_s3(self, all_detected_objects=None, search_detected_msg=None): if all_detected_objects is not None: # Store the actual value here self.s3.put_object(Body=all_detected_objects, Bucket=self.bucket, Key=self.all_detect_key) if search_detected_msg is not None: # Add a timestamp here msg = '{} @ {} (UTC)'.format(search_detected_msg, dt.datetime.utcnow().strftime('%H:%M:%S')) self.s3.put_object(Body=msg, Bucket=self.bucket, Key=self.search_detect_key) def notify(self, msg): found_objects = msg.data rospy.loginfo('(Notify) Detected {}'.format(found_objects)) try: self.log_to_s3(all_detected_objects=found_objects) if self.search_object in found_objects.split(','): rospy.loginfo('Found what I was looking for: {}'.format(self.search_object)) msg = 'Found {} {}'.format(self.article, self.search_object) self.log_to_s3(search_detected_msg=msg) elif not self.started: self.log_to_s3(search_detected_msg='Started searching') self.started = True except Exception as e: rospy.logerr('Error in Notifier.notify: {}'.format(e)) def main(): rospy.init_node('notifier', log_level=rospy.INFO) notifier = Notifier() rospy.spin() if __name__ == '__main__': main()