From cd8b5a7adf55cf28c5819b61fc8a148833eefa40 Mon Sep 17 00:00:00 2001 From: Nama Date: Tue, 15 Jun 2021 17:57:17 +0200 Subject: [PATCH] Made work with another plugin, see README.md Removed "output" logic, so they can be handled on Artemis --- README.md | 12 ++--- artemisremotecontrol/__init__.py | 26 ++++------ config.json | 83 ++++++++++++++---------------- scripts/powercfg.py | 5 +- scripts/tasmota.py | 88 ++++---------------------------- 5 files changed, 65 insertions(+), 149 deletions(-) diff --git a/README.md b/README.md index 4bfab14..27346f4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ArtemisRemoteControl -Library and script loader to control LEDs over the WebAPI of Artemis with [this plugin](https://gist.github.com/SpoinkyNL/fb7d03b22ddfb19ddc150de1b99053e2). +Library and script loader to control LEDs over the WebAPI of Artemis with [Cheerpipes plugin](https://github.com/Cheerpipe/Artemis.Plugins.Public/tree/master/src/DataModelExpansions/Artemis.Plugins.DataModelExpansions.DynamicExternalData). ## Usage ### Download exe Go to [Actions](https://github.com/Nama/ArtemisRemoteControl/actions) or [Releases](https://github.com/Nama/ArtemisRemoteControl/releases) and download ArtemisRemoteControl @@ -16,10 +16,10 @@ If you've downloaded the exe, run it. Else, run `run.py`. ## Scripting - Put your own scripts in to the folder `scripts` - Always use `from threading import Thread`, the plugin loader is blocking -- Do `from artemisremotecontrol import getleds, setleds` - - Use `getleds` to get a json file with all available LEDs - - To set LEDs: `setleds(devices, leds, '#AA00FF)` - - `devices` and `leds` need to be lists. So you can define a whole set of LEDs with the same conditions +- Do `from artemisremotecontrol import setleds` + - To set LEDs: `setleds(key, value)` + - This appears in Artemis as a DataModel + - Set in Artemis what you want to do with the data - You can use the `Config()` class for your own scripts Use these to initialize, so all scripts have their own "namespace" in the config: ```py @@ -35,7 +35,7 @@ except IndexError: config = Config() config.load(config_name) ... -config.add(config_name, [url, key, value, devices, leds, color1, color2, color3]) +Config.add(config_name, device) ``` ## venv diff --git a/artemisremotecontrol/__init__.py b/artemisremotecontrol/__init__.py index ac93ca6..b085e54 100644 --- a/artemisremotecontrol/__init__.py +++ b/artemisremotecontrol/__init__.py @@ -31,28 +31,20 @@ def _getapiurl(ep): return None -def getleds(): - url = _getapiurl('GetLeds') - if not url: - return - response = post(url) - leds = loads(response.content) - with open('leds.json', 'w') as leds_file: - leds_file.writelines(dumps(leds, indent=4)) - - -def setleds(deviceids: list, ledids: list, color: str): - url = _getapiurl('SetLeds') +def setleds(name: str, value: str): + # Weird, its an int if the string contains only an int + if isinstance(value, int): + url = _getapiurl('SetIntValue') + else: + url = _getapiurl('SetStringValue') if not url: return - data = list() - for i, led in enumerate(ledids): - data.append({'DeviceId': deviceids[i], 'LedId': led, 'Color': color}) + data = f'{name}:{value}' logging.debug(data) try: - response = post(url, data=str(data), timeout=0.3) + response = post(url, data=data, timeout=0.3) except (ConnectionError, ChunkedEncodingError, ReadTimeout): - logging.error('Can\'t connect to Artemis to set color') + logging.error('Can\'t connect to Artemis to send data') return if response.status_code != 200: logging.debug(response.content) diff --git a/config.json b/config.json index b62ac6f..4b1e5bb 100644 --- a/config.json +++ b/config.json @@ -4,68 +4,61 @@ "base_url": "http://192.168.1.13/cm?user=admin&password=pw&cmnd=", "data": [ { - "colors": [ - "#0000FF", - "#821700", - "#000000" - ], - "devices": [ - "Logitech G910v2-Logitech-G910v2-Keyboard", - "Logitech G910v2-Logitech-G910v2-Keyboard", - "Logitech G910v2-Logitech-G910v2-Keyboard", - "Logitech G910v2-Logitech-G910v2-Keyboard" - ], "key": "Dimmer", - "leds": [ - "Keyboard_Programmable6", - "Keyboard_Programmable7", - "Keyboard_Programmable8", - "Keyboard_Programmable9" - ], "uri": "Dimmer", - "value": [ - 15, - 45 - ] + "value": null }, { - "colors": [ - "#0000FF", - "#821700", - "#000000" - ], - "devices": [ - "Logitech G910v2-Logitech-G910v2-Keyboard" - ], "key": "POWER", - "leds": [ - "Keyboard_Programmable5" - ], "uri": "Power", "value": "ON" } - ] + ], + "name": "LivingRoomLight" }, { "base_url": "http://192.168.1.9/cm?user=admin&password=pw&cmnd=", "data": [ { - "colors": [ - "#0000FF", - "#821700", - "#000000" - ], - "devices": [ - "Logitech G910v2-Logitech-G910v2-Keyboard" - ], "key": "POWER", - "leds": [ - "Keyboard_Programmable2" - ], "uri": "Power", "value": "ON" } - ] + ], + "name": "NintendoSign" + }, + { + "base_url": "http://192.168.1.10/cm?user=admin&password=pw&cmnd=", + "data": [ + { + "key": "POWER", + "uri": "Power", + "value": "ON" + } + ], + "name": "TV" + }, + { + "base_url": "http://192.168.1.11/cm?user=admin&password=pw&cmnd=", + "data": [ + { + "key": "POWER", + "uri": "Power", + "value": "ON" + } + ], + "name": "Plug3" + }, + { + "base_url": "http://192.168.1.12/cm?user=admin&password=pw&cmnd=", + "data": [ + { + "key": "POWER", + "uri": "Power", + "value": "ON" + } + ], + "name": "3dprinter" } ] } \ No newline at end of file diff --git a/scripts/powercfg.py b/scripts/powercfg.py index a5f190d..14df1e4 100644 --- a/scripts/powercfg.py +++ b/scripts/powercfg.py @@ -10,10 +10,7 @@ def loop(): while True: output = check_output('powercfg /GetActiveScheme') activeplan = output.decode('utf-8').split('(')[1][:-1] - if activeplan == 'Energiesparmodus': - setleds(['Logitech G910v2-Logitech-G910v2-Keyboard'], ['Logo'], '#0000FF') - else: - setleds(['Logitech G910v2-Logitech-G910v2-Keyboard'], ['Logo'], '#FF0000') + setleds('powerplan', activeplan) sleep(3) diff --git a/scripts/tasmota.py b/scripts/tasmota.py index 943e492..ce684af 100644 --- a/scripts/tasmota.py +++ b/scripts/tasmota.py @@ -7,13 +7,11 @@ from requests import get from requests.exceptions import ConnectionError, ChunkedEncodingError, ReadTimeout from artemisremotecontrol.config import Config -from artemisremotecontrol import getleds, setleds +from artemisremotecontrol import setleds try: config_name = __name__.split('.')[1] except IndexError: - getleds() - print('I created a leds.json. Check this file and edit config.json to your needs.') print('Run run.py after config.') exit() @@ -31,100 +29,36 @@ def loop(): if status != 200: # send "Disconnected" state logging.debug('Can\'t connect to device') - setleds(uri['devices'], uri['leds'], uri['colors'][2]) + setleds(device['name'], 'down') continue text = loads(response.content.decode('utf-8')) logging.debug(text) - if isinstance(uri['value'], list): - percent = (int(text[uri['key']]) - uri['value'][0])/((uri['value'][1] - uri['value'][0])/100) - logging.debug(f'Percent: {percent}') - if len(uri['leds']) == 1: - decimal = 255 / 100 * percent # 255 is FF in hex - alpha = f'#{hex(decimal)}' - ocolor = uri['colors'][0].replace('#', '') - color = f'{alpha}{ocolor}' - setleds(uri['devices'], uri['leds'], color) - else: - led_count = len(uri['leds']) - led_light = (led_count / 100) * percent - devices = list() - leds = list() - logging.debug(led_count) - logging.debug(led_light) - if led_light > led_count: - led_light = led_count - for led in range(int(led_light)): - devices.append(uri['devices'][led]) - leds.append(uri['leds'][led]) - setleds(devices, leds, uri['colors'][0]) - devices = list() - leds = list() - for led in range(led_count-int(led_light)): - index = led + 1 - devices.append(uri['devices'][-index]) - leds.append(uri['leds'][-index]) - logging.debug(devices) - logging.debug(leds) - setleds(devices, leds, uri['colors'][1]) - else: - if text[uri['key']] == uri['value']: - # Send "true" state - logging.debug('Found searching string') - setleds(uri['devices'], uri['leds'], uri['colors'][0]) - else: - # Send "false" state - logging.debug('Not found searching string') - setleds(uri['devices'], uri['leds'], uri['colors'][1]) - sleep(1) + value = text[uri['key']] + logging.debug(f'Value: {value}') + setleds(f'{device["name"]}{uri["uri"]}', value) + sleep(0.1) sleep(5) def save(): device = dict() - device['base_url'] = 'http://192.168.1.12/cm?user=admin&password=pw&cmnd=' + device['name'] = 'Dimmer' + device['base_url'] = 'http://192.168.1.13/cm?user=admin&password=pw&cmnd=' device['data'] = list() - # Range based values + # Range based values, comment if not needed device['data'].append(dict()) device['data'][-1]['uri'] = 'Dimmer' device['data'][-1]['key'] = 'Dimmer' - device['data'][-1]['value'] = [15, 60] # make sure, that these are integers - device['data'][-1]['devices'] = [ # if you use only one led here, the opacity will be the range indicator - 'Logitech G910v2-Logitech-G910v2-Keyboard', - 'Logitech G910v2-Logitech-G910v2-Keyboard', - 'Logitech G910v2-Logitech-G910v2-Keyboard', - 'Logitech G910v2-Logitech-G910v2-Keyboard' - ] - device['data'][-1]['leds'] = [ - 'Keyboard_Programmable6', - 'Keyboard_Programmable7', - 'Keyboard_Programmable8', - 'Keyboard_Programmable9' - ] - device['data'][-1]['colors'] = [ - '#0000FF', # active color, omit alpha value if you use only one key - '#821700', # the "background" - '#000000' - ] + device['data'][-1]['value'] = None # bool values device['data'].append(dict()) device['data'][-1]['uri'] = 'Power' device['data'][-1]['key'] = 'POWER' device['data'][-1]['value'] = 'ON' - device['data'][-1]['devices'] = [ - 'Logitech G910v2-Logitech-G910v2-Keyboard' - ] - device['data'][-1]['leds'] = [ - 'Keyboard_Programmable4' - ] - device['data'][-1]['colors'] = [ - '#0000FF', - '#821700', - '#000000' - ] config.add(config_name, device) - logging.warning('Saved.') + logging.info('Saved.') config = Config()