#!/usr/bin/env python

import socket
import boto3
from time import sleep

#
# Dump1090 stuff
#

print "Setting up dump1090 connection..."
TCP_IP = '127.0.0.1'
TCP_PORT = 30003
BUFFER_SIZE = 102400

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "...done"


#
# Kinesis stuff
#
STREAMNAME  = "FRDemo";
MAXMSGS = 500;

print "Setting up Kinesis connection..."
kinesis = boto3.client('kinesis',
		region_name='eu-central-1',
		aws_access_key_id='MyAKI',
		aws_secret_access_key='MySAK')
print "...done"


#
# Main loop
#

data = ""

while True:	
	# Read data from socket
	data += s.recv(BUFFER_SIZE)

	# Debug
	#print "Read additional data. Data is now: " + data

	# Prepare the Kinesis request
	records = [];
        while '\n' in data:
		line, data = data.split('\n', 1);
		# Debug
		#print "Extracted a line: " + line

		# Extract partition key. We're using the ICAO Hex ID so all A/C records end up in one shard
		splitline = line.split(',')

		if( ( len(splitline)<5 ) or ( splitline[0] != 'MSG' ) ):
			# It's not a valid message. Skip.
			continue

		icaoid = splitline[4]
		records.append( { 'Data': line,
                        'PartitionKey': icaoid } ) 

	#print "No more lines in data. Data is: " + data

        print "To send to Kinesis: ", len( records ), " records"

	if len( records ) == 0:
		continue

	i=0;
	while i<len(records):
		# Write data to Kinesis
		request = { 'StreamName': STREAMNAME, 'Records': records[i:i+MAXMSGS] }

		print "...sending slice of records ", i, " through ", i+MAXMSGS, "..."
 		# Debug
		#print "Request: " 
		#print request

		response = kinesis.put_records( **request )
		#print "Response: " 
 		#print response
		print "...done"

		i += MAXMSGS


	#sleep( 0.5 );



#
# The bit below is not normally reached. I really should write a signal handler to handle the Ctrl-C
#

s.close()
