00001 import time
00002 from datetime import datetime, timedelta
00003 import unittest
00004
00005 from opcua import Client
00006 from opcua import Server
00007 from opcua import ua
00008
00009 from opcua.server.history_sql import HistorySQLite
00010 from opcua.server.history import HistoryDict
00011
00012 from tests_common import CommonTests, add_server_methods
00013
00014 from opcua.common.events import get_event_properties_from_type_node as get_props
00015
00016 port_num1 = 48530
00017 port_num2 = 48530
00018
00019
00020 class HistoryCommon(object):
00021 srv = Server
00022 clt = Client
00023
00024 @classmethod
00025 def start_server_and_client(cls):
00026 cls.srv = Server()
00027 cls.srv.set_endpoint('opc.tcp://localhost:{0:d}'.format(port_num1))
00028 cls.srv.start()
00029
00030 cls.clt = Client('opc.tcp://localhost:{0:d}'.format(port_num1))
00031 cls.clt.connect()
00032
00033 @classmethod
00034 def stop_server_and_client(cls):
00035 cls.clt.disconnect()
00036 cls.srv.stop()
00037
00038 @classmethod
00039 def create_var(cls):
00040 o = cls.srv.get_objects_node()
00041 cls.values = [i for i in range(20)]
00042 cls.var = o.add_variable(3, "history_var", 0)
00043 cls.srv.historize_node_data_change(cls.var, period=None, count=0)
00044 for i in cls.values:
00045 cls.var.set_value(i)
00046 time.sleep(1)
00047
00048
00049 def test_history_var_read_one(self):
00050
00051
00052 res = self.var.read_raw_history(None, None, 1)
00053 self.assertEqual(len(res), 1)
00054 self.assertEqual(res[0].Value.Value, self.values[-1])
00055
00056
00057 def test_history_var_read_none(self):
00058 res = self.var.read_raw_history(None, None, 0)
00059 self.assertEqual(len(res), 20)
00060 self.assertEqual(res[0].Value.Value, self.values[-1])
00061 self.assertEqual(res[-1].Value.Value, self.values[0])
00062
00063
00064 def test_history_var_read_last_3(self):
00065 res = self.var.read_raw_history(None, None, 3)
00066 self.assertEqual(len(res), 3)
00067 self.assertEqual(res[-1].Value.Value, self.values[-3])
00068 self.assertEqual(res[0].Value.Value, self.values[-1])
00069
00070
00071 def test_history_var_read_all2(self):
00072 res = self.var.read_raw_history(None, None, 9999)
00073 self.assertEqual(len(res), 20)
00074 self.assertEqual(res[-1].Value.Value, self.values[0])
00075 self.assertEqual(res[0].Value.Value, self.values[-1])
00076
00077
00078 def test_history_var_read_2_with_end(self):
00079 now = datetime.utcnow()
00080 old = now - timedelta(days=6)
00081
00082 res = self.var.read_raw_history(None, now, 2)
00083 self.assertEqual(len(res), 2)
00084 self.assertEqual(res[-1].Value.Value, self.values[-2])
00085
00086
00087 def test_history_var_read_all(self):
00088 now = datetime.utcnow()
00089 old = now - timedelta(days=6)
00090
00091 res = self.var.read_raw_history(old, now, 0)
00092 self.assertEqual(len(res), 20)
00093 self.assertEqual(res[-1].Value.Value, self.values[-1])
00094 self.assertEqual(res[0].Value.Value, self.values[0])
00095
00096 def test_history_var_read_5_in_timeframe(self):
00097 now = datetime.utcnow()
00098 old = now - timedelta(days=6)
00099
00100 res = self.var.read_raw_history(old, now, 5)
00101 self.assertEqual(len(res), 5)
00102 self.assertEqual(res[-1].Value.Value, self.values[4])
00103 self.assertEqual(res[0].Value.Value, self.values[0])
00104
00105
00106 def test_history_var_read_5_in_timeframe_start_greater_than_end(self):
00107 now = datetime.utcnow()
00108 old = now - timedelta(days=6)
00109
00110 res = self.var.read_raw_history(now, old, 5)
00111 self.assertEqual(len(res), 5)
00112 self.assertEqual(res[-1].Value.Value, self.values[-5])
00113 self.assertEqual(res[0].Value.Value, self.values[-1])
00114
00115
00116 def test_history_var_read_6_with_start(self):
00117 now = datetime.utcnow()
00118 old = now - timedelta(days=6)
00119 res = self.var.read_raw_history(old, None, 6)
00120 self.assertEqual(len(res), 6)
00121 self.assertEqual(res[-1].Value.Value, self.values[5])
00122 self.assertEqual(res[0].Value.Value, self.values[0])
00123
00124
00125 def test_history_var_read_all_with_start(self):
00126 now = datetime.utcnow()
00127 old = now - timedelta(days=6)
00128 res = self.var.read_raw_history(old, None, 0)
00129 self.assertEqual(len(res), 20)
00130 self.assertEqual(res[-1].Value.Value, self.values[-1])
00131 self.assertEqual(res[0].Value.Value, self.values[0])
00132
00133
00134 def test_history_var_read_all_with_end(self):
00135 end = datetime.utcnow() + timedelta(days=6)
00136 res = self.var.read_raw_history(None, end, 0)
00137 self.assertEqual(len(res), 20)
00138 self.assertEqual(res[-1].Value.Value, self.values[0])
00139 self.assertEqual(res[0].Value.Value, self.values[-1])
00140
00141
00142 def test_history_var_read_3_with_end(self):
00143 end = datetime.utcnow() + timedelta(days=6)
00144 res = self.var.read_raw_history(None, end, 3)
00145 self.assertEqual(len(res), 3)
00146 self.assertEqual(res[2].Value.Value, self.values[-3])
00147 self.assertEqual(res[0].Value.Value, self.values[-1])
00148
00149
00150 class TestHistoryEvents(object):
00151
00152 @classmethod
00153 def create_srv_events(cls):
00154 cls.ev_values = [i for i in range(20)]
00155 cls.srvevgen = cls.srv.get_event_generator()
00156
00157 cls.srv_node = cls.srv.get_node(ua.ObjectIds.Server)
00158 cls.srv.historize_node_event(cls.srv_node, period=None)
00159
00160 for i in cls.ev_values:
00161 cls.srvevgen.event.Severity = cls.ev_values[i]
00162 cls.srvevgen.trigger(message="test message")
00163 time.sleep(.1)
00164 time.sleep(2)
00165
00166
00167 def test_history_ev_read_2_with_end(self):
00168 now = datetime.utcnow()
00169 old = now - timedelta(days=6)
00170
00171 res = self.srv_node.read_event_history(None, now, 2)
00172 self.assertEqual(len(res), 2)
00173 self.assertEqual(res[-1].Severity, self.ev_values[-2])
00174
00175
00176 def test_history_ev_read_all(self):
00177 now = datetime.utcnow()
00178 old = now - timedelta(days=6)
00179
00180 res = self.srv_node.read_event_history(old, now, 0)
00181 self.assertEqual(len(res), 20)
00182 self.assertEqual(res[-1].Severity, self.ev_values[-1])
00183 self.assertEqual(res[0].Severity, self.ev_values[0])
00184
00185 def test_history_ev_read_5_in_timeframe(self):
00186 now = datetime.utcnow()
00187 old = now - timedelta(days=6)
00188
00189 res = self.srv_node.read_event_history(old, now, 5)
00190 self.assertEqual(len(res), 5)
00191 self.assertEqual(res[-1].Severity, self.ev_values[4])
00192 self.assertEqual(res[0].Severity, self.ev_values[0])
00193
00194
00195 def test_history_ev_read_5_in_timeframe_start_greater_than_end(self):
00196 now = datetime.utcnow()
00197 old = now - timedelta(days=6)
00198
00199 res = self.srv_node.read_event_history(now, old, 5)
00200 self.assertEqual(len(res), 5)
00201 self.assertEqual(res[-1].Severity, self.ev_values[-5])
00202 self.assertEqual(res[0].Severity, self.ev_values[-1])
00203
00204
00205 def test_history_ev_read_6_with_start(self):
00206 now = datetime.utcnow()
00207 old = now - timedelta(days=6)
00208 res = self.srv_node.read_event_history(old, None, 6)
00209 self.assertEqual(len(res), 6)
00210 self.assertEqual(res[-1].Severity, self.ev_values[5])
00211 self.assertEqual(res[0].Severity, self.ev_values[0])
00212
00213
00214 def test_history_ev_read_all_with_start(self):
00215 now = datetime.utcnow()
00216 old = now - timedelta(days=6)
00217 res = self.srv_node.read_event_history(old, None, 0)
00218 self.assertEqual(len(res), 20)
00219 self.assertEqual(res[-1].Severity, self.ev_values[-1])
00220 self.assertEqual(res[0].Severity, self.ev_values[0])
00221
00222
00223 def test_history_ev_read_all_with_end(self):
00224 end = datetime.utcnow() + timedelta(days=6)
00225 res = self.srv_node.read_event_history(None, end, 0)
00226 self.assertEqual(len(res), 20)
00227 self.assertEqual(res[-1].Severity, self.ev_values[0])
00228 self.assertEqual(res[0].Severity, self.ev_values[-1])
00229
00230
00231 def test_history_ev_read_3_with_end(self):
00232 end = datetime.utcnow() + timedelta(days=6)
00233 res = self.srv_node.read_event_history(None, end, 3)
00234 self.assertEqual(len(res), 3)
00235 self.assertEqual(res[2].Severity, self.ev_values[-3])
00236 self.assertEqual(res[0].Severity, self.ev_values[-1])
00237
00238
00239 def test_history_ev_read_all_filter_order_reversed(self):
00240 now = datetime.utcnow()
00241 old = now - timedelta(days=6)
00242 res = self.srv_node.read_event_history(old, None, 0)
00243 self.assertEqual(len(res), 20)
00244 self.assertEqual(res[-1].Severity, self.ev_values[-1])
00245 self.assertEqual(res[0].Severity, self.ev_values[0])
00246
00247
00248 class TestHistoryLimitsCommon(unittest.TestCase):
00249 id = ua.NodeId(123)
00250
00251 def setUp(self):
00252 self.history = self.createHistoryInstance()
00253
00254 def createHistoryInstance(self):
00255 assert(False)
00256
00257 def resultCount(self):
00258 results, cont = self.history.read_node_history(self.id, None, None, None)
00259 return len(results)
00260
00261 def addValue(self, age):
00262 value = ua.DataValue()
00263 value.ServerTimestamp = datetime.utcnow() - timedelta(hours = age)
00264 self.history.save_node_value(self.id, value)
00265
00266 def test_count_limit(self):
00267 self.history.new_historized_node(self.id, period=None, count=3)
00268 self.assertEqual(self.resultCount(), 0)
00269 self.addValue(5)
00270 self.assertEqual(self.resultCount(), 1)
00271 self.addValue(4)
00272 self.assertEqual(self.resultCount(), 2)
00273 self.addValue(3)
00274 self.assertEqual(self.resultCount(), 3)
00275 self.addValue(2)
00276 self.assertEqual(self.resultCount(), 3)
00277 self.addValue(1)
00278 self.assertEqual(self.resultCount(), 3)
00279
00280 def test_period_limit(self):
00281 self.history.new_historized_node(self.id, period=timedelta(hours=3))
00282 self.assertEqual(self.resultCount(), 0)
00283 self.addValue(5)
00284 self.assertEqual(self.resultCount(), 0)
00285 self.addValue(4)
00286 self.assertEqual(self.resultCount(), 0)
00287 self.addValue(2)
00288 self.assertEqual(self.resultCount(), 1)
00289 self.addValue(1)
00290 self.assertEqual(self.resultCount(), 2)
00291 self.addValue(0)
00292 self.assertEqual(self.resultCount(), 3)
00293
00294 def test_combined_limit(self):
00295 self.history.new_historized_node(self.id, period=timedelta(hours=3), count=2)
00296 self.assertEqual(self.resultCount(), 0)
00297 self.addValue(5)
00298 self.assertEqual(self.resultCount(), 0)
00299 self.addValue(4)
00300 self.assertEqual(self.resultCount(), 0)
00301 self.addValue(2)
00302 self.assertEqual(self.resultCount(), 1)
00303 self.addValue(1)
00304 self.assertEqual(self.resultCount(), 2)
00305 self.addValue(0)
00306 self.assertEqual(self.resultCount(), 2)
00307
00308
00309 class TestHistoryLimits(TestHistoryLimitsCommon):
00310 def createHistoryInstance(self):
00311 return HistoryDict()
00312
00313
00314 class TestHistorySQLLimits(TestHistoryLimitsCommon):
00315 def createHistoryInstance(self):
00316 return HistorySQLite(":memory:")
00317
00318
00319 class TestHistory(unittest.TestCase, HistoryCommon, TestHistoryEvents):
00320
00321 @classmethod
00322 def setUpClass(cls):
00323 cls.start_server_and_client()
00324 cls.create_var()
00325 cls.create_srv_events()
00326
00327 @classmethod
00328 def tearDownClass(cls):
00329 cls.stop_server_and_client()
00330
00331
00332 class TestHistorySQL(unittest.TestCase, HistoryCommon, TestHistoryEvents):
00333 @classmethod
00334 def setUpClass(cls):
00335 cls.start_server_and_client()
00336 cls.srv.iserver.history_manager.set_storage(HistorySQLite(":memory:"))
00337 cls.create_var()
00338 cls.create_srv_events()
00339
00340 @classmethod
00341 def tearDownClass(cls):
00342 cls.stop_server_and_client()