Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
420 lines (357 loc) · 16.3 KB
/
Copy pathserver.py
File metadata and controls
420 lines (357 loc) · 16.3 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python3
"""
meshcore-chat — Web-based MeshCore Companion Radio Client
https://github.com/Saarlandpower/meshcore-chat
Bridges a MeshCore Companion Radio (USB Serial) to a WebSocket-based web UI.
Supports: Direct Messages, Channels, Contact List, GPS Map.
"""
import asyncio
import os
import sqlite3
import threading
import logging
import time
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from meshcore import MeshCore
import db as chatdb
from meshcore.events import EventType
# ── Logging ───────────────────────────────────────────────────────────────────
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)
logger = logging.getLogger("meshcore-chat")
# ── Config (override via environment variables) ───────────────────────────────
SERIAL_PORT = os.environ.get("MC_SERIAL_PORT",
"/dev/serial/by-id/usb-YOUR_DEVICE_HERE")
BAUD_RATE = int(os.environ.get("MC_BAUD_RATE", "115200"))
HTTP_HOST = os.environ.get("MC_HTTP_HOST", "0.0.0.0")
HTTP_PORT = int(os.environ.get("MC_HTTP_PORT", "5003"))
NUM_CHANNELS = int(os.environ.get("MC_NUM_CHANNELS", "8")) # Set MC_NUM_CHANNELS=2 if you only have 2 channels
DB_PATH = os.environ.get("MC_DB_PATH", "/opt/meshcore-chat/messages.db")
MSG_HISTORY = int(os.environ.get("MC_MSG_HISTORY", "200"))
# ── Flask / SocketIO ──────────────────────────────────────────────────────────
app = Flask(__name__, template_folder="templates", static_folder="static")
app.config["SECRET_KEY"] = os.environ.get("MC_SECRET_KEY", "meshcore-chat-secret")
socketio = SocketIO(app, cors_allowed_origins="*", async_mode="threading")
# ── Global state ──────────────────────────────────────────────────────────────
mc = None
mc_loop = None
self_info = {}
contacts = {} # key_hex -> contact dict
channels = {} # idx -> channel dict
connected = False
def ts():
return int(time.time())
chatdb.init()
def conv_key_channel(ch_idx):
return f'ch_{ch_idx}'
def conv_key_direct(key):
return f'dm_{key[:16]}'
def contact_to_dict(key, c):
"""Normalize a meshcore contact dict for the frontend."""
lat = c.get("adv_lat", 0.0) or 0.0
lon = c.get("adv_lon", 0.0) or 0.0
ctype = c.get("type", 0)
type_label = {0: "client", 1: "client", 2: "repeater", 3: "room"}.get(ctype, "unknown")
last_advert = c.get("last_advert", 0) or 0
flags = c.get("flags", 0) or 0
return {
"key": key[:16],
"full_key": key,
"name": c.get("adv_name") or c.get("name") or key[:8],
"type": type_label,
"lat": lat,
"lon": lon,
"has_gps": (lat != 0.0 or lon != 0.0),
"last_advert": last_advert,
"last_seen": last_advert,
"flags": flags,
"out_path_len": c.get("out_path_len", -1),
"out_path": c.get("out_path", ""),
"out_path_hash_mode": c.get("out_path_hash_mode", -1),
# flag bits: bit0=battery, bit1=moving, bit3=store_fwd
"flag_battery": bool(flags & 0x01),
"flag_store_fwd": bool(flags & 0x08),
}
# ── MeshCore background thread ────────────────────────────────────────────────
def run_meshcore():
global mc_loop
mc_loop = asyncio.new_event_loop()
asyncio.set_event_loop(mc_loop)
mc_loop.run_until_complete(_meshcore_loop())
async def _meshcore_loop():
global mc, self_info, contacts, channels, connected
while True:
try:
logger.info(f"Connecting to {SERIAL_PORT} @ {BAUD_RATE} baud...")
mc = await MeshCore.create_serial(SERIAL_PORT, BAUD_RATE, auto_reconnect=True, default_timeout=10.0)
connected = True
socketio.emit("status", {"connected": True, "msg": f"Verbunden · {SERIAL_PORT}"})
logger.info("MeshCore connected")
# ── Event subscriptions ────────────────────────────────────────
def on_self_info(event):
global self_info
info = event.payload if hasattr(event, "payload") and event.payload is not None else {}
if isinstance(info, dict):
self_info = {
"name": info.get("name", "Unknown"),
"key": info.get("public_key", "")[:16],
"lat": info.get("adv_lat", 0.0),
"lon": info.get("adv_lon", 0.0),
"freq": info.get("radio_freq", 0),
"sf": info.get("radio_sf", 0),
"bw": info.get("radio_bw", 0),
"cr": info.get("radio_cr", 0),
"tx_power": info.get("tx_power", 0),
}
socketio.emit("self_info", self_info)
def on_contacts(event):
asyncio.ensure_future(_push_contacts())
def on_new_contact(event):
asyncio.ensure_future(_push_contacts())
def on_advert(event):
# Update last_seen on advertisement
asyncio.ensure_future(_push_contacts())
def on_direct_msg(event):
msg = event.payload if hasattr(event, "payload") and event.payload is not None else {}
if not isinstance(msg, dict):
return
sender_key = msg.get("pubkey", "") or msg.get("pub_key", "") or ""
if isinstance(sender_key, bytes):
sender_key = sender_key.hex()
sender_name = ""
if sender_key and sender_key in contacts:
sender_name = contacts[sender_key].get("name", sender_key[:8])
elif sender_key:
sender_name = sender_key[:8]
text = msg.get("text", "") or msg.get("msg", "")
ck = conv_key_direct(sender_key) if sender_key else "dm_unknown"
m = {
"type": "direct",
"from": sender_name or "Unknown",
"from_key": sender_key[:16] if sender_key else "",
"text": str(text),
"ts": ts(),
"self": False,
"conv_key": ck,
}
chatdb.save(m)
socketio.emit("message", m)
def on_channel_msg(event):
msg = event.payload if hasattr(event, "payload") and event.payload is not None else {}
if not isinstance(msg, dict):
return
raw_text = msg.get("text", "") or msg.get("msg", "")
ch_idx = msg.get("channel_idx", 0)
ch_name = channels.get(ch_idx, {}).get("name", f"Ch{ch_idx}")
# sender_name may be separate or embedded as "Name: message"
sender = msg.get("sender_name") or msg.get("sender") or ""
text = raw_text
if not sender and ": " in raw_text:
parts = raw_text.split(": ", 1)
sender = parts[0]
text = parts[1]
m = {
"type": "channel",
"from": str(sender) or "?",
"channel": ch_idx,
"ch_name": ch_name,
"text": str(text),
"ts": ts(),
"self": False,
"conv_key": conv_key_channel(ch_idx),
}
chatdb.save(m)
socketio.emit("message", m)
def on_disconnect(event):
global connected
connected = False
socketio.emit("status", {"connected": False, "msg": "Radio getrennt"})
mc.subscribe(EventType.SELF_INFO, on_self_info)
mc.subscribe(EventType.CONTACTS, on_contacts)
mc.subscribe(EventType.NEW_CONTACT, on_new_contact)
mc.subscribe(EventType.NEXT_CONTACT, on_new_contact)
mc.subscribe(EventType.ADVERTISEMENT, on_advert)
mc.subscribe(EventType.CONTACT_MSG_RECV, on_direct_msg)
mc.subscribe(EventType.CHANNEL_MSG_RECV, on_channel_msg)
mc.subscribe(EventType.DISCONNECTED, on_disconnect)
# Initial data load — contacts first, then start fetching, then channels
await asyncio.sleep(1)
await _push_contacts()
await mc.start_auto_message_fetching()
await asyncio.sleep(1)
await _push_channels()
while mc.is_connected:
await asyncio.sleep(2)
except Exception as e:
connected = False
mc = None
logger.error(f"MeshCore error: {e}", exc_info=True)
socketio.emit("status", {"connected": False, "msg": f"Verbindungsfehler: {e}"})
await asyncio.sleep(5)
async def _push_contacts():
global contacts
try:
await mc.ensure_contacts(follow=True)
raw = mc.contacts or {}
contacts = {}
if isinstance(raw, dict):
for key, c in raw.items():
if isinstance(c, dict):
contacts[key] = contact_to_dict(key, c)
socketio.emit("contacts", list(contacts.values()))
# Also push map markers for nodes with GPS
markers = [c for c in contacts.values() if c["has_gps"]]
socketio.emit("map_markers", markers)
except Exception as e:
logger.error(f"Contacts error: {e}")
async def _push_channels():
global channels
try:
ch_tmp = {}
for i in range(NUM_CHANNELS):
try:
res = await mc.commands.get_channel(i)
if res and hasattr(res, "payload") and isinstance(res.payload, dict):
p = res.payload
name = p.get("channel_name", "")
idx = p.get("channel_idx", i)
if name:
ch_tmp[idx] = {
"idx": idx,
"name": name,
"hash": p.get("channel_hash", ""),
}
await asyncio.sleep(1.2)
except Exception as e:
logger.warning(f"get_channel({i}) failed: {e}")
await asyncio.sleep(0.5)
channels = ch_tmp
socketio.emit("channels", list(channels.values()))
logger.info(f"Channels loaded: {[c['name'] for c in channels.values()]}")
except Exception as e:
logger.error(f"Channels error: {e}")
# ── Flask routes ──────────────────────────────────────────────────────────────
@app.route("/")
def index():
return render_template("index.html")
@app.route("/history/<path:conv_key>")
def history(conv_key):
from flask import jsonify
return jsonify(chatdb.get_history(conv_key))
@app.route("/conversations")
def conversations():
from flask import jsonify
return jsonify(chatdb.get_conversations())
# ── SocketIO events ───────────────────────────────────────────────────────────
@socketio.on("connect")
def on_ws_connect():
emit("status", {
"connected": connected,
"msg": "Verbunden" if connected else "Warte auf Radio..."
})
if self_info: emit("self_info", self_info)
if contacts: emit("contacts", list(contacts.values()))
if channels: emit("channels", list(channels.values()))
markers = [c for c in contacts.values() if c.get("has_gps")]
if markers: emit("map_markers", markers)
# Send stored history for all known conversations
convs = chatdb.get_conversations()
emit("conversations", convs)
for conv in convs:
msgs = chatdb.get_history(conv["conv_key"])
if msgs:
emit("history", {"conv_key": conv["conv_key"], "messages": msgs})
@socketio.on("send_message")
def on_send(data):
if not mc or not mc_loop:
emit("error", {"msg": "Kein Radio verbunden"})
return
target_key = data.get("to", "__channel__")
ch_idx = int(data.get("channel", 0))
text = data.get("text", "").strip()
if not text:
return
async def _send():
try:
if target_key == "__channel__":
await mc.commands.send_chan_msg(ch_idx, text)
ch_name = channels.get(ch_idx, {}).get("name", f"Ch{ch_idx}")
m = {
"type": "channel",
"from": self_info.get("name", "Ich"),
"channel": ch_idx,
"ch_name": ch_name,
"text": text,
"ts": ts(),
"self": True,
"conv_key": conv_key_channel(ch_idx),
}
chatdb.save(m)
socketio.emit("message", m)
else:
contact = mc.get_contact_by_key_prefix(target_key)
if contact:
await mc.commands.send_msg(contact, text)
name = contacts.get(target_key, {}).get("name", target_key[:8])
m = {
"type": "direct",
"from": self_info.get("name", "Ich"),
"to": name,
"to_key": target_key[:16],
"text": text,
"ts": ts(),
"self": True,
"conv_key": conv_key_direct(target_key),
}
chatdb.save(m)
socketio.emit("message", m)
else:
socketio.emit("error", {"msg": f"Kontakt nicht gefunden: {target_key[:8]}"})
except Exception as e:
logger.error(f"Send error: {e}")
socketio.emit("error", {"msg": str(e)})
asyncio.run_coroutine_threadsafe(_send(), mc_loop)
@socketio.on("refresh")
def on_refresh():
if mc and mc_loop:
asyncio.run_coroutine_threadsafe(_push_contacts(), mc_loop)
asyncio.run_coroutine_threadsafe(_push_channels(), mc_loop)
@socketio.on("get_history")
def on_get_history(data):
conv_key = data.get("conv_key", "")
emit("history", {
"conv_key": conv_key,
"messages": chatdb.get_history(conv_key)
})
@socketio.on("get_map_markers")
def on_get_markers():
markers = [c for c in contacts.values() if c.get("has_gps")]
emit("map_markers", markers)
@socketio.on("send_advert")
def on_send_advert(data):
if not mc or not mc_loop:
emit("advert_sent", {"ok": False, "msg": "Kein Radio verbunden"})
return
flood = bool(data.get("flood", False))
async def _advert():
try:
res = await mc.commands.send_advert(flood=flood)
ok = res and hasattr(res, "type") and res.type.name != "ERROR"
socketio.emit("advert_sent", {"ok": ok, "flood": flood, "msg": str(getattr(res, "payload", ""))})
logger.info(f"Advert sent (flood={flood}): {res}")
except Exception as e:
logger.error(f"Advert error: {e}")
socketio.emit("advert_sent", {"ok": False, "flood": flood, "msg": str(e)})
asyncio.run_coroutine_threadsafe(_advert(), mc_loop)
if __name__ == "__main__":
threading.Thread(target=run_meshcore, daemon=True).start()
socketio.run(
app,
host=HTTP_HOST,
port=HTTP_PORT,
debug=False,
allow_unsafe_werkzeug=True
)