-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_receiver.py
More file actions
112 lines (93 loc) · 3.19 KB
/
message_receiver.py
File metadata and controls
112 lines (93 loc) · 3.19 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
import sqlite3
import subprocess
import collections
import logging
import configparser
import json
import os
from datetime import datetime
# Logger opsætning
logging.basicConfig(
filename="messagereceiver.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
DB_PATH = "messages.db"
def load_config(filepath='config.txt'):
"""
Indlæser konfigurationsindstillinger fra config.txt.
"""
cfg = configparser.ConfigParser()
cfg.read(filepath)
return cfg
def insert_message_into_db(raw_message):
"""
Indsætter en rå besked i databasen.
"""
try:
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO messages (raw_message)
VALUES (?)
""", (raw_message,))
conn.commit()
#logging.info(f"Rå besked gemt i databasen: {raw_message}")
except sqlite3.Error as e:
logging.error(f"Fejl under indsættelse i databasen: {e}")
def start_message_receiver(cfg):
"""
Starter beskedmodtagerprocessen og gemmer rå beskeder i databasen.
"""
logging.info(f"Starter besked modtageren")
prots = cfg.get('multimon-ng', 'prot').split()
prots = ' -a '.join(prots)
if prots:
prots = '-a ' + prots
recent_messages = collections.deque(maxlen=100)
command = (
"rtl_fm {} -d {} -l {} -g {} -p {} -f {} -s {} | multimon-ng -C {} -t raw {} -f alpha /dev/stdin -"
.format(
cfg.get('rtl_fm', 'enable_option'),
cfg.get('rtl_fm', 'device_index'),
cfg.get('rtl_fm', 'squelch_level'),
cfg.get('rtl_fm', 'gain'),
cfg.get('rtl_fm', 'ppm_error'),
cfg.get('Frequencies', 'freq'),
cfg.get('rtl_fm', 'sample_rate'),
cfg.get('multimon-ng', 'pocsag_charset'),
prots
)
)
#logging.info(f"Kommando udført: {command}")
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
decode_format = cfg.get('Encoding', 'encoding_format')
try:
while True:
output = process.stdout.readline()
#if not output:
#continue
#logging.info(f"Rå output B: {output}")
if "Alpha" not in output:
continue
logging.info(f"Rå output A: {output}")
output = output.replace("<NUL>", "")
#if output in recent_messages:
#continue
recent_messages.append(output)
raw_message = output.split("Alpha:", 1)[1]
#if len(raw_message) < int(cfg.get('multimon-ng', 'min_len')):
#continue
# Gem rå besked i databasen
insert_message_into_db(raw_message)
except Exception as e:
logging.error(f"Fejl under beskedmodtagelse: {e}")
finally:
process.terminate()
logging.info("Message receiver-processen er afsluttet.")
if __name__ == "__main__":
try:
cfg = load_config()
start_message_receiver(cfg)
except Exception as e:
logging.error(f"Kritisk fejl: {e}")