import sys
import time
import threading
import win32event
import win32service
import win32serviceutil
from appBoot.logUtil import LogUtil
from dataAnalysisService.service.serviceOfDataAnalysis import ServiceOfDataAnalysis

logUtil=LogUtil()
logger=logUtil.getLogger()

def CommandLine(classService):
    win32serviceutil.HandleCommandLine(classService)

class WinService(win32serviceutil.ServiceFramework):
    _svc_name_ = "DataAnalysisService"
    _svc_display_name_ = "Data Analysis Service Of Turbine"
    _svc_description_ = "This is a sample service."

    def __init__(self, args):
        try:
            logger.info("execute init")
            win32serviceutil.ServiceFramework.__init__(self, args)
            self.stop_event = win32event.CreateEvent(None, 0, 0, None)
            self.service=ServiceOfDataAnalysis()
        except Exception as ex:
            logger.error(ex)

    def SvcStop(self):
        self.stop()

    def SvcDoRun(self):
        self.start()

    def main(self):
        while self.is_running:
            logger.info("Service is running...")
            self.service.run()

    def start(self):
        logger.info("execute start")
        try:
            self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
            self.is_running = True
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            self.main()
        except Exception as ex:
            logger.error(ex)

    def stop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.is_running = False
        win32event.SetEvent(self.stop_event)

    def status(self):
        if self.is_running:
            print("Service is running.")
        else:
            print("Service is not running.")