def test_null_string(self): v = ua.Variant(None, ua.VariantType.String) b = variant_to_binary(v) v2 = variant_from_binary(ua.utils.Buffer(b)) self.assertEqual(v.Value, v2.Value) v = ua.Variant("", ua.VariantType.String) b = variant_to_binary(v) v2 = variant_from_binary(ua.utils.Buffer(b)) self.assertEqual(v.Value, v2.Value)
def test_variant_array_none(self): v = ua.Variant(None, varianttype=ua.VariantType.Int32, is_array=True) data = variant_to_binary(v) v2 = variant_from_binary(ua.utils.Buffer(data)) self.assertEqual(v, v2) self.assertTrue(v2.is_array) v = ua.Variant(None, varianttype=ua.VariantType.Null, is_array=True) data = variant_to_binary(v) v2 = variant_from_binary(ua.utils.Buffer(data)) self.assertEqual(v, v2) self.assertTrue(v2.is_array)
def test_variant_array(self): v = ua.Variant([1, 2, 3, 4, 5]) self.assertEqual(v.Value[1], 2) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.Value, v2.Value) self.assertEqual(v.VariantType, v2.VariantType) now = datetime.utcnow() v = ua.Variant([now]) self.assertEqual(v.Value[0], now) self.assertEqual(v.VariantType, ua.VariantType.DateTime) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.Value, v2.Value) self.assertEqual(v.VariantType, v2.VariantType)
def test_variant_dimensions(self): l = [[[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0]], [[5.0, 5.0, 5.0, 5.0], [7.0, 8.0, 9.0, 01.0], [1.0, 1.0, 1.0, 1.0]]] v = ua.Variant(l) self.assertEqual(v.Dimensions, [2, 3, 4]) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v, v2) self.assertEqual(v.Dimensions, v2.Dimensions) # very special case l = [[[], [], []], [[], [], []]] v = ua.Variant(l, ua.VariantType.UInt32) self.assertEqual(v.Dimensions, [2, 3, 0]) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.Dimensions, v2.Dimensions) self.assertEqual(v, v2)
def test_variant_array(self): v = ua.Variant([1, 2, 3, 4, 5]) self.assertEqual(v.Value[1], 2) # self.assertEqual(v.VarianType, ua.VariantType.Int64) # we do not care, we should aonly test for sutff that matter v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.Value, v2.Value) self.assertEqual(v.VariantType, v2.VariantType) now = datetime.utcnow() v = ua.Variant([now]) self.assertEqual(v.Value[0], now) self.assertEqual(v.VariantType, ua.VariantType.DateTime) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.Value, v2.Value) self.assertEqual(v.VariantType, v2.VariantType)
def read_node_history(self, node_id, start, end, nb_values): with self._lock: _c_read = self._conn.cursor() table = self._get_table_name(node_id) start_time, end_time, order, limit = self._get_bounds(start, end, nb_values) cont = None results = [] # select values from the database; recreate UA Variant from binary try: for row in _c_read.execute('SELECT * FROM "{tn}" WHERE "SourceTimestamp" BETWEEN ? AND ? ' 'ORDER BY "_Id" {dir} LIMIT ?'.format(tn=table, dir=order), (start_time, end_time, limit,)): # rebuild the data value object dv = ua.DataValue(variant_from_binary(Buffer(row[6]))) dv.ServerTimestamp = row[1] dv.SourceTimestamp = row[2] dv.StatusCode = ua.StatusCode(row[3]) results.append(dv) except sqlite3.Error as e: self.logger.error('Historizing SQL Read Error for %s: %s', node_id, e) if nb_values: if len(results) > nb_values: cont = results[nb_values].SourceTimestamp results = results[:nb_values] return results, cont
def test_custom_variant(self): with self.assertRaises(ua.UaError): v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(89)) v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(61)) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.VariantType, v2.VariantType) self.assertEqual(v, v2)
def test_variant_array_dim(self): v = ua.Variant([1, 2, 3, 4, 5, 6], dimensions=[2, 3]) self.assertEqual(v.Value[1], 2) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(_reshape(v.Value, (2, 3)), v2.Value) self.assertEqual(v.VariantType, v2.VariantType) self.assertEqual(v.Dimensions, v2.Dimensions) self.assertEqual(v2.Dimensions, [2, 3])
def test_extension_object(self): obj = ua.UserNameIdentityToken() obj.UserName = "******" obj.Password = b"pass" obj2 = extensionobject_from_binary(ua.utils.Buffer(extensionobject_to_binary(obj))) self.assertEqual(type(obj), type(obj2)) self.assertEqual(obj.UserName, obj2.UserName) self.assertEqual(obj.Password, obj2.Password) v1 = ua.Variant(obj) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v1))) self.assertEqual(type(v1), type(v2)) self.assertEqual(v1.VariantType, v2.VariantType)
def test_variant(self): dv = ua.Variant(True, ua.VariantType.Boolean) self.assertEqual(dv.Value, True) self.assertEqual(type(dv.Value), bool) now = datetime.utcnow() v = ua.Variant(now) self.assertEqual(v.Value, now) self.assertEqual(v.VariantType, ua.VariantType.DateTime) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.Value, v2.Value) self.assertEqual(v.VariantType, v2.VariantType) # commonity method: self.assertEqual(v, ua.Variant(v))
def read_event_history(self, source_id, start, end, nb_values, evfilter): with self._lock: _c_read = self._conn.cursor() table = self._get_table_name(source_id) start_time, end_time, order, limit = self._get_bounds( start, end, nb_values) clauses, clauses_str = self._get_select_clauses( source_id, evfilter) cont = None cont_timestamps = [] results = [] # select events from the database; SQL select clause is built from EventFilter and available fields try: for row in _c_read.execute( 'SELECT "_Timestamp", {cl} FROM "{tn}" WHERE "_Timestamp" BETWEEN ? AND ? ORDER BY "_Id" {dir} LIMIT ?' .format(cl=clauses_str, tn=table, dir=order), (start_time, end_time, limit)): fdict = {} cont_timestamps.append(row[0]) for i, field in enumerate(row[1:]): if field is not None: fdict[clauses[i]] = variant_from_binary( Buffer(field)) else: fdict[clauses[i]] = ua.Variant(None) results.append(events.Event.from_field_dict(fdict)) except sqlite3.Error as e: self.logger.error( 'Historizing SQL Read Error events for node %s: %s', source_id, e) if nb_values: if len(results) > nb_values: # start > ua.get_win_epoch() and cont = cont_timestamps[nb_values] results = results[:nb_values] return results, cont
def read_event_history(self, source_id, start, end, nb_values, evfilter): with self._lock: _c_read = self._conn.cursor() table = self._get_table_name(source_id) start_time, end_time, order, limit = self._get_bounds(start, end, nb_values) clauses, clauses_str = self._get_select_clauses(source_id, evfilter) cont = None cont_timestamps = [] results = [] # select events from the database; SQL select clause is built from EventFilter and available fields try: for row in _c_read.execute( 'SELECT "_Timestamp", {cl} FROM "{tn}" WHERE "_Timestamp" BETWEEN ? AND ? ORDER BY "_Id" {dir} LIMIT ?' .format(cl=clauses_str, tn=table, dir=order), (start_time, end_time, limit)): fdict = {} cont_timestamps.append(row[0]) for i, field in enumerate(row[1:]): if field is not None: fdict[clauses[i]] = variant_from_binary(Buffer(field)) else: fdict[clauses[i]] = ua.Variant(None) results.append(events.Event.from_field_dict(fdict)) except sqlite3.Error as e: self.logger.error('Historizing SQL Read Error events for node %s: %s', source_id, e) if nb_values: if len(results) > nb_values: # start > ua.get_win_epoch() and cont = cont_timestamps[nb_values] results = results[:nb_values] return results, cont
def test_variant_empty_list(self): v = ua.Variant([], varianttype=ua.VariantType.Int32, is_array=True) data = variant_to_binary(v) v2 = variant_from_binary(ua.utils.Buffer(data)) self.assertEqual(v, v2) self.assertTrue(v2.is_array)
def test_custom_variant_array(self): v = ua.Variant([b"ljsdfljds", b"lkjsdljksdf"], ua.VariantTypeCustom(40)) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.VariantType, v2.VariantType) self.assertEqual(v, v2)
def test_guid(self): v = ua.Variant(uuid.uuid4(), ua.VariantType.Guid) v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) self.assertEqual(v.VariantType, v2.VariantType) self.assertEqual(v, v2)