forked from bobjacobsen/python-openlcb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_node_implementation.py
More file actions
199 lines (151 loc) · 6.03 KB
/
Copy pathexample_node_implementation.py
File metadata and controls
199 lines (151 loc) · 6.03 KB
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
'''
Demo of creating a virtual node to represent the application
(other local nodes are possible, but at least one is necessary
for the application to announce itself and provide SNIP info).
Usage:
python3 example_node_implementation.py [host|host:port]
Options:
host|host:port (optional) Set the address (or using a colon,
the address and port). Defaults to a hard-coded test
address and port.
'''
import os
import socket
from typing import Set, Union
# region same code as other examples
from examples_settings import Settings
from openlcb.convert import Convert
settings = Settings()
if __name__ == "__main__":
settings.load_cli_args(docstring=__doc__)
# endregion same code as other examples
from openlcb import prDim, precise_sleep # noqa: E402
from openlcb.tcplink.tcpsocket import TcpSocket # noqa: E402
from openlcb.canbus.canphysicallayergridconnect import ( # noqa: E402
CanPhysicalLayerGridConnect,
)
from openlcb.canbus.canlink import CanLink # noqa: E402
from openlcb.nodeid import NodeID # noqa: E402
from openlcb.datagramservice import DatagramService # noqa: E402
from openlcb.memoryservice import MemoryService # noqa: E402
from openlcb.message import Message # noqa: E402
from openlcb.mti import MTI # noqa: E402
from openlcb.localnodeprocessor import LocalNodeProcessor # noqa: E402
from openlcb.pip import PIP # noqa: E402
from openlcb.snip import SNIP # noqa: E402
from openlcb.node import Node # noqa: E402
# specify connection information
# region moved to settings
# host = "192.168.16.212"
# port = 12021
# localNodeID = "05.01.01.01.03.01"
# farNodeID = "09.00.99.03.00.35"
# endregion moved to settings
sock = TcpSocket()
# s.settimeout(30)
try:
sock.connect(settings['host'], settings['port'])
except socket.gaierror:
print("Failure accessing {}:{}"
.format(settings.get('host'), settings.get('port')))
raise
print("RR, SR are raw socket interface receive and send;"
" RL, SL are link interface; RM, SM are message interface")
# def sendToSocket(frame: CanFrame):
# string = frame.encodeAsString()
# print(" SR: {}".format(string.strip()))
# sock.sendString(string)
# physicalLayer.onFrameSent(frame)
me = os.path.basename(__file__)
def printFrame(frame):
prDim(" RL: {}".format(frame))
physicalLayer = CanPhysicalLayerGridConnect()
physicalLayer.registerFrameReceivedListener(printFrame)
def printMessage(message: Message):
"""Received message (RM) handler."""
prDim(f"RM: {message.mti} from {message.source}")
localNodeID = NodeID(settings['localNodeID'])
print()
print(f"[example_node_memory_implementation] localNodeID: {localNodeID}")
canLink = CanLink(physicalLayer, localNodeID)
datagramService = DatagramService(canLink)
canLink.registerMessageReceivedListener(datagramService.process)
canLink.registerMessageReceivedListener(printMessage)
def printDatagram(memo):
"""create a call-back to print datagram contents when received
Args:
memo (DatagramReadMemo): The datagram received
Returns:
bool: Always False (True would mean we sent a reply to the datagram,
but let the MemoryService do that).
"""
print("Datagram receive call back: {}".format(memo.data))
return False
datagramService.registerDatagramReceivedListener(printDatagram)
memoryService = MemoryService(datagramService)
# callbacks to get results of memory read
def memoryReadSuccess(memo):
print("successful memory read: {}".format(memo.data))
def memoryReadFail(memo):
print("memory read failed: {}".format(memo.data))
# create a node and connect it update
# This is a very minimal node, which just takes part in the low-level common
# protocols
localNode = Node(
localNodeID,
snip=SNIP("python-openlcb", "example_node_implementation",
"0.1", "0.2", "Example Node Implementation",
"Example from python-openlcb"),
pipSet=set([
PIP.SIMPLE_NODE_IDENTIFICATION_PROTOCOL,
PIP.DATAGRAM_PROTOCOL,
])
)
localNodeProcessor = LocalNodeProcessor(canLink, localNode)
canLink.registerMessageReceivedListener(localNodeProcessor.process)
# ^ Must be registered after CanPhysicalLayer constructor
# since that registers canLink.handleFrameReceived which
# maps aliases and allows us to reply if request was the
# first message to supply the far NodeID.
def displayOtherNodeIds(message: Message) :
"""Listener to identify connected nodes
Args:
message (Message): A response from the network
"""
if message.mti == MTI.Verified_NodeID :
print(f"[displayOtherNodeIds] Detected farNodeID {message.source}")
else:
print(f"[displayOtherNodeIds] {message.mti} from {message.source}")
canLink.registerMessageReceivedListener(displayOtherNodeIds)
#######################
# have the socket layer report up to bring the link layer up and get an alias
print(" SL : link up...")
physicalLayer.physicalLayerUp()
print(" SL : link up...waiting...")
while canLink.pollState() != CanLink.State.Permitted:
physicalLayer.receiveAll(sock, verbose=settings['trace'],
verbose_fn=prDim)
physicalLayer.sendAll(sock, verbose=True,
verbose_fn=prDim)
precise_sleep(.02)
print(" SL : link up")
# request that nodes identify themselves so that we can print their node IDs
message = Message(MTI.Verify_NodeID_Number_Global,
localNodeID, None)
canLink.sendMessage(message)
# process resulting activity
while True:
count = 0
try:
count += physicalLayer.sendAll(sock, verbose=True,
verbose_fn=prDim)
count += physicalLayer.receiveAll(sock, verbose=settings['trace'],
verbose_fn=prDim)
if count < 1:
precise_sleep(.01)
# else skip sleep to avoid latency (port already delayed)
except Exception:
print(f"localNodeID={localNodeID}")
raise
print("Calling physicalLayerDown...")
physicalLayer.physicalLayerDown()