01-10-2016, 04:17 PM
(This post was last modified: 01-10-2016, 04:18 PM by Cryptomaster.)
Hello All
First of all I want to say a big thanks for those involved in the pilight project.
A marvelous, highly efficient piece of code I got running on the raspberry.
I have written my own piece of python program to monitor if my phone (me) is home or not.
Unfortunately I was not able to do this reliably with pilight ping as the phone enters some of a "power save mode" and doesn't respond to ping for quite some time.
So the python code have a threshold so it requiters a defined amount of failed pings before saying that device is down.
The code I did is posted below, and tested running on windows machine for now. Would appreciate any comments on the method I use to write to control messages to pilight
First of all I want to say a big thanks for those involved in the pilight project.

A marvelous, highly efficient piece of code I got running on the raspberry.
I have written my own piece of python program to monitor if my phone (me) is home or not.
Unfortunately I was not able to do this reliably with pilight ping as the phone enters some of a "power save mode" and doesn't respond to ping for quite some time.
So the python code have a threshold so it requiters a defined amount of failed pings before saying that device is down.
The code I did is posted below, and tested running on windows machine for now. Would appreciate any comments on the method I use to write to control messages to pilight

Code:
######################
# Import necessary modules
import os
import sys
import subprocess
import time
import json
import requests
#####################
#######################################################
#######################-Config-########################
#-Name of file with hosts
hosts = "hosts.txt"
#-Number of pings each time
retries = 2
#-Delay between pings in seconds
delay = 2
#-Delay between program runs in seconds
delay_prog = 10
#-Number of failed pings before device is down. (24 works good for me)
failed = 24
#-Number of success pings before device is up
success = 1
#-pilight core adress ex: <http://192.168.1.162:5000>
pilight = "http://192.168.1.162:5000"
#-Update failed ping counts label to pilight label device "PythonFailCount"
pilight_count = True
#######################################################
#######################################################
#######################################################
def ping(host):
# Open a devnull device. "Black hole" to put shit in.
DEVNULL = open(os.devnull, 'w')
try:
# Do shell command and wait for return. Input/Output to black hole devnull.
subprocess.check_call(["ping","-n","1","-w","1",host],stdout=DEVNULL, stderr=DEVNULL)
# Return true if ping responded
return True
# CalledProcessError goes True when no response on ping.
except subprocess.CalledProcessError:
return False
def SendPilightCount():
# Copy global variable to local
count = count_down
# If count is 0 then label color should be green otherwise red
if count == 0:
color = "green"
else:
color = "red"
# Make JSON prepared string with data for pilight
data = {"action": "control", "code": {"device": "PythonFailCount", "values": {"label": count, "color": color}}}
# Convert to JSON
data_json = json.dumps(data)
# Copy to payload
payload = data_json
#-Send to pilight
requests.post(pilight, data=payload)
def SendPilightUp():
# Make JSON prepared string with data for pilight
data = {"action": "control", "code": {"device": "CustomPython", "state": "on"}}
# Convert to JSON
data_json = json.dumps(data)
# Copy to payload
payload = data_json
#-Send to pilight
requests.post(pilight, data=payload)
def SendPilightDown():
# Make JSON prepared string with data for pilight
data = {"action": "control", "code": {"device": "CustomPython", "state": "off"}}
# Convert to JSON
data_json = json.dumps(data)
# Copy to payload
payload = data_json
#-Send to pilight
requests.post(pilight, data=payload)
def action_up():
# Reset counters
count_down = 0
count_up = 0
# Tell function to use global variables
global up_exec
global down_exec
# Set flag that action_up have been executed and reset flag action_down
up_exec = True
down_exec = False
# Set request to pilight core with device and status
SendPilightUp()
# Function to do when devices is down
def action_down():
# Reset counters
count_down = 0
count_up = 0
# Tell function to use global variables
global down_exec
global up_exec
# Set flag that action_down have been executed and reset flag action_up
down_exec = True
up_exec = False
# Set request to pilight core with device and status
SendPilightDown()
# Declare and reset counter integers
count_down = 0
count_up = 0
# Declare and reset executed bools
down_exec = False
up_exec = False
#-Mainprogram
while True:
try:
#Do this indentation with file open for reading as hostsFile
with open(hosts,'r') as hostsFile:
#For each line in file do indentation.
for line in hostsFile:
#Do indentation
for i in range(0,retries):
#Call ping function with adress from line in file. .rstrip('\n') is to remove empty space.
#Assign return value to variable "is_up"
is_up=ping(line.rstrip('\n'))
#If ping respond, count up else count down.
if is_up:
#Don count if variable is above 99
if count_up <= 99:
count_up+=1
count_down=0
else:
#Don count if variable is above 99
if count_down <= 99:
count_down+=1
count_up=0
# Sleep for delay before next ping.
time.sleep(delay)
#Go back to for loop and do same for next host from file.
#-If function active send count information to pilight.
if pilight_count:
SendPilightCount()
#iF counter device up reached above success and action not executed. Execute action.
#Else if counter device down reached above failed and action not executed. Execute action.
if (count_up >= success and not up_exec):
action_up()
elif count_down >= failed and not down_exec:
action_down()
#Delay program before next ping session
time.sleep(delay_prog)
# Exit if somebody press CTRL + C in terminal
except KeyboardInterrupt:
break
# Print that we are exiting and exit.
print("Exiting")
sys.exit()