def zmq_producer(context,instrument):
    socket = context.socket(zmq.PUB)
    socket.connect('tcp://127.0.0.1:5000')

    while True:
		now_time=int(time.time())
		now=datetime.datetime.now(pytz.timezone(TIMEZONE))
		now_day=now.day
		now_month=now.month
		global start_time
		global start_day
		global count
		global f
		if ((now_time-start_time) > THRESHOLD_TIME) or (now_day!=start_day):
			if now_day!=start_day:
				count=find_count(now_day, now_month)-1 #Decrementing by one since we always increase by 1 o/w
				delete_older_folders(now) #Deleting 2 days older folder
			count=count+1
			start_time=now_time
			start_day=now_day
			start_month=now_month
			f.close()
			f=open(DATA_BASE_PATH+str(start_day)+"_"+str(start_month)+"/"+str(count)+".csv","wa")
			f.write(HEADER)

		else:
			try:
				readings_array = instrument.read_registers(EM6400_BASE_REGISTER,EM6400_NUMBER_OF_REGISTERS)
				row=str(now_time)+","
				for i in range(0,len(readings_array)-1,2):
					a=(readings_array[i+1]<<16) +readings_array[i]
					row=row+str(convert(a))+","
				socket.send(row)
				row=row[:-1]+"\n"
				f.write(row)
				#x = time.time() * 1000+GMT_TIME_DIFFERENCE_MILLISECONDS
				#y=float(row.split(",")[2])
				#socket.send(json.dumps(dict(x=x, y=y)))
				gevent.sleep(0.5)		
			except Exception as e:
				global log_file
				log_file=open(DATA_BASE_PATH+"log.txt","w")
				log_file.write(str(time.time())+" "+e.__str__())
				log_file.close()
				instrument = minimalmodbus.Instrument(METER_PORT, METER_ID)		
from configuration import METER_PORT, METER_ID, DATA_BASE_PATH, THRESHOLD_TIME, \
	TIMEZONE, EM6400_BASE_REGISTER, EM6400_NUMBER_OF_REGISTERS, BAUD_RATE, HEADER
from utilities import convert, find_count, delete_older_folders
import datetime
import minimalmodbus
import pytz
import time

instrument = minimalmodbus.Instrument(METER_PORT, METER_ID)
instrument.serial.baudrate = BAUD_RATE   # Baud

start_time=int(time.time())
now = datetime.datetime.now(pytz.timezone(TIMEZONE))
start_day=now.day
start_month=now.month
count=find_count(now.day, now.month)

f=open(DATA_BASE_PATH+str(start_day)+"_"+str(start_month)+"/"+str(count)+".csv","wa")
f.write(HEADER)
log_file=open(DATA_BASE_PATH+"log.txt","w")


while True:
	now_time=int(time.time())
	now = datetime.datetime.now(pytz.timezone(TIMEZONE))
	now_day=now.day
	now_month=now.month
	
	
	if ((now_time-start_time) > THRESHOLD_TIME) or (now_day!=start_day):
		if now_day!=start_day:
import json
import minimalmodbus
import os
import paste.urlparser
import pytz
import time
global start_time
start_time=int(time.time())
global now
now=datetime.datetime.now(pytz.timezone(TIMEZONE))
global start_date
start_day=now.day
global start_month
start_month=now.month
global count
count=find_count(now.day, now.month)
global f
global log_file
log_file=open(DATA_BASE_PATH+"log.txt","w")
   
def main():
	'''Set up zmq context and greenlets for all the servers, then launch the web
	browser and run the data producer'''
	context = zmq.Context()
	# zeromq: tcp to inproc gateway
	gevent.spawn(zmq_server, context)
	# websocket server: copies inproc zmq messages to websocket
	ws_server = gevent.pywsgi.WSGIServer(
        ('', 9999), WebSocketApp(context),
        handler_class=WebSocketHandler)
    # http server: serves up static files