# Path: core/interfaces/base.py
# class BaseInterface(ABC, Module):
# 
#     def  __init__(self, hplayer, name="INTERFACE", color="blue"):
#         super().__init__(hplayer, name, color)
#         self.hplayer = hplayer
# 
#         # stopping flag
#         self.stopped = threading.Event()
#         self.stopped.set()
# 
#         # Listen thread
#         self.recvThread = threading.Thread(target=self.listen)
# 
# 
#     # Receiver THREAD (ABSTRACT)
#     @abstractmethod
#     def listen(self):
#         self.stopped.wait()
# 
#     # Start
#     def start(self):
#         self.stopped.clear()
#         self.recvThread.start()
#         return self
# 
#     # Stop
#     def quit(self):
#         self.log("stopping...")
#         self.stopped.set()
#         self.recvThread.join()
#         self.log("stopped")
# 
# 	# is Running
#     def isRunning(self, state=None):
#         if state is not None:
#             self.stopped.clear() if state else self.stopped.set()
#         return not self.stopped.is_set()
# 
# Path: core/engine/network.py
# def get_allip():
#     ip = []
#     ifaces = ni.interfaces()
#     for iface in ifaces:
#         if iface.startswith("e"):
#             try:
#                 ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#             except:
#                 pass
#     else:
#         for iface in ifaces:
#             if iface.startswith("w"):
#                 try:
#                     ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#                 except:
#                     pass
#     return ip
# 
# def get_hostname():
#     import socket
#     return socket.gethostname()


from .base import BaseInterface
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from flask import Flask, render_template, session, request, send_from_directory
from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect
from werkzeug.utils import secure_filename
from ..engine.network import get_allip, get_hostname
from zeroconf import ServiceInfo, Zeroconf 
import eventlet
import threading, os, time, queue
import logging, sys, json
import socket



thread = None
thread_lock = threading.Lock()

REGIE_PATH1 = '/opt/RPi-Regie'
REGIE_PATH2 = '/data/RPi-Regie'


class RegieInterface (BaseInterface):

    def  __init__(self, hplayer, port, datapath):
        super(RegieInterface, self).__init__(hplayer, "Regie")
        self._port = port
        self._datapath = datapath
        self._server = None
        
        

    # HTTP receiver THREAD
    def listen(self):

        # Advertize on ZeroConf
        zeroconf = Zeroconf()
        info = ServiceInfo(
            "_http._tcp.local.",

# Path: core/interfaces/base.py
# class BaseInterface(ABC, Module):
# 
#     def  __init__(self, hplayer, name="INTERFACE", color="blue"):
#         super().__init__(hplayer, name, color)
#         self.hplayer = hplayer
# 
#         # stopping flag
#         self.stopped = threading.Event()
#         self.stopped.set()
# 
#         # Listen thread
#         self.recvThread = threading.Thread(target=self.listen)
# 
# 
#     # Receiver THREAD (ABSTRACT)
#     @abstractmethod
#     def listen(self):
#         self.stopped.wait()
# 
#     # Start
#     def start(self):
#         self.stopped.clear()
#         self.recvThread.start()
#         return self
# 
#     # Stop
#     def quit(self):
#         self.log("stopping...")
#         self.stopped.set()
#         self.recvThread.join()
#         self.log("stopped")
# 
# 	# is Running
#     def isRunning(self, state=None):
#         if state is not None:
#             self.stopped.clear() if state else self.stopped.set()
#         return not self.stopped.is_set()
# 
# Path: core/engine/network.py
# def get_allip():
#     ip = []
#     ifaces = ni.interfaces()
#     for iface in ifaces:
#         if iface.startswith("e"):
#             try:
#                 ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#             except:
#                 pass
#     else:
#         for iface in ifaces:
#             if iface.startswith("w"):
#                 try:
#                     ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#                 except:
#                     pass
#     return ip
# 
# def get_hostname():
#     import socket
#     return socket.gethostname()


# Path: core/interfaces/regie.py
from .base import BaseInterface
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from flask import Flask, render_template, session, request, send_from_directory
from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect
from werkzeug.utils import secure_filename
from ..engine.network import get_allip, get_hostname
from zeroconf import ServiceInfo, Zeroconf 
import eventlet
import threading, os, time, queue
import logging, sys, json
import socket




thread = None
thread_lock = threading.Lock()

REGIE_PATH1 = '/opt/RPi-Regie'
REGIE_PATH2 = '/data/RPi-Regie'


class RegieInterface (BaseInterface):

    def  __init__(self, hplayer, port, datapath):
        super(RegieInterface, self).__init__(hplayer, "Regie")
        self._port = port
        self._datapath = datapath
        self._server = None
        
        

    # HTTP receiver THREAD
    def listen(self):

        # Advertize on ZeroConf
        zeroconf = Zeroconf()
        info = ServiceInfo(
            "_http._tcp.local.",

            "Regie._"+get_hostname()+"._http._tcp.local.",