def main(): prompt() command_str = raw_input(">>>>") if command_str == "1": time_duration, switch = plan_trip_now() print "time_duration and ifswitch:" print time_duration print switch push_content = "Whether to switch?" + str(switch) + "; ETA: " + str( time_duration / 60) + "mins" client_sns = aws.getClient('sns', 'us-east-1') response = client_sns.publish( TopicArn='arn:aws:sns:us-east-1:486153037878:lab4ass1', Message='message from edison', Subject=push_content, MessageStructure='string', MessageAttributes={ 'string': { 'DataType': 'String', 'StringValue': 'String' #'BinaryValue': 'Binary' } }) elif command_str == "2": subscribe() elif command_str == "3": pass return 0
def imageRec(image): ''' imageRecognition takes in a image file Return the labels and texts detected from the image The labels and texts are ordered by confidence :image: file path :rtype: str() ''' with open(image, 'rb') as f: pic = f.read() rek = aws.getClient('rekognition', 'us-east-1') res1 = rek.detect_labels(Image={"Bytes": pic}) # , MaxLabels=10, MinConfidence=.7) res2 = rek.detect_text(Image={"Bytes": pic}) # , MaxLabels=10, MinConfidence=.7) label = res1["Labels"] text = res2["TextDetections"] # print label label.sort(reverse=True, key=lambda x: x['Confidence']) # print label labellst = [] for item in label: if item != '': labellst.append(item["Name"]) # print nmlst # print text txt = [] text.sort(reverse=True, key=lambda x: x["Confidence"]) for item in text: if item != '' and item["DetectedText"].lower() not in txt: txt.append(item["DetectedText"].lower()) labellst = labellst[:10] txt = txt[:10] d = {"labels": labellst, "texts": txt} return genTxt(d)
def main(trainingData): # Create an instance of S3 class ( S3.py) s3 = S3.S3(trainingData) # Upload data to S3 s3.uploadData() # AWS ML client aml = aws.getClient('machinelearning','us-east-1') # Check if aml has permission to the s3 bucket #s3.checkBucketPolicy() # Create training and evaluation data source trainingId = createDataSourceFromS3(aml,"training",0,70,True) evaluationId = createDataSourceFromS3(aml,"evaluation",70,100,False) # create ml model mlModelId = "ml-mtaPredictions-{0}".format(TIMESTAMP) # NOTE : In ML model target field( label) should be 0 or 1 , any other label will give error aml.create_ml_model(MLModelId=mlModelId, MLModelType="BINARY", TrainingDataSourceId=trainingId) evalMlId = "ev-ml-mtaPredictions-{0}".format(TIMESTAMP) # create an ml evaluation using the evaluation data set aml.create_evaluation(EvaluationId=evalMlId, MLModelId=mlModelId, EvaluationDataSourceId=evaluationId) # Poll to check if model is ready pollUntilReady(aml,evalMlId) print("done") evaluation = aml.get_evaluation(EvaluationId=evalMlId) print("Performance metric on the evaluation dataset: Binary AUC: " + str(evaluation['PerformanceMetrics']['Properties']))
def main(): prompt() command_str = raw_input(">>>>") if command_str == "1": time_duration, switch = plan_trip_now() print "time_duration and ifswitch:" print time_duration print switch push_content = "Whether to switch?" + str(switch) +"; ETA: "+ str(time_duration/60)+ "mins" client_sns = aws.getClient('sns','us-east-1') response = client_sns.publish( TopicArn='arn:aws:sns:us-east-1:486153037878:lab4ass1', Message='message from edison', Subject=push_content, MessageStructure='string', MessageAttributes={ 'string': { 'DataType': 'String', 'StringValue': 'String' #'BinaryValue': 'Binary' } } ) elif command_str == "2": subscribe() elif command_str == "3": pass return 0
def subscribe(): print "Please enter your phone number:" cell_num = raw_input(">>>>") client_sns = aws.getClient('sns', 'us-east-1') response = client_sns.subscribe( TopicArn='arn:aws:sns:us-east-1:486153037878:mtaSub', Protocol='sms', Endpoint=cell_num)
def build_model(data_s3_url, schema_fn, recipe_fn, name, train_percent=70): ml = aws.getClient('machinelearning', 'us-east-1') (train_ds_id, test_ds_id) = create_data_sources(ml, data_s3_url, schema_fn, train_percent, name) ml_model_id = create_model(ml, train_ds_id, recipe_fn, name) eval_id = create_evaluation(ml, ml_model_id, test_ds_id, name) return ml_model_id
def subscribe(): print "Please enter your phone number:" cell_num = raw_input(">>>>") client_sns = aws.getClient('sns','us-east-1') response = client_sns.subscribe( TopicArn='arn:aws:sns:us-east-1:486153037878:mtaSub', Protocol='sms', Endpoint= cell_num)
def run(self): global open_fan_edison global ml_flag app_serverPORT=5555 while True: try: app_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) app_soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) app_soc.bind(('0.0.0.0',app_serverPORT)) app_soc.listen(5) app_conn,app_addr = app_soc.accept() print 'New app connection' while True: try: app_conn.send("Ready"+'\r\n') app_conn.settimeout(2.0) app_msg1 = app_conn.recv(1024) app_conn.settimeout(None) if(app_msg1.find('OK') == -1): app_conn.close() break except socket.timeout: app_conn.close() break if (int(ml_flag) ==1): snsResource = aws.getResource('sns', 'us-east-1') snsClient = aws.getClient('sns', 'us-east-1') subject_content = 'Alert! ' message_content = subject_content topic = snsResource.Topic('**********') snsClient.publish(TopicArn='**********', Message=message_content, Subject=subject_content) alert_msg="Dangerous! Temperature Now is:"+str(temp_now) print ("Alert!!",alert_msg) app_conn.send(alert_msg+'\r\n') print "msg has been sent" app_msg2 = app_conn.recv(1024) appThread = threading.Lock() if appThread.acquire(): ml_flag = 0 #tempory 0 for one minute appThread.release() #If it can run to this sentence, it means that app has sent a msg, #Change here to open fan if (app_msg2.find('OPEN') == -1): app_conn.close() break open_fan_edison =1 print ("change in ML in FAN FLAG", open_fan_edison) print ("Got connection from",app_addr) print (app_msg2) except: app_soc.close() app_conn.close() continue
def build_model(data_s3_url, schema_fn, recipe_fn, name, train_percent=70): """Creates all the objects needed to build an ML Model & evaluate its quality. """ ml = aws.getClient('machinelearning', 'us-east-1') (train_ds_id, test_ds_id) = create_data_sources(ml, data_s3_url, schema_fn, train_percent, name) ml_model_id = create_model(ml, train_ds_id, recipe_fn, name) eval_id = create_evaluation(ml, ml_model_id, test_ds_id, name) return ml_model_id
def main(fileName): # connect to kinesis kinesis = aws.getClient('kinesis','us-east-1') data = [] # list of dictionaries will be sent to kinesis with open(fileName,'rb') as f: dataReader = csv.DictReader(f) for row in dataReader: kinesis.put_record(StreamName=KINESIS_STREAM_NAME, Data=json.dumps(row), PartitionKey='0') break f.close()
def main(): dynamodb = aws.getResource('dynamodb','us-east-1') snsClient = aws.getClient('sns','us-east-1') snsResource = aws.getResource('sns','us-east-1') dynamoTable = dynamodb.Table(DYNAMODB_TABLE_NAME) # Get list of all stopIds stations = buildStationssDB() while True: prompt() sys.stdout.write(">select a command : ") userIn = sys.stdin.readline().strip() if len(userIn) < 1 : print "Command not recognized" else: if userIn == '1': sys.stdout.write(">Enter source : ") sourceStop = sys.stdin.readline().strip() if sourceStop not in stations: sys.stdout.write(">Invalid stop id. Enter a valid stop id") sys.stdout.flush() continue sys.stdout.write(">Enter destination : ") destinationStop = sys.stdin.readline().strip() if destinationStop not in stations: sys.stdout.write(">Invalid stop id. Enter a valid stop id") sys.stdout.flush() continue sys.stdout.write(">Type N for uptown, S for downtown: ") direction = sys.stdin.readline().strip() # Validate direction if direction not in ['N','S']: sys.stdout.write(">Invalid direction. Enter a valid direction") sys.stdout.flush() continue ############################### # YOUR CODE HERE # ############################### elif userIn == '2': sys.stdout.write(">Enter phonenumber") phoneNumber = sys.stdin.readline().strip() ############################### # YOUR CODE HERE # ############################### else: sys.exit()
def main(fileName): # connect to kinesis with open('../utils/key.txt', 'rb') as keyfile: APIKEY = keyfile.read().rstrip('\n') keyfile.close() feedurl = 'http://datamine.mta.info/mta_esi.php?feed_id=1&key=' feedurl = feedurl + APIKEY feed = gtfs_realtime_pb2.FeedMessage() try: with contextlib.closing(urllib2.urlopen(feedurl)) as response: d = feed.ParseFromString(response.read()) except (urllib2.URLError, google.protobuf.message.DecodeError) as e: print "Error while connecting to mta server " + str(e) row = [] nearest = 0 #nearest_tripId ='' for entity in feed.entity: if entity.vehicle and entity.vehicle.trip.trip_id and entity.vehicle.stop_id: vehicleData = entity.vehicle tripId = vehicleData.trip.trip_id stopId = vehicleData.stop_id if (tripId[7] == '1') and (stopId[3] == 'S') and (int( str(stopId)[0:3]) <= 117) and (int(str(stopId)[0:3]) > nearest): nearest = int(stopId[0:3]) nearest_tripId = tripId push = int(str(nearest_tripId)[0:6]) / 100 row.append(push) row.append(datetime.today().weekday()) with open(fileName, 'a') as csvfile: spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) spamwriter.writerow(row) kinesis = aws.getClient('kinesis', 'us-east-1') data = [] # list of dictionaries will be sent to kinesis with open(fileName, 'rb') as f: dataReader = csv.DictReader(f) for row in dataReader: #print row kinesis.put_record(StreamName=KINESIS_STREAM_NAME, Data=json.dumps(row), PartitionKey='0') break f.close()
def main(fileName): print "1" # connect to kinesis kinesis = aws.getClient('kinesis','us-east-1') data = [] # list of dictionaries will be sent to kinesis print "2" with open(fileName,'rb') as f: print "hello" dataReader = csv.DictReader(f) for row in dataReader: print row #kinesis.put_record(StreamName=mtaStream, Data=json.dumps(row), PartitionKey='0') break f.close()
def main(fileName): # Ideally you should write code to get data from mta feed,clean it up and send to kinesis # Kinesis triggers a lambda function whcih will send the data to aws ML for classification # But for demo I am going to use the csv file given and in aws lambda we remove the label before # sending it to aws ml # connect to kinesis kinesis = aws.getClient('kinesis','us-east-1') data = [] # list of dictionaries will be sent to kinesis with open(fileName,'rb') as f: dataReader = csv.DictReader(f) for row in dataReader: kinesis.put_record(StreamName=KINESIS_STREAM_NAME, Data=json.dumps(row), PartitionKey='0') break f.close()
def main(fileName): # connect to kinesis with open('../utils/key.txt', 'rb') as keyfile: APIKEY = keyfile.read().rstrip('\n') keyfile.close() feedurl = 'http://datamine.mta.info/mta_esi.php?feed_id=1&key=' feedurl = feedurl + APIKEY feed = gtfs_realtime_pb2.FeedMessage() try: with contextlib.closing(urllib2.urlopen(feedurl)) as response: d = feed.ParseFromString(response.read()) except (urllib2.URLError, google.protobuf.message.DecodeError) as e: print "Error while connecting to mta server " +str(e) row=[] nearest = 0 #nearest_tripId ='' for entity in feed.entity: if entity.vehicle and entity.vehicle.trip.trip_id and entity.vehicle.stop_id: vehicleData = entity.vehicle tripId = vehicleData.trip.trip_id stopId = vehicleData.stop_id if (tripId[7] == '1') and (stopId[3] == 'S') and (int(str(stopId)[0:3]) <= 117) and (int(str(stopId)[0:3]) > nearest): nearest = int(stopId[0:3]) nearest_tripId = tripId push = int(str(nearest_tripId)[0:6])/100 row.append(push) row.append(datetime.today().weekday()) with open(fileName, 'a') as csvfile: spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) spamwriter.writerow(row) kinesis = aws.getClient('kinesis','us-east-1') data = [] # list of dictionaries will be sent to kinesis with open(fileName,'rb') as f: dataReader = csv.DictReader(f) for row in dataReader: #print row kinesis.put_record(StreamName=KINESIS_STREAM_NAME, Data=json.dumps(row), PartitionKey='0') break f.close()
def main(): prompt() command_str = raw_input(">>>>") if command_str == "1": start_stop_id = raw_input("Enter your source station: ") destination_stop_id = raw_input("Enter your destination station") if (int(start_stop_id) < int(destination_stop_id)): direction = 'S' start_stop_id = start_stop_id + 'S' destination_stop_id = destination_stop_id + 'S' else: direction = 'N' start_stop_id = start_stop_id + 'N' destination_stop_id = destination_stop_id + 'N' time_duration, switch = plan_trip_now(start_stop_id, destination_stop_id, direction) print "time_duration and whether to take Line 2,3:" print time_duration print switch push_content = "Whether to take Train 2/3?" + str( switch) + "; ETA: " + str(int(time_duration / 6) / 10) + "mins" client_sns = aws.getClient('sns', 'us-east-1') response = client_sns.publish( TopicArn='arn:aws:sns:us-east-1:486153037878:lab4ass1', Message='message from edison', Subject=push_content, MessageStructure='string', MessageAttributes={ 'string': { 'DataType': 'String', 'StringValue': 'String' #'BinaryValue': 'Binary' } }) elif command_str == "2": subscribe() elif command_str == "3": pass return 0
def main(): with open('./key.txt', 'rb') as keyfile: APIKEY = keyfile.read().rstrip('\n') keyfile.close() newRecord = mtaUpdates.mtaUpdates(APIKEY) station = prompt() data = getUpdates(newRecord,station) if len(data) < 1: print "Sorry, there is no available prediction at this time" return # connect to kinesis kinesis = aws.getClient('kinesis','us-east-1') # with open('trainRecordFinal.csv','rb') as f: # dataReader = csv.DictReader(f) # for row in dataReader: # print row response = kinesis.describe_stream( StreamName=KINESIS_STREAM_NAME, ) print data kinesis.put_record(StreamName=KINESIS_STREAM_NAME, Data=json.dumps(data), PartitionKey='0')
def main(): prompt() command_str = raw_input(">>>>") if command_str == "1": start_stop_id = raw_input("Enter your source station: ") destination_stop_id = raw_input("Enter your destination station") if (int(start_stop_id) < int(destination_stop_id)): direction = 'S' start_stop_id = start_stop_id + 'S' destination_stop_id = destination_stop_id + 'S' else: direction = 'N' start_stop_id = start_stop_id + 'N' destination_stop_id = destination_stop_id + 'N' time_duration, switch = plan_trip_now(start_stop_id, destination_stop_id, direction) print "time_duration and whether to take Line 2,3:" print time_duration print switch push_content = "Whether to take Train 2/3?" + str(switch) +"; ETA: "+ str(int(time_duration/6)/10)+ "mins" client_sns = aws.getClient('sns','us-east-1') response = client_sns.publish( TopicArn='arn:aws:sns:us-east-1:486153037878:lab4ass1', Message='message from edison', Subject=push_content, MessageStructure='string', MessageAttributes={ 'string': { 'DataType': 'String', 'StringValue': 'String' #'BinaryValue': 'Binary' } } ) elif command_str == "2": subscribe() elif command_str == "3": pass return 0
# Hayato Nakamura # hn2357 # Copyright 2020 Hayato Nakamura import sys, time import numpy as np from collections import OrderedDict from decimal import * from datetime import datetime from PIL import Image sys.path.append('amazon/') import aws dynamodb = aws.getResource('dynamodb', 'us-east-1') s3 = aws.getClient('s3', 'us-east-1') rekog = aws.getClient('rekognition', 'us-east-1') def check_picture(attributes = ['ALL']): name = 'temp_5_20' + '.jpg' response = rekog.detect_faces( Image={ "S3Object": { "Bucket": 'hayatopia', "Name": name, } }, Attributes=attributes, ) for face in response['FaceDetails']: print(face) boundingBox = face['BoundingBox']
# Creating aws machine learning model # This program uploads the finalData.csv file to S3, and used it as a data source to train a binary # classification model import time,sys,random import boto3 import S3 sys.path.append('utils') import aws client = aws.getClient('machinelearning','us-east-1') TIMESTAMP = time.strftime('%Y-%m-%d-%H-%M-%S') S3_BUCKET_NAME = "finalBaochan" S3_FILE_NAME = 'training_instrumental.csv' S3_URI = "s3://{0}/{1}".format(S3_BUCKET_NAME, S3_FILE_NAME) DATA_SCHEMA =open("Instrumentalness.csv.schema").read() DATA_SOURCE_ID = 'Instrumentalness0' ML_MODEL_ID = 'Instrumentalness0' #EVALUATION_ID = 'lab5Evaluation_13' testvariable = S3.S3(S3_FILE_NAME) #testvariable.uploadData() response = client.create_data_source_from_s3( DataSourceId=DATA_SOURCE_ID, DataSourceName='finaldataInstrumentalness', DataSpec={
# boto3 s3 import boto3 import aws from boto3.dynamodb.conditions import Key,Attr # ml import predict from sqlalchemy import * # initial s3 s3 = aws.getResource('s3', '') dynamodb = aws.getResource('dynamodb', "") curtime = calendar.timegm(time.gmtime()) """sns""" client = aws.getClient("","") topic_arn = "" TIMEPOINTER = None expire_table = dynamodb.Table('fruit-expire') def download_img(filename): s3.meta.client.download_file('iotfruit',filename, filename) def delete_img(filename): os.remove(filename) s3.Object('iotfruit',filename).delete()
def __init__(self): self.ml = aws.getClient('machinelearning', 'us-east-1')
import boto3 import mraa import aws import math temp_pin_number=1 temp = mraa.Aio(temp_pin_number) temperature = float(temp.read()) R = 1023.0/(temperature)-1.0 R = 100000.0*R temperature=1.0/(math.log(R/100000.0)/4275+1/298.15)-273.15 temperature = str(temperature) client_sns = aws.getClient('sns','us-east-1'); response = client_sns.publish( TopicArn='arn:aws:sns:us-east-1:486153037878:lab4ass1', Message='message from edison', Subject=temperature, MessageStructure='string', MessageAttributes={ 'string': { 'DataType': 'String', 'StringValue': 'String' #'BinaryValue': 'Binary' } } )
import aws from datetime import datetime import calendar from PIL import Image # postgresql # dynamoDB dynamodb = aws.getResource('dynamodb', "us-east-1") # S3 s3 = aws.getResource('s3', 'us-east-1') # s3 = aws.getClient('s3', 'us-east-1') # sns snsClient = aws.getClient("sns","us-east-1") # topic_arn = "arn:aws:sns:us-east-1:810588570945:fruit-alert" import sys MACHINE = sys.argv[1] print('machine{}'.format(MACHINE)) def create_topic(): table = dynamodb.Table('fruit-topic') response = table.get_item(Key={'machine' : MACHINE}) try: response['Item'] except: topic = snsClient.create_topic( Name = str(MACHINE),
import time, sys, random import boto3 import S3 sys.path.append('../utils') import aws TIMESTAMP = time.strftime('%Y-%m-%d-%H-%M-%S') S3_BUCKET_NAME = "mtaedisondatax007" S3_FILE_NAME = 'trainingData.csv' S3_URI = "s3://{0}/{1}".format(S3_BUCKET_NAME, S3_FILE_NAME) DATA_SCHEMA = "aml.csv.schema" machineLearningClient = aws.getClient('machinelearning', 'us-east-1') # Create a Data Source dataSourceResponse = machineLearningClient.create_data_source_from_s3( DataSourceId='ds_id' + TIMESTAMP, DataSourceName='ml_data1', DataSpec={ 'DataLocationS3': S3_URI, 'DataRearrangement': '{\"splitting\":{\"percentBegin\":10,\"percentEnd\":60}}', 'DataSchemaLocationS3': "s3://{0}/{1}".format(S3_BUCKET_NAME, DATA_SCHEMA) }, ComputeStatistics=True) # Create an ML model with a data source
python realtime.py ml_model_id --deleteEndpoint Multi-word text attributes can be specified like: python realtime.py ml-12345678901 "textVar=Multiple words grouped together" numericVar=123 """ import boto import boto3 import json import sys import time import aws danceclient = aws.getClient('machinelearning','us-east-1') instrumentclient = aws.getClient('machinelearning','us-east-1') acousticclient = aws.getClient('machinelearning','us-east-1') energyclient = aws.getClient('machinelearning','us-east-1') genreclient = aws.getClient('machinelearning','us-east-1') #ml_model_id = "MLModelId='finalmodel2'" """ def parse_args_to_dict(argv): ##Returns a record as a python dict by parsing key=value pairs ##from the command line record = {} for kv in argv: try: (k, v) = kv.split('=')
def __init__(self): self.snsClient = aws.getClient('sns', 'us-east-1') self.setTopic(self.topicName)
def main(): dynamodb = aws.getResource('dynamodb','us-east-1') snsClient = aws.getClient('sns','us-east-1') snsResource = aws.getResource('sns','us-east-1') dynamoTable = dynamodb.Table(DYNAMODB_TABLE_NAME) # Get list of all stopIds stations = buildStationssDB() while True: prompt() sys.stdout.write(">select a command : ") userIn = sys.stdin.readline().strip() if len(userIn) < 1 : print "Command not recognized" else: if userIn == '1': sys.stdout.write(">Enter source : ") sourceStop = sys.stdin.readline().strip() if sourceStop not in stations: sys.stdout.write(">Invalid stop id. Enter a valid stop id") sys.stdout.flush() continue sys.stdout.write(">Enter destination : ") destinationStop = sys.stdin.readline().strip() if destinationStop not in stations: sys.stdout.write(">Invalid stop id. Enter a valid stop id") sys.stdout.flush() continue sys.stdout.write(">Type N for uptown, S for downtown: ") direction = sys.stdin.readline().strip() # Validate direction if direction not in ['N','S']: sys.stdout.write(">Invalid direction. Enter a valid direction") sys.stdout.flush() continue response = getLocalTrains(dynamoTable,direction,'1',int(sourceStop[:-1])) earliestTrainData = getEarliestTrain(response,sourceStop) print "local train is ",earliestTrainData['tripId'] timeToReachExpressStation = getTimeToReachDestination(earliestTrainData,'120S') # get list of 2 and 3 trains # List of trains with route id 2 and are either at 96th or before it # Note that 2 train's stop starts from 201S and goes till 227S and merges with 96th street(120S) # range2 is a list of all stations on 2 train route before and including 96th range2 = range(201,228) range2.append(120) expressTrains2 = getExpress(dynamoTable,direction,'2',range2) # List of trains with route id 3 and are either at 96th or before it # Note that 3 train's stop starts from 301S and goes till 302S and merges with 2 train lane at 135th street lenox 224S # range3 is a list of all stations on 3 train route before and including 96th range3 = range(224,228) range3.extend([301,302,120]) expressTrains3 = getExpress(dynamoTable,direction,'3',range3) expressTrains = expressTrains2 + expressTrains3 # filter the trains that reach 96th before the earliest one reaches. for e in expressTrains: stops = json.loads(e['futureStopData']) if stops['120S'][0]['arrivalTime'] - timeToReachExpressStation < 10: expressTrains.remove(e) timeTakenByLocal = getTimeToReachDestination(earliestTrainData,destinationStop) print "time taken by local to 96",timeToReachExpressStation # If there is an express train find the time at which it reaches time square if expressTrains: expressTrainData = getEarliestTrain(expressTrains,'120S') print expressTrainData['tripId'] print "time taken by express to 96",getTimeToReachDestination(expressTrainData,'120S') timeTakenByExpress = getTimeToReachDestination(expressTrainData,destinationStop) timeTakenByLocal = getTimeToReachDestination(earliestTrainData,destinationStop) print "time taken by local to 42nd",timeTakenByLocal print "Time taken by expres to 42nd",timeTakenByExpress if timeTakenByLocal > timeTakenByExpress: print ">Switch to express" else: print ">Do not switch to express" else: print "No express at this time" print ">Do not switch to express" elif userIn == '2': sys.stdout.write(">Enter phonenumber") phoneNumber = sys.stdin.readline().strip() phoneNumber = '1'+phoneNumber topic = snsResource.Topic('arn:aws:sns:us-east-1:013162866752:mtaSub') topic.subscribe(Protocol='sms', Endpoint=phoneNumber) snsClient.publish(TopicArn='arn:aws:sns:us-east-1:013162866752:mtaSub', Message='You have subscribed to mta data', Subject='MTAFEED') else: sys.exit()
def __init__(self): self.rek = aws.getClient('rekognition', 'us-east-1')
# Hayato Nakamura # hn2357 # Copyright 2020 Hayato Nakamura from __future__ import print_function #compatible with python 2.7 import sys, time import numpy as np from picamera import PiCamera import aws from collections import OrderedDict from decimal import * from datetime import datetime #import threading dynamodb = aws.getResource('dynamodb', 'us-east-1') s3 = aws.getClient('s3', 'us-east-1') def search(mat): # Scan the matrix for y in range(24 - 3): for x in range(32 - 3): window = mat[y:y + 3, x:x + 3] print(window) print(np.mean(window)) if (np.mean(window) > 36 and np.mean(window) < 45): print("\n\nHIT\n\n") return True return False def process_pic(name): camera = PiCamera()
return month_string #Command Type- getPurchaseHistory- Purpose: To get a summary of Purchase History in a specific month if (request.json["type"] == "getPurchaseHistory"): uniquequantity = {} uniqueprices = {} uniquenames = [] purchaseHistory = mongo.db.purchaseHistory purchaseHistoryList = purchaseHistory.find( {'month': request.json["msg"]}) month_string = "" for item in purchaseHistoryList: if item['itemname'] not in uniquequantity: uniquenames.append(item['itemname']) uniquequantity[item['itemname']] = int(item['quantity']) uniqueprices[item['itemname']] = item['price'] else: uniquequantity[item['itemname']] += int(item['quantity']) for i in uniquenames: month_string += str( uniquequantity[i]) + 'X $' + uniqueprices[i] + ',' + i + ';' print month_string return month_string return "done" if __name__ == '__main__': snsClient = aws.getClient('sns', 'us-east-1') app.run(debug=True, host="0.0.0.0", port=80)
import boto3 import mraa import aws import math temp_pin_number = 1 temp = mraa.Aio(temp_pin_number) temperature = float(temp.read()) R = 1023.0 / (temperature) - 1.0 R = 100000.0 * R temperature = 1.0 / (math.log(R / 100000.0) / 4275 + 1 / 298.15) - 273.15 temperature = str(temperature) client_sns = aws.getClient('sns', 'us-east-1') response = client_sns.publish( TopicArn='arn:aws:sns:us-east-1:486153037878:lab4ass1', Message='message from edison', Subject=temperature, MessageStructure='string', MessageAttributes={ 'string': { 'DataType': 'String', 'StringValue': 'String' #'BinaryValue': 'Binary' } })
def main(fileName): # connect to kinesis kinesis = aws.getClient('kinesis', 'us-east-1') data = [] # list of dictionaries will be sent to kinesis listcsv = list() l = [] try: while (True): tu, timest = importmta.getTripUpdates() print 'Entering feed\n' listlcl = [] listexp = [] for t in tu: if t.routeId == '1' and t.direction == 'S': if t.vehicleData[0] == '120S' and t.vehicleData[ 1] == 'STOPPED_AT': time96 = t.vehicleData[2] time96_dt = datetime.fromtimestamp(time96, TIMEZONE) elif '120S' in t.futureStops.keys(): time96 = t.futureStops['120S'][0] time96_dt = datetime.fromtimestamp(time96, TIMEZONE) else: time96 = 'None' time96_dt = 'None' if t.vehicleData[0] == '127S' and t.vehicleData[ 1] == 'STOPPED_AT': time42 = t.vehicleData[2] time42_dt = datetime.fromtimestamp(time42, TIMEZONE) elif '127S' in t.futureStops.keys(): time42 = t.futureStops['127S'][0] time42_dt = datetime.fromtimestamp(time42, TIMEZONE) else: time42 = 'None' time42_dt = 'None' if timest.strftime("%a") == 'Sun' or timest.strftime( "%a") == 'Sat': day = 'Weekend' else: day = 'Weekday' route = 'local' #mtaData.put_item(data={'tripId':t.tripId,'timeStamp':str(int(str(timest)[14:16])+int(str(timest)[11:13])*60),'Day of the week':day,'route':route,'Time at 96th':time96,'Time at 42nd':time42},overwrite=True) if time96_dt != 'None' and time42_dt != 'None': l = [ str(t.tripId), str( int(str(timest)[14:16]) + int(str(timest)[11:13]) * 60), str(day), str(route), str( int(str(time96_dt)[14:16]) + int(str(time96_dt)[11:13]) * 60) if time96_dt != 'None' else 'None', str( int(str(time42_dt)[14:16]) + int(str(time42_dt)[11:13]) * 60) if time42_dt != 'None' else 'None' ] listlcl.append(l) elif (t.routeId == '2' or t.routeId == '3') and t.direction == 'S': if t.vehicleData[0] == '120S' and t.vehicleData[ 1] == 'STOPPED_AT': time96 = t.vehicleData[2] time96_dt = datetime.fromtimestamp(time96, TIMEZONE) elif '120S' in t.futureStops.keys(): time96 = t.futureStops['120S'][0] time96_dt = datetime.fromtimestamp(time96, TIMEZONE) else: time96 = 'None' time96_dt = 'None' if t.vehicleData[0] == '127S' and t.vehicleData[ 1] == 'STOPPED_AT': time42 = t.vehicleData[2] time42_dt = datetime.fromtimestamp(time42, TIMEZONE) elif '127S' in t.futureStops.keys(): time42 = t.futureStops['127S'][0] time42_dt = datetime.fromtimestamp(time42, TIMEZONE) else: time42 = 'None' time42_dt = 'None' if timest.strftime("%a") == 'Sun' or timest.strftime( "%a") == 'Sat': day = 'Weekend' else: day = 'Weekday' route = 'exp' #mtaData.put_item(data={'tripId':t.tripId,'timeStamp':str(int(str(timest)[14:16])+int(str(timest)[11:13])*60),'Day of the week':day,'route':route,'Time at 96th':time96,'Time at 42nd':time42},overwrite=True) if time96_dt != 'None' and time42_dt != 'None': l = [ str(t.tripId), str( int(str(timest)[14:16]) + int(str(timest)[11:13]) * 60), str(day), str(route), str( int(str(time96_dt)[14:16]) + int(str(time96_dt)[11:13]) * 60) if time96_dt != 'None' else 'None', str( int(str(time42_dt)[14:16]) + int(str(time42_dt)[11:13]) * 60) if time42_dt != 'None' else 'None' ] listexp.append(l) else: pass for i in listlcl: for j in listexp: if i[4] != 'None' and j[4] != 'None': if int(i[4]) <= int(j[4]): row = { 'timest': i[1], 'day': i[2], 'time96lcl': i[4], 'time96exp': j[4] } kinesis.put_record( StreamName=KINESIS_STREAM_NAME, Data=json.dumps(row), PartitionKey='0') print 'Done Entering\n' except KeyboardInterrupt: exit
def main(): dynamodb = aws.getResource('dynamodb', 'us-east-1') snsClient = aws.getClient('sns', 'us-east-1') snsResource = aws.getResource('sns', 'us-east-1') # Create topic if it doesn't already exist createResponse = snsClient.create_topic(Name='mtaSub') topicArn = createResponse['TopicArn'] dynamoTable = dynamodb.Table(DYNAMODB_TABLE_NAME) # Get list of all stopIds stations = buildStationssDB() while True: prompt() sys.stdout.write(">select a command : ") userIn = sys.stdin.readline().strip() if len(userIn) < 1: print "Command not recognized" else: if userIn == '1': sys.stdout.write(">Enter source : ") sourceStop = sys.stdin.readline().strip() if sourceStop not in stations: sys.stdout.write(">Invalid stop id. Enter a valid stop id") sys.stdout.flush() continue if not isValidSourceStation(sourceStop): sys.stdout.write( '>You must start north of 96th street or at Times Sq.') sys.stdout.flush() continue sys.stdout.write(">Enter destination : ") destinationStop = sys.stdin.readline().strip() if destinationStop not in stations: sys.stdout.write(">Invalid stop id. Enter a valid stop id") sys.stdout.flush() continue if not isValidDestinationStation(sourceStop): sys.stdout.write( '>You must end north of 96th street or at Times Sq.') sys.stdout.flush() continue if sourceStop == destinationStop: sys.stdout.write('>You are already at the station!') sys.stdout.flush() continue sys.stdout.write(">Type N for uptown, S for downtown: ") direction = sys.stdin.readline().strip() # Validate direction if direction not in ['N', 'S']: sys.stdout.write( ">Invalid direction. Enter a valid direction") sys.stdout.flush() continue # You can figure out the direction from the source and destinations actualDirection = getDirectionOfStops(stations, sourceStop, destinationStop) if actualDirection != direction: sys.stdout.write(">Wrong direction!") sys.stdout.flush() continue if sourceStop == timesSquareStopId: sourceStop = getDirectedStopId(sourceStop, direction) destinationStop = getDirectedStopId( destinationStop, direction) # Find incoming local trains incomingLocals = getIncomingTrainsToStation( dynamoTable, direction, '1', sourceStop) print "Local trains incoming to ", sourceStop for l in incomingLocals: print l['tripId'] earliestLocal = getEarliestTrain(incomingLocals, sourceStop, direction) print "The next local train coming is ", earliestLocal[ 'tripId'] print "It arrives at ", earliestLocal['futureStopData'][ sourceStop][0]['arrivalTime'] incomingExpresses = getIncomingTrainsToStation( dynamoTable, direction, ['2', '3'], sourceStop) print "Express trains incoming to ", sourceStop for e in incomingExpresses: print e["tripId"] earliestExpress = getEarliestTrain(incomingExpresses, sourceStop, direction) print "The next express train coming is ", earliestExpress[ 'tripId'] print "It arrives at ", earliestExpress['futureStopData'][ sourceStop][0]['arrivalTime'] # Go directly on local localArrivalTime = getTimeToReachFutureStop( earliestLocal, destinationStop, direction) print "The local train will arrive at destination ", destinationStop, " at ", localArrivalTime # Stop at 96th and get off the express expressTo96thTime = getTimeToReachFutureStop( earliestExpress, ninetySixthStopId, direction) # Next local arriving at 96th after that incomingLocals = getIncomingTrainsToStation( dynamoTable, direction, '1', ninetySixthStopId) earliestLocal = getEarliestTrain(incomingLocals, ninetySixthStopId, direction, expressTo96thTime) expressAndLocalArrivalTime = getTimeToReachFutureStop( earliestLocal, destinationStop, direction) print "If you take an express, you must switch at ", ninetySixthStopId print "Express will be at your destination at ", expressAndLocalArrivalTime if localArrivalTime < expressAndLocalArrivalTime: print "Take the next local" sendNotification(snsClient, topicArn, "Take Local") else: print "Take the express to 96th" sendNotification(snsClient, topicArn, "Take Express") continue sourceStop = getDirectedStopId(sourceStop, direction) destinationStop = getDirectedStopId(destinationStop, direction) ninetySixthStop = getDirectedStopId(ninetySixthStopId, direction) # The next train coming at your source station potentialTrains = getIncomingTrainsToStation( table=dynamoTable, direction=direction, routeId='1', stopId=sourceStop) currentLocalTrain = getEarliestTrain(trains=potentialTrains, stopId=sourceStop, direction=direction) print "The next local train to ", sourceStop, " will arrive at ", \ currentLocalTrain['futureStopData'][sourceStop][0]['arrivalTime'] # Time it gets to 96th street arrival96th = currentLocalTrain['futureStopData'][ ninetySixthStop][0]['arrivalTime'] print "It will arrive at 96th Street at ", arrival96th # Next local train when you arrive at 96th print "When you get to 96th street . . . " localsTo96th = getIncomingTrainsToStation( dynamoTable, direction, '1', ninetySixthStop) # Task 1: Print trip IDs for l in localsTo96th: print l['tripId'] local96th = getEarliestTrain(localsTo96th, ninetySixthStop, direction, arrival96th) # Task 3 print "Earliest Local Train: ", local96th['tripId'] print "It will arrive at ", local96th['futureStopData'][ ninetySixthStop][0]['arrivalTime'] # Next express train when you arrive at 96th expressesTo96th = getIncomingTrainsToStation( dynamoTable, direction, ['2', '3'], ninetySixthStop) # Task 2: Print express trip IDs for e in expressesTo96th: print e['tripId'] express96th = getEarliestTrain(expressesTo96th, ninetySixthStop, direction, arrival96th) print "Earliest Express Train: ", express96th['tripId'] print "It will arrive at ", express96th['futureStopData'][ ninetySixthStop][0]['arrivalTime'] # local time to get to 42nd timeToDestinationLocal = currentLocalTrain['futureStopData'][ destinationStop][0]['arrivalTime'] timeToDestinationExpress = express96th['futureStopData'][ destinationStop][0]['arrivalTime'] # Task 5 print "Local will arrive at destination ", destinationStop, " at ", timeToDestinationLocal print "Express will arrive at destination ", destinationStop, " at ", timeToDestinationExpress # Task 6a if timeToDestinationLocal > timeToDestinationExpress: print "Switch over to express" sendNotification(snsClient, topicArn, "Switch to express @ 96th") else: print "Stay with Local" sendNotification(snsClient, topicArn, "Stay on local @ 96th") elif userIn == '2': sys.stdout.write(">Enter Phone No. ") phone_number = sys.stdin.readline().strip() # Task 9 addSubscriber(snsClient, topicArn, phone_number) elif userIn == '3': print "Bye, Felicia . . ." exit(0) else: print "Unknown command. Terminating." sys.exit(6)