Skip to content

Instantly share code, notes, and snippets.

@Jammink2
Last active May 25, 2019 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jammink2/d190644ad9593159bcb7e7b083935aa7 to your computer and use it in GitHub Desktop.
Save Jammink2/d190644ad9593159bcb7e7b083935aa7 to your computer and use it in GitHub Desktop.
iBeacon Producer simulator for Aiven Kafka
# iBeacon Producer simulator for Aiven Kafka
# This script takes, as an argument, the number of messages you want to iterate on.
# example usage: python ibeacon.py 50000
# to run continuously, try: clear && python ibeacon_producer.py 5000000
# jammink@aiven.io
from kafka import KafkaProducer
import sys, random, string, os
from time import sleep
chars = '1234567890'
# get number of iterations from commandline
iterator = int(sys.argv[1])
def generate_uuid(length):
#arrange numbers in chars into random string of length = length
uuid = ''.join([random.choice(chars) for n in xrange(length)])
return uuid
def generate_major(length):
major = ''.join([random.choice(chars) for n in xrange(length)])
return major
def generate_measuredPower():
measured_power = random.choice([-39, -40, -41, -42, -43, -44, -45, -46, -47, -48, -49, -51, -53, -55, -57, -59, -61, -63, -65])
return measured_power
def generate_rssi():
rssi = random.choice([ -21, -27, -28, -29, -30, -31, -37, -41, -47, -51, -57, -61, -67, -77, -89, -95, -100])
return rssi
def generate_accuracy_substring(length):
accuracy_substring = ''.join([random.choice(chars) for n in xrange(length)])
return accuracy_substring
def choose_proximity():
proximity_choice = random.choice(['near', 'immediate', 'far', 'distant'])
return proximity_choice
# method that sends the message
# be sure to copy your ca.pem, service.cert and service.key to local directory from your Aiven Kafka instance.
producer = KafkaProducer(
bootstrap_servers="<URI_to_Aiven_Kafka>.aivencloud.com:27974",
security_protocol="SSL",
ssl_cafile="ca.pem",
ssl_certfile="service.cert",
ssl_keyfile="service.key",
)
for i in range(iterator):
#slow down / throttle produce calls somewhat
sleep(1)
# get values
uuid = str(generate_uuid(length=27))
major = str(generate_major(length=5))
measured_power = str(generate_measuredPower())
rssi = str(generate_rssi())
accuracy_substring = str(generate_accuracy_substring(length=16))
proximity_choice = str(choose_proximity())
message = "message number " + str(i) + " uuid: " + uuid + " major: " + " measured_power: " + measured_power + " rssi: " + rssi + " accuracy_substring: " + accuracy_substring + " proximity_choice: " + proximity_choice
print(message)
print("Sending: {}".format(message))
producer.send("demo-topic", message.encode("utf-8"))
producer.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment