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 _format_event(self, event): """ Convert an event object triggered by the subscription into ordered lists for the SQL insert string Args: event: The event returned by the subscription Returns: List of event fields (SQL column names), List of '?' placeholders, Tuple of variant binaries """ placeholders = [] ev_variant_binaries = [] ev_variant_dict = event.get_event_props_as_fields_dict() names = list(ev_variant_dict.keys()) names.sort() # sort alphabetically since dict is not sorted # split dict into two synchronized lists which will be converted to SQL strings # note that the variants are converted to binary objects for storing in SQL BLOB format for name in names: variant = ev_variant_dict[name] placeholders.append('?') ev_variant_binaries.append(sqlite3.Binary(variant_to_binary(variant))) return self._list_to_sql_str(names), self._list_to_sql_str(placeholders, False), tuple(ev_variant_binaries)
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 _format_event(self, event): """ Convert an event object triggered by the subscription into ordered lists for the SQL insert string Args: event: The event returned by the subscription Returns: List of event fields (SQL column names), List of '?' placeholders, Tuple of variant binaries """ placeholders = [] ev_variant_binaries = [] ev_variant_dict = event.get_event_props_as_fields_dict() names = list(ev_variant_dict.keys()) names.sort() # sort alphabetically since dict is not sorted # split dict into two synchronized lists which will be converted to SQL strings # note that the variants are converted to binary objects for storing in SQL BLOB format for name in names: variant = ev_variant_dict[name] placeholders.append('?') ev_variant_binaries.append( sqlite3.Binary(variant_to_binary(variant))) return self._list_to_sql_str(names), self._list_to_sql_str( placeholders, False), tuple(ev_variant_binaries)
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 save_node_value(self, node_id, datavalue): with self._lock: _c_sub = self._conn.cursor() table = self._get_table_name(node_id) # insert the data change into the database try: _c_sub.execute( 'INSERT INTO "{tn}" VALUES (NULL, ?, ?, ?, ?, ?, ?)'. format(tn=table), (datavalue.ServerTimestamp, datavalue.SourceTimestamp, datavalue.StatusCode.value, str(datavalue.Value.Value), datavalue.Value.VariantType.name, sqlite3.Binary(variant_to_binary(datavalue.Value)))) except sqlite3.Error as e: self.logger.error('Historizing SQL Insert Error for %s: %s', node_id, e) self._conn.commit() # get this node's period from the period dict and calculate the limit period, count = self._datachanges_period[node_id] def execute_sql_delete(condition, args): query = ('DELETE FROM "{tn}" WHERE ' + condition).format(tn=table) try: _c_sub.execute(query, args) except sqlite3.Error as e: self.logger.error( 'Historizing SQL Delete Old Data Error for %s: %s', node_id, e) self._conn.commit() if period: # after the insert, if a period was specified delete all records older than period date_limit = datetime.utcnow() - period execute_sql_delete('SourceTimestamp < ?', (date_limit, )) if count: # ensure that no more than count records are stored for the specified node execute_sql_delete( 'SourceTimestamp = (SELECT CASE WHEN COUNT(*) > ? ' 'THEN MIN(SourceTimestamp) ELSE NULL END FROM "{tn}")', (count, ))
def save_node_value(self, node_id, datavalue): with self._lock: _c_sub = self._conn.cursor() table = self._get_table_name(node_id) # insert the data change into the database try: _c_sub.execute('INSERT INTO "{tn}" VALUES (NULL, ?, ?, ?, ?, ?, ?)'.format(tn=table), ( datavalue.ServerTimestamp, datavalue.SourceTimestamp, datavalue.StatusCode.value, str(datavalue.Value.Value), datavalue.Value.VariantType.name, sqlite3.Binary(variant_to_binary(datavalue.Value)) ) ) except sqlite3.Error as e: self.logger.error('Historizing SQL Insert Error for %s: %s', node_id, e) self._conn.commit() # get this node's period from the period dict and calculate the limit period, count = self._datachanges_period[node_id] def execute_sql_delete(condition, args): query = ('DELETE FROM "{tn}" WHERE ' + condition).format(tn=table) try: _c_sub.execute(query, args) except sqlite3.Error as e: self.logger.error('Historizing SQL Delete Old Data Error for %s: %s', node_id, e) self._conn.commit() if period: # after the insert, if a period was specified delete all records older than period date_limit = datetime.utcnow() - period execute_sql_delete('SourceTimestamp < ?', (date_limit,)) if count: # ensure that no more than count records are stored for the specified node execute_sql_delete('SourceTimestamp = (SELECT CASE WHEN COUNT(*) > ? ' 'THEN MIN(SourceTimestamp) ELSE NULL END FROM "{tn}")', (count,))
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)
def to_binary(self): from opcua.ua.ua_binary import variant_to_binary return variant_to_binary(self)