Passed
Push — master ( 88bbc4...c87855 )
by Olivier
02:27
created

server-create-custom-structures.main()   B

Complexity

Conditions 3

Size

Total Lines 78
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 49
nop 0
dl 0
loc 78
rs 8.669
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
import logging
2
import asyncio
3
4
from IPython import embed
5
6
from asyncua import ua, Server
7
from asyncua.common.type_dictionary_buider import DataTypeDictionaryBuilder
8
9
10
async def main():
11
    server = Server()
12
    await server.init()
13
    server.set_endpoint('opc.tcp://0.0.0.0:4840/UA/SampleServer')
14
    server.set_server_name('Custom structure demo server')
15
16
    # idx name will be used later for creating the xml used in data type dictionary
17
    url = 'http://examples.freeopcua.github.io'
18
    idx = await server.register_namespace(url)
19
20
    dict_builder = DataTypeDictionaryBuilder(server, idx, url, 'MyDictionary')
21
    await dict_builder.init()
22
23
    # add one basic structure
24
    basic_struct_name = 'BasicStructure'
25
    basic_struct = await dict_builder.create_data_type(basic_struct_name)
26
    basic_struct.add_field('ID', ua.VariantType.Int32)
27
    basic_struct.add_field('Gender', ua.VariantType.Boolean)
28
    basic_struct.add_field('Comments', ua.VariantType.String)
29
30
    # add an advance structure which uses our basic structure
31
    nested_struct_name = 'NestedStructure'
32
    nested_struct = await dict_builder.create_data_type(nested_struct_name)
33
    nested_struct.add_field('Name', ua.VariantType.String)
34
    nested_struct.add_field('Surname', ua.VariantType.String)
35
    # add a list of simple structure as field
36
    nested_struct.add_field('StuffArray', basic_struct, is_array=True)
37
38
    # this operation will write the OPC dict string to our new data type dictionary
39
    # namely the 'MyDictionary'
40
41
    await dict_builder.set_dict_byte_string()
42
43
    # get the working classes
44
    await server.load_type_definitions()
45
46
47
    # Create one test structure in our address space
48
    basic_var = await server.nodes.objects.add_variable(
49
        idx,
50
        'BasicStruct',
51
        None,
52
        datatype=basic_struct.data_type,
53
    )
54
55
    await basic_var.set_writable()
56
    var = ua.BasicStructure()
57
    var.ID = 3
58
    var.Gender = True
59
    var.Comments = 'Test string'
60
    await basic_var.write_value(var)
61
62
    # Create one advance test structure
63
    nested_var = await server.nodes.objects.add_variable(
64
        idx,
65
        'NestedStruct',
66
        None,
67
        datatype=nested_struct.data_type,
68
    )
69
70
    await nested_var.set_writable()
71
    var2 = ua.NestedStructure()
72
    var2.StuffArray = [var, var]
73
    var2.Name = 'Max'
74
    var2.Surname = 'Karl'
75
    await nested_var.write_value(var2)
76
77
    async with server:
78
        # see the xml value in our customized dictionary 'MyDictionary', only for debugging use
79
        print(getattr(dict_builder, '_type_dictionary').get_dict_value())
80
81
        # values can be write back and retrieved with the codes below.
82
        v1 = await basic_var.read_value()
83
        v2 = await nested_var.read_value()
84
85
        #embed()
86
        while True:
87
            await asyncio.sleep(1)
88
89
90
if __name__ == '__main__':
91
    logging.basicConfig(level=logging.INFO)
92
    asyncio.run(main())
93