Feller-AG / wiser-api

MIT License
12 stars 1 forks source link

Python libraries #11

Open Ced20 opened 1 year ago

Ced20 commented 1 year ago

I want to order devices in http (GET) it worked with Zeptrion but impossible with wiser I tried different script to send a very simple command: http://192.168.1.140/leds.cgi?led=4=Commute

Unfortunately I always have an error message with different scripts because the Gateway does not have a module like: requests, urllib, http client, asyncio, urandom, ….

Ced20 commented 1 year ago

__import requests

def send_ipx800_command(): command = { "req": "GET", "loc": "192.168.1.140", "pth": "/leds.cgi", "bdy": "led=4=Commute" } url = command['loc'] + command['pth'] if command['req'] == 'GET': url += '?' + command['bdy'] response = requests.get(url) print(response.text) # Afficher la réponse de la requête

send_ipx800_command()

import urllib.parse import urllib.request

def send_ipx800_command(): command = { "req": "GET", "loc": "192.168.1.140", "pth": "/leds.cgi", "bdy": "led=4=Commute" } url = command['loc'] + command['pth'] if command['req'] == 'GET': url += '?' + command['bdy'] response = urllib.request.urlopen(url) print(response.read().decode('utf-8')) # Afficher la réponse de la requête

send_ipx800_command()

import http.client

def send_ipx800_command(): command = { "req": "GET", "loc": "192.168.1.140", "pth": "/leds.cgi", "bdy": "led=4=Commute" } conn = http.client.HTTPConnection(command['loc']) url = command['pth'] + '?' + command['bdy'] if command['req'] == 'GET' else command['pth'] conn.request(command['req'], url) response = conn.getresponse() print(response.read().decode('utf-8')) # Afficher la réponse de la requête

send_ipx800_command()

import urandom import asyncio from sf.lib import aiocurl, alog

async def send_ipx800_command(): command = { "req": "GET", "loc": "192.168.1.140", "pth": "/leds.cgi", "bdy": "led=4=Commute" } url = f"http://{command['loc']}{command['pth']}?{command['bdy']}" response = await aiocurl(command['req'], url) print(response[1]) # Afficher la réponse de la requête

Exécuter la fonction principale asynchrone

asyncio.run(send_ipx800_command())

import requests

def send_command(req, loc, pth, bdy): url = loc + pth headers = {} if req == 'GET': url += '?' + bdy response = requests.get(url, headers=headers) elif req == 'POST': headers = {"Content-Type": "application/x-www-form-urlencoded"} response = requests.post(url, headers=headers, data=bdy)

return response.text

def send_ipx800_command(): command = { "req": "GET", "loc": "192.168.1.140", "pth": "/leds.cgi", "bdy": "led=4=Commute" } response = send_command(command['req'], command['loc'], command['pth'], command['bdy']) print(response) # Afficher la réponse de la requête

send_ipx800_command()

import urllib.request

url = "http://192.168.1.140/leds.cgi?led=4=Commute"

try: response = urllib.request.urlopen(url) print(response.read().decode()) except urllib.error.URLError as e: print("Erreur lors de la requête :", e)__

woodworm commented 1 year ago

Use follow script to trigger the service via smart-button or rocket-button on the web interface.

from sf.lib import aiocurl

# Called from job on smart-button event
async def onButtonEvent(*argv):
    await aiocurl('http://192.168.1.140/leds.cgi?led=4=Commute')

# Called from the Web-GUI 'start'-button
async def onStart(*argv):
    await aiocurl('http://192.168.1.140/leds.cgi?led=4=Commute')

More information about scripts can be found here. https://github.com/Feller-AG/wiser-tutorial/blob/main/doc/api_scripts.md

Ced20 commented 1 year ago

Thank you for your answer unfortunately, I alwa like:ys have an answer like:

can you tell me which library and module are installed ?

woodworm commented 1 year ago

The script engine does not offer the possibility to import CPython modules. The main idea of the scripting engine is to provide the possibility to send web-services to an API of an external system with a smart button. The Hue and Sonos examples should give you a good overview of what is possible.

Let me know what you're missing for your use cases so we can support those features in the future.

Ced20 commented 1 year ago

Hello, I want to do something very simple. Just send an http request in GET to modules like the GCEelectronics IPX or trigger senaria on a Jeedom box. As I did with Zepptrion but apparently it is not supported because it lacks libraries. I tried several methods (see script above) But maybe I missed a step, I'm not a programmer (just an electrician)!

Thanks for your help

https://wiki.gce-electronics.com/index.php?title=API_V3 https://wiki.gce-electronics.com/index.php?title=API_V4 https://doc.jeedom.com/fr_FR/core/4.3/api_http

woodworm commented 1 year ago

All you need for this is the aiocurl() function. You can use it to run any service. I can't test it... but something like that should work.

from sf.lib import aiocurl

ipx800_v4 = '192.168.1.140'  # set here the ip address of the ipx800 device
apikey = 'apikey' # set here the apikey... you should be able to read it somewhere

async def _relay_request(cmd, id):
    await aiocurl('http://'+IPX800_V4+'/api/xdevices.json?key='+apikey+'&'+cmd+'R='+ id)

async def onStart():
    # Toggle Relay 1 if you press the rocket-button on the web interface
    await _relay_request('Toggle', 1)

async def onButtonEvent(*argv):
    # Switch Relay 1 on and off if you press a I/0 smart-Button
    button_event = argv[0]
    button_type = argv[1]

    if (button_event, button_type) == ('click', 'up'):
        await _relay_request('Set', 1)
    elif (button_event, button_type) == ('click', 'down'):
         await _relay_request('Clear', 1)
    else:  # button_type == 'toggle':
        await _relay_request('Toggle', 1)
Ced20 commented 1 year ago

Hello, I would first like to order 2 IPXv3 with 8 relays as well as scenarios on a Jeddom home automation box which would make the possibility of ordering unlimited...

There are quite a few possibilities: aiocurl, request or urequests. But every time I get an error for example:

I can also order it like this: [ {"req":"GET", "loc":"192.168.1.140", "pth":"/leds.cgi?led=4=Commute", "bdy":"" }]

woodworm commented 1 year ago

Have you tried this script 1:1? Is there an error when you run this script? This script does exactly the same thing as you did with the zeptrion configuration {"req":"GET", "loc":"192.168.1.140", "pth":"/leds.cgi?led=4=Commute", "bdy":"" }

from sf.lib import aiocurl

# Called from job on smart-button event
async def onButtonEvent(*argv):
    await aiocurl('http://192.168.1.140/leds.cgi?led=4=Commute')

# Called from the Web-GUI with rocket-button
async def onStart(*argv):
    await aiocurl('http://192.168.1.140/leds.cgi?led=4=Commute')

Do not import request or urequests or aiohttp! Only import aiocurl with the following line from sf.lib import aiocurl as in the example

Ced20 commented 1 year ago

Hello thank you for your help. I have two commands that work well !

from sf.lib import aiocurl

Called from job on smart-button event

async def onButtonEvent(*argv): await aiocurl('http://192.168.1.140/preset.htm?set3=1&set4=1&set5=1&set6=1&set7=1&set8=1')

Called from the Web-GUI with rocket-button

async def onStart(*argv): await aiocurl('http://192.168.1.140/preset.htm?set3=1&set4=1&set5=1&set6=1&set7=1&set8=1')

from sf.lib import aiocurl

Called from job on smart-button event

async def onButtonEvent(*argv): await aiocurl('http://192.168.1.140/leds.cgi?led=4=Commute')

Called from the Web-GUI with rocket-button

async def onStart(*argv): await aiocurl('http://192.168.1.140/leds.cgi?led=4=Commute')

Ced20 commented 1 year ago

Next step, ordering my Jeedom home automation box. Thanks again !

Do you think it is possible to resume the status feedback to control the Wiser LED?