-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
73 lines (48 loc) · 1.79 KB
/
server.py
File metadata and controls
73 lines (48 loc) · 1.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
from enum import Flag
import random
from time import sleep
import opcua
#Import OPCUA
from opcua import uamethod
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.
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).
flag_var=False
#Method to initiate Lathe Operation
@uamethod
def initiateOperation(parent,flag):
global flag_var
flag_var=flag
return 0
opc_object.add_method(1,"Lathe Operation A", initiateOperation )
#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,70),1)
#Generating random value from 50.0 to 60.0
else: #Temperature of tool during operation
temperature_Var=round(random.uniform(50,60),1)
#Generating random value from 50.0 to 60.0
temperature.set_value((str(temperature_Var)+(" c")))
#Setting the random generated value to the temperature variable in opc ua server
sleep(1)