-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeshcore.py
More file actions
executable file
·218 lines (162 loc) · 6.38 KB
/
meshcore.py
File metadata and controls
executable file
·218 lines (162 loc) · 6.38 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
import asyncio
from aiotools import TaskGroup
from binascii import unhexlify, hexlify
import sys
import logging
from identity import IdentityStore, FileIdentityStore, SelfIdentity, AdvertType
from exceptions import *
from ed25519_wrapper import ED25519_Wrapper
from dispatch import default_dispatch
import configuration
from misc import validate_latlon
from sensors import HardwarePlatform
import interfaces.interface as interface
# Config file
if len(sys.argv)>1:
configfile = sys.argv[1]
config = configuration.get_config(configfile)
else:
config = configuration.get_config()
# Default logging configuration
if sys.version_info >= (3,12):
# 3.12 adds 'taskName' to the LogRecord attributes
logformat = '%(asctime)s %(levelname)-8s %(taskName)s: %(name)s: %(message)s'
else:
logformat = '%(asctime)s %(levelname)-8s: %(name)s: %(message)s'
logging_default_config = configuration.get_config(
{'format': logformat,
'dateformat': '%Y-%m-%d %H:%M:%S',
'level': 'debug',
'file': 'meshcore.log' })
loggingconfig = config.get('log', view=True)
loggingconfig.set_default(logging_default_config)
loglevel = getattr(logging, loggingconfig.get('level','DEBUG').upper(), logging.DEBUG)
logger = logging.getLogger(__name__)
logging.basicConfig(
format=loggingconfig.get('format'),
datefmt=loggingconfig.get('dateformat'),
filename=loggingconfig.get('file'),
level=loglevel)
logger.debug("Logging configured")
# Print a summary of system statistics every 5 minutes
async def stats_printer():
while True:
print("---- Device statistics ----")
for d in devices:
print(f"{d.internalname}, {d.me.name.decode(errors='replace')}:")
stats = d.get_stats()
for k in sorted(stats):
print(f" {k}: {stats[k]}")
await asyncio.sleep(300)
###
### Main program
###
logger.info("MeshcorePi starting")
# Hardware interface (battery reading)
hardware = HardwarePlatform()
# Create interfaces
packetinterfaces = interface.configure_interfaces(config)
# Create dispatcher
dispatcher = default_dispatch()
dispatcher.pass_internal = config.get('dispatcher.pass_internal', False)
# Configure devices
devices = []
device_list = config.get('devices', None)
if device_list is None:
logger.error("No devices configured")
print("No devices configured")
exit(1)
for device in device_list:
logger.info(f"Configuring device: {device}")
data = config.get('device.'+device)
if data is None:
logger.error(f"No configuration for device {device}")
continue
device_type = data.get("type", device)
logger.debug(f"Configuring device {device}, type {device_type}")
# Common configuration items:
# privatekey - hex encoded private key (default: new random key)
# name - device name (default: "[type] "+first 4 bytes of public key)
# lat - latitude (default: unset)
# lon - longitude (default: unset)
# contacts - identity store file (default: memory only)
k = data.get('privatekey')
if k is not None:
k = unhexlify(k.encode())
private_key = ED25519_Wrapper(k)
name = data.get('name', default=f"{device_type.capitalize()} {hexlify(private_key.public_key[0:4]).decode('utf8')}")
if k is None:
logger.info(f"Created private key {hexlify(private_key.private_key).decode()} for {name}")
print(f"Created private key: {hexlify(private_key.private_key).decode()}")
else:
print("Loaded private key from config")
print(f"{name}, public key: {hexlify(private_key.public_key).decode()}\n")
lat = data.get('lat')
lon = data.get('lon')
if lat is not None and lon is not None:
try:
latlon = validate_latlon(lat, lon)
except ValueError as e:
logger.error(f"Device {device} has invalid lat/lon: {e}")
latlon = None
else:
latlon = None
ids_file = data.get('contacts')
if ids_file is None:
logger.debug("No identity file configured, using memory store")
ids = IdentityStore()
else:
logger.debug(f"Using identity file {ids_file}")
ids = FileIdentityStore(ids_file, private_key)
if device_type == "companion":
import groupchannel
numchannels = data.get('channels', 32)
channelfile = data.get('channelfile')
add_public = data.get('add_public_channel', True)
channels = groupchannel.channels(channelfile, numchannels, add_public)
me = SelfIdentity(private_key=private_key, name=name, latlon=latlon, devicetype=AdvertType.CHAT)
from companionradio import CompanionRadio
companion = CompanionRadio(me, ids, channels, dispatcher, data)
devices.append(companion)
elif device_type == "room":
me = SelfIdentity(private_key=private_key, name=name, latlon=latlon, devicetype=AdvertType.ROOM)
from roomserver import Room
room = Room(me, ids, dispatcher, hardware, data)
devices.append(room)
elif device_type == "repeater":
me = SelfIdentity(private_key=private_key, name=name, latlon=latlon, devicetype=AdvertType.REPEATER)
from repeater import Repeater
repeater = Repeater(me, ids, dispatcher, hardware, data)
devices.append(repeater)
else:
logger.error(f"Device {device} is unknown type {device_type}")
continue
if len(devices)==0:
logger.error("No valid device configuration found")
print("No valid device configuration found")
exit(1)
if len(devices)==1:
# No point doing this if there's only one device
dispatcher.pass_internal = False
async def main():
async with TaskGroup() as tg:
# Start all the interfaces
for packetinterface in packetinterfaces:
await packetinterface.start()
dispatcher.add_interface(packetinterface)
# Start the dispatcher
await dispatcher.start()
# Start all the devices
for d in devices:
logger.debug(f"Starting {type(d).__name__}, {d.me.name.decode(errors='replace')}")
await d.start()
print(f"Started {d.internalname}, {d.me.name.decode(errors='replace')}")
# Start stats printer
# Writes stats every 5 minutes to stdout; disabled by default
# tg.create_task(stats_printer())
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("Exiting on keyboard interrupt")
print("\nBye")