import os.path

from tron import g, hub
from tron.Hub.Command.Encoders.ASCIICmdEncoder import ASCIICmdEncoder
from tron.Hub.Nub.TCCShellNub import TCCShellNub
from tron.Hub.Reply.Decoders.ASCIIReplyDecoder import ASCIIReplyDecoder


name = 'tcc'


def start(poller):

    stop()

    initCmds = ('show version', 'show users', 'show time', 'show status', 'show inst/full',
                'show object/full', 'show axisconfig', 'show focus', 'axis status', 'show scale',
                'mir status')

    safeCmds = r'(^show )|(status$)'

    d = ASCIIReplyDecoder(EOL='\r', stripChars='\n', CIDfirst=False, debug=1)
    e = ASCIICmdEncoder(EOL='\r', debug=1, CIDfirst=False)
    tcc = TCCShellNub(poller, [
        '/usr/bin/ssh', '-1', '-e', 'none', '-a', '-x', '-i',
        os.path.expanduser('~/.ssh/tron'), '-T', 'tccuser@tcc25m'
    ],
        initCmds=initCmds,
        safeCmds=safeCmds,
        needsAuth=True,
        name=name,
        encoder=e,
        decoder=d,
        logDir=os.path.join(g.logDir, name),
        debug=1)

    hub.addActor(tcc)


def stop():
    n = hub.findActor(name)
    if n:
        hub.dropActor(n)
        del n

[(22, 'tron.Hub.Reply.Decoders.ASCIIReplyDecoder.ASCIIReplyDecoder', 'ASCIIReplyDecoder', False, 'from tron.Hub.Reply.Decoders.ASCIIReplyDecoder import ASCIIReplyDecoder\n'), (23, 'tron.Hub.Command.Encoders.ASCIICmdEncoder.ASCIICmdEncoder', 'ASCIICmdEncoder', False, 'from tron.Hub.Command.Encoders.ASCIICmdEncoder import ASCIICmdEncoder\n'), (37, 'tron.hub.addActor', 'hub.addActor', False, 'from tron import g, hub\n'), (41, 'tron.hub.findActor', 'hub.findActor', False, 'from tron import g, hub\n'), (43, 'tron.hub.dropActor', 'hub.dropActor', False, 'from tron import g, hub\n')]