2013-11-21, 05:21
I've made a bunch of modifications to the script, which I'm posting below. Python isn't my first (or second, or third, or fourth) language, so please excuse if things aren't as tight as they could/should be. Some changes:
- Accept parameter for cameraID and cameraName. Use case: I call this from BlueIris, which results in XBMC pulling a stream from BlueIris based on the camID.
- The existing image refresh didn't work for me (Window 8, on Gotham). So I made each image pulled have a unique name
- Cleanup the contents of the temp directory after it's done
- urlretrieve didn't work on my pc for some reason. So I switched out to urlopen
- Corrected some paths for images etc.
- Accept parameter for cameraID and cameraName. Use case: I call this from BlueIris, which results in XBMC pulling a stream from BlueIris based on the camID.
- The existing image refresh didn't work for me (Window 8, on Gotham). So I made each image pulled have a unique name
- Cleanup the contents of the temp directory after it's done
- urlretrieve didn't work on my pc for some reason. So I switched out to urlopen
- Corrected some paths for images etc.
Code:
# Import the XBMC/XBMCGUI modules.
import xbmc, xbmcgui, time, urllib2, xbmcvfs, xbmcaddon, os, glob
__addon__ = xbmcaddon.Addon()
__addonid__ = __addon__.getAddonInfo('id')
#############
#Grab (/ set default) parameters
#############
args = ['CamID','CamName']
dbvar = {'CamID': 'cam1',
'CamName': 'Camera 1'}
for item in sys.argv:
arg = item.split('=')
i = arg[0]
if arg[0] in args:
j = arg[1]
dbvar.update({arg[0]:arg[1]})
#############
# Global Vars
#############
url='http://IPADDRESS/image/'+str(dbvar['CamID'])
path = xbmc.translatePath('special://profile/addon_data/%s' % __addonid__)
imagefile_template = os.path.join(path, 'camera_'+str(dbvar['CamID'])+'_image')
imagefile_ext = '.jpg'
imagefile = imagefile_template+imagefile_ext
video_length = 14
notification_length = 13800
#############
#If path to temp files doesn't exist, create it
#############
if not xbmcvfs.exists(path):
xbmcvfs.mkdir(path)
####################################################################################
#Retrieves a file from a given URL
####################################################################################
def getFile(file_name,file_mode,base_url):
#create the url and the request
req = urllib2.Request(base_url)
# Open the url
try:
f = urllib2.urlopen(req)
# Open our local file for writing
local_file = open(file_name, "wb" + file_mode)
#Write to our local file
local_file.write(f.read())
local_file.close()
f.close()
del local_file
del f
#handle errors
except urllib2.HTTPError, e:
print "HTTP Error:",e.code , url
f.close()
del local_file
del f
except urllib2.URLError, e:
print "URL Error:",e.reason , url
f.close()
del local_file
del f
####################################################################################
#Deletes all files matching the mask that we have created in this call of the script
####################################################################################
def delFiles():
del_mask = imagefile_template+'*'+imagefile_ext
for del_filename in glob.glob(del_mask) :
os.remove( del_filename )
####################################################################################
#Main class and function
####################################################################################
class CamView(xbmcgui.WindowDialog):
def __init__(self):
#set the initial image before the window is shown
imagefile = imagefile_template+imagefile_ext
getFile(imagefile,"",url)
self.image = xbmcgui.ControlImage(870, 383, 380, 253, "")
self.addControl(self.image)
viewer = CamView()
viewer.show()
start_time = time.time()
firstimage = True
while(time.time() - start_time <= video_length):
curr_time = round(time.time() - start_time, 0)
imagefile = imagefile_template+'_'+str(curr_time)+imagefile_ext
getFile(imagefile,"",url)
viewer.image.setImage("")
viewer.image.setImage(imagefile)
if firstimage:
nowtime=time.strftime("%I:%M %p")
xoptions="Notification(\"IP Camera "+str(dbvar['CamName'])+" alert\",%s, notification_length, special://home/addons/script.doorbell/bell_icon.png)" % (nowtime)
xbmc.executebuiltin(xoptions)
viewer.image.setAnimations([('conditional', 'effect=fade start=0 end=90 time=250 delay=125 condition=true',), ('conditional', 'effect=slide start=0,400 end=0,0 time=250 condition=true',)])
firstimage = False
elif curr_time == video_length:
viewer.image.setAnimations([('conditional', 'effect=fade start=90 end=90 time=0 condition=true',), ('conditional', 'effect=slide start=0,0 end=0,400 time=250 condition=true',)])
#print "catch"
else:
viewer.image.setAnimations([('conditional', 'effect=fade start=90 end=90 time=0 condition=true',)])
#print curr_time
xbmc.sleep(500)
viewer.close()
delFiles()
del viewer