def CreateFromRange(firstTime: datetime, lastTime: datetime): """ Creates a seek filter over a single date range (inclusive list). Parameters ---------- firstTime: the first time if the query (inclusive). lastTime: the last time of the query (inclusive). """ return timestampSeekFilter(Ticks.FromDateTime(firstTime), Ticks.FromDateTime(lastTime))
def CreateFromRangeInterval(firstTime: datetime, lastTime: datetime, interval: timedelta, tolerance: timedelta): """ Creates a seek filter over a single date range (inclusive list), skipping values based on specified `timedelta` interval and tolerance. Parameters ---------- firstTime: the first time if the query (inclusive). lastTime: the last time of the query (inclusive). interval: the exact interval for the scan. Example: 0.1 seconds. tolerance: the width of every window. Example: 0.001 seconds. """ return timestampSeekFilter(Ticks.FromDateTime(firstTime), Ticks.FromDateTime(lastTime), Ticks.FromTimeDelta(interval), Ticks.FromTimeDelta(interval), Ticks.FromTimeDelta(tolerance))
def CreateFromRangeSubInterval(firstTime: datetime, lastTime: datetime, mainInterval: timedelta, subInterval: timedelta, tolerance: timedelta): """ Creates a seek filter over a single date range (inclusive list), skipping values based on specified `timedelta` main /sub intervals and tolerance. Parameters ---------- firstTime: the first time if the query (inclusive). lastTime: the last time of the query (inclusive). mainInterval: the smallest interval that is exact. Example: 0.1 seconds. subInterval: the interval that will be parsed (round possible). Example: 0.0333333 seconds. tolerance: the width of every window. Example: 0.001 seconds. """ return timestampSeekFilter(Ticks.FromDateTime(firstTime), Ticks.FromDateTime(lastTime), Ticks.FromTimeDelta(mainInterval), Ticks.FromTimeDelta(subInterval), Ticks.FromTimeDelta(tolerance))
def writeTest(): # Create historian connection (the root API object) historian = historianConnection("localhost") instance: Optional[historianInstance] = None try: print("Connecting to openHistorian...") historian.Connect() if not historian.IsConnected or len(historian.InstanceNames) == 0: print("No openHistorian instances detected!") else: # Get first historian instance initialInstance = historian.InstanceNames[0] print(f"Opening \"{initialInstance}\" database instance...") instance = historian.OpenInstance(initialInstance) key = historianKey() key.PointID = 1 key.Timestamp = Ticks.FromDateTime(datetime.utcnow()) value = historianValue() value.AsSingle = np.float32(1000.98) value.AsQuality = QualityFlags.WARNINGHIGH print("Writing test point...") instance.Write(key, value) except Exception as ex: print(f"Failed to connect: {ex}") finally: if instance is not None: instance.Dispose() if historian.IsConnected: print("Disconnecting from openHistorian") historian.Disconnect()
def AsDateTime(self, value: datetime): """ Sets `Timestamp` type cast from a `datetime` """ self.Timestamp = Ticks.FromDateTime(value)
def TestWriteAndVerify(instance: historianInstance): print("\r\nExecuting test write and read verification...\r\n") utcTime = datetime.utcnow() pointID = 1 pointTime = Ticks.FromDateTime(utcTime) pointValue = np.float32(1000.98) pointQuality = QualityFlags.WARNINGHIGH key = historianKey() value = historianValue() key.PointID = pointID key.Timestamp = pointTime value.AsSingle = pointValue value.AsQuality = pointQuality print("Source values compared to those assigned to key/value pair:") print( f" Point ID Match = {key.PointID}, match: {pointID == key.PointID}" ) print( f" Point Time Match = {key.AsDateTime}, match: {pointTime == key.Timestamp}" ) print( f" Point Value Match = {value.AsSingle}, match: {pointValue == value.AsSingle}" ) print( f" Point Quality Match = {value.AsQuality}, match: {pointQuality == value.AsQuality}" ) print("\r\nWriting a test point...") instance.Write(key, value) sleep(0.5) # Wait a moment before read timeFilter = timestampSeekFilter.CreateFromRange( utcTime - timedelta(milliseconds=1), utcTime) pointFilter = pointIDMatchFilter.CreateFromList([pointID]) reader = instance.Read(timeFilter, pointFilter) count = 0 print("\r\nReading test point...\r\n") while reader.Read(key, value): count += 1 print( "Source values compared to those read from historian into key/value pair:" ) print( f" Point ID Match = {key.PointID}, match: {pointID == key.PointID}" ) print( f" Point Time Match = {key.AsDateTime}, match: {pointTime == key.Timestamp}" ) print( f" Point Value Match = {value.AsSingle}, match: {pointValue == value.AsSingle}" ) print( f" Point Quality Match = {value.AsQuality}, match: {pointQuality == value.AsQuality}" ) print(f" Point Count Match = {count == 1}\r\n")