Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 compatibility in example scripts #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions scripts-dist/ds18b20.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
output = "terminal"

if len (sys.argv) < 2 :
print "Usage: " + sys.argv[0] + " [C|F] {fifo}"
print " C or F must be given"
print " If \"fifo\" arg is present, write to PiKrellCam FIFO"
print("Usage: " + sys.argv[0] + " [C|F] {fifo}")
print(" C or F must be given")
print(" If \"fifo\" arg is present, write to PiKrellCam FIFO")
sys.exit(1)

mode = sys.argv[1]
if (mode != "F") and (mode != "C"):
print "Usage: " + sys.argv[0] + " [C|F] {fifo}"
print " C or F must be given"
print " If \"fifo\" arg is present, write to PiKrellCam FIFO"
print("Usage: " + sys.argv[0] + " [C|F] {fifo}")
print(" C or F must be given")
print(" If \"fifo\" arg is present, write to PiKrellCam FIFO")
sys.exit(1)

if len (sys.argv) == 3 :
Expand All @@ -30,9 +30,8 @@ def read_temp(device):
global mode
for count in range(0, 3):
path = "/sys/bus/w1/devices/" + device.rstrip() + "/w1_slave"
file = open(path)
lines = file.read()
file.close()
with open(path) as file:
lines = file.read()

line1 = lines.split("\n")[0]
line2 = lines.split("\n")[1]
Expand Down Expand Up @@ -69,6 +68,6 @@ def read_temp(device):
fifo.write("annotate_string append ds18b20 " + out_string + "\n")
fifo.close()
else:
print out_string
print(out_string)
except:
print "no devices"
print("no devices")
35 changes: 20 additions & 15 deletions scripts-dist/example-motion-detects-fifo
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
import os
import sys
import signal
import fcntl
import atexit
import select
import StringIO
import time

try:
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO as _StringIO
def StringIO(initial_value=b''):
"""Take bytes as input but return a unicode IO object"""
return _StringIO(initial_value.decode())


# Turn on pikrellcam motion detects writing to the motion_detects_FIFO
#
Expand All @@ -34,7 +40,7 @@ os.system('echo "motion_detects_fifo_enable on" > /home/pi/pikrellcam/www/FIFO')
# read from the motion_detects_FIFO and exit.
#
def signal_handler(signum, frame):
print ' Turning off motion_detects_fifo_enable'
print(' Turning off motion_detects_fifo_enable')
os.system('echo "motion_detects_fifo_enable off" > /home/pi/pikrellcam/www/FIFO')
sys.exit(0)

Expand Down Expand Up @@ -62,39 +68,38 @@ while True:
# region vectors. If you care only about overall frame motion, look
# for 'f' frame vectors.
#
lines = StringIO.StringIO(data)
lines = StringIO(data)
for line in lines.readlines():
line = line.strip('\n')
print "FIFO read: " + line
print("FIFO read: " + line)

# If some program sends a "motion_detects_fifo_enable off" to the
# command FIFO, an <off> tag will be written to the motion_detects_FIFO.
#
if (line.find('<off>') == 0):
print " detected motion_detects_fifo_enable off - exiting."
print(" detected motion_detects_fifo_enable off - exiting.")
sys.exit(1)
elif (line.find('<motion') == 0):
state = 'motion'
# <motion t > where t is system time with .1 second precision
m, t, b = line.split()
print " motion detected - system time " + t + " => " + time.asctime( time.localtime( int(float(t)) ) )
print(" motion detected - system time " + t + " => " + time.asctime( time.localtime( int(float(t)) ) ))
elif (line.find('</motion>') == 0):
print ""
print("")
state = 'wait'
elif (state == 'motion'):
if (line[0] == "f"):
r, x, y, dx, dy, mag, count = line.split()
print " motion vector - frame:" + " mag=" + mag + " count=" + count
print(" motion vector - frame:" + " mag=" + mag + " count=" + count)
elif (line[0].isdigit()):
r, x, y, dx, dy, mag, count = line.split()
print " motion vector - region " + line[0] + ": mag=" + mag + " count=" + count
print(" motion vector - region " + line[0] + ": mag=" + mag + " count=" + count)
elif (line[0] == "b"):
b, count = line.split()
print " burst detect - count=" + count
print(" burst detect - count=" + count)
elif (line[0] == "e"):
e, code = line.split()
print " external trigger - code: " + code
print(" external trigger - code: " + code)
elif (line[0] == "a"):
a, level = line.split()
print " audio trigger - level=" + level

print(" audio trigger - level=" + level)
13 changes: 6 additions & 7 deletions scripts-dist/example-motion-events
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# is run from a terminal.

import sys
import socket
import time

filename = '/run/pikrellcam/motion-events'
Expand All @@ -23,7 +22,7 @@ state = 'wait'
event = 1

# sys.argv[1] is the $e value: motion, external or audio
print "on_motion_begin triggered by: " + sys.argv[1]
print("on_motion_begin triggered by: " + sys.argv[1])

while 1:
where = file.tell()
Expand All @@ -34,7 +33,7 @@ while 1:
continue
line = line.strip('\n')
if (line.find('<motion') == 0):
print " motion event number: " + str(event)
print(" motion event number: " + str(event))
event = event + 1
state = 'motion'
elif (line.find('</motion>') == 0):
Expand All @@ -43,13 +42,13 @@ while 1:
sys.exit(1)
elif (state == 'motion'):
if (line[0].isdigit()):
print " motion direction - region: " + line[0]
print(" motion direction - region: " + line[0])
elif (line[0] == "b"):
b, count = line.split()
print " burst detect - count: " + count
print(" burst detect - count: " + count)
elif (line[0] == "e"):
e, code = line.split()
print " external trigger - code: " + code
print(" external trigger - code: " + code)
elif (line[0] == "a"):
a, level = line.split()
print " audio trigger - level: " + level
print(" audio trigger - level: " + level)
5 changes: 2 additions & 3 deletions scripts-dist/example-motion-send-alarm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# With this script only one alarm is sent per motion detect video.
# See example-motion-send-alarm2 for a different approach.

import sys
import socket
import time

Expand All @@ -40,6 +39,6 @@ send_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
msg_id = 1
for x in range(0, repeat):
msg = "%s:%d %s message alarm" % (from_host, msg_id, to_hosts)
# print msg
send_socket.sendto(msg, (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
# print(msg)
send_socket.sendto(msg.encode(), (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
time.sleep(delay)
12 changes: 6 additions & 6 deletions scripts-dist/example-motion-send-alarm2
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import time
# It should be greater than the holdoff in pkc-alarm.
#
to_hosts = "gkrellm4,rpi0,rpi1"
motion_region = 3
motion_region = '3'
magnitude_limit = 12
count_limit = 20
holdoff = 6
Expand Down Expand Up @@ -75,24 +75,24 @@ while 1:
file.seek(where)
continue
line = line.strip('\n')
# print '==>' + line
# print('==>' + line)
if (line.find('<motion') == 0):
state = 'motion'
elif (line.find('</motion>') == 0):
state = 'wait'
elif (line.find('<end>') == 0):
sys.exit(1)
elif (state == 'motion'):
if (line.startswith(str(motion_region))):
# print line
if (line.startswith(motion_region)):
# print(line)
tnow = time.time()
if (tnow >= tsent + holdoff):
r, x, y, dx, dy, mag, count = line.split()
if (int(mag) >= magnitude_limit and int(count) >= count_limit):
for x in range(0, repeat):
msg = "%s:%d %s message alarm" % (from_host, msg_id, to_hosts)
# print msg
send_socket.sendto(msg, (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
# print(msg)
send_socket.sendto(msg.encode(), (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
time.sleep(delay)
tsent = tnow
msg_id = msg_id + 1
Expand Down
6 changes: 3 additions & 3 deletions scripts-dist/pkc-alarm
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def host_match(from_host, to_hosts):


while True:
line = sock.recv(256)
line = sock.recv(256).decode()
tnow = time.time()
# print line
# print(line)

from_host, to_hosts, msg_type, msg_body = line.split(' ', 3)
if (msg_type != 'message' or msg_body != 'alarm'):
Expand All @@ -84,7 +84,7 @@ while True:
continue

if (id != idprev or tnow - tplay >= holdoff):
print audio_command
print(audio_command)
os.system(audio_command)
tplay = tnow

Expand Down
16 changes: 8 additions & 8 deletions scripts-dist/pkc-motion
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ repeat = 4
delay = .05

def usage():
print "usage: pkc-motion {host|host-list|all} <on|off>"
print " Multicast a motion_enable command to PiKrellCams on a LAN."
print " If host, host-list or all is not specified, all is assumed."
print " host-list is a comma separated list of hosts."
print " on or off must be given."
print("usage: pkc-motion {host|host-list|all} <on|off>")
print(" Multicast a motion_enable command to PiKrellCams on a LAN.")
print(" If host, host-list or all is not specified, all is assumed.")
print(" host-list is a comma separated list of hosts.")
print(" on or off must be given.")
sys.exit()

argc = len(sys.argv)
Expand All @@ -48,14 +48,14 @@ if onoff == "on":
elif onoff == "off":
msg_id = 2
else:
print 'Bad arg: ' + onoff
print('Bad arg: ' + onoff)
usage()

hostname = socket.gethostname()

for x in range(1, repeat + 1):
msg = "%s:%d %s command @motion_enable %s" % (hostname, msg_id, hosts, onoff)
# print msg
send_socket.sendto(msg, (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
# print(msg)
send_socket.sendto(msg.encode(), (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
time.sleep(delay)

18 changes: 9 additions & 9 deletions scripts-dist/pkc-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ repeat = 4
delay = .05

def usage():
print "usage: pkc-reboot <host|host-list|all>"
print "Multicast a reboot command to PiKrellCams on a LAN."
print " host, host-list or all must be given."
print " host-list is a comma separated list of hosts."
print("usage: pkc-reboot <host|host-list|all>")
print("Multicast a reboot command to PiKrellCams on a LAN.")
print(" host, host-list or all must be given.")
print(" host-list is a comma separated list of hosts.")
sys.exit()

argc = len(sys.argv)
Expand All @@ -44,16 +44,16 @@ if argc == 1:
elif argc == 2:
hosts = sys.argv[1]
else:
print 'Bad number of args'
print('Bad number of args')
usage()

hostname = socket.gethostname()

msg_id = 1
for x in range(0, repeat):
msg = "%s:%d %s command @reboot" % (hostname, msg_id, hosts)
# print msg
send_socket.sendto(msg, (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
# print(msg)
send_socket.sendto(msg.encode(), (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
time.sleep(delay)


Expand All @@ -64,6 +64,6 @@ time.sleep(1)
msg_id = 2
for x in range(0, repeat):
msg = "%s:%d %s command @reboot" % (hostname, msg_id, hosts)
# print msg
send_socket.sendto(msg, (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
# print(msg)
send_socket.sendto(msg.encode(), (PKC_MULTICAST_GROUP, PKC_MULTICAST_PORT))
time.sleep(delay)
2 changes: 1 addition & 1 deletion scripts-dist/pkc-recv
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ mreq = struct.pack("4sl", socket.inet_aton(PKC_MULTICAST_GROUP_IP), socket.INADD
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
print sock.recv(1024)
print(sock.recv(1024).decode())