Conditions | 3 |
Total Lines | 64 |
Code Lines | 45 |
Lines | 11 |
Ratio | 17.19 % |
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 | import asyncio, logging |
||
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: |
|
|
|||
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 | |||
75 |