-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.py
More file actions
108 lines (66 loc) · 2.79 KB
/
server_test.py
File metadata and controls
108 lines (66 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import random
from time import sleep
import opcua
#Import OPCUA
myserver = opcua.Server()
#Creating server instance
url="localhost"
endpoint = "opc.tcp://"+url+":5002"
myserver.set_endpoint(endpoint)
#Establishing Server endpoint
name="OPC_lathe"
addspace= myserver.register_namespace(name)
#Registering a namespace for the server
opc_object = myserver.get_objects_node()
#Creating a OPC object
param=opc_object.add_object(addspace,"parameter")
#Adding a parameter (param) to the object.
temperature=param.add_variable(addspace,"Temperature","0 c")
#Adding a variable (temperature) to the parameter (param), by assigning 0 as the initial value.
Coolant=param.add_variable(addspace,"Coolant",False)
#Adding a variable (Coolant) to the parameter (param), by assigning False as the initial value.
workpieceID=param.add_variable(addspace,"Workpiece ID","nil")
#Adding a variable (workpieceID) to the parameter (param), by assigning False as the initial value.
temperature.set_writable()
#Setting the temperature variable to be writable (can be updated).
Coolant.set_writable()
#Setting the Coolant variable to be writable (can be updated).
workpieceID.set_writable()
#Setting the workpieceID variable to be writable (can be updated).
flag_var=False
@opcua.uamethod
def start_a_machining_operation(parent,flag):
global flag_var
flag_var=flag
global workpieceID
workpieceID.set_value("Operation_lathe"+str(random.randint(0,100))) #Assigning a workpiece ID to the workpieceID variable
Remaining_time=random.randint(10,16)
#Set a random fix value to the program remaining run time which in real world is a variable
print("ready")
return Remaining_time #Returning remaining time
@opcua.uamethod
def coolant_status_on(parent):
Coolant.set_value(True)
#Turning On Coolant
return 0
@opcua.uamethod
def coolant_status_off(parent):
Coolant.set_value(False)
#Turning Off Coolant
return 0
opc_object.add_method(1,"Lathe Operation A", start_a_machining_operation)
opc_object.add_method(1,"Coolant Management", coolant_status_on )
opc_object.add_method(1,"Coolant Management", coolant_status_off )
#Adding OPC Method to the Python OPC Objects
myserver.start()
#Starting the server
while True:
if flag_var: #Temperature of tool during operation
temperature_Var=round(random.uniform(60,100),1)
#Generating a random value from 60.0 to 100.0 to simulate the machinging temperature going up
else: #Temperature of tool during operation
temperature_Var=round(random.uniform(20,30),1)
#Generating a random value from 20.0 to 30.0 to simulate the temperature at the standby mode
temperature.set_value((str(temperature_Var)))
#Setting the random generated value to the temperature variable in opc ua server
sleep(1)