-
Notifications
You must be signed in to change notification settings - Fork 1
/
loranode.py
71 lines (64 loc) · 2.32 KB
/
loranode.py
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
from loranode_template import LoraNodeTemplate
from random import expovariate
from nwkstack import NetworkStack
from phylayer import PhyLayer
class PeriodicLoraNode(LoraNodeTemplate):
def __init__(self, id, location, config, netstack, sim, period):
"""
A node that sends data periodically
Parameters:
id -- id of node
location-- location
config -- config, such as sf, txpower, bw, cr
netstack-- network stack to use
sim -- simulator to access timing and events
period -- data send period
"""
self.id = id
self.location = location
self.config = config
self.netstack = netstack
self.phy = PhyLayer(sim, config, None)
self.phy.myid = id
self.sim = sim
# TODO: period should be kwargs
self.period = period
self.channel = None
def config_net_stack(self, layers, logger, channel):
if self.config is None or self.sim is None:
# TODO throw exception
pass
self.channel = channel
nwkstack = NetworkStack(logger, layers, self.sim, self.config,
self.app_receive, self.id)
self.netstack = nwkstack
# Connect the network stack to the phy layer
nwkstack.layers[-1].lower = self.phy
self.phy.upper = nwkstack.layers[-1]
self.phy.channel = channel
self.phy.logger = logger
self.phy.sim = self.sim
self.phy.config = self.config
def set_config(self, config):
"""
Updates the node's TX configuration
"""
self.config = config
self.phy.config = config
self.channel.update_node(self, config)
def app_process(self):
"""
Application process that generates packets
and feeds them to the network stack
Makes use of the simulator for timings, events, etc.
"""
while True:
self.netstack.transmit("Hello", 0) # Send "Hello" to the GW
#print('New packet at ', self.sim.now)
yield self.sim.timeout(expovariate(1.0/float(self.period)))
def app_receive(self, data, src):
"""
Callback to be passed to the network stack constructor.
The net stack will call this when packets are received.
"""
pass