"""
from obspy.clients.seedlink.easyseedlink import EasySeedLinkClient

# Subclass the client class
class MyClient(EasySeedLinkClient):
    # Implement the on_data callback
    def on_data(self, trace):
        print('Received trace:')
        print(trace)

# Connect to a SeedLink server
client = MyClient('192.168.3.1:18000')

# Retrieve INFO:STREAMS
streams_xml = client.get_info('STREAMS')
print(streams_xml)

# Select a stream and start receiving data
client.select_stream('LK','GEO12','HNN')
#client.select_stream('LK','LUNI','EHN')
client.run()
"""


from obspy.clients.seedlink.easyseedlink import create_client
import matplotlib.pyplot as plt
import numpy as np
import time

data = []

def handle_data(trace):
    print('Recevied new data:')
    print(trace)
    global data
    data += [i for i in trace.data]
    print(len(data))

    #if len(data) > 2000:
    #    plt.cla()
    #    plt.plot(data[-2000:], 'b')
    #    plt.show(block=False)
    #    plt.pause(0.001)


client = create_client('192.168.1.108:18000', handle_data)
client.select_stream('LK','GE008','HNN')
#client.run()

import threading
th = threading.Thread(target=client.run)
th.daemon = True
th.start()

while 1:
    if len(data) > 2000:
        plt.cla()
        plt.plot(data[-2000:], 'b')
        plt.show(block=False)
        plt.pause(0.001)


"""
from obspy import UTCDateTime
import obspy.clients.seedlink.basic_client as sl

if 0:
    host = 'geofon.gfz-potsdam.de'
    net = 'BW'
    sta = 'MANZ'
    cha = 'EHZ'

if 1:
    host = '192.168.1.108'
    net = 'LK'
    sta = 'GE008'
    cha = 'EHN'

if 0:
    host = 'antmac.crs.inogs.it'
    net = 'ST'
    sta = 'ZIAN'
    cha = 'EHN'

client = sl.Client(host, port=18000, timeout=10, debug=True)

t = UTCDateTime()
st = client.get_waveforms(net, sta, "", cha, t, t+5)
st = st.sort(reverse=True)
st.plot()
"""
