# Path: acquisition.py
# class UDPThread(QtCore.QThread):
#   newPacket = QtCore.pyqtSignal(object)
#   
#   def __init__(self, port=8888):
#     super(UDPThread, self).__init__()
# 
#     self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
#     self.socket.bind(('127.0.0.1', port))
# 
#     self.buffer = []
# 
#   def run(self):
#     while True:
#       data, addr = self.socket.recvfrom(1024)
#       data = eval(data)
#       
#       self.newPacket.emit(eval(str(data)))
#       #QtGui.QApplication.instance().processEvents()
# 
# Path: launcher.py
# class LuaLauncher(object):
# 	def __init__(self, context, path, dockarea):
# 		self.context = context
# 		self.path = path
# 		self.dockarea = dockarea
# 
# 		self.watcher = QtCore.QFileSystemWatcher()
# 		self.watcher.addPath(path)
# 		self.watcher.fileChanged.connect(self.handle_reload)
# 
# 		self.guiBlocks = []
# 		
# 		self.load_protocol(path)
# 
# 	def load_protocol(self, path):
# 		lua = lupa.LuaRuntime(attribute_handlers=(getter, setter))
# 		self.lua = lua
# 		lua.globals()["flow"] = flow
# 		lua.globals()["channels"] = self.context.get_channels()
# 		lua.globals()["context"] = self.context
# 		source = open(path).read()
# 
# 		try:
# 			lua.execute(source)
# 			self.guiBlocks = lua.eval('setup()')
# 			#lua.eval('gui()')
# 		except Exception as e:
# 			print ('Lua Exception occured: ', e, type(e))
# 			raise
# 
# 		for block in self.guiBlocks:
# 			dock = Dock(block.name)
# 			dock.addWidget(block.widget())
# 			self.dockarea.addDock(dock)
# 
# 		if 'doc_config' in lua.globals():
# 			self.restore_layout()
# 
# 
# 	def handle_reload(self):
# 		print(self.path, 'changed, reloading')
# 
# 		# Delete all the stuff
# 		del self.lua
# 
# 		for i in self.guiBlocks:
# 			if hasattr(i, 'block'):
# 				del i.widget().block
# 		self.guiBlocks = ()
# 
# 		self.context.clear_signals()
# 
# 		# Weird pyqtgraph bug, need to clear the DockArea twice to remove all components
# 		self.dockarea.clear()
# 		self.dockarea.clear()
# 
# 		# Reload protocol
# 		self.load_protocol(self.path)
# 
# 	config_marker = '\n\n' + '-' * 23 + "Auto-Generated config - DO NOT EDIT" + "-" * 23
# 
# 	def save_layout(self):
# 		save = to_lua(self.dockarea.saveState())
# 
# 		source = open(self.path).read()
# 
# 		# Throw away auto-generated part
# 		source = source.split(self.config_marker)[0]
# 
# 		writer = open(self.path, 'w')
# 		writer.write(source)
# 		writer.write(self.config_marker)
# 		writer.write('\nfunction doc_config()\n\treturn ')
# 		writer.write(save)
# 		writer.write('\nend')
# 		writer.close()
# 
# 	def restore_layout(self):
# 		try:
# 			state = to_python(self.lua.eval('doc_config()'))
# 			self.dockarea.restoreState(state)
# 		except:
# 			print ('Sorry, can not restore dock state...')


from pyqtgraph import QtCore, QtGui
from pyqtgraph.dockarea import DockArea, Dock
from acquisition import UDPThread
from launcher import LuaLauncher
import sys
import signal as _signal
import flow




_signal.signal(_signal.SIGINT, _signal.SIG_DFL)
myApp = QtGui.QApplication(sys.argv)

context = flow.Context()
context.register_channel(0, 'Channel 1')
context.register_channel(1, 'Channel 2')


# Path: acquisition.py
# class UDPThread(QtCore.QThread):
#   newPacket = QtCore.pyqtSignal(object)
#   
#   def __init__(self, port=8888):
#     super(UDPThread, self).__init__()
# 
#     self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
#     self.socket.bind(('127.0.0.1', port))
# 
#     self.buffer = []
# 
#   def run(self):
#     while True:
#       data, addr = self.socket.recvfrom(1024)
#       data = eval(data)
#       
#       self.newPacket.emit(eval(str(data)))
#       #QtGui.QApplication.instance().processEvents()
# 
# Path: launcher.py
# class LuaLauncher(object):
# 	def __init__(self, context, path, dockarea):
# 		self.context = context
# 		self.path = path
# 		self.dockarea = dockarea
# 
# 		self.watcher = QtCore.QFileSystemWatcher()
# 		self.watcher.addPath(path)
# 		self.watcher.fileChanged.connect(self.handle_reload)
# 
# 		self.guiBlocks = []
# 		
# 		self.load_protocol(path)
# 
# 	def load_protocol(self, path):
# 		lua = lupa.LuaRuntime(attribute_handlers=(getter, setter))
# 		self.lua = lua
# 		lua.globals()["flow"] = flow
# 		lua.globals()["channels"] = self.context.get_channels()
# 		lua.globals()["context"] = self.context
# 		source = open(path).read()
# 
# 		try:
# 			lua.execute(source)
# 			self.guiBlocks = lua.eval('setup()')
# 			#lua.eval('gui()')
# 		except Exception as e:
# 			print ('Lua Exception occured: ', e, type(e))
# 			raise
# 
# 		for block in self.guiBlocks:
# 			dock = Dock(block.name)
# 			dock.addWidget(block.widget())
# 			self.dockarea.addDock(dock)
# 
# 		if 'doc_config' in lua.globals():
# 			self.restore_layout()
# 
# 
# 	def handle_reload(self):
# 		print(self.path, 'changed, reloading')
# 
# 		# Delete all the stuff
# 		del self.lua
# 
# 		for i in self.guiBlocks:
# 			if hasattr(i, 'block'):
# 				del i.widget().block
# 		self.guiBlocks = ()
# 
# 		self.context.clear_signals()
# 
# 		# Weird pyqtgraph bug, need to clear the DockArea twice to remove all components
# 		self.dockarea.clear()
# 		self.dockarea.clear()
# 
# 		# Reload protocol
# 		self.load_protocol(self.path)
# 
# 	config_marker = '\n\n' + '-' * 23 + "Auto-Generated config - DO NOT EDIT" + "-" * 23
# 
# 	def save_layout(self):
# 		save = to_lua(self.dockarea.saveState())
# 
# 		source = open(self.path).read()
# 
# 		# Throw away auto-generated part
# 		source = source.split(self.config_marker)[0]
# 
# 		writer = open(self.path, 'w')
# 		writer.write(source)
# 		writer.write(self.config_marker)
# 		writer.write('\nfunction doc_config()\n\treturn ')
# 		writer.write(save)
# 		writer.write('\nend')
# 		writer.close()
# 
# 	def restore_layout(self):
# 		try:
# 			state = to_python(self.lua.eval('doc_config()'))
# 			self.dockarea.restoreState(state)
# 		except:
# 			print ('Sorry, can not restore dock state...')


# Path: lua_protocol.py
from pyqtgraph import QtCore, QtGui
from pyqtgraph.dockarea import DockArea, Dock
from acquisition import UDPThread
from launcher import LuaLauncher
import sys
import signal as _signal
import flow





_signal.signal(_signal.SIGINT, _signal.SIG_DFL)
myApp = QtGui.QApplication(sys.argv)

context = flow.Context()
context.register_channel(0, 'Channel 1')
context.register_channel(1, 'Channel 2')


sourceThread = UDPThread()