-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsn_data_archive.py
41 lines (30 loc) · 1 KB
/
wsn_data_archive.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
# Standard Library
from datetime import date
import json
import os
from mq import MQ
class Consumer(MQ):
name = 'wsn_data_archive'
def sub_to(self):
return ('wsn_data', 'fanout', self.name, self.handle_message)
def get_dirname(self, body):
source_addr = body.get('source_addr')
if source_addr is None:
return 'null'
return source_addr
def handle_message(self, body):
# Create parent directory
dirname = self.get_dirname(body)
dirpath = os.path.join(datadir, dirname)
os.makedirs(dirpath, exist_ok=True)
# Append
received = body['received']
received = date.fromtimestamp(received).strftime('%Y%m%d')
filepath = os.path.join(dirpath, received)
with open(filepath, 'a+') as f:
body = json.dumps(body)
f.write(body + '\n')
if __name__ == '__main__':
datadir = os.path.join(os.getcwd(), 'data', 'cooked')
with Consumer() as consumer:
consumer.start()