# Path: extras/python/iotsaControl/mdns.py
# class PlatformMDNSCollector(object):
#     """Default mDNS handling: ask the user"""
#     def __init__(self):
#         pass
#         
#     def run(self, timeout=5):
#         """Run for a short while (5 seconds default) and collect all iotsa devices found. Return list."""
#         raise UserIntervention("Please browse for mDNS services _iotsa._tcp.local")
# 
# Path: extras/python/iotsaControl/consts.py
# class UserIntervention(Exception):
#     pass
# 
# VERBOSE=[]


from future import standard_library
from builtins import str
from builtins import object
from .mdns import PlatformMDNSCollector        
from .consts import UserIntervention, VERBOSE
import sys
import time
import os
import socket
    import subprocess
    import plistlib
        """Return all currently connected wifi networks"""
        return []

if sys.platform == 'darwin':
    class PlatformWifi(object):
        def __init__(self):
            self.wifiInterface = os.getenv('IOTSA_WIFI')
            if not self.wifiInterface:
                cmd = 'networksetup -listnetworkserviceorder'
                p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, universal_newlines=True)
                data = p.stdout.readlines()
                for line in data:
                    line = line.strip()
                    searchString = '(Hardware Port: Wi-Fi, Device: '
                    if line[:len(searchString)] == searchString and line[-1:] == ')':
                        self.wifiInterface = line[len(searchString):-1]
                        if VERBOSE: print('Using WiFi interface', self.wifiInterface)
                        break
                    else:
                        self.wifiInterface = 'en1'
                        
        def platformListWifiNetworks(self):
            if VERBOSE: print('Listing wifi networks (OSX)')
            p = subprocess.Popen('/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport --scan --xml', shell=True, stdout=subprocess.PIPE)
            if hasattr(plistlib, 'load'):
                data = plistlib.load(p.stdout, fmt=plistlib.FMT_XML)
            else:
                data = plistlib.readPlist(p.stdout)
            wifiNames = [d['SSID_STR'] for d in data]
            wifiNames.sort()

# Path: extras/python/iotsaControl/mdns.py
# class PlatformMDNSCollector(object):
#     """Default mDNS handling: ask the user"""
#     def __init__(self):
#         pass
#         
#     def run(self, timeout=5):
#         """Run for a short while (5 seconds default) and collect all iotsa devices found. Return list."""
#         raise UserIntervention("Please browse for mDNS services _iotsa._tcp.local")
# 
# Path: extras/python/iotsaControl/consts.py
# class UserIntervention(Exception):
#     pass
# 
# VERBOSE=[]


# Path: extras/python/iotsaControl/wifi.py
from future import standard_library
from builtins import str
from builtins import object
from .mdns import PlatformMDNSCollector        
from .consts import UserIntervention, VERBOSE
import sys
import time
import os
import socket
    import subprocess
    import plistlib

        """Return all currently connected wifi networks"""
        return []

if sys.platform == 'darwin':
    class PlatformWifi(object):
        def __init__(self):
            self.wifiInterface = os.getenv('IOTSA_WIFI')
            if not self.wifiInterface:
                cmd = 'networksetup -listnetworkserviceorder'
                p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, universal_newlines=True)
                data = p.stdout.readlines()
                for line in data:
                    line = line.strip()
                    searchString = '(Hardware Port: Wi-Fi, Device: '
                    if line[:len(searchString)] == searchString and line[-1:] == ')':
                        self.wifiInterface = line[len(searchString):-1]
                        if VERBOSE: print('Using WiFi interface', self.wifiInterface)
                        break
                    else:
                        self.wifiInterface = 'en1'
                        
        def platformListWifiNetworks(self):
            if VERBOSE: print('Listing wifi networks (OSX)')
            p = subprocess.Popen('/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport --scan --xml', shell=True, stdout=subprocess.PIPE)
            if hasattr(plistlib, 'load'):
                data = plistlib.load(p.stdout, fmt=plistlib.FMT_XML)
            else:
                data = plistlib.readPlist(p.stdout)
            wifiNames = [d['SSID_STR'] for d in data]
            wifiNames.sort()

            if VERBOSE: print('Wifi networks found:', wifiNames)