#!/usr/bin/env python
import threading

from MKFlowInput import MKFlowInput
from MKFlowCommunication import MKFlowCommunication
from MKDatabase import MKDatabase

class MKFlow():
    def __init__(self, port1 = '/dev/ttyUSB2', port2 = '/dev/ttyUSB3', instrument = 0):
        self.Input = MKFlowInput()
        self.Storage = MKFlowCommunication()
        self.Database = MKDatabase()
        self.Input.setBridge(port1, port2)
        #self.Input.setLogFile('/home/matthias/Documents/programs/python/swnt-reactor/data/log/bridge/testing/log1.log')
        self.instrument = instrument

    def start(self):
        try:
            self.thread = threading.Thread(target=self.loop)
            self.thread.daemon = True
            self.thread.start()
        except:
            self.stop()
            raise

    def stop(self):
        self.Input.stop()

    def join(self):
        self.thread.join()

    def loop(self):
        while self.Input.isAlive():
            try:
                Message = self.Input.getMessage()
                SubMessage = Message.getSubType()
            except:
                break
            else:
                if not Message.isInvalid:
                    node = Message.getNode()
                    sequence = Message.getSequence()
                    Entity = self.Storage.Node(node).Sequence(sequence)
                    try:
                        if Message.isError:
                            Entity.setError(Message)
                        elif Message.isStatus:
                            Entity.setStatus(Message)
                        elif Message.isSent:
                            Entity.setAnswer(Message)
                        elif Message.isSentStatus:
                            Entity.setWriteRequest(Message)
                        elif Message.isRequest:
                            Entity.setReadRequest(Message)
                        else:
                            raise
                    except:
                        print "--- something happened ---"
                        try:
                            Message.stdout()
                            Submessage.stdout()
                            Entity.reset()
                        except:
                            raise
                    else:
                        # store if two messages are present in current Entity
                        # reset Entity afterwards.
                        try:
                            #Entity.output()
                            Entity.save(self.Database, self.instrument)
                        except ValueError:
                            raise
                            pass
                        except:
                            raise
                else:
                    print "--- invalid ---"
                    SubMessage.stdoutShort(Message.stdoutShort())
                    pass


@@ -577,16 +577,97 @@
 strument
+%0A        self.debug = False%0A%0A    def startDebug(self):%0A        self.debug = False
 %0A%0A    de
@@ -2558,17 +2558,146 @@
         
-#
+if self.debug:%0A                                print %22Buffer: %25i%22 %25 self.Input.input.buffersize()%0A                                
 Entity.o
@@ -2724,32 +2724,33 @@
                 
+#
 Entity.save(self
@@ -2777,16 +2777,49 @@
 rument)%0A
+                            pass%0A
         

debug functionality