Skip to content

Commit 027dccd

Browse files
committed
creation dinasore-lite
1 parent 8fe159c commit 027dccd

File tree

7 files changed

+168
-48
lines changed

7 files changed

+168
-48
lines changed

resources/data_model.fboot

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
;<Request ID="1" Action="CREATE"><FB Name="DIN1" Type="EMB_RES" /></Request>
2-
DIN1;<Request ID="2" Action="CREATE"><FB Name="WRITE_CSV" Type="WRITE_CSV" /></Request>
3-
DIN1;<Request ID="3" Action="WRITE"><Connection Source="/home/eliseu/PycharmProjects/db.csv" Destination="WRITE_CSV.PATH" /></Request>
4-
DIN1;<Request ID="4" Action="CREATE"><FB Name="DATA_SIMULATOR" Type="DATA_SIMULATOR" /></Request>
5-
DIN1;<Request ID="5" Action="WRITE"><Connection Source="[((15, 0.4), (10, 0.4)), ((19, 0.1), (16, 0.1))]" Destination="DATA_SIMULATOR.PARAMS" /></Request>
6-
DIN1;<Request ID="6" Action="WRITE"><Connection Source="1" Destination="DATA_SIMULATOR.RATIO" /></Request>
7-
DIN1;<Request ID="7" Action="WRITE"><Connection Source="1" Destination="DATA_SIMULATOR.DELAY" /></Request>
8-
DIN1;<Request ID="8" Action="CREATE"><Connection Source="DATA_SIMULATOR.READ_O" Destination="WRITE_CSV.RUN" /></Request>
9-
DIN1;<Request ID="9" Action="CREATE"><Connection Source="DATA_SIMULATOR.DATA" Destination="WRITE_CSV.DATA" /></Request>
10-
DIN1;<Request ID="10" Action="START"/>
1+

resources/function_blocks/DATA_SIMULATOR.py

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<!DOCTYPE FBType SYSTEM "http://www.holobloc.com/xml/LibraryElement.dtd">
3-
<FBType Name="DATA_SIMULATOR" OpcUa="DEVICE.SENSOR">
3+
<FBType Name="MACHINE_SIMULATOR" OpcUa="DEVICE.SENSOR">
44
<InterfaceList>
55
<EventInputs>
66
<Event Name="INIT" Type="Event"/>
77
<Event Name="READ" Type="Event"/>
8+
<Event Name="ON-OFF" Type="Event"/>
89
</EventInputs>
910
<EventOutputs>
1011
<Event Name="INIT_O" Type="Event"/>
1112
<Event Name="READ_O" Type="Event">
1213
<With Var="DATA"/>
14+
<With Var="STATE"/>
1315
</Event>
1416
</EventOutputs>
1517
<InputVars>
16-
<VarDeclaration Name="PARAMS" Type="STRING"/>
17-
<VarDeclaration Name="RATIO" Type="REAL"/>
18-
<VarDeclaration Name="DELAY" Type="INT"/>
18+
<VarDeclaration Name="PARAMS_DATA" Type="STRING"/>
19+
<VarDeclaration Name="LIM_ANOM" Type="REAL"/>
20+
<VarDeclaration Name="PARAMS_MTBF" Type="STRING"/>
21+
<VarDeclaration Name="PARAMS_MTTS" Type="STRING"/>
22+
<VarDeclaration Name="PARAMS_MTTR" Type="STRING"/>
23+
<VarDeclaration Name="DELAY" Type="REAL"/>
1924
</InputVars>
2025
<OutputVars>
2126
<VarDeclaration Name="DATA" Type="STRING"/>
27+
<VarDeclaration Name="STATE" Type="STRING"/>
2228
</OutputVars>
2329
</InterfaceList>
2430
</FBType>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import numpy as np
2+
import time
3+
4+
5+
class MACHINE_SIMULATOR:
6+
7+
def __init__(self):
8+
self.state = 'IDLE'
9+
self.prev_state = 'WORK'
10+
self.ttf = 0
11+
self.tts = 0
12+
self.ttr = 0
13+
self.mtta = 0
14+
15+
def schedule(self, event_name, event_value, params_data, lim_anom, params_mtbf, params_mtts, params_mttr, delay):
16+
if event_name == 'INIT':
17+
# init the states
18+
self.state = 'IDLE'
19+
self.prev_state = 'WORK'
20+
# computes (gets from the distribution) the ttf
21+
self.ttf = self.generate_time(params_mtbf)
22+
self.mtta = self.ttf*lim_anom
23+
return [event_value, None, '', self.state]
24+
25+
elif event_name == 'READ':
26+
# checks the state
27+
if self.state == 'IDLE':
28+
time.sleep(delay)
29+
return [None, None, '', self.state]
30+
31+
elif self.state == 'WORK':
32+
# wait some time (performs the work)
33+
time.sleep(delay)
34+
# computes (decreases) the current TTF
35+
self.ttf -= delay
36+
# checks if is 0 -> failure
37+
if self.ttf <= 0:
38+
# computes (gets from the distribution) the tts (time to start the repair)
39+
self.tts = self.generate_time(params_mtts)
40+
self.state = 'BREAK'
41+
return [None, event_value, '', self.state]
42+
# normal operation
43+
else:
44+
# generates data
45+
anom = 1 if self.ttf <= self.mtta else 0
46+
data_str = self.generate_data(params_data, anom)
47+
return [None, event_value, data_str, self.state]
48+
49+
elif self.state == 'BREAK':
50+
# simulates the waiting time to repair
51+
time.sleep(delay)
52+
# computes (decreases) the current TTF
53+
self.tts -= delay
54+
# checks if is 0 -> starts repairing
55+
if self.tts <= 0:
56+
# computes (gets from the distribution) the ttr (time to repair)
57+
self.ttr = self.generate_time(params_mttr)
58+
self.state = 'REPAIR'
59+
return [None, event_value, '', self.state]
60+
# break state
61+
else:
62+
return [None, event_value, '', self.state]
63+
64+
elif self.state == 'REPAIR':
65+
# simulates the waiting time to repair
66+
time.sleep(delay)
67+
# computes (decreases) the current TTF
68+
self.ttr -= delay
69+
# checks if is 0 -> starts repairing
70+
if self.ttr <= 0:
71+
# computes (gets from the distribution) the ttr (time to repair)
72+
self.ttf = self.generate_time(params_mtbf)
73+
self.mtta = self.ttf * lim_anom
74+
self.state = 'WORK'
75+
return [None, event_value, '', self.state]
76+
# break state
77+
else:
78+
return [None, event_value, '', self.state]
79+
80+
elif event_name == 'ON-OFF':
81+
# switches the button
82+
if self.state == 'IDLE':
83+
# updates the data
84+
self.state = self.prev_state
85+
return [None, event_value, '', self.state]
86+
else:
87+
self.prev_state = self.state
88+
self.state = 'IDLE'
89+
return [None, None, '', self.state]
90+
91+
@staticmethod
92+
def generate_data(params_str, anom):
93+
params = list(eval(params_str))
94+
# generates the values
95+
sample = []
96+
for norm_param, anom_param in params:
97+
# checks if an anomaly to generate
98+
mu, std = norm_param if anom == 0 else anom_param
99+
# generates the value
100+
value = round(np.random.normal(mu, std), 3)
101+
# stores the value
102+
sample.append(value)
103+
# appends the anom at the end and converts it to string
104+
sample.append(anom)
105+
data_str = ','.join(['{0}'.format(x) for x in sample])
106+
return data_str
107+
108+
@staticmethod
109+
def generate_time(params_str):
110+
params = list(eval(params_str))
111+
t = round(np.random.normal(params[0], params[1]))
112+
return t
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE FBType SYSTEM "http://www.holobloc.com/xml/LibraryElement.dtd">
3+
<FBType Name="PROD_MAN" OpcUa="SERVICE">
4+
<InterfaceList>
5+
<EventInputs>
6+
<Event Name="INIT" Type="Event"/>
7+
<Event Name="ON-OFF" Type="Event">
8+
</Event>
9+
</EventInputs>
10+
<EventOutputs>
11+
<Event Name="INIT_O" Type="Event"/>
12+
<Event Name="ON-OFF_O" Type="Event">
13+
<With Var="STATE"/>
14+
</Event>
15+
</EventOutputs>
16+
<InputVars>
17+
</InputVars>
18+
<OutputVars>
19+
<VarDeclaration Name="STATE" Type="STRING"/>
20+
</OutputVars>
21+
</InterfaceList>
22+
</FBType>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
class PROD_MAN:
4+
5+
def __init__(self):
6+
self.state = 'IDLE'
7+
8+
def schedule(self, event_name, event_value):
9+
10+
if event_name == 'INIT':
11+
self.state = 'IDLE'
12+
return [event_value, None, 'IDLE']
13+
14+
elif event_name == 'ON-OFF':
15+
# switches the button
16+
if self.state == 'IDLE':
17+
# updates the data
18+
self.state = 'WORK'
19+
return [None, event_value, self.state]
20+
else:
21+
self.state = 'IDLE'
22+
return [None, event_value, self.state]

resources/function_blocks/WRITE_CSV.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def schedule(self, event_input_name, event_input_value, data, path):
1818
if data is None:
1919
return [None, event_input_value, "No data received"]
2020

21-
print("Data on write CSV:", data)
21+
# print("Data on write CSV:", data)
2222

2323
with open(path, "a+") as f:
2424
f.write(data)

0 commit comments

Comments
 (0)