#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Tomas
#
# Created:     29/08/2017
# Copyright:   (c) Tomas 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------

#!/usr/bin/python

#============================ adjust path =====================================

import sys
import os
import hartDefinition
if __name__ == '__main__':
    here = sys.path[0]
    sys.path.insert(0, os.path.join(here, '..'))

#============================ imports =========================================

import Tkinter
import traceback

##from dustUI import dustGuiLib, dustFrame, dustStyle
##import dustFrame
from   dustUI import dustGuiLib, dustFrame
from   dustUI.dustStyle                  import dustStyle


from   SmartMeshSDK.ApiException  import CommandError, \
                                         ConnectionError, \
                                         APIError
from   SmartMeshSDK.HartMgrConnector  import HartMgrConnector

#============================ body ============================================

class hartFrameMotes(dustFrame.dustFrame):

    def __init__(self,parentElem,guiLock,
                                 hartDef,
                                 frameName="Motes",
                                 row=0,column=0):

        # record variables
        self.fieldsFrame     = None
        self.hartDef         = hartDef

        # main variable
        self.selectedMac     = 'Broadcast'

        # init parent
        dustFrame.dustFrame.__init__(
            self,
            parentElem  = parentElem,
            guiLock     = guiLock,
            frameName   = frameName,
            row         = row,
            column      = column,
            scrollable  = True,
        )


    def updateMotesList(self):
        try:
            motesList = self.connector.dn_getMotes()
        except:
            print('not connected, cannot get motes')
            return

        macAddress = ['Broadcast']
        for i in motesList:
            # if this is operationl mote
            if (i[3] == 'Operational') and (i[7] == False):
                macAddress.append(i[1])

        macAddress.sort()
        self.commandDropDownOptions = macAddress

        with self.guiLock:
            self.moteToAddress = Tkinter.StringVar()
            self.moteToAddress.trace_variable('w',self._handleMote)

        with self.guiLock:
            self.commandDropDown = dustGuiLib.OptionMenu(
                self.container,
                self.moteToAddress,
                *self.commandDropDownOptions
            )
            self._add(self.commandDropDown,0,0)

    def _handleMote(self, name, index, mode):
        self.selectedMac = self.moteToAddress.get()

    def getMoteMac(self):
        return self.selectedMac

def main():
    pass

if __name__ == '__main__':
    main()
