import services.Service
import hardware_adapter
import configuration_manager
from log import setup_logger
import time

logger = setup_logger("Static Light Service")
cm = configuration_manager.Configuration()


class StaticLightService(services.Service.Service):

    def __init__(self):
        super().__init__()
        self.requires_gpio = True

    def run(self):
        hardware_adapter.enable_gpio()
        next_preset = None

        self.mutex.acquire()
        self.message_queue.append(cm.static_light.initial_preset)
        self.mutex.release()

        while True:
            self.mutex.acquire()
            next_preset = next_preset if len(self.message_queue) == 0 else self.message_queue.pop()
            self.mutex.release()

            if 'type' in next_preset:
                if next_preset['type'] == 'color':
                    hardware_adapter.set_color(next_preset)
                elif next_preset['type'] == 'fade':
                    hardware_adapter.set_fade(next_preset)
                elif next_preset['type'] == 'sequence':
                    hardware_adapter.set_sequence(next_preset)
                elif next_preset['type'] == 'none':
                    hardware_adapter.set_off()
                self.mutex.acquire()
                self.public_vars['current_preset'] = next_preset
                self.mutex.release()
            else:
                time.sleep(.0125)

            self.mutex.acquire()
            if self.should_stop:
                break
            self.mutex.release()
        hardware_adapter.disable_gpio()
        logger.info("Thread cleanly stopped.")


@@ -426,16 +426,46 @@
 t = None
+%0A        current_preset = None
 %0A%0A      
@@ -808,16 +808,70 @@
 preset:%0A
+                if next_preset != current_preset:%0A    
         
@@ -925,32 +925,36 @@
                 
+   
+ 
 hardware_adapter
@@ -969,32 +969,139 @@
 or(next_preset)%0A
+                    elif next_preset%5B'type'%5D == 'none':%0A                        hardware_adapter.set_off()%0A
                 
@@ -1316,108 +1316,27 @@
 eset
-)%0A                elif next_preset%5B'type'%5D == 'none':%0A                    hardware_adapter.set_off()
+, self.check_cont)%0A
 %0A   
@@ -1421,16 +1421,33 @@
 eset'%5D =
+ current_preset =
  next_pr
@@ -1491,60 +1491,8 @@
 se()
-%0A            else:%0A                time.sleep(.0125)
 %0A%0A  
@@ -1698,8 +1698,148 @@
 ped.%22)%0A%0A
+    def check_cont(self):%0A        self.mutex.acquire()%0A        cont = not self.should_stop%0A        self.mutex.release()%0A        return cont%0A

remove unneeded calls to hardware_adapter