This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_local.py
55 lines (39 loc) · 1.51 KB
/
sync_local.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
import sys
from argparse import ArgumentParser
from m4m_sync import serializers
from database import DatabaseManager
from m4m_sync.encrypt import AesStreamWrapper
from m4m_sync.stores import LocalStore, Sensor, Controller
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
)
logger = logging.getLogger(__name__)
def main():
parser = ArgumentParser()
parser.add_argument("--db-uri", required=True)
parser.add_argument("--serializer", default="CsvRawSerializer")
parser.add_argument("--root", required=True)
args = parser.parse_args()
logger.info("init")
db = DatabaseManager(args.db_uri)
store = LocalStore(root=args.root)
for controller in db.get_controllers():
c = Controller(name=controller.name, mac=controller.mac)
store.prepare_for_sync_controller(c)
for sensor in db.get_sensors(controller):
first_date = db.get_first_sensor_data_date(sensor.id)
s = Sensor(name=sensor.name, id=sensor.id, controller=c)
store.prepare_for_sync_sensor(s)
store.sync(
sensor=s,
serializer=getattr(serializers, args.serializer)(),
stream_wrapper=AesStreamWrapper(key=db.get_encryption_key()),
first_date=first_date,
get_data=lambda time_range: db.get_sensor_data(sensor.id, time_range),
)
logger.info("done")
if __name__ == "__main__":
main()