MCPcopy Create free account
hub / github.com/FreeOpcUa/python-opcua / save_node_value

Method save_node_value

opcua/server/history_sql.py:55–99  ·  view source on GitHub ↗
(self, node_id, datavalue)

Source from the content-addressed store, hash-verified

53 self._conn.commit()
54
55 def save_node_value(self, node_id, datavalue):
56 with self._lock:
57 _c_sub = self._conn.cursor()
58
59 table = self._get_table_name(node_id)
60
61 # insert the data change into the database
62 try:
63 _c_sub.execute('INSERT INTO "{tn}" VALUES (NULL, ?, ?, ?, ?, ?, ?)'.format(tn=table),
64 (
65 datavalue.ServerTimestamp,
66 datavalue.SourceTimestamp,
67 datavalue.StatusCode.value,
68 str(datavalue.Value.Value),
69 datavalue.Value.VariantType.name,
70 sqlite3.Binary(variant_to_binary(datavalue.Value))
71 )
72 )
73 except sqlite3.Error as e:
74 self.logger.error('Historizing SQL Insert Error for %s: %s', node_id, e)
75
76 self._conn.commit()
77
78 # get this node's period from the period dict and calculate the limit
79 period, count = self._datachanges_period[node_id]
80
81 def execute_sql_delete(condition, args):
82 query = ('DELETE FROM "{tn}" WHERE ' + condition).format(tn=table)
83
84 try:
85 _c_sub.execute(query, args)
86 except sqlite3.Error as e:
87 self.logger.error('Historizing SQL Delete Old Data Error for %s: %s', node_id, e)
88
89 self._conn.commit()
90
91 if period:
92 # after the insert, if a period was specified delete all records older than period
93 date_limit = datetime.utcnow() - period
94 execute_sql_delete('SourceTimestamp < ?', (date_limit,))
95
96 if count:
97 # ensure that no more than count records are stored for the specified node
98 execute_sql_delete('SourceTimestamp = (SELECT CASE WHEN COUNT(*) > ? '
99 'THEN MIN(SourceTimestamp) ELSE NULL END FROM "{tn}")', (count,))
100
101 def read_node_history(self, node_id, start, end, nb_values):
102 with self._lock:

Callers

nothing calls this directly

Calls 2

_get_table_nameMethod · 0.95
variant_to_binaryFunction · 0.90

Tested by

no test coverage detected