#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

import datetime
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg #, NavigationToolbar2TkAgg



from matplotlib.figure import Figure

#import sys
import tkinter as Tk
import sys, traceback
import stomp
import json
from py4j.java_gateway import JavaGateway
gateway = JavaGateway()    
gateway.launch_gateway

client = gateway.entry_point.getClient()

stomp_server = 'localhost'
stomp_port = 61612    
agg_topic = '/topic/pmu/PMU_1/PMU_2/agg'
username = 'pmu_user'
pw = 'password'
startDateStr = "2014-07-10 00:00:00.000"
formatter = gateway.jvm.java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")

#values for the graph
graphTimes = []
phasorValues = []
phasor2Values = []
differences = []
showGraph=False

class StompListener(object):
    def on_error(self, headers, message):
        print('received an error %s' % message)
    def on_message(self, headers, message):
        topic = headers.pop("destination", "")
        if(topic==agg_topic):
            #Parse json message
            jdata = json.loads(message)
            timeStr = jdata['timestamp']
            time = formatter.parse(timeStr)
            
            print(' uploading %s' % message)
            #Send to goss using client api
            uploadReq = gateway.entry_point.createUploadRequest(time, jdata['phasor1'], jdata['phasor2'], jdata['difference'],"Tutorial")
            print(uploadReq)            
            client.getResponse(uploadReq)
            print('after upload')
            
            #Update last posted label
            newLabel = str(time)+", "+str(jdata['phasor1'])+", "+str(jdata['phasor2'])+", "+str(jdata['difference'])
            addedTextVar.set(newLabel)
            
            #update graph data
            if showGraph:
                print('update graph')
                timestamp = datetime.datetime.strptime(jdata['timestamp'], '%Y-%m-%d %H:%M:%S.%f')
                graphTimes.append(timestamp)
                phasorValues.append(jdata['phasor1'])
                phasor2Values.append(jdata['phasor2'])
                differences.append(abs(jdata['difference']))
                graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate=False, linestyle='-', marker='None')
                figure.canvas.draw()
            
        else:
            print('received a message on another topic %s ' % topic)




#set up the window
root = Tk.Tk()
root.wm_title("GOSS tutorial Client")
widthFrame = Tk.Frame(root, width=800, height=1)
widthFrame.pack(fill=Tk.X, expand=True)
heightFrame = Tk.Frame(root, width=1, height=600)
heightFrame.pack(side=Tk.RIGHT, fill=Tk.Y, expand=True)

figure = Figure(figsize=(5,4), dpi=150)
graphPlotA = figure.add_subplot(111)
graphPlotA.fmt_xdata = matplotlib.dates.DateFormatter('%m-%d %H-%M-%S') 
# this rotates the ticklabels to help with overlapping 
figure.autofmt_xdate() 
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(figure, master=root)



#figure.title("Phase Angle Difference")






def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate

buttonFrame = Tk.Frame(root )
buttonFrame.pack( side = Tk.TOP)                    

#button = Tk.Button(master=root, text='Quit', command=_quit)
#button.pack(side=Tk.BOTTOM)

conn = None
def _monitor():
    global conn
    if monitorbutton.config('text')[-1] == 'Start Monitoring':
        monitorbutton.config(text='Stop Monitoring')
        # do monitoring stuff
        print('Start Monitoring')
        graphbutton.grid(row=2, column=0)
        
        #start connection
        
        conn = stomp.Connection([(stomp_server,stomp_port)])
        conn.set_listener('', StompListener())
        conn.start()
        conn.connect(username, pw)
        conn.subscribe(destination=agg_topic, id=1, ack='auto')
        
        #conn.subscribe(destination='/topic//pmu/PMU_1', id=1, ack='auto')
        
        addedTextLabel.grid(row=1, column=3)
        addedText.grid(row=1, column=4)
    else:
        monitorbutton.config(text='Start Monitoring')
        #stop the monitoring
        #hide the graph
        graphbutton.grid_forget()
        addedTextLabel.grid_forget()
        addedText.grid_forget()
        canvas._tkcanvas.pack_forget()
        print('Stop Monitoring')    
        if(conn!=None):
            conn.disconnect()
                        

monitorbutton = Tk.Button(master=buttonFrame, text='Start Monitoring', command=_monitor)
monitorbutton.grid(row=1, column=0)

addedTextLabel = Tk.Label(master=buttonFrame, text="Last Entry Added:")
#addedTextLabel.grid(row=1, column=3)

addedTextVar = Tk.StringVar()
addedText = Tk.Label(master=buttonFrame, textvariable=addedTextVar)
#addedText.grid(row=1, column=4)


def _graph():
    #values for the graph
    global graphTimes 
    global phasorValues 
    global phasor2Values 
    global differences 
    global showGraph 
    if graphbutton.config('text')[-1] == 'Show Graph':
        graphbutton.config(text='Hide Graph')
        # start the poll for graph data
        # show the graph
        print('Show Graph')
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.BOTTOM, fill=Tk.BOTH, expand=1, )
       
        #send request for graph data
        startDate = formatter.parse(startDateStr)
        dataRequest = gateway.entry_point.createDownloadRequest(startDate)
        print('Created Request')
        response = client.getResponse(dataRequest)
        data = response.getData()
        print(data)
        jdata = json.loads(data)
        
        for x in jdata:
            timestamp = datetime.datetime.strptime(x['timestamp'], '%Y-%m-%d %H:%M:%S.%f')
            print(timestamp,' ',x['phasor1'],' ',x['phasor2'],' ',abs(x['difference']))
            graphTimes.append(timestamp)
            phasorValues.append(x['phasor1'])
            phasor2Values.append(x['phasor2'])
            differences.append(abs(x['difference']))
            
        #graphPlotA.plot_date(x=graphTimes, y=phasorValues, xdate=True, ydate=False, linestyle='-', marker='None')
        #graphPlotA.plot_date(x=graphTimes, y=phasor2Values, xdate=True, ydate=False, linestyle='-', marker='None')
        graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate=False, linestyle='-', marker='None')
       
        showGraph=True
       
    else:
        graphbutton.config(text='Show Graph')
        # stop the poll for graph data
        # hide the graph
        print('Hide Graph')  
        canvas._tkcanvas.pack_forget()
        showGraph=False
        graphTimes = []
        phasorValues = []
        phasor2Values = []
        differences = []
        

graphbutton = Tk.Button(master=buttonFrame, text='Show Graph', command=_graph)

Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.
74d9f57b1b936547f94cc74e451c80494871ccf0
<mask token>


class StompListener(object):

    def on_error(self, headers, message):
        print('received an error %s' % message)

    def on_message(self, headers, message):
        topic = headers.pop('destination', '')
        if topic == agg_topic:
            jdata = json.loads(message)
            timeStr = jdata['timestamp']
            time = formatter.parse(timeStr)
            print(' uploading %s' % message)
            uploadReq = gateway.entry_point.createUploadRequest(time, jdata
                ['phasor1'], jdata['phasor2'], jdata['difference'], 'Tutorial')
            print(uploadReq)
            client.getResponse(uploadReq)
            print('after upload')
            newLabel = str(time) + ', ' + str(jdata['phasor1']) + ', ' + str(
                jdata['phasor2']) + ', ' + str(jdata['difference'])
            addedTextVar.set(newLabel)
            if showGraph:
                print('update graph')
                timestamp = datetime.datetime.strptime(jdata['timestamp'],
                    '%Y-%m-%d %H:%M:%S.%f')
                graphTimes.append(timestamp)
                phasorValues.append(jdata['phasor1'])
                phasor2Values.append(jdata['phasor2'])
                differences.append(abs(jdata['difference']))
                graphPlotA.plot_date(x=graphTimes, y=differences, xdate=
                    True, ydate=False, linestyle='-', marker='None')
                figure.canvas.draw()
        else:
            print('received a message on another topic %s ' % topic)


<mask token>


def _monitor():
    global conn
    if monitorbutton.config('text')[-1] == 'Start Monitoring':
        monitorbutton.config(text='Stop Monitoring')
        print('Start Monitoring')
        graphbutton.grid(row=2, column=0)
        conn = stomp.Connection([(stomp_server, stomp_port)])
        conn.set_listener('', StompListener())
        conn.start()
        conn.connect(username, pw)
        conn.subscribe(destination=agg_topic, id=1, ack='auto')
        addedTextLabel.grid(row=1, column=3)
        addedText.grid(row=1, column=4)
    else:
        monitorbutton.config(text='Start Monitoring')
        graphbutton.grid_forget()
        addedTextLabel.grid_forget()
        addedText.grid_forget()
        canvas._tkcanvas.pack_forget()
        print('Stop Monitoring')
        if conn != None:
            conn.disconnect()


<mask token>


def _graph():
    global graphTimes
    global phasorValues
    global phasor2Values
    global differences
    global showGraph
    if graphbutton.config('text')[-1] == 'Show Graph':
        graphbutton.config(text='Hide Graph')
        print('Show Graph')
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.BOTTOM, fill=Tk.BOTH, expand=1)
        startDate = formatter.parse(startDateStr)
        dataRequest = gateway.entry_point.createDownloadRequest(startDate)
        print('Created Request')
        response = client.getResponse(dataRequest)
        data = response.getData()
        print(data)
        jdata = json.loads(data)
        for x in jdata:
            timestamp = datetime.datetime.strptime(x['timestamp'],
                '%Y-%m-%d %H:%M:%S.%f')
            print(timestamp, ' ', x['phasor1'], ' ', x['phasor2'], ' ', abs
                (x['difference']))
            graphTimes.append(timestamp)
            phasorValues.append(x['phasor1'])
            phasor2Values.append(x['phasor2'])
            differences.append(abs(x['difference']))
        graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate
            =False, linestyle='-', marker='None')
        showGraph = True
    else:
        graphbutton.config(text='Show Graph')
        print('Hide Graph')
        canvas._tkcanvas.pack_forget()
        showGraph = False
        graphTimes = []
        phasorValues = []
        phasor2Values = []
        differences = []


<mask token>

<mask token>


class StompListener(object):

    def on_error(self, headers, message):
        print('received an error %s' % message)

    def on_message(self, headers, message):
        topic = headers.pop('destination', '')
        if topic == agg_topic:
            jdata = json.loads(message)
            timeStr = jdata['timestamp']
            time = formatter.parse(timeStr)
            print(' uploading %s' % message)
            uploadReq = gateway.entry_point.createUploadRequest(time, jdata
                ['phasor1'], jdata['phasor2'], jdata['difference'], 'Tutorial')
            print(uploadReq)
            client.getResponse(uploadReq)
            print('after upload')
            newLabel = str(time) + ', ' + str(jdata['phasor1']) + ', ' + str(
                jdata['phasor2']) + ', ' + str(jdata['difference'])
            addedTextVar.set(newLabel)
            if showGraph:
                print('update graph')
                timestamp = datetime.datetime.strptime(jdata['timestamp'],
                    '%Y-%m-%d %H:%M:%S.%f')
                graphTimes.append(timestamp)
                phasorValues.append(jdata['phasor1'])
                phasor2Values.append(jdata['phasor2'])
                differences.append(abs(jdata['difference']))
                graphPlotA.plot_date(x=graphTimes, y=differences, xdate=
                    True, ydate=False, linestyle='-', marker='None')
                figure.canvas.draw()
        else:
            print('received a message on another topic %s ' % topic)


<mask token>


def _quit():
    root.quit()
    root.destroy()


<mask token>


def _monitor():
    global conn
    if monitorbutton.config('text')[-1] == 'Start Monitoring':
        monitorbutton.config(text='Stop Monitoring')
        print('Start Monitoring')
        graphbutton.grid(row=2, column=0)
        conn = stomp.Connection([(stomp_server, stomp_port)])
        conn.set_listener('', StompListener())
        conn.start()
        conn.connect(username, pw)
        conn.subscribe(destination=agg_topic, id=1, ack='auto')
        addedTextLabel.grid(row=1, column=3)
        addedText.grid(row=1, column=4)
    else:
        monitorbutton.config(text='Start Monitoring')
        graphbutton.grid_forget()
        addedTextLabel.grid_forget()
        addedText.grid_forget()
        canvas._tkcanvas.pack_forget()
        print('Stop Monitoring')
        if conn != None:
            conn.disconnect()


<mask token>


def _graph():
    global graphTimes
    global phasorValues
    global phasor2Values
    global differences
    global showGraph
    if graphbutton.config('text')[-1] == 'Show Graph':
        graphbutton.config(text='Hide Graph')
        print('Show Graph')
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.BOTTOM, fill=Tk.BOTH, expand=1)
        startDate = formatter.parse(startDateStr)
        dataRequest = gateway.entry_point.createDownloadRequest(startDate)
        print('Created Request')
        response = client.getResponse(dataRequest)
        data = response.getData()
        print(data)
        jdata = json.loads(data)
        for x in jdata:
            timestamp = datetime.datetime.strptime(x['timestamp'],
                '%Y-%m-%d %H:%M:%S.%f')
            print(timestamp, ' ', x['phasor1'], ' ', x['phasor2'], ' ', abs
                (x['difference']))
            graphTimes.append(timestamp)
            phasorValues.append(x['phasor1'])
            phasor2Values.append(x['phasor2'])
            differences.append(abs(x['difference']))
        graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate
            =False, linestyle='-', marker='None')
        showGraph = True
    else:
        graphbutton.config(text='Show Graph')
        print('Hide Graph')
        canvas._tkcanvas.pack_forget()
        showGraph = False
        graphTimes = []
        phasorValues = []
        phasor2Values = []
        differences = []


<mask token>

<mask token>
matplotlib.use('TkAgg')
<mask token>
gateway.launch_gateway
<mask token>


class StompListener(object):

    def on_error(self, headers, message):
        print('received an error %s' % message)

    def on_message(self, headers, message):
        topic = headers.pop('destination', '')
        if topic == agg_topic:
            jdata = json.loads(message)
            timeStr = jdata['timestamp']
            time = formatter.parse(timeStr)
            print(' uploading %s' % message)
            uploadReq = gateway.entry_point.createUploadRequest(time, jdata
                ['phasor1'], jdata['phasor2'], jdata['difference'], 'Tutorial')
            print(uploadReq)
            client.getResponse(uploadReq)
            print('after upload')
            newLabel = str(time) + ', ' + str(jdata['phasor1']) + ', ' + str(
                jdata['phasor2']) + ', ' + str(jdata['difference'])
            addedTextVar.set(newLabel)
            if showGraph:
                print('update graph')
                timestamp = datetime.datetime.strptime(jdata['timestamp'],
                    '%Y-%m-%d %H:%M:%S.%f')
                graphTimes.append(timestamp)
                phasorValues.append(jdata['phasor1'])
                phasor2Values.append(jdata['phasor2'])
                differences.append(abs(jdata['difference']))
                graphPlotA.plot_date(x=graphTimes, y=differences, xdate=
                    True, ydate=False, linestyle='-', marker='None')
                figure.canvas.draw()
        else:
            print('received a message on another topic %s ' % topic)


<mask token>
root.wm_title('GOSS tutorial Client')
<mask token>
widthFrame.pack(fill=Tk.X, expand=True)
<mask token>
heightFrame.pack(side=Tk.RIGHT, fill=Tk.Y, expand=True)
<mask token>
figure.autofmt_xdate()
<mask token>


def _quit():
    root.quit()
    root.destroy()


<mask token>
buttonFrame.pack(side=Tk.TOP)
<mask token>


def _monitor():
    global conn
    if monitorbutton.config('text')[-1] == 'Start Monitoring':
        monitorbutton.config(text='Stop Monitoring')
        print('Start Monitoring')
        graphbutton.grid(row=2, column=0)
        conn = stomp.Connection([(stomp_server, stomp_port)])
        conn.set_listener('', StompListener())
        conn.start()
        conn.connect(username, pw)
        conn.subscribe(destination=agg_topic, id=1, ack='auto')
        addedTextLabel.grid(row=1, column=3)
        addedText.grid(row=1, column=4)
    else:
        monitorbutton.config(text='Start Monitoring')
        graphbutton.grid_forget()
        addedTextLabel.grid_forget()
        addedText.grid_forget()
        canvas._tkcanvas.pack_forget()
        print('Stop Monitoring')
        if conn != None:
            conn.disconnect()


<mask token>
monitorbutton.grid(row=1, column=0)
<mask token>


def _graph():
    global graphTimes
    global phasorValues
    global phasor2Values
    global differences
    global showGraph
    if graphbutton.config('text')[-1] == 'Show Graph':
        graphbutton.config(text='Hide Graph')
        print('Show Graph')
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.BOTTOM, fill=Tk.BOTH, expand=1)
        startDate = formatter.parse(startDateStr)
        dataRequest = gateway.entry_point.createDownloadRequest(startDate)
        print('Created Request')
        response = client.getResponse(dataRequest)
        data = response.getData()
        print(data)
        jdata = json.loads(data)
        for x in jdata:
            timestamp = datetime.datetime.strptime(x['timestamp'],
                '%Y-%m-%d %H:%M:%S.%f')
            print(timestamp, ' ', x['phasor1'], ' ', x['phasor2'], ' ', abs
                (x['difference']))
            graphTimes.append(timestamp)
            phasorValues.append(x['phasor1'])
            phasor2Values.append(x['phasor2'])
            differences.append(abs(x['difference']))
        graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate
            =False, linestyle='-', marker='None')
        showGraph = True
    else:
        graphbutton.config(text='Show Graph')
        print('Hide Graph')
        canvas._tkcanvas.pack_forget()
        showGraph = False
        graphTimes = []
        phasorValues = []
        phasor2Values = []
        differences = []


<mask token>
Tk.mainloop()

<mask token>
matplotlib.use('TkAgg')
<mask token>
gateway = JavaGateway()
gateway.launch_gateway
client = gateway.entry_point.getClient()
stomp_server = 'localhost'
stomp_port = 61612
agg_topic = '/topic/pmu/PMU_1/PMU_2/agg'
username = 'pmu_user'
pw = 'password'
startDateStr = '2014-07-10 00:00:00.000'
formatter = gateway.jvm.java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSS')
graphTimes = []
phasorValues = []
phasor2Values = []
differences = []
showGraph = False


class StompListener(object):

    def on_error(self, headers, message):
        print('received an error %s' % message)

    def on_message(self, headers, message):
        topic = headers.pop('destination', '')
        if topic == agg_topic:
            jdata = json.loads(message)
            timeStr = jdata['timestamp']
            time = formatter.parse(timeStr)
            print(' uploading %s' % message)
            uploadReq = gateway.entry_point.createUploadRequest(time, jdata
                ['phasor1'], jdata['phasor2'], jdata['difference'], 'Tutorial')
            print(uploadReq)
            client.getResponse(uploadReq)
            print('after upload')
            newLabel = str(time) + ', ' + str(jdata['phasor1']) + ', ' + str(
                jdata['phasor2']) + ', ' + str(jdata['difference'])
            addedTextVar.set(newLabel)
            if showGraph:
                print('update graph')
                timestamp = datetime.datetime.strptime(jdata['timestamp'],
                    '%Y-%m-%d %H:%M:%S.%f')
                graphTimes.append(timestamp)
                phasorValues.append(jdata['phasor1'])
                phasor2Values.append(jdata['phasor2'])
                differences.append(abs(jdata['difference']))
                graphPlotA.plot_date(x=graphTimes, y=differences, xdate=
                    True, ydate=False, linestyle='-', marker='None')
                figure.canvas.draw()
        else:
            print('received a message on another topic %s ' % topic)


root = Tk.Tk()
root.wm_title('GOSS tutorial Client')
widthFrame = Tk.Frame(root, width=800, height=1)
widthFrame.pack(fill=Tk.X, expand=True)
heightFrame = Tk.Frame(root, width=1, height=600)
heightFrame.pack(side=Tk.RIGHT, fill=Tk.Y, expand=True)
figure = Figure(figsize=(5, 4), dpi=150)
graphPlotA = figure.add_subplot(111)
graphPlotA.fmt_xdata = matplotlib.dates.DateFormatter('%m-%d %H-%M-%S')
figure.autofmt_xdate()
canvas = FigureCanvasTkAgg(figure, master=root)


def _quit():
    root.quit()
    root.destroy()


buttonFrame = Tk.Frame(root)
buttonFrame.pack(side=Tk.TOP)
conn = None


def _monitor():
    global conn
    if monitorbutton.config('text')[-1] == 'Start Monitoring':
        monitorbutton.config(text='Stop Monitoring')
        print('Start Monitoring')
        graphbutton.grid(row=2, column=0)
        conn = stomp.Connection([(stomp_server, stomp_port)])
        conn.set_listener('', StompListener())
        conn.start()
        conn.connect(username, pw)
        conn.subscribe(destination=agg_topic, id=1, ack='auto')
        addedTextLabel.grid(row=1, column=3)
        addedText.grid(row=1, column=4)
    else:
        monitorbutton.config(text='Start Monitoring')
        graphbutton.grid_forget()
        addedTextLabel.grid_forget()
        addedText.grid_forget()
        canvas._tkcanvas.pack_forget()
        print('Stop Monitoring')
        if conn != None:
            conn.disconnect()


monitorbutton = Tk.Button(master=buttonFrame, text='Start Monitoring',
    command=_monitor)
monitorbutton.grid(row=1, column=0)
addedTextLabel = Tk.Label(master=buttonFrame, text='Last Entry Added:')
addedTextVar = Tk.StringVar()
addedText = Tk.Label(master=buttonFrame, textvariable=addedTextVar)


def _graph():
    global graphTimes
    global phasorValues
    global phasor2Values
    global differences
    global showGraph
    if graphbutton.config('text')[-1] == 'Show Graph':
        graphbutton.config(text='Hide Graph')
        print('Show Graph')
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.BOTTOM, fill=Tk.BOTH, expand=1)
        startDate = formatter.parse(startDateStr)
        dataRequest = gateway.entry_point.createDownloadRequest(startDate)
        print('Created Request')
        response = client.getResponse(dataRequest)
        data = response.getData()
        print(data)
        jdata = json.loads(data)
        for x in jdata:
            timestamp = datetime.datetime.strptime(x['timestamp'],
                '%Y-%m-%d %H:%M:%S.%f')
            print(timestamp, ' ', x['phasor1'], ' ', x['phasor2'], ' ', abs
                (x['difference']))
            graphTimes.append(timestamp)
            phasorValues.append(x['phasor1'])
            phasor2Values.append(x['phasor2'])
            differences.append(abs(x['difference']))
        graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate
            =False, linestyle='-', marker='None')
        showGraph = True
    else:
        graphbutton.config(text='Show Graph')
        print('Hide Graph')
        canvas._tkcanvas.pack_forget()
        showGraph = False
        graphTimes = []
        phasorValues = []
        phasor2Values = []
        differences = []


graphbutton = Tk.Button(master=buttonFrame, text='Show Graph', command=_graph)
Tk.mainloop()

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

import datetime
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg #, NavigationToolbar2TkAgg



from matplotlib.figure import Figure

#import sys
import tkinter as Tk
import sys, traceback
import stomp
import json
from py4j.java_gateway import JavaGateway
gateway = JavaGateway()    
gateway.launch_gateway

client = gateway.entry_point.getClient()

stomp_server = 'localhost'
stomp_port = 61612    
agg_topic = '/topic/pmu/PMU_1/PMU_2/agg'
username = 'pmu_user'
pw = 'password'
startDateStr = "2014-07-10 00:00:00.000"
formatter = gateway.jvm.java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")

#values for the graph
graphTimes = []
phasorValues = []
phasor2Values = []
differences = []
showGraph=False

class StompListener(object):
    def on_error(self, headers, message):
        print('received an error %s' % message)
    def on_message(self, headers, message):
        topic = headers.pop("destination", "")
        if(topic==agg_topic):
            #Parse json message
            jdata = json.loads(message)
            timeStr = jdata['timestamp']
            time = formatter.parse(timeStr)
            
            print(' uploading %s' % message)
            #Send to goss using client api
            uploadReq = gateway.entry_point.createUploadRequest(time, jdata['phasor1'], jdata['phasor2'], jdata['difference'],"Tutorial")
            print(uploadReq)            
            client.getResponse(uploadReq)
            print('after upload')
            
            #Update last posted label
            newLabel = str(time)+", "+str(jdata['phasor1'])+", "+str(jdata['phasor2'])+", "+str(jdata['difference'])
            addedTextVar.set(newLabel)
            
            #update graph data
            if showGraph:
                print('update graph')
                timestamp = datetime.datetime.strptime(jdata['timestamp'], '%Y-%m-%d %H:%M:%S.%f')
                graphTimes.append(timestamp)
                phasorValues.append(jdata['phasor1'])
                phasor2Values.append(jdata['phasor2'])
                differences.append(abs(jdata['difference']))
                graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate=False, linestyle='-', marker='None')
                figure.canvas.draw()
            
        else:
            print('received a message on another topic %s ' % topic)




#set up the window
root = Tk.Tk()
root.wm_title("GOSS tutorial Client")
widthFrame = Tk.Frame(root, width=800, height=1)
widthFrame.pack(fill=Tk.X, expand=True)
heightFrame = Tk.Frame(root, width=1, height=600)
heightFrame.pack(side=Tk.RIGHT, fill=Tk.Y, expand=True)

figure = Figure(figsize=(5,4), dpi=150)
graphPlotA = figure.add_subplot(111)
graphPlotA.fmt_xdata = matplotlib.dates.DateFormatter('%m-%d %H-%M-%S') 
# this rotates the ticklabels to help with overlapping 
figure.autofmt_xdate() 
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(figure, master=root)



#figure.title("Phase Angle Difference")






def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate

buttonFrame = Tk.Frame(root )
buttonFrame.pack( side = Tk.TOP)                    

#button = Tk.Button(master=root, text='Quit', command=_quit)
#button.pack(side=Tk.BOTTOM)

conn = None
def _monitor():
    global conn
    if monitorbutton.config('text')[-1] == 'Start Monitoring':
        monitorbutton.config(text='Stop Monitoring')
        # do monitoring stuff
        print('Start Monitoring')
        graphbutton.grid(row=2, column=0)
        
        #start connection
        
        conn = stomp.Connection([(stomp_server,stomp_port)])
        conn.set_listener('', StompListener())
        conn.start()
        conn.connect(username, pw)
        conn.subscribe(destination=agg_topic, id=1, ack='auto')
        
        #conn.subscribe(destination='/topic//pmu/PMU_1', id=1, ack='auto')
        
        addedTextLabel.grid(row=1, column=3)
        addedText.grid(row=1, column=4)
    else:
        monitorbutton.config(text='Start Monitoring')
        #stop the monitoring
        #hide the graph
        graphbutton.grid_forget()
        addedTextLabel.grid_forget()
        addedText.grid_forget()
        canvas._tkcanvas.pack_forget()
        print('Stop Monitoring')    
        if(conn!=None):
            conn.disconnect()
                        

monitorbutton = Tk.Button(master=buttonFrame, text='Start Monitoring', command=_monitor)
monitorbutton.grid(row=1, column=0)

addedTextLabel = Tk.Label(master=buttonFrame, text="Last Entry Added:")
#addedTextLabel.grid(row=1, column=3)

addedTextVar = Tk.StringVar()
addedText = Tk.Label(master=buttonFrame, textvariable=addedTextVar)
#addedText.grid(row=1, column=4)


def _graph():
    #values for the graph
    global graphTimes 
    global phasorValues 
    global phasor2Values 
    global differences 
    global showGraph 
    if graphbutton.config('text')[-1] == 'Show Graph':
        graphbutton.config(text='Hide Graph')
        # start the poll for graph data
        # show the graph
        print('Show Graph')
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.BOTTOM, fill=Tk.BOTH, expand=1, )
       
        #send request for graph data
        startDate = formatter.parse(startDateStr)
        dataRequest = gateway.entry_point.createDownloadRequest(startDate)
        print('Created Request')
        response = client.getResponse(dataRequest)
        data = response.getData()
        print(data)
        jdata = json.loads(data)
        
        for x in jdata:
            timestamp = datetime.datetime.strptime(x['timestamp'], '%Y-%m-%d %H:%M:%S.%f')
            print(timestamp,' ',x['phasor1'],' ',x['phasor2'],' ',abs(x['difference']))
            graphTimes.append(timestamp)
            phasorValues.append(x['phasor1'])
            phasor2Values.append(x['phasor2'])
            differences.append(abs(x['difference']))
            
        #graphPlotA.plot_date(x=graphTimes, y=phasorValues, xdate=True, ydate=False, linestyle='-', marker='None')
        #graphPlotA.plot_date(x=graphTimes, y=phasor2Values, xdate=True, ydate=False, linestyle='-', marker='None')
        graphPlotA.plot_date(x=graphTimes, y=differences, xdate=True, ydate=False, linestyle='-', marker='None')
       
        showGraph=True
       
    else:
        graphbutton.config(text='Show Graph')
        # stop the poll for graph data
        # hide the graph
        print('Hide Graph')  
        canvas._tkcanvas.pack_forget()
        showGraph=False
        graphTimes = []
        phasorValues = []
        phasor2Values = []
        differences = []
        

graphbutton = Tk.Button(master=buttonFrame, text='Show Graph', command=_graph)

Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.