def test_changing_port(self):
     """ Validate port parsing """
     led = LedController("127.0.0.1", port=123)
     self.assertEqual(led.gateway_port, 123)
     led = LedController("127.0.0.1", port="123")
     self.assertEqual(led.gateway_port, 123)
     with self.assertRaises(ValueError):
         LedController("127.0.0.1", port=0)
 def test_changing_repeat_commands(self):
     """ Change number of repeated commands """
     led = LedController("127.0.0.1", repeat_commands=0)
     self.assertEqual(led.repeat_commands, 1)
     led = LedController("127.0.0.1", repeat_commands=1)
     self.assertEqual(led.repeat_commands, 1)
     led = LedController("127.0.0.1", repeat_commands=2)
     self.assertEqual(led.repeat_commands, 2)
     with self.assertRaises(ValueError):
         LedController("127.0.0.1", repeat_commands=-1)
 def test_default_constructor(self):
     """ Verify default settings """
     led = LedController("127.0.0.1")
     self.assertEqual(led.gateway_ip, "127.0.0.1")
     self.assertEqual(led.gateway_port, 8899)
     self.assertEqual(led.repeat_commands, 3)
     self.assertEqual(led.pause_between_commands, 0.1)
Example #4
0
 def setUp(self):
     self.led = LedController("127.0.0.1",
                              pause_between_commands=0,
                              repeat_commands=0,
                              group_1="rgbw",
                              group_2="white",
                              group_3="rgbw",
                              group_4="white")
Example #5
0
def run_timed_actions():
    """ Runs light programs """
    led = LedController(settings.MILIGHT_IP)
    now = timezone.now()

    allowed_groups = set()
    for group in range(1, 5):
        if redis_instance.get("lightcontrol-no-automatic-%s" % group) is None:
            allowed_groups.add(group)

    for item in LightAutomation.objects.filter(running=True):
        if not item.is_running(now):
            continue
        percent_done = item.percent_done(now)
        logger.debug("Running %s, done %s%%", item.action, percent_done)

        brightness = None # Set brightness

        if item.action == "evening" or item.action == "evening-weekend":
            brightness = int((1-percent_done)*100)
        elif item.action == "morning" or item.action == "morning-weekend":
            brightness = int(percent_done * 100)

        if not item.action_if_off:
            # Don't turn on lights
            for group in list(allowed_groups): # cast to list to avoid "Set changed size during iteration"
                group_item, _ = LightGroup.objects.get_or_create(group_id=group)
                if group_item.on == False:
                    allowed_groups.remove(group)
        logger.debug("Only run on %s", allowed_groups)

        if item.set_white:
            for group in allowed_groups:
                led.white(group)
                logger.debug("Set %s to white", group)
                update_lightstate(group, None, "white", important=False)
        if brightness:
            logger.debug("Setting brightness to %s%%", brightness)
            publish_ws("lightcontrol-timed-brightness-%s" % item.action, brightness)
            for group in allowed_groups:
                group_brightness = brightness
                group_item, _ = LightGroup.objects.get_or_create(group_id=group)
                if item.no_brighten:
                    logger.debug("Current brightness: %s%%", group_item.current_brightness)
                    if group_item.current_brightness is not None:
                        group_brightness = min(group_item.current_brightness, group_brightness)
                if item.no_dimming:
                    logger.debug("Current brightness: %s%%", group_item.current_brightness)
                    if group_item.current_brightness is not None:
                        group_brightness = max(group_item.current_brightness, group_brightness)
                if group_item.current_brightness:
                    if led.get_brightness_level(group_brightness) == led.get_brightness_level(group_item.current_brightness):
                        logger.debug("Not sending brightness update to %s: no difference in brightness level", group)
                        continue
                logger.debug("Setting %s to %s", (group, group_brightness))
                led.set_brightness(group_brightness, group)
                update_lightstate(group, group_brightness, important=False)
            set_destination_brightness()
Example #6
0
 def test_when_no_tag_outputs_zeros(self):
     device = Mock()
     controller = LedController(device)
     controller.show()
     device.show.assert_called_with([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
 def test_sleep(self):  # pylint: disable=no-self-use
     """ Verify sleeps between subsequent commands """
     led = LedController("127.0.0.1",
                         pause_between_commands=0.5,
                         repeat_commands=1)
     start_time = time.time()
     led.on()
     self.assertLess(time.time() - start_time,
                     0.45)  # there is no sleep for a single command
     led.off()  # this command needs to wait almost 0.5 seconds
     self.assertGreater(time.time() - start_time, 0.45)
Example #8
0
 def test_when_tacking_tag_set_then_cleared_will_show_zero(self):
     device = Mock()
     controller = LedController(device)
     controller.set_have_tracking_tag()
     controller.clear_tag()
     controller.show()
     device.show.assert_called_with([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
Example #9
0
 def test_when_progress_tag_set_then_cleared_will_show_zero(self):
     device = Mock()
     controller = LedController(device)
     controller.set_progress(60, 30)
     controller.clear_tag()
     controller.show()
     device.show.assert_called_with([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
                           [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
Example #10
0
 def test_when_set_unknown_tag_it_calls_show_unknown_tag(self):
     device = Mock()
     controller = LedController(device)
     controller.set_unknown_tag()
     controller.show()
     device.show.assert_called_once()
     device.show.assert_called_with([[0, 0, 255], [255, 0, 0], [0, 0, 255], [255, 0, 0], [0, 0, 255],
                           [255, 0, 0], [0, 0, 255], [255, 0, 0], [0, 0, 255], [255, 0, 0],
                           [0, 0, 255], [255, 0, 0], [0, 0, 255], [255, 0, 0], [0, 0, 255],
                           [255, 0, 0], [0, 0, 255], [255, 0, 0], [0, 0, 255], [255, 0, 0],
                           [0, 0, 255], [255, 0, 0], [0, 0, 255], [255, 0, 0]])
Example #11
0
 def test_when_set_have_tracking_tag_it_calls_show_tracking_display(self):
     device = Mock()
     controller = LedController(device)
     controller.set_have_tracking_tag()
     controller.show()
     controller.show()
     self.assertEqual(device.show.call_count, 2)
     device.show.assert_called_with([[0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255],
                           [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255],
                           [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255],
                           [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255],
                           [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255]])
Example #12
0
 def setUp(self):
     self.led = LedController("127.0.0.1",
                              pause_between_commands=0,
                              repeat_commands=0)
Example #13
0
 def test_changing_repeat_commands(self):
     led = LedController("127.0.0.1", repeat_commands=0)
     self.assertEqual(led.repeat_commands, 1)
Example #14
0
 def test_changing_pause(self):
     led = LedController("127.0.0.1", pause_between_commands=0.8)
     self.assertEqual(led.pause_between_commands, 0.8)
Example #15
0
 def test_sleep(self):
     led = LedController("127.0.0.1")
     led.on()
     led.off()
Example #16
0
 def test_changing_port(self):
     led = LedController("127.0.0.1", port=123)
     self.assertEqual(led.gateway_port, 123)
     led = LedController("127.0.0.1", port="123")
     self.assertEqual(led.gateway_port, 123)
 def test_changing_pause(self):
     """ Change pause times """
     led = LedController("127.0.0.1", pause_between_commands=0.8)
     self.assertEqual(led.pause_between_commands, 0.8)
     with self.assertRaises(ValueError):
         LedController("127.0.0.1", pause_between_commands=-0.1)
Example #18
0
"""
LED Driver

This is the actual thing that drives the LEDS
"""

from patternmanager import PatternManager
from ledcontroller import LedController
from ledview import LedView

LED_DIMENSIONS = (14, 14)
colors = []
for row in range(LED_DIMENSIONS[0]):
    new = []
    for col in range(LED_DIMENSIONS[1]):
        new.append(0xff00ff)
    colors.append(new)

pm = PatternManager(colors, LED_DIMENSIONS)
lv = LedView(colors, LED_DIMENSIONS, pm.update)
lc = LedController(pm)
lv.draw()

Example #19
0
from django.conf import settings
from django.core import serializers
from django.db import models
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.utils import timezone
from homedisplay.utils import publish_ws
from ledcontroller import LedController
import datetime
import json
import logging
import redis

__all__ = ["LightGroup", "LightAutomation", "update_lightstate", "is_any_timed_running", "get_serialized_timed_action", "get_serialized_lightgroup"]

led = LedController(settings.MILIGHT_IP)
redis_instance = redis.StrictRedis()
logger = logging.getLogger(__name__)


def get_morning_light_level(group_id=None):
    # TODO: this is called relatively often, and this fetches all LightGroup objects on every iteration.
    max_brightness = 0
    items = LightGroup.objects.filter(on=True)
    if group_id is None or group_id == 0:
        # Process all groups
        for g in items:
            max_brightness = max(g.current_brightness or 0, max_brightness)
        return min(10, max_brightness)
    else:
        # Process only a single group
Example #20
0
    def log_tag(self, tag_id, start, end):
        self.tag_repository.log_tag(tag_id, self.device_id, start, end)


if __name__ == "__main__":
    from ledcontroller import LedController

    if len(sys.argv) == 2 and sys.argv[1] == "test":
        from timeularapi import TimularApi
        from tagrepository import TagRepository
        from mockleddevice import MockLedDevice
        from mockrfireader import MockRfiReader
        logging.basicConfig(level=logging.DEBUG)
        led_device = MockLedDevice()
        led_controller = LedController(led_device)
        reader = MockRfiReader()
        configuration = Configuration("debug_config.json")
        tag_repository = TagRepository(configuration)
        api = TimularApi(configuration, tag_repository)
    else:
        if len(sys.argv) == 2 and sys.argv[1] == "debug":
            logging.basicConfig(level=logging.DEBUG)
        if len(sys.argv) == 2 and sys.argv[1] == "info":
            logging.basicConfig(level=logging.INFO)
        from rfidevice import RfiDevice
        from timeularapi import TimularApi
        from leddevice import LedDevice
        from tagrepository import TagRepository
        configuration = Configuration("configuration.json")
        led_device = LedDevice(configuration)