<jupyter_start><jupyter_text>Kafka Producer Test Script Creates mock readings for testing the back end processing if an IoTBB device is not available.This script allows you to test out the functionality of the Spark Streaming and machine learing components of the project.<jupyter_code>import time
from kafka import KafkaProducer
import json
import random
import csv
import uuid
import datetime
"""
Usage:  bin/spark-submit ~/spark/kafkaProducrerTest.py
"""
producer = KafkaProducer(bootstrap_servers='localhost:9092')
pitch = 0
position = 0<jupyter_output><empty_output><jupyter_text>The "getRandomPitch" method will generate a mock "reading" between a min and max range.  The position can be arbitrarily defined or if you want to be very accurate, the debug output of the machine learning model can be used here.<jupyter_code>def getRandomPitch(min,max):
    pitch = random.uniform(min,max)
    if pitch < -25:
        position=2
    elif pitch < -15:
        position=1
    else:
        position=0
    return pitch, position<jupyter_output><empty_output><jupyter_text>The "sendSensorReadings" method creates mock "actual" readings.  A random reading is generated and a JSON packet is created and sent to Kafka using the "LumbarSensorReadings" topic.<jupyter_code>def sendSensorReadings(iterations):
	for i in range(iterations):
		readingID = str(uuid.uuid4())
                pitch, position = getRandomPitch(-50,25)
                if pitch <= -15:
                        if pitch <= -25:
                                position = 2.0
                        elif pitch > -25:
                                if pitch <= -15:
                                        position = 1.0
		packet = {
				"readingID":readingID,
				"deviceID":"5d681c54e66ff4a5654e55c6d5a5b54",
				"readingTime":datetime.datetime.now().isoformat(),
				"metricTypeID":6,
				"uomID":4,
				"actual":{"y":-30,"p":pitch,"r":120},
				"setPoints":{"y":25,"p":45,"r":100},
				"prevAvg":{"y":15,"p":40,"r":88}
			 }
		print(packet) 
		producer.send('LumbarSensorReadings', json.dumps(packet))<jupyter_output><empty_output><jupyter_text>The "sendSensorTrainingReadings" method creates mock Training readings that are subesquently used in the machine learning model generation.  A random reading is generated with a known posture and a JSON packet is created and sent to Kafka using the "LumbarSensorTrainingReadings" topic.<jupyter_code>def sendSensorTrainingReadings(iterations):
	
	for i in range(iterations): 
		pitch, position = getRandomPitch(-50,25)
		if pitch <= -15:
			if pitch <= -25:
				position = 2.0
			elif pitch > -25:
				if pitch <= -15:
					position = 1.0
		packet = {
					"deviceID":"5d681c54e66ff4a5654e55c6d5a5b54",
					"positionID":position,
					"readingTime":"2016-07-25T15:45:07.12",
					"metricTypeID":6,
					"uomID":4,
					"actual":{"y":18,"p":pitch,"r":120},
					"setPoints":{"y":25,"p":45,"r":100},
					"prevAvg":{"y":15,"p":40,"r":88}
				 }
		producer.send('LumbarSensorTrainingReadings', json.dumps(packet))<jupyter_output><empty_output><jupyter_text>The main method calls the methods with a defined number of iterations.<jupyter_code>def main():
	sendSensorReadings(50)
	sendSensorTrainingReadings(50)


if __name__ == "__main__":
    main()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11391251518833535, 'fraction_numerical': 0.044349939246658567, 'mean_word_length': 3.651129943502825, 'pattern_counts': {'":': 35, '<': 29, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '29547', 'license': 'Apache-2.0', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1037, 'n_words': 273, 'path': 'testing/KafkaProducerTest.ipynb', 'repo_name': 'kringen/Spark-CRM-App', 'size': '3292'}