Python : script detection erreur log domoticz

Vous avez des questions concernant les scripts dans Domoticz, posez vos questions ici, quelque soit le langage nous tenterons des réponses pertinentes ...
Répondre
MisterT
Messages : 53
Enregistré le : 12 avr. 2015, 19:04

Python : script detection erreur log domoticz

Message par MisterT »

Salut a tous et bonne année

Je viens de faire une fresh install de mon raspberry ainsi que de domoticz (V4.9700) et je viens de réinstaller ma database ainsi que mes scripts et j'ai desormais un message d'erreur sur le script python de détection de presence.
script initial:
https://www.domoticz.com/wiki/Presence_detection

J'ai d'abord fait des recherches pour résoudre le problème (et j'ai aussi posé la question pour résoudre le problème):
http://www.domoticz.com/forum/viewtopic.php?f=6&t=23951

mais il y a toujours un probleme de script qui est:

2019-01-04 14:59:40.069 Error: EventSystem: Failed to execute python event script "/home/pi/domoticz/scripts/python/check_device_online.py"
2019-01-04 14:59:40.069 Error: EventSystem: Traceback (most recent call last):
2019-01-04 14:59:40.069 Error: EventSystem: File "/home/pi/domoticz/scripts/python/check_device_online.py", line 37, in <module>
2019-01-04 14:59:40.069 Error: EventSystem: if len(sys.argv) != 5 :
2019-01-04 14:59:40.069 Error: EventSystem: AttributeError: module 'sys' has no attribute 'argv'

si une personne douée en python passe par la, ça serait vraiment cool
merci pour les futures réponses

le script ci dessous

Code : Tout sélectionner

#!/usr/bin/python
#   Title: check_device_online.py
#   Author: Chopper_Rob
#   Date: 25-02-2015
#   Info: Checks the presence of the given device on the network and reports back to domoticz
#   URL : https://www.chopperrob.nl/domoticz/5-report-devices-online-status-to-domoticz
#   Version : 1.6.2.1 modified by SjoerdNLD for python 3
 
import sys
import datetime
import time
import os
import subprocess
import urllib.request
import json
import base64
 
# Settings for the domoticz server
domoticzserver="127.0.0.1:8080/domoticz"
domoticzpasscode = "Black"
 
# If enabled. The script will log to the file _.log
# Logging to file only happens after the check for other instances, before that it only prints to screen.
log_to_file = False
 
# The script supports two types to check if another instance of the script is running.
# One will use the ps command, but this does not work on all machine (Synology has problems)
# The other option is to create a pid file named _.pid. The script will update the timestamp
# every interval. If a new instance of the script spawns it will check the age of the pid file.
# If the file doesn't exist or it is older then 3 * Interval it will keep running, otherwise is stops.
# Please chose the option you want to use "ps" or "pid", if this option is kept empty it will not check and just run.
check_for_instances = "pid"
 
 
 
# DO NOT CHANGE BEYOND THIS LINE
if len(sys.argv) != 5 :
  print ("Not enough parameters. Needs %Host %Switchid %Interval %Cooldownperiod.")
  sys.exit(0)
 
device=sys.argv[1]
switchid=sys.argv[2]
interval=sys.argv[3]
cooldownperiod=sys.argv[4]
previousstate=-1
lastsuccess=datetime.datetime.now()
lastreported=-1
domoticzurl = 'http://'+domoticzserver+'/json.htm?type=devices&filter=all&used=true&order=Name'
 
if check_for_instances.lower() == "pid":
  pidfile = sys.argv[0] + '_' + sys.argv[1] + '.pid'
  if os.path.isfile( pidfile ):
    print (datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists")
    if (time.time() - os.path.getmtime(pidfile)) < (float(interval) * 3):
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- script seems to be still running, exiting")
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- If this is not correct, please delete file " + pidfile)
      sys.exit(0)
    else:
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- Seems to be an old file, ignoring.")
  else:
    open(pidfile, 'w').close() 
 
if check_for_instances.lower() == "ps":
  if int(subprocess.check_output('ps x | grep \'' + sys.argv[0] + ' ' + sys.argv[1] + '\' | grep -cv grep', shell=True)) > 2 :
    print (datetime.datetime.now().strftime("%H:%M:%S") + "- script already running. exiting.")
    sys.exit(0)
 
def log(message):
  print (message)
  if log_to_file == True:
    logfile = open(sys.argv[0] + '_' + sys.argv[1] + '.log', "a")
    logfile.write(message + "\n")
    logfile.close()
 
def domoticzstatus ():
  json_object = json.loads(domoticzrequest(domoticzurl))
  status = 0
  switchfound = False
  if json_object["status"] == "OK":
    for i, v in enumerate(json_object["result"]):
      if json_object["result"][i]["idx"] == switchid:
        switchfound = True
        if json_object["result"][i]["Status"] == "On": 
          status = 1
        if json_object["result"][i]["Status"] == "Off": 
          status = 0
  if switchfound == False: print (datetime.datetime.now().strftime("%H:%M:%S") + "- Error. Could not find switch idx in Domoticz response. Defaulting to switch off.")
  return status
 
def domoticzrequest (url):
  request = urllib.request.Request(url)
  response = urllib.request.urlopen(request)
  return response.read()
 
log (datetime.datetime.now().strftime("%H:%M:%S") + "- script started.")
 
lastreported = domoticzstatus()
if lastreported == 1 :
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- according to domoticz, " + device + " is online")
if lastreported == 0 :
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- according to domoticz, " + device + " is offline")
 
while 1==1:
  # currentstate = subprocess.call('ping -q -c1 -W 1 '+ device + ' > /dev/null', shell=True)
  currentstate = subprocess.call('sudo arping -q -c10 -w500000 '+ device + ' > /dev/null', shell=True)
 
  if currentstate == 0 : lastsuccess=datetime.datetime.now()
  if currentstate == 0 and currentstate != previousstate and lastreported == 1 : 
    log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, no need to tell domoticz")
  if currentstate == 0 and currentstate != previousstate and lastreported != 1 :
    if domoticzstatus() == 0 :
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, tell domoticz it's back")
      domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=On&level=0" + "&passcode=" + domoticzpasscode)
    else:
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, but domoticz already knew")
    lastreported=1
 
  if currentstate == 1 and currentstate != previousstate :
    log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, waiting for it to come back")
 
  if currentstate == 1 and (datetime.datetime.now()-lastsuccess).total_seconds() > float(cooldownperiod) and lastreported != 0 :
    if domoticzstatus() == 1 :
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, tell domoticz it's gone")
      domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=Off&level=0" + "&passcode=" + domoticzpasscode)
    else:
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, but domoticz already knew")
    lastreported=0
 
  time.sleep (float(interval))
 
  previousstate=currentstate
  if check_for_instances.lower() == "pid": open(pidfile, 'w').close()

pipiche
Messages : 1263
Enregistré le : 04 avr. 2018, 16:16
Contact :

Re: Python : script detection erreur log domoticz

Message par pipiche »

Peut-etre un problème de compatibilité de version de python !

Domoticz necessite python3 (voir 3.5) et >
Je maintiens le plugin Zigate sur mon temps libre .
Si vous appréciez ce que je fais, vous pouvez contribuer par un don Paypal , ce qui me permettra notamment l'achat d’équipements et de les tester/valider pour le plugin.

chat sur Keybase.io

Répondre