18
18
import thread
19
19
20
20
21
-
22
21
class Error (Exception ):
23
22
"""Base class for exceptions in this module."""
24
23
@@ -112,8 +111,6 @@ def __init__(self, intermediate_yaml_path, yaml_index):
112
111
self .plcs_ready = False
113
112
114
113
for tag in set (dependant_sensors ) - set (plc_sensors ):
115
-
116
- #todo: We should query the DB for this
117
114
self .cache [tag ] = Decimal (0 )
118
115
self .tag_fresh [tag ] = False
119
116
@@ -132,7 +129,6 @@ def generate_real_tags(sensors, dependants, actuators):
132
129
"""
133
130
Generates real tags with all sensors, dependants, and actuators
134
131
attached to the plc.
135
-
136
132
:param sensors: list of sensors attached to the plc
137
133
:param dependants: list of dependant sensors (from other plcs)
138
134
:param actuators: list of actuators controlled by the plc
@@ -155,7 +151,6 @@ def generate_real_tags(sensors, dependants, actuators):
155
151
def generate_tags (taggable ):
156
152
"""
157
153
Generates tags from a list of taggable entities (sensor or actuator)
158
-
159
154
:param taggable: a list of strings containing names of things like tanks, pumps, and valves
160
155
"""
161
156
tags = []
@@ -171,7 +166,6 @@ def generate_tags(taggable):
171
166
def create_controls (controls_list ):
172
167
"""
173
168
Generates list of control objects for a plc
174
-
175
169
:param controls_list: a list of the control dicts to be converted to Control objects
176
170
"""
177
171
ret = []
@@ -195,7 +189,6 @@ def create_controls(controls_list):
195
189
@staticmethod
196
190
def create_attacks (attack_list ):
197
191
"""This function will create an array of DeviceAttacks
198
-
199
192
:param attack_list: A list of attack dicts that need to be converted to DeviceAttacks
200
193
"""
201
194
attacks = []
@@ -226,7 +219,6 @@ def pre_loop(self, sleep=0.5):
226
219
"""
227
220
The pre loop of a PLC. In everything is setup. Like starting the sending thread through
228
221
the :class:`~dhalsim.python2.basePLC` class.
229
-
230
222
:param sleep: (Default value = 0.5) The time to sleep after setting everything up
231
223
"""
232
224
self .logger .debug (self .intermediate_plc ['name' ] + ' enters pre_loop' )
@@ -259,7 +251,6 @@ def pre_loop(self, sleep=0.5):
259
251
def get_tag (self , tag ):
260
252
"""
261
253
Get the value of a tag that is connected to this PLC or over the network.
262
-
263
254
:param tag: The tag to get
264
255
:type tag: str
265
256
:return: value of that tag
@@ -317,15 +308,14 @@ def update_cache(self, a, cache_update_time):
317
308
res = self .get_tag_for_cache (cached_tag , plc_data ["public_ip" ], cache_update_time )
318
309
if self .get_master_clock () == start_iteration :
319
310
self .tag_fresh [cached_tag ] = res
320
-
311
+
321
312
if not self .tag_fresh [cached_tag ]:
322
313
self .logger .info ("Warning: Cache for tag " + str (cached_tag ) + " could not be updated" )
323
314
self .tag_fresh [cached_tag ] = True
324
315
325
316
def set_tag (self , tag , value ):
326
317
"""
327
318
Set a tag that is connected to this PLC to a value.
328
-
329
319
:param tag: Which tag to set
330
320
:type tag: str
331
321
:param value: value to set the Tag to
@@ -349,14 +339,10 @@ def db_query(self, query, write=False, parameters=None):
349
339
On a :code:`sqlite3.OperationalError` it will retry with a max of :code:`DB_TRIES` tries.
350
340
Before it reties, it will sleep for :code:`DB_SLEEP_TIME` seconds.
351
341
This is necessary because of the limited concurrency in SQLite.
352
-
353
342
:param query: The SQL query to execute in the db
354
343
:type query: str
355
-
356
344
:param write: Boolean flag to indicate if this query will write into the database
357
-
358
345
:param parameters: The parameters to put in the query. This must be a tuple.
359
-
360
346
:raise DatabaseError: When a :code:`sqlite3.OperationalError` is still raised after
361
347
:code:`DB_TRIES` tries.
362
348
"""
@@ -389,9 +375,7 @@ def get_master_clock(self):
389
375
Get the value of the master clock of the physical process through the database.
390
376
On a :code:`sqlite3.OperationalError` it will retry with a max of :code:`DB_TRIES` tries.
391
377
Before it reties, it will sleep for :code:`DB_SLEEP_TIME` seconds.
392
-
393
378
:return: Iteration in the physical process.
394
-
395
379
:raise DatabaseError: When a :code:`sqlite3.OperationalError` is still raised after
396
380
:code:`DB_TRIES` tries.
397
381
"""
@@ -403,9 +387,7 @@ def get_sync(self, flag):
403
387
Get the sync flag of this plc.
404
388
On a :code:`sqlite3.OperationalError` it will retry with a max of :code:`DB_TRIES` tries.
405
389
Before it reties, it will sleep for :code:`DB_SLEEP_TIME` seconds.
406
-
407
390
:return: False if physical process wants the plc to do a iteration, True if not.
408
-
409
391
:raise DatabaseError: When a :code:`sqlite3.OperationalError` is still raised after
410
392
:code:`DB_TRIES` tries.
411
393
"""
@@ -418,10 +400,8 @@ def set_sync(self, flag):
418
400
knows this plc finished the requested iteration.
419
401
On a :code:`sqlite3.OperationalError` it will retry with a max of :code:`DB_TRIES` tries.
420
402
Before it reties, it will sleep for :code:`DB_SLEEP_TIME` seconds.
421
-
422
403
:param flag: True for sync to 1, False for sync to 0
423
404
:type flag: bool
424
-
425
405
:raise DatabaseError: When a :code:`sqlite3.OperationalError` is still raised after
426
406
:code:`DB_TRIES` tries.
427
407
"""
@@ -433,13 +413,10 @@ def set_attack_flag(self, flag, attack_name):
433
413
provided name is currently running. When it is 0, it is not.
434
414
On a :code:`sqlite3.OperationalError` it will retry with a max of :code:`DB_TRIES` tries.
435
415
Before it reties, it will sleep for :code:`DB_SLEEP_TIME` seconds.
436
-
437
416
:param flag: True for running to 1, False for running to 0
438
417
:type flag: bool
439
-
440
418
:param attack_name: The name of the attack
441
419
:type attack_name: str
442
-
443
420
:raise DatabaseError: When a :code:`sqlite3.OperationalError` is still raised after
444
421
:code:`DB_TRIES` tries.
445
422
"""
@@ -451,7 +428,6 @@ def stop_cache_update(self):
451
428
def main_loop (self , sleep = 0.5 , test_break = False ):
452
429
"""
453
430
The main loop of a PLC. In here all the controls will be applied.
454
-
455
431
:param sleep: (Default value = 0.5) Not used
456
432
:param test_break: (Default value = False) used for unit testing, breaks the loop after one iteration
457
433
"""
@@ -498,6 +474,7 @@ def main_loop(self, sleep=0.5, test_break=False):
498
474
if test_break :
499
475
break
500
476
477
+
501
478
def is_valid_file (parser_instance , arg ):
502
479
if not os .path .exists (arg ):
503
480
parser_instance .error (arg + " does not exist" )
@@ -516,4 +493,4 @@ def is_valid_file(parser_instance, arg):
516
493
args = parser .parse_args ()
517
494
plc = GenericPLC (
518
495
intermediate_yaml_path = Path (args .intermediate_yaml ),
519
- yaml_index = args .index )
496
+ yaml_index = args .index )
0 commit comments