我不熟悉 Python,但我在 Google 上搜索时制作了以下脚本。
该脚本从设备中提取数据并将其注入 Domoticz。当我在我的 Windows 机器上运行脚本时,它每分钟都会正确循环并将数据注入 Domoitcz。当我在我的 Raspberry Pi 上运行相同的脚本时,它返回一个 SyntaxError: invalid syntax (line 155) on the except Exception 行...
即使我在该行之前放了一个 # 也会引发错误。
#!/usr/bin/env python3
from APSystemsECUR import APSystemsECUR
import time
import asyncio
import urllib.request
import urllib.parse
import urllib
from pprint import pprint
ecu_ip = "192.168.178.xx"
sleep = 60
url = 'http://192.168.178.xx:8080/json.htm?'
puntcomma = '\u003B'
loop = asyncio.get_event_loop()
ecu = APSystemsECUR(ecu_ip)
while True:
try:
data = loop.run_until_complete(ecu.async_query_ecu())
#pprint(data)
today_energy_kwh = str(data.get('today_energy')*1000)
print('Today energy [kWh]: ' + today_energy_kwh)
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 212, 'svalue': (today_energy_kwh)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
#print(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 213, 'svalue': data.get('current_power')}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
#print(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 195, 'svalue': data.get('timestamp')}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
#print(url + urllib.parse.urlencode(getVars))
#inverter values
inverters = data.get('inverters')
#count number of inverters
Inverter_qty = len(data.get('inverters'))
print('Inverter_cnt: ' + str(Inverter_qty))
# loop trough all inverters and get the data
for i in range(Inverter_qty):
Inverter = list(inverters.keys())[i]
print('InverterId: ' + Inverter)
InverterOnline = data['inverters'][Inverter]['online']
print('Online: ' + str(InverterOnline))
InverterTemperature = data['inverters'][Inverter]['temperature']
print('Temperature: ' + str(InverterTemperature))
nPower = len(data['inverters'][Inverter]['power'])
for x in range(nPower):
power = data['inverters'][Inverter]['power'][x]
print('Power inverter ' + str(i + 1) + ' panel ' + str(x + 1) + ': ' + str(power) + ' W')
#upload values to Domoticz voor inverter 1
if (i == 0) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 173, 'svalue': InverterTemperature}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
if InverterOnline == True :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 174, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
else :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 174, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
#upload values to Domoticz voor inverter 2
if (i == 1) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 179, 'svalue': InverterTemperature}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
if InverterOnline == True :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 180, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
else :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 180, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
#upload values to Domoticz voor inverter 3
if (i == 2) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 185, 'svalue': InverterTemperature}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
if InverterOnline == True :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 186, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
else :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 186, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
#upload values to Domoticz voor inverter 4
if (i == 3) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 191, 'svalue': InverterTemperature}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
if InverterOnline == True :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 192, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
else :
getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 192, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
#upload power values to Domoticz voor inverter 1
if (i == 0) and (x == 0) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 196, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 0) and (x == 1) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 197, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 0) and (x == 2) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 198, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 0) and (x == 3) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 199, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
#upload power values to Domoticz voor inverter 2
if (i == 1) and (x == 0) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 200, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 1) and (x == 1) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 201, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 1) and (x == 2) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 202, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 1) and (x == 3) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 203, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
#upload power values to Domoticz voor inverter 3
if (i == 2) and (x == 0) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 204, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 2) and (x == 1) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 205, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 2) and (x == 2) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 206, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 2) and (x == 3) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 207, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
#upload power values to Domoticz voor inverter 4
if (i == 3) and (x == 0) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 208, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 3) and (x == 1) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 209, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 3) and (x == 2) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 210, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
elif (i == 3) and (x == 3) :
getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 211, 'svalue': (power)}
webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
except Exception as err:
print(f"[ERROR]", {err})
#print(f"Sleeping for {sleep} sec")
time.sleep(sleep)
为什么它可以在我的 Windows 机器上正确运行,而不是在 Raspberry Pi 上?