-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinuxcncopcua.py
215 lines (184 loc) · 9.15 KB
/
linuxcncopcua.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import inspect
import linuxcnc
import types
from opcua import ua
from os.path import expanduser
from functionmanager import *
from functions.cnchoming import *
from functions.createfilefunction import *
from functions.movecnctorobotremove import *
from functions.selectandstartprogram import *
from main import variableMap, server, startSelectedProgramm, pauseSelectedProgramm, resumeSelectedProgramm, setModeMDI, \
setModeAuto, setModeManual, setMoveRobotRemovePos
debugprint = False
pythonMethodMembers = ["clear",
"copy",
"fromkeys",
"get",
"items",
"keys",
"pop",
"popitem",
"setdefault",
"setdefault",
"update",
"values",
"count",
"index",
"has_key",
"iteritems",
"iterkeys",
"itervalues",
"viewitems",
"viewkeys",
"viewvalues",
"poll"]
def add_entry_to_model(ns, newKey, opcname, opcobject, value):
if debugprint :
print "func", inspect.isfunction(value), "meth", inspect.ismethod(
value), "methDesc", inspect.ismethoddescriptor(
value), "dataDesc", inspect.isdatadescriptor(value), "frame", inspect.isframe(
value), "abstract", inspect.isabstract(
value), "gen", inspect.isgenerator(value), "class", inspect.isclass(value), \
"buildIn", inspect.isbuiltin(value), "code", inspect.iscode(
value), "memberDesc", inspect.ismemberdescriptor(value), \
"getset", inspect.isgetsetdescriptor(value), "routine", inspect.isroutine(
value), "trace", inspect.istraceback(value), \
"module", inspect.ismodule(value)
if isinstance(value, (dict, types.TypeType, types.ClassType)):
if debugprint:
print "addDictionary:", newKey, value
variableMap[newKey] = opcobject.add_object(ns, opcname)
if debugprint:
print dir(value)
for dicKey in value.keys():
if not dicKey.startswith('_') and dicKey not in pythonMethodMembers and not inspect.isroutine(
value[dicKey]) and not inspect.isbuiltin(value[dicKey]):
add_entry_to_model(ns, str(dicKey) + "-" + newKey, dicKey, variableMap[newKey], value[dicKey])
elif isinstance(value, list):
if debugprint:
print "addList:", newKey, value
if len(value) > 0:
if isinstance(value[0], (dict, tuple)):
variableMap[newKey] = opcobject.add_object(ns, opcname)
for listElement in value:
add_layer_to_model(ns, newKey, variableMap[newKey], listElement)
else:
variableMap[newKey] = opcobject.add_variable(createNamespace(ns, newKey), opcname, value)
elif isinstance(value, (tuple)):
if debugprint:
print "addTuple:", newKey, value
variableMap[newKey] = opcobject.add_object(ns, opcname)
add_layer_to_model(ns, newKey, variableMap[newKey], value)
else:
if debugprint:
print "addSimpleTo", ns, opcobject
print "addSimpleValue", newKey, value
if "tool_result" in str(value):
if debugprint:
print "tool_result has to be skipped/only displayed as string for now", str(value)
variableMap[newKey] = opcobject.add_variable(createNamespace(ns, newKey), opcname, str(value))
else:
variableMap[newKey] = opcobject.add_variable(createNamespace(ns, newKey), opcname, value)
def add_object_layer_to_model(ns, key, opcobject, layerobject):
for x in dir(layerobject):
newKey = x + "-" + key
value = getattr(layerobject, x)
if not x.startswith('_') and x not in pythonMethodMembers and not inspect.isroutine(value) and not inspect.isbuiltin(value):
add_entry_to_model(ns, newKey, str(x), opcobject, value)
def add_layer_to_model(ns, key, opcobject, layerobject):
if isinstance(layerobject, (tuple)):
if debugprint:
print "TupleValues", len(layerobject), layerobject
counter = 0
for entry in layerobject:
newKey = str(counter) + "-" + key
add_entry_to_model(ns, newKey, str(counter), opcobject, entry)
counter = counter + 1
else:
add_object_layer_to_model(ns, key, opcobject, layerobject)
def update_entry_to_model( newKey, value):
if isinstance(value, (dict, types.TypeType, types.ClassType)):
for dicKey in value.keys():
if not dicKey.startswith('_') and dicKey not in pythonMethodMembers and not inspect.isroutine(
value[dicKey]) and not inspect.isbuiltin(value[dicKey]):
update_entry_to_model(str(dicKey) + "-" + newKey, value[dicKey])
elif isinstance(value, list):
if len(value) > 0:
if isinstance(value[0], (dict, tuple)):
for listElement in value:
update_layer_to_model( newKey, listElement)
else:
variableMap[newKey].set_value(value)
elif isinstance(value, (tuple)):
update_layer_to_model( newKey, value)
else:
if "tool_result" in str(value):
variableMap[newKey].set_value(str(value))
else:
variableMap[newKey].set_value(value)
def update_object_layer_to_model( key, layerobject):
for x in dir(layerobject):
newKey = x + "-" + key
value = getattr(layerobject, x)
if not x.startswith('_') and x not in pythonMethodMembers and not inspect.isroutine(value) and not inspect.isbuiltin(value):
update_entry_to_model( newKey, value)
def update_layer_to_model( key, layerobject):
if isinstance(layerobject, (tuple)):
counter = 0
newKey = str(counter) + "-" + key
for entry in layerobject:
update_entry_to_model( newKey, entry)
counter = counter + 1
else:
update_object_layer_to_model( key, layerobject)
def update_layer( key, layerobject):
update_object_layer_to_model(key, layerobject)
def start_server():
# starting!
server.start()
def init_variables_start_server():
s = linuxcnc.stat() # create a connection to the status channel
s.poll() # get current values
# now setup our server
# server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/")
server.set_endpoint("opc.tcp://0.0.0.0:4840/")
server.set_server_name("FreeOpcUa Example Server")
server.set_security_policy([ua.SecurityPolicyType.NoSecurity])
# setup our own namespace
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
# get Objects node, this is where we should put our custom stuff
objects = server.get_objects_node()
# populating our address space
myfolder = objects.add_folder(idx, "pocketnc")
myobj = myfolder.add_object(idx, "PocketNC")
variableMap["counter"] = myobj.add_variable(createNamespace(idx, "loop.counter"), "counter", 0)
# Use a breakpoint in the code line below to debug your script.
try:
s = linuxcnc.stat() # create a connection to the status channel
s.poll() # get current values
add_object_layer_to_model(idx, "", myobj, s)
customFunc = myfolder.add_object(idx, "CustomFunc")
startRunMethod = customFunc.add_method(idx, "startSelectedProgramm", startSelectedProgramm)
pauseRunMethod = customFunc.add_method(idx, "pauseSelectedProgramm", pauseSelectedProgramm)
resumeRunMethod = customFunc.add_method(idx, "resumeSelectedProgramm", resumeSelectedProgramm)
homeAll = customFunc.add_method(idx, "homeAllAxis", homeAllAxis)
setModeMDIF = customFunc.add_method(idx, "setModeMDI", setModeMDI)
setModeAutoF = customFunc.add_method(idx, "setModeAuto", setModeAuto)
setModeMANUALF = customFunc.add_method(idx, "setModeManual", setModeManual)
moveRobotRemovePos = customFunc.add_method(idx, "setMoveRobotRemovePos", setMoveRobotRemovePos)
createFunction(customFunc, idx, startHomingFunc, checkFinishedHomed, checkErrorHomed, {}, "Home")
createFunction(customFunc, idx, startGoToRobotRemovePositionFunc, checkFinishedGoToRobotRemovePosition,
checkErrorGoToRobotRemovePosition, {}, "RobotRemovePos")
createFunction(customFunc, idx, startCreateFileFunc, checkFinisedCreateFile, checkErrorCreateFile, createfilefunction_defaultparams, "WriteNCFile")
createFunction(customFunc, idx, startNCProgrammFunc, checkFinisedNCProgramm, checkErrorNCProgramm,
selectandstartprogram_defaultparams, "startProgram")
except linuxcnc.error, detail:
print "error", detail
def update_function():
variableMap["counter"].set_value(variableMap["counter"].get_value() + 1)
s = linuxcnc.stat() # create a connection to the status channel
s.poll() # get current values
update_function_lifecycle(s)
update_layer("", s)