-
-
Save BobCochran/61062eb5be4a314d651e71f3ccd95d30 to your computer and use it in GitHub Desktop.
Generates sample IoT Data in Python for MongoDB, sample usage: python3 GenerateIoTData.py -s 3 -g 2 -n 200 -m 60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import csv | |
import argparse | |
import pymongo | |
from datetime import date, timedelta | |
from datetime import datetime as dt | |
import threading | |
import sys | |
import json | |
lock = threading.Lock() | |
MONGO_URI = 'mongodb://localhost:27020/IoTData' | |
#MONGO_URI='mongodb+srv://USER:PASSWORD@ATLASCLUSTER/test?retryWrites=true' | |
volatility = 1 | |
sensors = [] | |
gateways = [] | |
def getvalue(old_value): | |
change_percent = volatility * \ | |
random.uniform(0.0, .001) # 001 - flat .01 more | |
change_amount = old_value * change_percent | |
if bool(random.getrandbits(1)): | |
new_value = old_value + change_amount | |
else: | |
new_value = old_value - change_amount | |
return round(new_value, 2) | |
def main(): | |
global args | |
# capture parameters from the command line | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-s", help="number of sensors per gateway") | |
parser.add_argument("-g", help="number of gateways") | |
parser.add_argument("-n", help="number of values per document") | |
parser.add_argument('-m', help="number of minutes to create data") | |
args = parser.parse_args() | |
# fill the symbols array with the ticker symbols we will fill our data with | |
if int(args.s) < 1: | |
args.s = 1 | |
if int(args.s) > 999: | |
args.s = 999 | |
if int(args.g) < 1: | |
args.g = 1 | |
if int(args.n) < 1: | |
args.n = 200 | |
if int(args.m) < 1: | |
args.m = 1 | |
c = pymongo.MongoClient(MONGO_URI) | |
db = c.get_database(name='IoTData') | |
db['SensorData'].drop() | |
gateways = int(args.g) | |
threads = [] | |
for i in range(0, gateways+1): | |
t = threading.Thread(target=worker, args=[int(i+1), int(args.s)]) | |
threads.append(t) | |
t.start() | |
for t2 in threads: | |
t2.join() | |
def worker(gateway, numofsensors): | |
try: | |
n=args.n | |
last_value = round(random.uniform(32, 75), 2) | |
print('Starting Values=' + str(last_value) +'\n') | |
c = pymongo.MongoClient(MONGO_URI) | |
db = c.get_database(name='IoTData') | |
starttime = int(dt.now().strftime('%s')) - \ | |
(60000*int(args.m)) # 60k ms per min | |
endtime = int(dt.now().strftime('%s')) | |
print('Processing Gateway #' + str(gateway)) | |
for time in range(starttime, endtime, 1000): | |
for sn in range(numofsensors, 0, -1): | |
sensornumber = gateway*1000+sn | |
x = getvalue(last_value) | |
sample = {'val': x, 'time': time} | |
last_value = x | |
day = dt.utcfromtimestamp(time) | |
# one doc per day per max number of samples | |
day = day.replace(minute=0, hour=0, second=0) | |
db['SensorData'].update_one({'deviceid': int(gateway), 'sensorid': int(sensornumber), | |
'nsamples': {'$lt': int(n)}, | |
'day': day}, | |
{'$push': {'samples': sample}, | |
'$min': {'first': int(time)}, | |
'$max': {'last': int(time)}, | |
'$inc': {'nsamples': 1}}, upsert=True) | |
except: | |
print('Unexpected error:', sys.exc_info()[0]) | |
raise | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment