|
1
|
1 |
|
import time |
|
2
|
1 |
|
from datetime import datetime, timedelta |
|
3
|
1 |
|
import unittest |
|
4
|
|
|
|
|
5
|
1 |
|
from opcua import Client |
|
6
|
1 |
|
from opcua import Server |
|
7
|
1 |
|
from opcua import ua |
|
8
|
1 |
|
from opcua.server.history_sql import HistorySQLite |
|
9
|
|
|
|
|
10
|
1 |
|
from tests_common import CommonTests, add_server_methods |
|
11
|
|
|
|
|
12
|
1 |
|
port_num1 = 48530 |
|
13
|
1 |
|
port_num2 = 48530 |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
1 |
|
class HistoryCommon(object): |
|
18
|
1 |
|
srv = Server |
|
19
|
1 |
|
clt = Client |
|
20
|
|
|
|
|
21
|
1 |
|
def start_server_and_client(self): |
|
22
|
1 |
|
self.srv = Server() |
|
23
|
1 |
|
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num1) |
|
24
|
1 |
|
self.srv.start() |
|
25
|
|
|
|
|
26
|
1 |
|
self.clt = Client('opc.tcp://localhost:%d' % port_num1) |
|
27
|
1 |
|
self.clt.connect() |
|
28
|
|
|
|
|
29
|
1 |
|
def stop_server_and_client(self): |
|
30
|
1 |
|
self.clt.disconnect() |
|
31
|
1 |
|
self.srv.stop() |
|
32
|
|
|
|
|
33
|
1 |
|
def create_var(self): |
|
34
|
1 |
|
o = self.srv.get_objects_node() |
|
35
|
1 |
|
self.values = [i for i in range(20)] |
|
36
|
1 |
|
self.var = o.add_variable(3, "history_var", 0) |
|
37
|
1 |
|
self.srv.iserver.enable_history(self.var, period=None, count=10) |
|
38
|
1 |
|
for i in self.values: |
|
39
|
1 |
|
self.var.set_value(i) |
|
40
|
1 |
|
time.sleep(1) |
|
41
|
|
|
|
|
42
|
|
|
# no start and no end is not defined by spec, return reverse order |
|
43
|
1 |
|
def test_history_read_one(self): |
|
44
|
|
|
# Spec says that at least two parameters should be provided, so |
|
45
|
|
|
# this one is out of spec |
|
46
|
1 |
|
res = self.var.read_raw_history(None, None, 1) |
|
47
|
1 |
|
self.assertEqual(len(res), 1) |
|
48
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
49
|
|
|
|
|
50
|
|
|
# no start and no end is not defined by spec, return reverse order |
|
51
|
1 |
|
def test_history_read_none(self): |
|
52
|
1 |
|
res = self.var.read_raw_history(None, None, 0) |
|
53
|
1 |
|
self.assertEqual(len(res), 20) |
|
54
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
55
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[0]) |
|
56
|
|
|
|
|
57
|
|
|
# no start and no end is not defined by spec, return reverse order |
|
58
|
1 |
|
def test_history_read_last_3(self): |
|
59
|
1 |
|
res = self.var.read_raw_history(None, None, 3) |
|
60
|
1 |
|
self.assertEqual(len(res), 3) |
|
61
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[-3]) |
|
62
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
63
|
|
|
|
|
64
|
|
|
# no start and no end is not defined by spec, return reverse order |
|
65
|
1 |
|
def test_history_read_all2(self): |
|
66
|
1 |
|
res = self.var.read_raw_history(None, None, 9999) |
|
67
|
1 |
|
self.assertEqual(len(res), 20) |
|
68
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[0]) |
|
69
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
70
|
|
|
|
|
71
|
|
|
# only has end time, should return reverse order |
|
72
|
1 |
|
def test_history_read_2_with_end(self): |
|
73
|
1 |
|
now = datetime.utcnow() |
|
74
|
1 |
|
old = now - timedelta(days=6) |
|
75
|
|
|
|
|
76
|
1 |
|
res = self.var.read_raw_history(None, now, 2) |
|
77
|
1 |
|
self.assertEqual(len(res), 2) |
|
78
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[-2]) |
|
79
|
|
|
|
|
80
|
|
|
# both start and endtime, return from start to end |
|
81
|
1 |
View Code Duplication |
def test_history_read_all(self): |
|
|
|
|
|
|
82
|
1 |
|
now = datetime.utcnow() |
|
83
|
1 |
|
old = now - timedelta(days=6) |
|
84
|
|
|
|
|
85
|
1 |
|
res = self.var.read_raw_history(old, now, 0) |
|
86
|
1 |
|
self.assertEqual(len(res), 20) |
|
87
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[-1]) |
|
88
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[0]) |
|
89
|
|
|
|
|
90
|
1 |
View Code Duplication |
def test_history_read_5_in_timeframe(self): |
|
|
|
|
|
|
91
|
1 |
|
now = datetime.utcnow() |
|
92
|
1 |
|
old = now - timedelta(days=6) |
|
93
|
|
|
|
|
94
|
1 |
|
res = self.var.read_raw_history(old, now, 5) |
|
95
|
1 |
|
self.assertEqual(len(res), 5) |
|
96
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[4]) |
|
97
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[0]) |
|
98
|
|
|
|
|
99
|
|
|
# start time greater than end time, should return reverse order |
|
100
|
1 |
View Code Duplication |
def test_history_read_5_in_timeframe_start_greater_than_end(self): |
|
|
|
|
|
|
101
|
1 |
|
now = datetime.utcnow() |
|
102
|
1 |
|
old = now - timedelta(days=6) |
|
103
|
|
|
|
|
104
|
1 |
|
res = self.var.read_raw_history(now, old, 5) |
|
105
|
1 |
|
self.assertEqual(len(res), 5) |
|
106
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[-5]) |
|
107
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
108
|
|
|
|
|
109
|
|
|
# only start return original order |
|
110
|
1 |
View Code Duplication |
def test_history_read_6_with_start(self): |
|
|
|
|
|
|
111
|
1 |
|
now = datetime.utcnow() |
|
112
|
1 |
|
old = now - timedelta(days=6) |
|
113
|
1 |
|
res = self.var.read_raw_history(old, None, 6) |
|
114
|
1 |
|
self.assertEqual(len(res), 6) |
|
115
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[5]) |
|
116
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[0]) |
|
117
|
|
|
|
|
118
|
|
|
# only start return original order |
|
119
|
1 |
View Code Duplication |
def test_history_read_all_with_start(self): |
|
|
|
|
|
|
120
|
1 |
|
now = datetime.utcnow() |
|
121
|
1 |
|
old = now - timedelta(days=6) |
|
122
|
1 |
|
res = self.var.read_raw_history(old, None, 0) |
|
123
|
1 |
|
self.assertEqual(len(res), 20) |
|
124
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[-1]) |
|
125
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[0]) |
|
126
|
|
|
|
|
127
|
|
|
# only end return reversed order |
|
128
|
1 |
|
def test_history_read_all_with_end(self): |
|
129
|
1 |
|
end = datetime.utcnow() + timedelta(days=6) |
|
130
|
1 |
|
res = self.var.read_raw_history(None, end, 0) |
|
131
|
1 |
|
self.assertEqual(len(res), 20) |
|
132
|
1 |
|
self.assertEqual(res[-1].Value.Value, self.values[0]) |
|
133
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
134
|
|
|
|
|
135
|
|
|
# only end return reversed order |
|
136
|
1 |
|
def test_history_read_3_with_end(self): |
|
137
|
1 |
|
end = datetime.utcnow() + timedelta(days=6) |
|
138
|
1 |
|
res = self.var.read_raw_history(None, end, 3) |
|
139
|
1 |
|
self.assertEqual(len(res), 3) |
|
140
|
1 |
|
self.assertEqual(res[2].Value.Value, self.values[-3]) |
|
141
|
1 |
|
self.assertEqual(res[0].Value.Value, self.values[-1]) |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
1 |
|
class TestHistory(unittest.TestCase, HistoryCommon): |
|
147
|
|
|
|
|
148
|
1 |
|
@classmethod |
|
149
|
|
|
def setUpClass(self): |
|
150
|
1 |
|
self.start_server_and_client(self) |
|
151
|
1 |
|
self.create_var(self) |
|
152
|
|
|
|
|
153
|
1 |
|
@classmethod |
|
154
|
|
|
def tearDownClass(self): |
|
155
|
1 |
|
self.stop_server_and_client(self) |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
1 |
|
class TestHistorySQL(unittest.TestCase, HistoryCommon): |
|
159
|
1 |
|
@classmethod |
|
160
|
|
|
def setUpClass(self): |
|
161
|
1 |
|
self.start_server_and_client(self) |
|
162
|
1 |
|
self.srv.iserver.history_manager.set_storage(HistorySQLite(":memory:")) |
|
163
|
1 |
|
self.create_var(self) |
|
164
|
|
|
|
|
165
|
1 |
|
@classmethod |
|
166
|
|
|
def tearDownClass(self): |
|
167
|
1 |
|
self.stop_server_and_client(self) |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
|