#!/usr/bin/env python2

"""
Within this program we receive a stream of KV78turbo data
because KV78turbo consists of a static part, KV7turbo, we
will store this for later use. Obviously we store it in
the compressed form it arrived in. A unix-timestamp is
used as filename.
"""

basepath = '/home/projects/openov/kv7/log-py/'

import zmq
from time import time

context = zmq.Context()

subscriber = context.socket(zmq.SUB)
subscriber.connect("tcp://127.0.0.1:7817")
subscriber.setsockopt(zmq.SUBSCRIBE, "/GOVI/KV7planning")
subscriber.setsockopt(zmq.SUBSCRIBE, "/GOVI/KV7kalender")

while True:
	multipart = subscriber.recv_multipart()
	address = multipart[0]

	f = open(basepath + address + '/' + "%.4f" % time() + '.gz', 'wb')
	#for part in multipart[2:]:
	#	f.write(part)
	f.write(''.join(multipart[1:]))
	
	f.close()

subscriber.close()
context.term()
