|
1 | 1 |
|
| 2 | +import sys |
2 | 3 | import os.path |
3 | 4 | import time |
4 | 5 | import datetime |
| 6 | +import sqlite3 |
5 | 7 | from struct import pack |
6 | 8 |
|
7 | 9 | from opcua import ua |
@@ -83,6 +85,7 @@ class AddressSpaceSQLite(AddressSpace): |
83 | 85 | Load the standard address space nodes from a SQLite database. |
84 | 86 | Intended for slow devices, such as Raspberry Pi, to greatly improve start up time |
85 | 87 | """ |
| 88 | + PY2 = sys.version_info < (3, 0) |
86 | 89 | ATTR_TABLE_NAME = 'Attributes' |
87 | 90 | REFS_TABLE_NAME = 'References' |
88 | 91 | CUR_TIME_NODEID = NumericNodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime, 0) |
@@ -258,11 +261,18 @@ def _write_references(self, nodeid, ndata): |
258 | 261 | def _read_nodedata(backend, nodeid, ndata): |
259 | 262 | # Search key = numeric nodeid in opc-ua binary format |
260 | 263 | numNodeId = AddressSpaceSQLite._nodeid_to_numeric(nodeid) |
261 | | - hexNodeId = ua.ua_binary.nodeid_to_binary(numNodeId).hex() |
| 264 | + hexNodeId = AddressSpaceSQLite._to_hex(ua.ua_binary.nodeid_to_binary(numNodeId)) |
262 | 265 |
|
263 | 266 | AddressSpaceSQLite._read_attributes(backend, hexNodeId, ndata) |
264 | 267 | AddressSpaceSQLite._read_references(backend, hexNodeId, ndata) |
265 | 268 |
|
| 269 | + @staticmethod |
| 270 | + def _to_hex(b): |
| 271 | + if AddressSpaceSQLite.PY2: |
| 272 | + return "".join("{:02x}".format(ord(c)) for c in b) |
| 273 | + else: |
| 274 | + return b.hex() |
| 275 | + |
266 | 276 | @staticmethod |
267 | 277 | def _read_attributes(backend, hexNodeId, ndata, attrTable=ATTR_TABLE_NAME): |
268 | 278 | cmd = 'SELECT * FROM "{tn}" WHERE NodeId = x\'{h}\''.format(tn=attrTable, h=hexNodeId) |
@@ -339,15 +349,15 @@ def _insert_attribute(backend, nodeid, attrId, attr, table=ATTR_TABLE_NAME): |
339 | 349 |
|
340 | 350 | cmd = 'INSERT OR REPLACE INTO "{tn}" VALUES ({q})'.format(tn=table, q=', '.join('?'*10)) |
341 | 351 | params = ( |
342 | | - memoryview(primaryKey), |
343 | | - memoryview(binNodeId), |
| 352 | + sqlite3.Binary(primaryKey), |
| 353 | + sqlite3.Binary(binNodeId), |
344 | 354 | int(attrId), |
345 | 355 | attr.value.ServerTimestamp, |
346 | 356 | None if attr.value.ServerPicoseconds is None else int(attr.value.ServerPicoseconds), |
347 | 357 | attr.value.SourceTimestamp, |
348 | 358 | None if attr.value.SourcePicoseconds is None else int(attr.value.SourcePicoseconds), |
349 | 359 | int(attr.value.StatusCode.value), |
350 | | - memoryview(ua.ua_binary.variant_to_binary(attr.value.Value)), |
| 360 | + sqlite3.Binary(ua.ua_binary.variant_to_binary(attr.value.Value)), |
351 | 361 | str(nodeid) |
352 | 362 | ) |
353 | 363 | backend.execute_write(cmd, params=params) |
@@ -425,18 +435,18 @@ def _insert_reference(backend, nodeid, ref, table=REFS_TABLE_NAME): |
425 | 435 |
|
426 | 436 | cmd = 'INSERT OR REPLACE INTO "{tn}" VALUES ({q})'.format(tn=table, q=', '.join('?'*13)) |
427 | 437 | params = ( |
428 | | - memoryview(primaryKey), |
429 | | - memoryview(binNodeId), |
430 | | - memoryview(ua.ua_binary.nodeid_to_binary(ref.ReferenceTypeId)), |
| 438 | + sqlite3.Binary(primaryKey), |
| 439 | + sqlite3.Binary(binNodeId), |
| 440 | + sqlite3.Binary(ua.ua_binary.nodeid_to_binary(ref.ReferenceTypeId)), |
431 | 441 | int(bool(ref.IsForward)), |
432 | | - memoryview(refNodeId), |
| 442 | + sqlite3.Binary(refNodeId), |
433 | 443 | int(ref.BrowseName.NamespaceIndex), |
434 | 444 | None if ref.BrowseName.Name is None else str(ref.BrowseName.Name), |
435 | 445 | None if ref.DisplayName.Text is None else str(ref.DisplayName.Text), |
436 | 446 | None if ref.DisplayName.Locale is None else str(ref.DisplayName.Locale), |
437 | 447 | int(ref.DisplayName.Encoding), |
438 | 448 | int(ref.NodeClass), |
439 | | - memoryview(ua.ua_binary.nodeid_to_binary(ref.TypeDefinition)), |
| 449 | + sqlite3.Binary(ua.ua_binary.nodeid_to_binary(ref.TypeDefinition)), |
440 | 450 | str(nodeid) |
441 | 451 | ) |
442 | 452 | backend.execute_write(cmd, params=params) |
|
0 commit comments