|
| 1 | +# Copyright (c) 2019-2020 AnimatedLEDStrip |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +import json |
| 22 | +from typing import AnyStr, Optional |
| 23 | + |
| 24 | +from .utils import check_data_type |
| 25 | + |
| 26 | + |
| 27 | +class StripInfo(object): |
| 28 | + """Stores information about a LED strip""" |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + self.num_leds: int = 0 |
| 32 | + self.pin: Optional[int] = None |
| 33 | + self.image_debugging: bool = False |
| 34 | + self.file_name: Optional[str] = None |
| 35 | + self.renders_before_save: Optional[int] = None |
| 36 | + self.thread_count: int = 100 |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def from_json(cls, input_str: AnyStr) -> 'StripInfo': |
| 40 | + """Create an StripInfo instance from a JSON representation""" |
| 41 | + # Parse the JSON |
| 42 | + input_json = json.loads(input_str[5:]) |
| 43 | + |
| 44 | + # Create a new StripInfo instance |
| 45 | + new_instance = cls() |
| 46 | + |
| 47 | + # Get each property from the JSON, reverting to defaults if it can't be found |
| 48 | + new_instance.num_leds = input_json.get('numLEDs', new_instance.num_leds) |
| 49 | + new_instance.pin = input_json.get('pin', new_instance.pin) |
| 50 | + new_instance.image_debugging = input_json.get('imageDebugging', new_instance.image_debugging) |
| 51 | + new_instance.file_name = input_json.get('fileName', new_instance.file_name) |
| 52 | + new_instance.renders_before_save = input_json.get('rendersBeforeSave', new_instance.renders_before_save) |
| 53 | + new_instance.thread_count = input_json.get('threadCount', new_instance.thread_count) |
| 54 | + |
| 55 | + # Double check that everything has the right data type |
| 56 | + new_instance.check_data_types() |
| 57 | + |
| 58 | + return new_instance |
| 59 | + |
| 60 | + def check_data_types(self) -> bool: |
| 61 | + """Check that all parameter types are correct""" |
| 62 | + good_types = True |
| 63 | + |
| 64 | + good_types = good_types and check_data_type('num_leds', self.num_leds, int) |
| 65 | + good_types = good_types and check_data_type('pin', self.pin, int, allow_none=True) |
| 66 | + good_types = good_types and check_data_type('image_debugging', self.image_debugging, bool) |
| 67 | + good_types = good_types and check_data_type('file_name', self.file_name, str, allow_none=True) |
| 68 | + good_types = good_types and check_data_type('renders_before_save', self.renders_before_save, int, allow_none=True) |
| 69 | + good_types = good_types and check_data_type('thread_count', self.thread_count, int) |
| 70 | + |
| 71 | + return good_types |
0 commit comments