Revize 57cc3beb
Přidáno uživatelem Martin Forejt před téměř 4 roky(ů)
aswi2021vochomurka/main.py | ||
---|---|---|
7 | 7 |
print("hello world Vochomurka") |
8 | 8 |
app = Application(sys.argv) |
9 | 9 |
sys.exit(app.exec_()) |
10 |
|
|
11 |
|
|
12 |
if __name__ == '__main__': |
|
13 |
start() |
aswi2021vochomurka/service/file_manager.py | ||
---|---|---|
1 |
import os |
|
2 |
import time |
|
3 |
|
|
4 |
from aswi2021vochomurka.model.Message import Message |
|
5 |
from typing import TextIO |
|
6 |
|
|
7 |
trans = str.maketrans({ |
|
8 |
"/": "_", |
|
9 |
"\\": "_", |
|
10 |
":": "_", |
|
11 |
"\"": "_", |
|
12 |
"$": "_", |
|
13 |
"*": "_", |
|
14 |
".": "_"}) |
|
15 |
|
|
16 |
|
|
17 |
def create_filename(message: Message): |
|
18 |
name = "data/" + message.topic.translate(trans) + "/" + message.date + "_" + message.time + ".csv" |
|
19 |
return name |
|
20 |
|
|
21 |
|
|
22 |
class FileManager: |
|
23 |
topic: str |
|
24 |
lastUpdate: float |
|
25 |
file: TextIO |
|
26 |
|
|
27 |
def __init__(self, topic: str, message: Message): |
|
28 |
self.topic = topic |
|
29 |
|
|
30 |
try: |
|
31 |
filename = create_filename(message) |
|
32 |
os.makedirs(os.path.dirname(filename), exist_ok=True) |
|
33 |
self.file = open(filename, "w+") |
|
34 |
except Exception as error: |
|
35 |
print(error) |
|
36 |
raise error |
|
37 |
self.write(message) |
|
38 |
|
|
39 |
def write(self, message: Message): |
|
40 |
self.file.write(message.date + ";" + message.time + ";" + str(message.index) + ";" + str(message.value) + "\n") |
|
41 |
self.lastUpdate = time.time() |
|
42 |
|
|
43 |
def close(self): |
|
44 |
self.file.flush() |
|
45 |
self.file.close() |
aswi2021vochomurka/service/mqtt/mqtt_subscriber.py | ||
---|---|---|
10 | 10 |
def on_connect(self, client, userdata, flags, rc, properties=None): |
11 | 11 |
print("Connected with result code " + str(rc)) |
12 | 12 |
self.callback.onConnected() |
13 |
|
|
13 | 14 |
# Subscribing in on_connect() means that if we lose the connection and |
14 | 15 |
# reconnect then subscriptions will be renewed. |
15 | 16 |
for topic in self.params.topics: |
... | ... | |
22 | 23 |
m = parse_mqtt_message(message) |
23 | 24 |
print(m) |
24 | 25 |
self.callback.onMessage(m) |
26 |
self.write_to_file(m) |
|
25 | 27 |
except ParseException as error: |
26 | 28 |
print('invalid message data format') |
29 |
client.unsubscribe(message.topic) |
|
27 | 30 |
# TODO better reaction on bad format |
28 | 31 |
pass |
29 | 32 |
|
30 | 33 |
def on_disconnect(self, client, userdata, rc): |
31 | 34 |
self.callback.onDisconnected() |
35 |
self.stop() |
|
32 | 36 |
|
33 | 37 |
def start(self): |
38 |
super().start() |
|
34 | 39 |
client = mqtt.Client() |
35 | 40 |
client.on_connect = self.on_connect |
36 | 41 |
client.on_message = self.on_message |
aswi2021vochomurka/service/subscriber.py | ||
---|---|---|
1 |
import time |
|
2 |
|
|
3 |
from apscheduler.schedulers.background import BackgroundScheduler |
|
4 |
|
|
5 |
from aswi2021vochomurka.model.Message import Message |
|
6 |
from aswi2021vochomurka.service.file_manager import FileManager |
|
1 | 7 |
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback |
2 | 8 |
from aswi2021vochomurka.service.subscriber_params import SubscriberParams |
9 |
from typing import Dict |
|
3 | 10 |
|
4 | 11 |
|
5 | 12 |
class Subscriber: |
6 | 13 |
callback: SubscriberCallback |
7 | 14 |
params: SubscriberParams |
8 | 15 |
|
16 |
scheduler = BackgroundScheduler() |
|
17 |
files: Dict[str, FileManager] = {} |
|
18 |
|
|
9 | 19 |
def __init__(self, callback: SubscriberCallback, params: SubscriberParams): |
10 | 20 |
self.callback = callback |
11 | 21 |
self.params = params |
12 | 22 |
|
13 |
def start(self): raise NotImplementedError |
|
23 |
def start(self): |
|
24 |
# start scheduler to check closed topics |
|
25 |
self.scheduler.add_job(self.check_closed_topics, 'interval', seconds=self.params.closeLimit) |
|
26 |
self.scheduler.start() |
|
27 |
|
|
28 |
def stop(self): |
|
29 |
self.scheduler.shutdown() |
|
30 |
self.close_files() |
|
31 |
|
|
32 |
def close_files(self): |
|
33 |
for topic in self.files: |
|
34 |
self.files.get(topic).close() |
|
35 |
self.files = {} |
|
36 |
|
|
37 |
def check_closed_topics(self): |
|
38 |
t = time.time() |
|
39 |
for topic in list(self.files): |
|
40 |
file = self.files.get(topic) |
|
41 |
if t - file.lastUpdate > self.params.closeLimit: |
|
42 |
self.callback.onCloseTopic(topic) |
|
43 |
file.close() |
|
44 |
self.files.pop(topic) |
|
45 |
|
|
46 |
def write_to_file(self, message: Message): |
|
47 |
if message.topic in self.files: |
|
48 |
self.files.get(message.topic).write(message) |
|
49 |
else: |
|
50 |
fm = FileManager(message.topic, message) |
|
51 |
self.files[message.topic] = fm |
aswi2021vochomurka/view/main_view.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
params = SubscriberParams( |
35 | 35 |
["/home/1", "/home/2"], |
36 |
120,
|
|
36 |
10, |
|
37 | 37 |
ConnectionParams("localhost", 1883, 60), |
38 | 38 |
True |
39 | 39 |
) |
poetry.lock | ||
---|---|---|
1 |
[[package]] |
|
2 |
name = "apscheduler" |
|
3 |
version = "3.7.0" |
|
4 |
description = "In-process task scheduler with Cron-like capabilities" |
|
5 |
category = "main" |
|
6 |
optional = false |
|
7 |
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" |
|
8 |
|
|
9 |
[package.dependencies] |
|
10 |
pytz = "*" |
|
11 |
six = ">=1.4.0" |
|
12 |
tzlocal = ">=2.0,<3.0" |
|
13 |
|
|
14 |
[package.extras] |
|
15 |
asyncio = ["trollius"] |
|
16 |
doc = ["sphinx", "sphinx-rtd-theme"] |
|
17 |
gevent = ["gevent"] |
|
18 |
mongodb = ["pymongo (>=3.0)"] |
|
19 |
redis = ["redis (>=3.0)"] |
|
20 |
rethinkdb = ["rethinkdb (>=2.4.0)"] |
|
21 |
sqlalchemy = ["sqlalchemy (>=0.8)"] |
|
22 |
testing = ["pytest (<6)", "pytest-cov", "pytest-tornado5", "mock", "pytest-asyncio (<0.6)", "pytest-asyncio"] |
|
23 |
tornado = ["tornado (>=4.3)"] |
|
24 |
twisted = ["twisted"] |
|
25 |
zookeeper = ["kazoo"] |
|
26 |
|
|
1 | 27 |
[[package]] |
2 | 28 |
name = "cycler" |
3 | 29 |
version = "0.10.0" |
... | ... | |
107 | 133 |
[package.dependencies] |
108 | 134 |
six = ">=1.5" |
109 | 135 |
|
136 |
[[package]] |
|
137 |
name = "pytz" |
|
138 |
version = "2021.1" |
|
139 |
description = "World timezone definitions, modern and historical" |
|
140 |
category = "main" |
|
141 |
optional = false |
|
142 |
python-versions = "*" |
|
143 |
|
|
110 | 144 |
[[package]] |
111 | 145 |
name = "recordclass" |
112 | 146 |
version = "0.14.3" |
... | ... | |
117 | 151 |
|
118 | 152 |
[[package]] |
119 | 153 |
name = "six" |
120 |
version = "1.15.0"
|
|
154 |
version = "1.16.0"
|
|
121 | 155 |
description = "Python 2 and 3 compatibility utilities" |
122 | 156 |
category = "main" |
123 | 157 |
optional = false |
124 | 158 |
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" |
125 | 159 |
|
160 |
[[package]] |
|
161 |
name = "tzlocal" |
|
162 |
version = "2.1" |
|
163 |
description = "tzinfo object for the local timezone" |
|
164 |
category = "main" |
|
165 |
optional = false |
|
166 |
python-versions = "*" |
|
167 |
|
|
168 |
[package.dependencies] |
|
169 |
pytz = "*" |
|
170 |
|
|
126 | 171 |
[metadata] |
127 | 172 |
lock-version = "1.1" |
128 | 173 |
python-versions = "^3.8" |
129 |
content-hash = "ee7528e0c87657eed5dd2e5bd18a5f3a9bcf2ace546c4b13c76a4a781f0b4bbf"
|
|
174 |
content-hash = "6c4a3eb9c9d005c7858ec86442c9824aa10ea6d2be7db26d77ccd4f3e12a6e9b"
|
|
130 | 175 |
|
131 | 176 |
[metadata.files] |
177 |
apscheduler = [ |
|
178 |
{file = "APScheduler-3.7.0-py2.py3-none-any.whl", hash = "sha256:c06cc796d5bb9eb3c4f77727f6223476eb67749e7eea074d1587550702a7fbe3"}, |
|
179 |
{file = "APScheduler-3.7.0.tar.gz", hash = "sha256:1cab7f2521e107d07127b042155b632b7a1cd5e02c34be5a28ff62f77c900c6a"}, |
|
180 |
] |
|
132 | 181 |
cycler = [ |
133 | 182 |
{file = "cycler-0.10.0-py2.py3-none-any.whl", hash = "sha256:1d8a5ae1ff6c5cf9b93e8811e581232ad8920aeec647c37316ceac982b08cb2d"}, |
134 | 183 |
{file = "cycler-0.10.0.tar.gz", hash = "sha256:cd7b2d1018258d7247a71425e9f26463dfb444d411c39569972f4ce586b0c9d8"}, |
... | ... | |
296 | 345 |
{file = "python-dateutil-2.8.1.tar.gz", hash = "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c"}, |
297 | 346 |
{file = "python_dateutil-2.8.1-py2.py3-none-any.whl", hash = "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a"}, |
298 | 347 |
] |
348 |
pytz = [ |
|
349 |
{file = "pytz-2021.1-py2.py3-none-any.whl", hash = "sha256:eb10ce3e7736052ed3623d49975ce333bcd712c7bb19a58b9e2089d4057d0798"}, |
|
350 |
{file = "pytz-2021.1.tar.gz", hash = "sha256:83a4a90894bf38e243cf052c8b58f381bfe9a7a483f6a9cab140bc7f702ac4da"}, |
|
351 |
] |
|
299 | 352 |
recordclass = [ |
300 | 353 |
{file = "recordclass-0.14.3-cp27-cp27m-win32.whl", hash = "sha256:016a70b9e1d93e184a065a23d9bd15387c154375a166fd8868db96f362d77d94"}, |
301 | 354 |
{file = "recordclass-0.14.3-cp27-cp27m-win_amd64.whl", hash = "sha256:c9941edde67077f95e83dfd69795b2bc6cff13e05ef537b4a57cddfb8fac55f9"}, |
... | ... | |
310 | 363 |
{file = "recordclass-0.14.3.tar.gz", hash = "sha256:f2a0a42ec1128be81e28b8ba82ef1a1dccc41efbec6ecf97866915649645222f"}, |
311 | 364 |
] |
312 | 365 |
six = [ |
313 |
{file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"}, |
|
314 |
{file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, |
|
366 |
{file = "six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, |
|
367 |
{file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, |
|
368 |
] |
|
369 |
tzlocal = [ |
|
370 |
{file = "tzlocal-2.1-py2.py3-none-any.whl", hash = "sha256:e2cb6c6b5b604af38597403e9852872d7f534962ae2954c7f35efcb1ccacf4a4"}, |
|
371 |
{file = "tzlocal-2.1.tar.gz", hash = "sha256:643c97c5294aedc737780a49d9df30889321cbe1204eac2c2ec6134035a92e44"}, |
|
315 | 372 |
] |
pyproject.toml | ||
---|---|---|
8 | 8 |
python = "^3.8" |
9 | 9 |
PyQt5 = "^5.15.4" |
10 | 10 |
recordclass = "^0.14.3" |
11 |
APScheduler = "^3.7.0" |
|
11 | 12 |
numpy = "^1.20.2" |
12 | 13 |
matplotlib = "^3.4.1" |
13 | 14 |
paho-mqtt = "^1.5.1" |
Také k dispozici: Unified diff
Feature/8731 files