Passed
Pull Request — master (#348)
by
unknown
03:33 queued 01:15
created

statemachine-example.main()   B

Complexity

Conditions 3

Size

Total Lines 64
Code Lines 45

Duplication

Lines 11
Ratio 17.19 %

Importance

Changes 0
Metric Value
cc 3
eloc 45
nop 0
dl 11
loc 64
rs 8.8
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 asyncio, logging
2
from asyncua import Server, ua, Node
3
from Statemachine import StateMachine
4
5
logging.basicConfig(level=logging.INFO)
6
_logger = logging.getLogger('asyncua')
7
8
if __name__ == "__main__":
9
    async def main():
10
        server = Server()
11
        await server.init()
12
        server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
13
        idx = await server.register_namespace("http://examples.freeopcua.github.io")
14
15
        # get a instance of the StateMachine-Class def "__init__(self, server=None, parent=None, idx=None, name=None):"
16
        mystatemachine = StateMachine(server, server.nodes.objects, idx, "StateMachine")
17
        # call statemachine.install() to instantiate the statemachinetype (with or without optional nodes)
18
        await mystatemachine.install(optionals=True)
19
20
        # the StateMachine provides helperclasses for states and transition each class is a representation of the state- and transition-variabletype
21
        # "def __init__(self, name, id=0, node=None):"
22
        # if the state node already exist for example from xml model you can assign it here aswell as if its a substate this is importent for the change of the state
23
        state1 = mystatemachine.State("Idle", 1)
24
        # adds the state (StateType) to the statemachine childs - this is optional!
25
        await mystatemachine.add_state(state1, state_type=ua.NodeId(2309, 0)) #this is a init state -> InitialStateType: ua.NodeId(2309, 0)
26
        state2 = mystatemachine.State("Loading", 2)
27
        await mystatemachine.add_state(state2)
28
        state3 = mystatemachine.State("Initializing", 3)
29
        await mystatemachine.add_state(state3)
30
        state4 = mystatemachine.State("Processing", 4)
31
        await mystatemachine.add_state(state4)
32
        state5 = mystatemachine.State("Finished", 5)
33
        await mystatemachine.add_state(state5)
34
35
        # setup your transition helperclass 
36
        # "def __init__(self, name, id=0, node=None):"
37
        # if the transition node already exist for example from xml model you can assign it here        
38
        trans1 = mystatemachine.Transition("to Idle", 1)
39
        # adds the state (TransitionType) to the statemachine childs - this is optional!
40
        await mystatemachine.add_transition(trans1)
41
        trans2 = mystatemachine.Transition("to Loading", 2)
42
        await mystatemachine.add_transition(trans2)
43
        trans3 = mystatemachine.Transition("to Initializing", 3)
44
        await mystatemachine.add_transition(trans3)
45
        trans4 = mystatemachine.Transition("to Processing", 4)
46
        await mystatemachine.add_transition(trans4)
47
        trans5 = mystatemachine.Transition("to Finished", 5)
48
        await mystatemachine.add_transition(trans5)
49
50
        # initialise the StateMachine by call change_state() with the InitialState
51
        # if the statechange should trigger an TransitionEvent the Message can be assigned here 
52
        # if event_msg is None no event will be triggered
53
        await mystatemachine.change_state(state1, trans1, f"{mystatemachine._name}: Idle", 300)
54
55
        mystatemachine2 = StateMachine(server, server.nodes.objects, idx, "StateMachine2")
56
        await mystatemachine2.install(optionals=False)
57
        sm2state1 = mystatemachine2.State("Idle", 1)
58
        await mystatemachine2.add_state(sm2state1)
59
        await mystatemachine2.change_state(sm2state1)
60
61
        async with server:
62 View Code Duplication
            while 1:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
                await asyncio.sleep(2)
64
                await mystatemachine.change_state(state2, trans2, f"{mystatemachine._name}: Loading", 350)
65
                await asyncio.sleep(2)
66
                await mystatemachine.change_state(state3, trans3, f"{mystatemachine._name}: Initializing", 400)
67
                await asyncio.sleep(2)
68
                await mystatemachine.change_state(state4, trans4, f"{mystatemachine._name}: Processing", 600)
69
                await asyncio.sleep(2)
70
                await mystatemachine.change_state(state5, trans5, f"{mystatemachine._name}: Finished", 800)
71
                await asyncio.sleep(2)
72
                await mystatemachine.change_state(state1, trans1, f"{mystatemachine._name}: Idle", 500)
73
74
    asyncio.run(main())
75