Conditions | 3 |
Total Lines | 72 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | from asyncua import Server, ua, uamethod |
||
4 | async def main(): |
||
5 | |||
6 | @uamethod |
||
7 | async def callback(parent, in_extobj): |
||
8 | out_extobj = ua.uaprotocol_auto.AxisInformation() # get new instanace of AxisInformation |
||
9 | out_extobj.EngineeringUnits = in_extobj.EngineeringUnits |
||
10 | out_extobj.EURange.Low = in_extobj.EURange.Low |
||
11 | out_extobj.EURange.High = in_extobj.EURange.High |
||
12 | out_extobj.Title = in_extobj.Title |
||
13 | out_extobj.AxisScaleType = in_extobj.AxisScaleType |
||
14 | out_extobj.AxisSteps = in_extobj.AxisSteps |
||
15 | |||
16 | await axis_info.set_value(out_extobj) #write values to variable |
||
17 | |||
18 | ret = ( |
||
19 | ua.Variant(out_extobj, ua.VariantType.ExtensionObject), |
||
20 | ua.Variant("test", ua.VariantType.String) |
||
21 | ) |
||
22 | |||
23 | return ret |
||
24 | |||
25 | server = Server() |
||
26 | await server.init() |
||
27 | server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/") |
||
28 | server.set_security_policy([ua.SecurityPolicyType.NoSecurity]) |
||
29 | |||
30 | obj = server.get_objects_node() |
||
31 | idx = await server.register_namespace("http://examples.freeopcua.github.io") |
||
32 | |||
33 | await server.load_data_type_definitions() |
||
34 | |||
35 | inarg_extobj = ua.Argument() |
||
36 | inarg_extobj.Name = "In" |
||
37 | inarg_extobj.DataType = ua.NodeId(12079, 0) |
||
38 | inarg_extobj.ValueRank = -1 |
||
39 | inarg_extobj.ArrayDimensions = [] |
||
40 | inarg_extobj.Description = ua.LocalizedText("Wanted AxisInformation") |
||
41 | |||
42 | outarg_extobj = ua.Argument() |
||
43 | outarg_extobj.Name = "Out" |
||
44 | outarg_extobj.DataType = ua.NodeId(12079, 0) |
||
45 | outarg_extobj.ValueRank = -1 |
||
46 | outarg_extobj.ArrayDimensions = [] |
||
47 | outarg_extobj.Description = ua.LocalizedText("Actual AxisInformation") |
||
48 | |||
49 | status = ua.Argument() |
||
50 | status.Name = "Status" |
||
51 | status.DataType = ua.NodeId(12, 0) |
||
52 | status.ValueRank = -1 |
||
53 | status.ArrayDimensions = [] |
||
54 | status.Description = ua.LocalizedText("MSG") |
||
55 | |||
56 | method_parent = await obj.add_object(idx, "Methods") |
||
57 | method_node = await method_parent.add_method( |
||
58 | idx, |
||
59 | "SetAxisInformation", |
||
60 | callback, |
||
61 | [ |
||
62 | inarg_extobj |
||
63 | ], |
||
64 | [ |
||
65 | outarg_extobj, |
||
66 | status |
||
67 | ] |
||
68 | ) |
||
69 | |||
70 | #add a variable of type AxisInformation |
||
71 | axis_info = await obj.add_variable(idx, "AxisInformation", ua.uaprotocol_auto.AxisInformation(), varianttype=ua.VariantType.ExtensionObject) |
||
72 | |||
73 | async with server: |
||
74 | while 1: |
||
75 | await asyncio.sleep(0) |
||
76 | |||
78 | asyncio.run(main()) |