I've asked this question already but I think I asked it in the wrong way, or at least I might be barking up the wrong tree with where the problem lies.
I've got a Python/Flask script with the following route:
@app.route('/heatingadjust')
def heatingadjust(hiveSessionId=None, score=None):
import requests
import time
import datetime
import MySQLdb
conn = MySQLdb.connect(host="localhost", user = "admin", passwd = "xxxxxxxxxx", db = "mydb")
cursor = conn.cursor()
cursor.execute("select score from OccScore")
data = cursor.fetchone()
score = data[0]
url = "https://api.prod.bgchprod.info:443/omnia/users"
if 'hiveSessionId' in session:
hiveSessionId = session['hiveSessionId']
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Hive Web Dashboard",
'X-Omnia-Access-Token': hiveSessionId,
'Cache-Control': "no-cache"
}
response = requests.request("GET", url, headers=headers)
data=response.json()
if 'errors' in data:
return redirect(url_for('hivelogin'))
if (score == 0):
url = "https://api-prod.bgchprod.info:443/omnia/nodes/xxxxxxxxxx"
payload = "{\n \"nodes\": [{\n \"attributes\": {\n \"targetHeatTemperature\": {\n \"targetValue\": 15\n }\n }\n }]\n}"
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Dashboard",
'X-Omnia-Access-token': hiveSessionId,
'Cache-Control': "no-cache",
}
response = requests.request("PUT", url, data=payload, headers=headers)
else:
url = "https://api-prod.bgchprod.info:443/omnia/nodes/xxxxxxx"
payload = "{\n \"nodes\": [{\n \"attributes\": {\n \"targetHeatTemperature\": {\n \"targetValue\": 18\n }\n }\n }]\n}"
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Dashboard",
'X-Omnia-Access-token': hiveSessionId,
'Cache-Control': "no-cache",
}
response = requests.request("PUT", url, data=payload, headers=headers)
return str(score)
Basically, I've got one route that gets called using wget that sniffs for Bluetooth devices, increments as score if it finds a device and writes the score to a MySQL table. This is to see if there's anyone at home.
This route reads the score and turns the heating off using the Hive api if the house is empty. Called from the URL, it does exactly what it should and I can see the heating get turned off.
However, what I'm trying to do is call a script with the wget from crontab.
The crontab runs and I can see it in syslog. The apache access log shows the URL being called.
But the heating doesn't get turned off as it does if I call the exact same thing through the browser.
I have a suspicion from something else I saw that it might be down to wget not liking the fact that another URL is called.
Can anyone tell me if my suspicion is correct? I've seen something else about using urllib instead of wget but I don't know what I'd need to change to call the URL with the headers.
wget http://your-flask-instanceis failing because the Flask route makes its own HTTP request and wget "doesn't like that"? No, that's a nonsense idea.