Saving token to Domoticz variable in bash

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
JeroenG
Posts: 27
Joined: Monday 06 January 2020 20:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 20231
Contact:

Saving token to Domoticz variable in bash

Post by JeroenG »

Hi,

to retreive to information about my solar-panales I need to use a token which is 30 minutes valid. Retreiving this works fine. I con not retreive a token evry few minutes, otherwise I run into an error (to many request or so).

My proposed solution:
Create 2 Domoticx variables: 1 holding the last token value, and 1 holding the last timestamp a token was retreived. Whenever a script needs the token, first check is the timestamp is > 25 minutes old. If so, get a new token from Huawei, otherwise use the token from the Domoticz variable.

This all works fine except writing a new token to the Domoticz variable. In a small testing script this works, but when adding more logic to it, it fails :-(.

Code: Select all

# Write new token to Domoticz

 myVarName=Huawei_XSRF_token

 echo -----
 echo "myDomoticzURL: "$myDomoticzURL
 echo "myToken: "$myToken
 echo "myVar: "$myVarName
 echo -----
 echo
 echo "$myDomoticzURL/json.htm?type=command&param=updateuservariable&vname=$myVarName&vtype=2&vvalue=$myToken"
 echo -----
 echo

 myURL="$myDomoticzURL/json.htm?type=command&param=updateuservariable&vname=$myVarName&vtype=2&vvalue=$myToken"

 echo "myURL: "$myURL

 curl "$myURL"
All variables are filled correctly, but the script fails with:

Code: Select all

-----
myDomoticzURL: http://192.168.178.34:8080
myToken: x-hg5jsbo83xtik7ddtd7zen1c1jumnv48iqampg9htjdglf091g49jsg89gnttgtd9dju5humk5vyemc8ers87sg8dirs45k4ZZZZZk0beremukhcvx3wemumo5dd6lnu
myVar: Huawei_XSRF_token
-----

http://192.168.178.34:8080/json.htm?type=command&param=updateuservariable&vname=Huawei_XSRF_token&vtype=2&vvalue=x-hg5jsbo83xtik7ddtd7zen1c1jumnv48iqampg9htjdglf091g49jsg89gnttgtd9dju5humk5vyemc8ers87sg8dirs45k4ZZZZZk0beremukhcvx3wemumo5dd6lnu
-----

myURL: http://192.168.178.34:8080/json.htm?type=command&param=updateuservariable&vname=Huawei_XSRF_token&vtype=2&vvalue=x-hg5jsbo83xtik7ddtd7zen1c1jumnv48iqampg9htjdglf091g49jsg89gnttgtd9dju5humk5vyemc8ers87sg8dirs45k4ZZZZZ0beremukhcvx3wemumo5dd6lnu
curl: (3) URL using bad/illegal format or missing URL
If I execute curl followed by the line printed, it works like a charm, only from the script it fails :-(.

What works is this script:

Code: Select all

#!/bin/bash

# Set enviroment
#####################################
setEnvironment () {
  mydir=`dirname $0`
  myTempFile=$mydir/results.$$
  source $mydir/HuaweiSolar.env
  #source $mydir/get_token.sh
}

CheckToken () {
myVarName=Huawei_XSRF_token
myToken="x-YYyZzjsbo83xtik7ddtd7zen1c1jumnv48iqampg9htjdglf091g49jsg89gnttgtd9dju5humk5vyemc8ers87sg8dirs45k4XXXXXXXeremukhcvx3wemumo5dd6lnuZZZ"
echo -----
echo "myDomoticzURL: "$myDomoticzURL
echo "myToken: "$myToken
echo "myVar: "$myVarName
echo -----
echo
curl "$myDomoticzURL/json.htm?type=command&param=updateuservariable&vname=$myVarName&vtype=2&vvalue=$myToken"
}

setEnvironment
CheckToken
Anybody who has a clue?

Thanks,
Jeroen
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Saving token to Domoticz variable in bash

Post by waltervl »

Did you try the curl command in script without quotes:
curl $myURL
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
JeroenG
Posts: 27
Joined: Monday 06 January 2020 20:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 20231
Contact:

Re: Saving token to Domoticz variable in bash

Post by JeroenG »

Hi Walter,

yes tried with and withou single and double quotes, no luck so far.

Meanwhile re-thinking my design. Maybe will put the token and timestamp not in Domotic variables, but store them in a small file. Not as nice as it could be, but should be working also.

Jeroen
Darkneo
Posts: 24
Joined: Sunday 10 February 2019 16:22
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Saving token to Domoticz variable in bash

Post by Darkneo »

Hi, I also faced this kind of problems trying to use tokens...
What I finally used is a cookie, to save the connection (with wget, and not curl). I do not know this can also be a workaround for you or not.
Anyway interested to see if anybody finds a fix for this!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest