def main(): local = sys.argv[1] == "local" #define needed variables COMMANDS_TOPIC = "streamsx/iot/device/commands/send" #topic to publish commands to EVENTS_TOPIC = "streamsx/iot/device/events" #topic to subscribe to for events incoming_schema = schema.StreamSchema("tuple <rstring typeId, rstring deviceId, rstring eventId,rstring jsonString>") cmd_schema = schema.StreamSchema('tuple<rstring typeId, rstring deviceId, rstring cmdId, rstring jsonString>') topo = Topology('ReadingsFromIot') #Subscribe to events events = topo.subscribe(EVENTS_TOPIC, incoming_schema,"AllEventsAsJSON") sensor_events = events.filter(lambda tuple: tuple["eventId"] == "sensors","SensorEventsAsJSON") readings = sensor_events.map(get_event_data,"ReadingsStream") readings.print() #send a command cmd_stream = sensor_events.map(get_cmd, "CommandsAsJSON") #convert the commands stream to a SPL structured schema commands_to_publish = cmd_stream.map(lambda x : (x["typeId"],x["deviceId"],x["cmdId"],x["jsonString"],), schema = cmd_schema, name="CommandsToPublish") commands_to_publish.publish(COMMANDS_TOPIC, cmd_schema) if local and len(sys.argv) > 2: username = sys.argv[2] password = sys.argv[3] result = submit_to_service(topo, local, username, password) else: result = submit_to_service(topo, local) print("Submitted job to the service, job id = " + str(result.job.id))
def main(): #define needed variables COMMANDS_TOPIC = "streamsx/iot/device/commands/send" #topic to publish commands to EVENTS_TOPIC = "streamsx/iot/device/events" #topic to subscribe to for events incoming_schema = schema.StreamSchema( "tuple <rstring typeId, rstring deviceId, rstring eventId,rstring jsonString>" ) cmd_schema = schema.StreamSchema( 'tuple<rstring typeId, rstring deviceId, rstring cmdId, rstring jsonString>' ) topo = Topology('ReadingsFromIot') #Subscribe to events events = topo.subscribe(EVENTS_TOPIC, incoming_schema) sensor_events = events.filter(lambda tuple: tuple["eventId"] == "sensors") readings = sensor_events.map(get_event_data) readings.print() #send a command cmd_stream = sensor_events.map(get_cmd) #convert the commands stream to a SPL structured schema commands_to_publish = cmd_stream.map(lambda x: ( x["typeId"], x["deviceId"], x["cmdId"], x["jsonString"], ), schema=cmd_schema) commands_to_publish.publish(COMMANDS_TOPIC, cmd_schema) commands_to_publish.print() result = submit_to_service(topo) print("Submitted job to the service, job id = " + str(result.job.id))
def main(): topo = Topology("patient_data") #patientData = topo.source(Observations) patientData = topo.subscribe('ingest-beacon', schema.CommonSchema.Json) heartRate = patientData.filter( lambda tuple: (tuple['reading']['readingType']['code'] == '8867-4')) heartRate.sink(print) streamsx.topology.context.submit("DISTRIBUTED", topo)
def main(): local = sys.argv[1] == "local" #define needed variables COMMANDS_TOPIC = "streamsx/iot/device/commands/send" #topic to publish commands to EVENTS_TOPIC = "streamsx/iot/device/events" #topic to subscribe to for events incoming_schema = schema.StreamSchema( "tuple <rstring typeId, rstring deviceId, rstring eventId,rstring jsonString>" ) cmd_schema = schema.StreamSchema( 'tuple<rstring typeId, rstring deviceId, rstring cmdId, rstring jsonString>' ) topo = Topology('ReadingsFromIot') #Subscribe to events events = topo.subscribe(EVENTS_TOPIC, incoming_schema, "AllEventsAsJSON") sensor_events = events.filter(lambda tuple: tuple["eventId"] == "sensors", "SensorEventsAsJSON") readings = sensor_events.map(get_event_data, "ReadingsStream") readings.print() #send a command cmd_stream = sensor_events.map(get_cmd, "CommandsAsJSON") #convert the commands stream to a SPL structured schema commands_to_publish = cmd_stream.map(lambda x: ( x["typeId"], x["deviceId"], x["cmdId"], x["jsonString"], ), schema=cmd_schema, name="CommandsToPublish") commands_to_publish.publish(COMMANDS_TOPIC, cmd_schema) if local and len(sys.argv) > 2: username = sys.argv[2] password = sys.argv[3] result = submit_to_service(topo, local, username, password) else: result = submit_to_service(topo, local) print("Submitted job to the service, job id = " + str(result.job.id))
def run(self, context="DISTRIBUTED"): ## Create topology topo = Topology("HealthcareDemo") ## Ingest, preprocess and aggregate patient data patientData = topo.subscribe("ingest-physionet", schema.CommonSchema.Json) \ .map(functions.identity) \ .filter(healthcare_functions.PatientFilter(self.patient_id)) \ .transform(healthcare_functions.GenTimestamp(self.sample_rate)) \ .transform(SlidingWindow(length=self.sample_rate, trigger=self.sample_rate-1)) \ .transform(healthcare_functions.aggregate) \ ## Calculate RPeak and RR delta rpeak_data_stream = patientmonitoring_functions.streaming_rpeak(patientData, self.sample_rate, data_label='ECG Lead II') ## Create a view of the data self.view_data = rpeak_data_stream.view() ## Compile Python Streams application and submit job streamsx.topology.context.submit(context, topo.graph, username=self.username, password=self.password)
def run(self, context="DISTRIBUTED"): ## Create topology topo = Topology("HealthcareDemo") ## Ingest, preprocess and aggregate patient data patientData = topo.subscribe("ingest-physionet", schema.CommonSchema.Json) \ .map(functions.identity) \ .filter(healthcare_functions.PatientFilter(self.patient_id)) \ .transform(healthcare_functions.GenTimestamp(self.sample_rate)) \ .transform(SlidingWindow(length=self.sample_rate, trigger=self.sample_rate-1)) \ .transform(healthcare_functions.aggregate) \ ## Calculate RPeak and RR delta rpeak_data_stream = patientmonitoring_functions.streaming_rpeak( patientData, self.sample_rate, data_label='ECG Lead II') ## Create a view of the data self.view_data = rpeak_data_stream.view() ## Compile Python Streams application and submit job streamsx.topology.context.submit(context, topo.graph, username=self.username, password=self.password)
#COMMANDS_TOPIC = "iot-2/type/SMARTBIN_PI_V2/id/SMARTBIN001/cmd/command/fmt/json" #topic to publish commands to #EVENTS_TOPIC = "iot-2/type/SMARTBIN_PI_V2/id/SMARTBIN001/evt/status/fmt/json" #topic to subscribe to for events EVENTS_TOPIC = "streamsx/iot/device/events" incoming_schema = schema.StreamSchema( "tuple <rstring typeId, rstring deviceId, rstring eventId, rstring jsonString>" ) cmd_schema = schema.StreamSchema( 'tuple<rstring typeId, rstring deviceId, rstring cmdId, rstring jsonString>' ) #cmd_schema = schema.StreamSchema('tuple<rstring d>') #Topology object is the Streams application graph topology = Topology('ReadingsFromIot') #Subscribe to events events = topology.subscribe(EVENTS_TOPIC, incoming_schema, "AllEventsAsJSON") sensor_events = events.filter(lambda tuple: tuple["eventId"] == "status", "SensorEventsAsJSON") # sensor_events operator passes a tuple to get_event_data() function ## use flat_map() to split data such as gps coordinate to x and y, same for sensor data to sensor1, sensor2,... readings = sensor_events.map(get_event_data, "ReadingsStream") #print out readings print("received data: ") readings.print() #send a command cmd_stream = sensor_events.map(get_cmd, "CommandsAsJSON") #convert the commands stream to a SPL structured schema commands_to_publish = cmd_stream.map(lambda x: ( x["typeId"], x["deviceId"],
# Set up access to Streaming Analytics service vs = { 'streaming-analytics': [{ 'name': service_name, 'credentials': credentials }] } cfg = {ConfigParams.SERVICE_NAME: service_name, ConfigParams.VCAP_SERVICES: vs} # Define data source # Create Topology and read from data source topo = Topology() patient_data_stream = topo.subscribe('ingest-beacon', schema.CommonSchema.Json) # Anonymize patient anonymous_patient_stream = patient_data_stream.map(anonymize) # Filtering data heart_rate_stream = anonymous_patient_stream.filter( lambda tuple_: (tuple_['reading']['readingType']['code'] == '8867-4')) # Calculate the patient's heart rate average based on the latest 10 tuples heart_average_stream = heart_rate_stream.map(Avg(10)) # Creating view heart_average_view = heart_average_stream.view() # Print data stream