def main(test=False): """This function is the main body of the SDS Time Series sample script""" exception = None try: config = configparser.ConfigParser() config.read('config.ini') namespace_id = config.get('Configurations', 'Namespace') # step 1 ocs_client: OCSClient = OCSClient( config.get('Access', 'ApiVersion'), config.get('Access', 'Tenant'), config.get('Access', 'Resource'), config.get('Credentials', 'ClientId'), config.get('Credentials', 'ClientSecret'), False) # step 2 print('Creating value and time type') time_value_type = get_type_value_time() time_value_type = ocs_client.Types.getOrCreateType( namespace_id, time_value_type) # step 3 print('Creating a stream for pressure and temperature') pressure_stream = SdsStream( id=STREAM_PRESSURE_NAME, typeId=time_value_type.Id, description="A stream for pressure data of tank1") ocs_client.Streams.createOrUpdateStream(namespace_id, pressure_stream) temperature_stream = SdsStream( id=STREAM_TEMP_NAME, typeId=time_value_type.Id, description="A stream for temperature data of tank1") ocs_client.Streams.createOrUpdateStream(namespace_id, temperature_stream) # step 4 ocs_client.Streams.insertValues(namespace_id, pressure_stream.Id, json.dumps((get_pressure_data()))) ocs_client.Streams.insertValues(namespace_id, temperature_stream.Id, json.dumps((get_temperature_data()))) # step 5 print('Creating a tank type that has both stream and temperature') tank_type = get_type_press_temp_time() tank_type = ocs_client.Types.getOrCreateType(namespace_id, tank_type) # step 6 print('Creating a tank stream') tank_stream = SdsStream(id=STREAM_TANK_1, typeId=tank_type.Id, description="A stream for data of tank1s") ocs_client.Streams.createOrUpdateStream(namespace_id, tank_stream) # step 7 ocs_client.Streams.insertValues(namespace_id, STREAM_TANK_1, json.dumps(get_data())) print() print() print('Looking at the data in the system. In this case we have some' 'null values that are encoded as 0 for the value.') data = get_data() tank_1_sorted = sorted(data, key=lambda x: x['time'], reverse=False) print() print('Value we sent:') print(tank_1_sorted[1]) first_time = tank_1_sorted[0]['time'] last_time = tank_1_sorted[-1]['time'] # step 8 results = ocs_client.Streams.getWindowValues(namespace_id, STREAM_PRESSURE_NAME, None, first_time, last_time) print() print('Value from pressure stream:') print((results)[1]) print() print('Value from tank1 stream:') results = ocs_client.Streams.getWindowValues(namespace_id, STREAM_TANK_1, None, first_time, last_time) print((results)[1]) # step 9 print() print() print("turning on verbosity") ocs_client.acceptverbosity = True print("This means that will get default values back (in our case" " 0.0 since we are looking at doubles)") print() print('Value from pressure stream:') results = ocs_client.Streams.getWindowValues(namespace_id, STREAM_PRESSURE_NAME, None, first_time, last_time) print((results)[1]) print() print('Value from tank1 stream:') results = ocs_client.Streams.getWindowValues(namespace_id, STREAM_TANK_1, None, first_time, last_time) print((results)[1]) # step 10 print() print() print("Getting data summary") # the count of 1 refers to the number of intervals requested summary_results = ocs_client.Streams.getSummaries( namespace_id, STREAM_TANK_1, None, first_time, last_time, 1) print(summary_results) print() print() print('Now we want to look at data across multiple tanks.') print('For that we can take advantage of bulk stream calls') print('Creating new tank streams') tank_stream = SdsStream(id=STREAM_TANK_2, typeId=tank_type.Id, description="A stream for data of tank2") ocs_client.Streams.createOrUpdateStream(namespace_id, tank_stream) data_tank_2 = get_data_tank_2() ocs_client.Streams.insertValues(namespace_id, STREAM_TANK_2, json.dumps(get_data_tank_2())) tank_2_sorted = sorted(data_tank_2, key=lambda x: x['time'], reverse=False) first_time_tank_2 = tank_2_sorted[0]['time'] last_time_tank_2 = tank_2_sorted[-1]['time'] tank_stream = SdsStream(id=STREAM_TANK_0, typeId=tank_type.Id, description="") ocs_client.Streams.createOrUpdateStream(namespace_id, tank_stream) ocs_client.Streams.insertValues(namespace_id, STREAM_TANK_0, json.dumps(get_data())) time.sleep(10) # step 11 print('Getting bulk call results') results = ocs_client.Streams.getStreamsWindow( namespace_id, [STREAM_TANK_0, STREAM_TANK_2], None, first_time_tank_2, last_time_tank_2) print(results) except Exception as ex: exception = ex print(f"Encountered Error: {ex}") print() finally: # step 12 print() print() print() print("Cleaning up") print("Deleting the stream") suppress_error(lambda: ocs_client.Streams.deleteStream( namespace_id, STREAM_PRESSURE_NAME)) suppress_error(lambda: ocs_client.Streams.deleteStream( namespace_id, STREAM_TEMP_NAME)) suppress_error(lambda: ocs_client.Streams.deleteStream( namespace_id, STREAM_TANK_0)) suppress_error(lambda: ocs_client.Streams.deleteStream( namespace_id, STREAM_TANK_1)) suppress_error(lambda: ocs_client.Streams.deleteStream( namespace_id, STREAM_TANK_2)) print("Deleting the types") suppress_error(lambda: ocs_client.Types.deleteType( namespace_id, TYPE_PRESSURE_TEMPERATURE_TIME_NAME)) suppress_error(lambda: ocs_client.Types.deleteType( namespace_id, TYPE_VALUE_TIME_NAME)) if test and exception is not None: raise exception print('Complete!')
def main(test=False): """This function is the main body of the SDS sample script""" exception = None try: config = configparser.ConfigParser() config.read('config.ini') # Step 1 tenant_id = config.get('Access', 'Tenant') namespace_id = config.get('Configurations', 'Namespace') if tenant_id == 'default': sds_client = EDSClient(config.get('Access', 'ApiVersion'), config.get('Access', 'Resource')) else: sds_client = OCSClient(config.get('Access', 'ApiVersion'), config.get('Access', 'Tenant'), config.get('Access', 'Resource'), config.get('Credentials', 'ClientId'), config.get('Credentials', 'ClientSecret')) namespace_id = config.get('Configurations', 'Namespace') print(r"------------------------------------------") print(r" _________ .___ __________ ") print(r" / _____/ __| _/_____\______ \___.__.") print(r" \_____ \ / __ |/ ___/| ___< | |") print(r" / \/ /_/ |\___ \ | | \___ |") print(r"/_______ /\____ /____ >|____| / ____|") print(r" \/ \/ \/ \/ ") print(r"------------------------------------------") print("Sds endpoint at {url}".format(url=sds_client.uri)) print() # Step 2 ####################################################################### # SdsType get or creation ####################################################################### print("Creating an SdsType") wave_type = get_wave_data_type(SAMPLE_TYPE_ID) wave_type = sds_client.Types.getOrCreateType(namespace_id, wave_type) assert wave_type.Id == SAMPLE_TYPE_ID, "Error getting back wave Type" # Step 3 ####################################################################### # Sds Stream creation ####################################################################### print("Creating an SdsStream") stream = SdsStream() stream.Id = SAMPLE_STREAM_ID stream.Name = "WaveStreamPySample" stream.Description = "A Stream to store the WaveData events" stream.TypeId = wave_type.Id sds_client.Streams.createOrUpdateStream(namespace_id, stream) # Step 4 ####################################################################### # CRUD operations for events ####################################################################### print("Inserting data") # Insert a single event event = next_wave(0, 2.0) sds_client.Streams.insertValues(namespace_id, stream.Id, [event]) # Insert a list of events waves = [] for error in range(2, 20, 2): waves.append(next_wave(error, 2.0)) sds_client.Streams.insertValues(namespace_id, stream.Id, waves) # Step 5 # Get the last inserted event in a stream print("Getting latest event") wave = sds_client.Streams.getLastValue(namespace_id, stream.Id, WaveData) print(to_string(wave)) print() # Get all the events waves = sds_client.Streams.getWindowValues(namespace_id, stream.Id, WaveData, 0, 180) print("Getting all events") print("Total events found: " + str(len(waves))) for wave in waves: print(to_string(wave)) print() # Step 6 # get all values with headers waves = sds_client.Streams.getWindowValuesForm(namespace_id, stream.Id, None, 0, 180, "tableh") print("Getting all events in table format") print(waves) # Step 7 print("Updating events") # Update the first event event = next_wave(0, 4.0) sds_client.Streams.updateValues(namespace_id, stream.Id, [event]) # Update the rest of the events, adding events that have no prior # index entry updated_events = [] for error in range(2, 40, 2): event = next_wave(error, 4.0) updated_events.append(event) sds_client.Streams.updateValues(namespace_id, stream.Id, updated_events) # Get all the events waves = sds_client.Streams.getWindowValues(namespace_id, stream.Id, WaveData, 0, 40) print("Getting updated events") print("Total events found: " + str(len(waves))) for wave in waves: print(to_string(wave)) print() # Step 8 print("Replacing events") # replace one value event = next_wave(0, 5.0) sds_client.Streams.replaceValues(namespace_id, stream.Id, [event]) # replace multiple values replaced_events = [] for error in range(2, 40, 2): event = next_wave(error, 5.0) replaced_events.append(event) sds_client.Streams.replaceValues(namespace_id, stream.Id, replaced_events) # Step 9 # Get all the events waves = sds_client.Streams.getWindowValues(namespace_id, stream.Id, WaveData, 0, 180) print("Getting replaced events") print("Total events found: " + str(len(waves))) for wave in waves: print(to_string(wave)) print() retrieved_interpolated = sds_client.Streams.getRangeValuesInterpolated( namespace_id, stream.Id, None, "5", "32", 4) print("Sds can interpolate or extrapolate data at an index location " "where data does not explicitly exist:") print(retrieved_interpolated) print() # Step 10 # Filtering from all values print("Getting filtered events") filtered_events = sds_client.Streams.getWindowValues( namespace_id, SAMPLE_STREAM_ID, WaveData, 0, 50, 'Radians lt 3') print("Total events found: " + str(len(filtered_events))) for wave in filtered_events: print(to_string(wave)) print() # Step 11 # Sampling from all values print("Getting sampled values") sampled_waves = sds_client.Streams.getSampledValues( namespace_id, stream.Id, WaveData, 0, 40, "sin", 4) print("Total events found: " + str(len(sampled_waves))) for wave in sampled_waves: print(to_string(wave)) print() # Step 12 ####################################################################### # Property Overrides ####################################################################### print("Property Overrides") print("Sds can interpolate or extrapolate data at an index location " "where data does not explicitly exist:") print() # We will retrieve three events using the default behavior, Continuous waves = sds_client.Streams.getRangeValues( namespace_id, stream.Id, WaveData, "1", 0, 3, False, SdsBoundaryType.ExactOrCalculated) print("Default (Continuous) requesting data starting at index location" " '1', where we have not entered data, Sds will interpolate a " "value for each property:") for wave in waves: print(("Order: {order}: Radians: {radians} Cos: {cos}".format( order=wave.order, radians=wave.radians, cos=wave.cos))) # Create a Discrete stream PropertyOverride indicating that we do not # want Sds to calculate a value for Radians and update our stream property_override = SdsStreamPropertyOverride() property_override.SdsTypePropertyId = 'Radians' property_override.InterpolationMode = 3 # update the stream props = [property_override] stream.PropertyOverrides = props sds_client.Streams.createOrUpdateStream(namespace_id, stream) waves = sds_client.Streams.getRangeValues( namespace_id, stream.Id, WaveData, "1", 0, 3, False, SdsBoundaryType.ExactOrCalculated) print() print("We can override this read behavior on a property by property" "basis, here we override the Radians property instructing Sds" " not to interpolate.") print("Sds will now return the default value for the data type:") for wave in waves: print(("Order: {order}: Radians: {radians} Cos: {cos}".format( order=wave.order, radians=wave.radians, cos=wave.cos))) # Step 13 ####################################################################### # Stream Views ####################################################################### # Create additional types to define our targets wave_target_type = get_wave_data_target_type(SAMPLE_TARGET_TYPE_ID) wave_target_type = sds_client.Types.getOrCreateType( namespace_id, wave_target_type) wave_integer_type = get_wave_data_integer_type(SAMPLE_INTEGER_TYPE_ID) wave_integer_type = sds_client.Types.getOrCreateType( namespace_id, wave_integer_type) # Create an SdsStreamViewProperty objects when we want to explicitly # map one property to another vp1 = SdsStreamViewProperty() vp1.SourceId = "Order" vp1.TargetId = "OrderTarget" vp2 = SdsStreamViewProperty() vp2.SourceId = "Sin" vp2.TargetId = "SinInt" vp3 = SdsStreamViewProperty() vp3.SourceId = "Cos" vp3.TargetId = "CosInt" vp4 = SdsStreamViewProperty() vp4.SourceId = "Tan" vp4.TargetId = "TanInt" # Create a streamView mapping our original type to our target type, # data shape is the same so let Sds handle the mapping stream_view = SdsStreamView() stream_view.Id = SAMPLE_STREAM_VIEW_ID stream_view.Name = "SampleStreamView" stream_view.TargetTypeId = wave_target_type.Id stream_view.SourceTypeId = wave_type.Id # Data shape and data types are different so include explicit mappings # between properties manual_stream_view = SdsStreamView() manual_stream_view.Id = SAMPLE_STREAM_VIEW_INT_ID manual_stream_view.Name = "SampleIntStreamView" manual_stream_view.TargetTypeId = wave_integer_type.Id manual_stream_view.SourceTypeId = wave_type.Id manual_stream_view.Properties = [vp1, vp2, vp3, vp4] automatic_stream_view = sds_client.Streams.getOrCreateStreamView( namespace_id, stream_view) manual_stream_view = sds_client.Streams.getOrCreateStreamView( namespace_id, manual_stream_view) stream_view_map_1 = SdsStreamViewMap() stream_view_map_1 = sds_client.Streams.getStreamViewMap( namespace_id, automatic_stream_view.Id) stream_view_map_2 = SdsStreamViewMap() stream_view_map_2 = sds_client.Streams.getStreamViewMap( namespace_id, manual_stream_view.Id) range_waves = sds_client.Streams.getRangeValues( namespace_id, stream.Id, WaveData, "1", 0, 3, False, SdsBoundaryType.ExactOrCalculated) print() print("SdsStreamViews") print("Here is some of our data as it is stored on the server:") for way in range_waves: print(("Sin: {sin}, Cos: {cos}, Tan: {tan}".format(sin=way.sin, cos=way.cos, tan=way.tan))) # StreamView data when retrieved with a streamView range_waves = sds_client.Streams.getRangeValues( namespace_id, stream.Id, WaveDataTarget, "1", 0, 3, False, SdsBoundaryType.ExactOrCalculated, automatic_stream_view.Id) print() print("Specifying a streamView with an SdsType of the same shape" "returns values that are automatically mapped to the target" " SdsType's properties:") for way in range_waves: print(("SinTarget: {sinTarget}, CosTarget: {cosTarget}, TanTarget:" " {tanTarget}").format(sinTarget=way.sin_target, cosTarget=way.cos_target, tanTarget=way.tan_target)) range_waves = sds_client.Streams.getRangeValues( namespace_id, stream.Id, WaveDataInteger, "1", 0, 3, False, SdsBoundaryType.ExactOrCalculated, manual_stream_view.Id) print() print("SdsStreamViews can also convert certain types of data, here we" " return integers where the original values were doubles:") for way in range_waves: print(( "SinInt: {sinInt}, CosInt: {cosInt}, TanInt: {tanInt}").format( sinInt=way.sin_int, cosInt=way.cos_int, tanInt=way.tan_int)) print() print("We can query Sds to return the SdsStreamViewMap for our " "SdsStreamView, here is the one generated automatically:") for prop in stream_view_map_1.Properties: print(("{source} => {dest}".format(source=prop.SourceId, dest=prop.TargetId))) print() print("Here is our explicit mapping, note SdsStreamViewMap will return" " all properties of the Source Type, even those without a " "corresponding Target property:") for prop in stream_view_map_2.Properties: if hasattr(prop, 'TargetId'): print(("{source} => {dest}".format(source=prop.SourceId, dest=prop.TargetId))) else: print(("{source} => {dest}".format(source=prop.SourceId, dest='Not mapped'))) # Step 14 print("We will now update the stream type based on the streamview") first_val = sds_client.Streams.getFirstValue(namespace_id, stream.Id, None) sds_client.Streams.updateStreamType(namespace_id, stream.Id, SAMPLE_STREAM_VIEW_ID) new_stream = sds_client.Streams.getStream(namespace_id, SAMPLE_STREAM_ID) first_val_updated = sds_client.Streams.getFirstValue( namespace_id, SAMPLE_STREAM_ID, None) print("The new type id" + new_stream.TypeId + " compared to the " "original one " + stream.TypeId) print("The new type value " + str(first_val) + " compared to the " "original one " + str(first_val_updated)) # Step 15 types = sds_client.Types.getTypes(namespace_id, 0, 100) types_query = sds_client.Types.getTypes(namespace_id, 0, 100, "Id:*Target*") print() print("All Types: ") for type_i in types: print(type_i.Id) print("Types after Query: ") for type_i in types_query: print(type_i.Id) if tenant_id != 'default': # Step 16 ####################################################################### # Tags and Metadata (OCS ONLY) ####################################################################### print() print("Let's add some Tags and Metadata to our stream:") tags = ["waves", "periodic", "2018", "validated"] metadata = { "Region": "North America", "Country": "Canada", "Province": "Quebec" } sds_client.Streams.createOrUpdateTags(namespace_id, stream.Id, tags) sds_client.Streams.createOrUpdateMetadata(namespace_id, stream.Id, metadata) print() print("Tags now associated with ", stream.Id) print(sds_client.Streams.getTags(namespace_id, stream.Id)) region = sds_client.Streams.getMetadata(namespace_id, stream.Id, "Region") country = sds_client.Streams.getMetadata(namespace_id, stream.Id, "Country") province = sds_client.Streams.getMetadata(namespace_id, stream.Id, "Province") print() print("Metadata now associated with", stream.Id, ":") print("Metadata key Region: ", region) print("Metadata key Country: ", country) print("Metadata key Province: ", province) print() # Step 17 ####################################################################### # Update Metadata (OCS ONLY) ####################################################################### print() print("Let's update the Metadata on our stream:") patch = [{ "op": "remove", "path": "/Region" }, { "op": "replace", "path": "/Province", "value": "Ontario" }, { "op": "add", "path": "/City", "value": "Toronto" }] sds_client.Streams.patchMetadata(namespace_id, stream.Id, patch) country = sds_client.Streams.getMetadata(namespace_id, stream.Id, "Country") province = sds_client.Streams.getMetadata(namespace_id, stream.Id, "Province") city = sds_client.Streams.getMetadata(namespace_id, stream.Id, "City") print() print("Metadata now associated with", stream.Id, ":") print("Metadata key Country: ", country) print("Metadata key Province: ", province) print("Metadata key City: ", city) print() # Step 17 ####################################################################### # Delete events ####################################################################### print() print('Deleting values from the SdsStream') # remove a single value from the stream sds_client.Streams.removeValue(namespace_id, stream.Id, 0) # remove multiple values from the stream sds_client.Streams.removeWindowValues(namespace_id, stream.Id, 0, 40) try: event = sds_client.Streams.getLastValue(namespace_id, stream.Id, WaveData) if event is not None: raise ValueError except TypeError: pass print("All values deleted successfully!") # Step 18 print("Adding a stream with a secondary index.") index = SdsStreamIndex() index.SdsTypePropertyId = "Radians" secondary = SdsStream() secondary.Id = STREAM_ID_SECONDARY secondary.TypeId = SAMPLE_TYPE_ID secondary.Indexes = [index] secondary = sds_client.Streams.getOrCreateStream( namespace_id, secondary) count = 0 if stream.Indexes: count = len(stream.Indexes) print("Secondary indexes on streams original:" + str(count) + ". New one: " + str(len(secondary.Indexes))) print() # Modifying an existing stream with a secondary index. print("Modifying a stream to have a secondary index.") sample_stream = sds_client.Streams.getStream(namespace_id, SAMPLE_STREAM_ID) index = SdsStreamIndex() index.SdsTypePropertyId = "RadiansTarget" sample_stream.Indexws = [index] sds_client.Streams.createOrUpdateStream(namespace_id, sample_stream) sample_stream = sds_client.Streams.getStream(namespace_id, SAMPLE_STREAM_ID) # Modifying an existing stream to remove the secondary index print("Removing a secondary index from a stream.") secondary.Indexes = [] sds_client.Streams.createOrUpdateStream(namespace_id, secondary) secondary = sds_client.Streams.getStream(namespace_id, secondary.Id) original_length = "0" if stream.Indexes: original_length = str(len(stream.Indexes)) secondary_length = "0" if secondary.Indexes: secondary_length = str(len(secondary.Indexes)) print("Secondary indexes on streams original:" + original_length + ". New one: " + secondary_length) # Step 19 # Adding Compound Index Type print("Creating an SdsType with a compound index") type_compound = get_wave_compound_data_type(COMPOUND_TYPE_ID) sds_client.Types.getOrCreateType(namespace_id, type_compound) # create an SdsStream print("Creating an SdsStream off of type with compound index") stream_compound = SdsStream() stream_compound.Id = STREAM_ID_COMPOUND stream_compound.TypeId = type_compound.Id sds_client.Streams.createOrUpdateStream(namespace_id, stream_compound) # Step 20 print("Inserting data") waves = [] waves.append(next_wave(1, 10)) waves.append(next_wave(2, 2)) waves.append(next_wave(3, 1)) waves.append(next_wave(10, 3)) waves.append(next_wave(10, 8)) waves.append(next_wave(10, 10)) sds_client.Streams.insertValues(namespace_id, STREAM_ID_COMPOUND, waves) latest_compound = sds_client.Streams.getLastValue( namespace_id, STREAM_ID_COMPOUND, None) first_compound = sds_client.Streams.getFirstValue( namespace_id, STREAM_ID_COMPOUND, None) window_val = sds_client.Streams.getWindowValues( namespace_id, STREAM_ID_COMPOUND, None, "2|1", "10|8") print("First data: " + str(first_compound) + " Latest data: " + str(latest_compound)) print("Window Data:") print(str(window_val)) except Exception as error: print((f'Encountered Error: {error}')) print() traceback.print_exc() print() exception = error finally: # Step 21 ####################################################################### # SdsType, SdsStream, and SdsStreamView deletion ####################################################################### # Clean up the remaining artifacts print("Cleaning up") print("Deleting the stream") suppress_error(lambda: sds_client.Streams.deleteStream( namespace_id, SAMPLE_STREAM_ID)) suppress_error(lambda: sds_client.Streams.deleteStream( namespace_id, STREAM_ID_SECONDARY)) suppress_error(lambda: sds_client.Streams.deleteStream( namespace_id, STREAM_ID_COMPOUND)) print("Deleting the streamViews") suppress_error(lambda: sds_client.Streams.deleteStreamView( namespace_id, SAMPLE_STREAM_VIEW_ID)) suppress_error(lambda: sds_client.Streams.deleteStreamView( namespace_id, SAMPLE_STREAM_VIEW_INT_ID)) print("Deleting the types") suppress_error( lambda: sds_client.Types.deleteType(namespace_id, SAMPLE_TYPE_ID)) suppress_error(lambda: sds_client.Types.deleteType( namespace_id, SAMPLE_TARGET_TYPE_ID)) suppress_error(lambda: sds_client.Types.deleteType( namespace_id, SAMPLE_INTEGER_TYPE_ID)) suppress_error(lambda: sds_client.Types.deleteType( namespace_id, COMPOUND_TYPE_ID)) if test and exception is not None: raise exception print('Complete!')
def create_data(namespace_id, ocs_client): """Creates sample data for the script to use""" double_type = SdsType(id='doubleType', sdsTypeCode=SdsTypeCode.Double) datetime_type = SdsType(id='dateTimeType', sdsTypeCode=SdsTypeCode.DateTime) pressure_property = SdsTypeProperty(id='pressure', sdsType=double_type) temperature_property = SdsTypeProperty(id=SAMPLE_FIELD_TO_CONSOLIDATE_TO, sdsType=double_type) ambient_temperature_property = SdsTypeProperty( id=SAMPLE_FIELD_TO_CONSOLIDATE, sdsType=double_type) time_property = SdsTypeProperty(id='time', sdsType=datetime_type, isKey=True) sds_type_1 = SdsType( id=SAMPLE_TYPE_ID_1, description='This is a sample Sds type for storing Pressure type ' 'events for Data Views', sdsTypeCode=SdsTypeCode.Object, properties=[pressure_property, temperature_property, time_property]) sds_type_2 = SdsType( id=SAMPLE_TYPE_ID_2, description='This is a new sample Sds type for storing Pressure type ' 'events for Data Views', sdsTypeCode=SdsTypeCode.Object, properties=[ pressure_property, ambient_temperature_property, time_property ]) print('Creating SDS Types...') ocs_client.Types.getOrCreateType(namespace_id, sds_type_1) ocs_client.Types.getOrCreateType(namespace_id, sds_type_2) stream1 = SdsStream( id=SAMPLE_STREAM_ID_1, name=SAMPLE_STREAM_NAME_1, description='A Stream to store the sample Pressure events', typeId=SAMPLE_TYPE_ID_1) stream2 = SdsStream( id=SAMPLE_STREAM_ID_2, name=SAMPLE_STREAM_NAME_2, description='A Stream to store the sample Pressure events', typeId=SAMPLE_TYPE_ID_2) print('Creating SDS Streams...') ocs_client.Streams.createOrUpdateStream(namespace_id, stream1) ocs_client.Streams.createOrUpdateStream(namespace_id, stream2) sample_start_time = datetime.datetime.now() - datetime.timedelta(hours=1) sample_end_time = datetime.datetime.now() values1 = [] values2 = [] def value_with_time(timestamp, value, field_name, value2): """Formats a JSON data object""" return f'{{"time": "{timestamp}", "pressure": {str(value)}, "{field_name}": {str(value2)}}}' print('Generating values...') for i in range(1, 30, 1): timestamp = (sample_start_time + datetime.timedelta(minutes=i * 2)).isoformat( timespec='seconds') val1 = value_with_time(timestamp, random.uniform(0, 100), SAMPLE_FIELD_TO_CONSOLIDATE_TO, random.uniform(50, 70)) val2 = value_with_time(timestamp, random.uniform(0, 100), SAMPLE_FIELD_TO_CONSOLIDATE, random.uniform(50, 70)) values1.append(val1) values2.append(val2) print('Sending values...') ocs_client.Streams.insertValues(namespace_id, SAMPLE_STREAM_ID_1, str(values1).replace("'", "")) ocs_client.Streams.insertValues(namespace_id, SAMPLE_STREAM_ID_2, str(values2).replace("'", "")) return (sample_start_time, sample_end_time)
def main(): global namespaceId, streamPressureName, streamTempName, exception try: config = configparser.ConfigParser() config.read('config.ini') namespaceId = config.get('Configurations', 'Namespace') # step 1 ocsClient: OCSClient = OCSClient( config.get('Access', 'ApiVersion'), config.get('Access', 'Tenant'), config.get('Access', 'Resource'), config.get('Credentials', 'ClientId'), config.get('Credentials', 'ClientSecret'), False) # step 2 print('Creating value and time type') timeValueType = GetType_ValueTime() timeValueType = ocsClient.Types.getOrCreateType( namespaceId, timeValueType) # step 3 print('Creating a stream for pressure and temperature') pressureStream = SdsStream( id=streamPressureName, typeId=timeValueType.Id, description="A stream for pressure data of tank1") ocsClient.Streams.createOrUpdateStream(namespaceId, pressureStream) temperatureStream = SdsStream( id=streamTempName, typeId=timeValueType.Id, description="A stream for temperature data of tank1") ocsClient.Streams.createOrUpdateStream(namespaceId, temperatureStream) # step 4 ocsClient.Streams.insertValues(namespaceId, pressureStream.Id, json.dumps((GetPressureData()))) ocsClient.Streams.insertValues(namespaceId, temperatureStream.Id, json.dumps((GetTemperatureData()))) # step 5 print('Creating a tank type that has both stream and temperature') tankType = GetType_PressTempTime() tankType = ocsClient.Types.getOrCreateType(namespaceId, tankType) # step 6 print('Creating a tank stream') tankStream = SdsStream(id=streamTank1, typeId=tankType.Id, description="A stream for data of tank1s") ocsClient.Streams.createOrUpdateStream(namespaceId, tankStream) # step 7 ocsClient.Streams.insertValues(namespaceId, streamTank1, json.dumps(GetData())) print() print() print('Looking at the data in the system. In this case we have some' 'null values that are encoded as 0 for the value.') data = GetData() tank1Sorted = sorted(data, key=lambda x: x['time'], reverse=False) print() print('Value we sent:') print(tank1Sorted[1]) firstTime = tank1Sorted[0]['time'] lastTime = tank1Sorted[-1]['time'] # step 8 results = ocsClient.Streams.getWindowValues(namespaceId, streamPressureName, None, firstTime, lastTime) print() print('Value from pressure stream:') print((results)[1]) print() print('Value from tank1 stream:') results = ocsClient.Streams.getWindowValues(namespaceId, streamTank1, None, firstTime, lastTime) print((results)[1]) # step 9 print() print() print("turning on verbosity") ocsClient.acceptverbosity = True print("This means that will get default values back (in our case" " 0.0 since we are looking at doubles)") print() print('Value from pressure stream:') results = ocsClient.Streams.getWindowValues(namespaceId, streamPressureName, None, firstTime, lastTime) print((results)[1]) print() print('Value from tank1 stream:') results = ocsClient.Streams.getWindowValues(namespaceId, streamTank1, None, firstTime, lastTime) print((results)[1]) # step 10 print() print() print("Getting data summary") # the count of 1 refers to the number of intervals requested summaryResults = ocsClient.Streams.getSummaries( namespaceId, streamTank1, None, firstTime, lastTime, 1) print(summaryResults) print() print() print('Now we want to look at data across multiple tanks.') print('For that we can take advantage of bulk stream calls') print('Creating new tank streams') tankStream = SdsStream(id=streamTank2, typeId=tankType.Id, description="A stream for data of tank2") ocsClient.Streams.createOrUpdateStream(namespaceId, tankStream) dataTank2 = GetData_Tank2() ocsClient.Streams.insertValues(namespaceId, streamTank2, json.dumps(GetData_Tank2())) tank2Sorted = sorted(dataTank2, key=lambda x: x['time'], reverse=False) firstTimeTank2 = tank2Sorted[0]['time'] lastTimeTank2 = tank2Sorted[-1]['time'] tankStream = SdsStream(id=streamTank0, typeId=tankType.Id, description="") ocsClient.Streams.createOrUpdateStream(namespaceId, tankStream) ocsClient.Streams.insertValues(namespaceId, streamTank0, json.dumps(GetData())) time.sleep(10) # step 11 print('Getting bulk call results') results = ocsClient.Streams.getStreamsWindow( namespaceId, [streamTank0, streamTank2], None, firstTimeTank2, lastTimeTank2) print(results) except Exception as ex: exception = ex print(f"Encountered Error: {ex}") print() finally: # step 12 print() print() print() print("Cleaning up") print("Deleting the stream") supressError(lambda: ocsClient.Streams.deleteStream( namespaceId, streamPressureName)) supressError(lambda: ocsClient.Streams.deleteStream( namespaceId, streamTempName)) supressError( lambda: ocsClient.Streams.deleteStream(namespaceId, streamTank0)) supressError( lambda: ocsClient.Streams.deleteStream(namespaceId, streamTank1)) supressError( lambda: ocsClient.Streams.deleteStream(namespaceId, streamTank2)) print("Deleting the types") supressError(lambda: ocsClient.Types.deleteType( namespaceId, typePressureTemperatureTimeName)) supressError( lambda: ocsClient.Types.deleteType(namespaceId, typeValueTimeName)) if (exception): raise exception
def createData(ocsClient): import random global namespaceId, startTime doubleType = SdsType(id="doubleType", sdsTypeCode=SdsTypeCode.Double) dateTimeType = SdsType(id="dateTimeType", sdsTypeCode=SdsTypeCode.DateTime) pressureDoubleProperty = SdsTypeProperty(id="pressure", sdsType=doubleType) temperatureDoubleProperty = SdsTypeProperty(id="temperature", sdsType=doubleType) timeDateTimeProperty = SdsTypeProperty(id="time", sdsType=dateTimeType, isKey=True) pressure_SDSType = SdsType( id=samplePressureTypeId, description="This is a sample Sds type for storing Pressure type " "events for Dataviews", sdsTypeCode=SdsTypeCode.Object, properties=[pressureDoubleProperty, timeDateTimeProperty]) temperature_SDSType = SdsType( id=sampleTemperatureTypeId, description="This is a sample Sds type for storing Temperature type " "events for Dataviews", sdsTypeCode=SdsTypeCode.Object, properties=[temperatureDoubleProperty, timeDateTimeProperty]) print('Creating SDS Type') ocsClient.Types.getOrCreateType(namespaceId, pressure_SDSType) ocsClient.Types.getOrCreateType(namespaceId, temperature_SDSType) pressureStream = SdsStream( id=samplePressureStreamId, name=samplePressureStreamName, description="A Stream to store the sample Pressure events", typeId=samplePressureTypeId) temperatureStream = SdsStream( id=sampleTemperatureStreamId, name=sampleTemperatureStreamName, description="A Stream to store the sample Temperature events", typeId=sampleTemperatureTypeId) print('Creating SDS Streams') ocsClient.Streams.createOrUpdateStream(namespaceId, pressureStream) ocsClient.Streams.createOrUpdateStream(namespaceId, temperatureStream) start = datetime.datetime.now() - datetime.timedelta(hours=1) pressureValues = [] temperatureValues = [] def valueWithTime(timestamp, sensor, value): return f'{{"time": "{timestamp}", "{sensor}": {str(value)} }}' print('Generating Values') for i in range(1, 30, 1): pv = str(random.uniform(0, 100)) tv = str(random.uniform(50, 70)) timestamp = (start + datetime.timedelta(minutes=i * 2)).isoformat( timespec='seconds') pVal = valueWithTime(timestamp, "pressure", random.uniform(0, 100)) tVAl = valueWithTime(timestamp, "temperature", random.uniform(50, 70)) pressureValues.append(pVal) temperatureValues.append(tVAl) print('Sending Pressure Values') ocsClient.Streams.insertValues(namespaceId, samplePressureStreamId, str(pressureValues).replace("'", "")) print('Sending Temperature Values') ocsClient.Streams.insertValues(namespaceId, sampleTemperatureStreamId, str(temperatureValues).replace("'", "")) startTime = start
def createData(ocsClient): import random global namespaceId, firstData doubleType = SdsType(id="doubleType", sdsTypeCode=SdsTypeCode.Double) dateTimeType = SdsType(id="dateTimeType", sdsTypeCode=SdsTypeCode.DateTime) pressureDoubleProperty = SdsTypeProperty(id="pressure", sdsType=doubleType) temperatureDoubleProperty = SdsTypeProperty(id="temperature", sdsType=doubleType) timeDateTimeProperty = SdsTypeProperty(id="time", sdsType=dateTimeType, isKey=True) pressure_SDSType = SdsType( id=samplePressureTypeId, description="This is a sample Sds type for storing Pressure type " "events for Dataviews", sdsTypeCode=SdsTypeCode.Object, properties=[pressureDoubleProperty, timeDateTimeProperty]) temperature_SDSType = SdsType( id=sampleTemperatureTypeId, description="This is a sample Sds type for storing Temperature type " "events for Dataviews", sdsTypeCode=SdsTypeCode.Object, properties=[temperatureDoubleProperty, timeDateTimeProperty]) print('Creating SDS Type') ocsClient.Types.getOrCreateType(namespaceId, pressure_SDSType) ocsClient.Types.getOrCreateType(namespaceId, temperature_SDSType) pressureStream = SdsStream( id=samplePressureStreamId, name=samplePressureStreamName, description="A Stream to store the sample Pressure events", typeId=samplePressureTypeId) temperatureStream = SdsStream( id=sampleTemperatureStreamId, name=sampleTemperatureStreamName, description="A Stream to store the sample Temperature events", typeId=sampleTemperatureTypeId) print('Creating SDS Streams') ocsClient.Streams.createOrUpdateStream(namespaceId, pressureStream) ocsClient.Streams.createOrUpdateStream(namespaceId, temperatureStream) start = datetime.datetime.now() - datetime.timedelta(hours=1) pressureValues = [] temperatureValues = [] print('Sending Values') for i in range(1, 60, 1): pv = str(random.uniform(0, 100)) tv = str(random.uniform(50, 70)) pVal = ('{"time" : "' + (start + datetime.timedelta(minutes=i * 1)) .strftime("%Y-%m-%dT%H:%M:%SZ")+'", "pressure":' + str(random.uniform(0, 100)) + '}') tVAl = ('{"time" : "' + (start + datetime.timedelta(minutes=i * 1)) .strftime("%Y-%m-%dT%H:%M:%SZ")+'", "temperature":' + str(random.uniform(50, 70)) + '}') if i == 1: timeHolder = (start + datetime.timedelta(minutes=i * 1) ).strftime("%Y-%m-%dT%H:%M:%SZ") firstData = ('{"time" : "' + timeHolder + '", "pressure":' + pv + ', "temperature":' + tv + '}') pressureValues.append(pVal) temperatureValues.append(tVAl) print('Sending Pressure Values') ocsClient.Streams.insertValues( namespaceId, samplePressureStreamId, str(pressureValues).replace("'", "")) print('Sending Temperature Values') ocsClient.Streams.insertValues( namespaceId, sampleTemperatureStreamId, str(temperatureValues).replace("'", ""))
def createData(ocsClient): import random global namespaceId, startTime, endTime doubleType = SdsType(id="doubleType", sdsTypeCode=SdsTypeCode.Double) dateTimeType = SdsType(id="dateTimeType", sdsTypeCode=SdsTypeCode.DateTime) pressureDoubleProperty = SdsTypeProperty(id="pressure", sdsType=doubleType) temperatureDoubleProperty = SdsTypeProperty(id=fieldToConsolidateTo, sdsType=doubleType) ambientTemperatureDoubleProperty = SdsTypeProperty(id=fieldToConsolidate, sdsType=doubleType) timeDateTimeProperty = SdsTypeProperty(id="time", sdsType=dateTimeType, isKey=True) sDSType1 = SdsType( id=sampleTypeId, description="This is a sample Sds type for storing Pressure type " "events for Data Views", sdsTypeCode=SdsTypeCode.Object, properties=[pressureDoubleProperty, temperatureDoubleProperty, timeDateTimeProperty]) sDSType2 = SdsType( id=samplePressureId2, description="This is a new sample Sds type for storing Pressure type " "events for Data Views", sdsTypeCode=SdsTypeCode.Object, properties=[pressureDoubleProperty, ambientTemperatureDoubleProperty, timeDateTimeProperty]) print('Creating SDS Type') ocsClient.Types.getOrCreateType(namespaceId, sDSType1) ocsClient.Types.getOrCreateType(namespaceId, sDSType2) stream1 = SdsStream( id=sampleStreamId, name=sampleStreamName, description="A Stream to store the sample Pressure events", typeId=sampleTypeId) stream2 = SdsStream( id=sampleStreamId2, name=sampleStreamName2, description="A Stream to store the sample Pressure events", typeId=samplePressureId2) print('Creating SDS Streams') ocsClient.Streams.createOrUpdateStream(namespaceId, stream1) ocsClient.Streams.createOrUpdateStream(namespaceId, stream2) start = datetime.datetime.now() - datetime.timedelta(hours=1) endTime = datetime.datetime.now() values = [] values2 = [] def valueWithTime(timestamp, value, fieldName, value2): return f'{{"time": "{timestamp}", "pressure": {str(value)}, "{fieldName}": {str(value2)}}}' print('Generating Values') for i in range(1, 30, 1): timestamp = (start + datetime.timedelta(minutes=i * 2) ).isoformat(timespec='seconds') pVal = valueWithTime(timestamp, random.uniform( 0, 100), fieldToConsolidateTo, random.uniform(50, 70)) pVal2 = valueWithTime(timestamp, random.uniform( 0, 100), fieldToConsolidate, random.uniform(50, 70)) values.append(pVal) values2.append(pVal2) print('Sending Values') ocsClient.Streams.insertValues( namespaceId, sampleStreamId, str(values).replace("'", "")) ocsClient.Streams.insertValues( namespaceId, sampleStreamId2, str(values2).replace("'", "")) startTime = start