AppleScript C Perl Shell Xcode Other

Updating Dahua IPC-HFW4300S security camera settings

Post Reply
coding / applescript     Views: 302Prev .. Next
Updating Dahua IPC-HFW4300S security camera settingsPosted: Thursday, January 15, 2015 [22:55:35] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 188
Dahua cameras are very good and wok flawlessly with SecuritySpy software.
Although one thing in Dahua IPC-HFW4300S camera software requires constant update - camera schedule for switching from day to night mode and back.
It has to be done manually by adjusting sliders in Camera -> Conditions -> Profile Management.
AppleScrpt/Python/Perl/crontab help solving this little problem.

Dahua software has a system Backup/Restore update form in System -> Import/Export section which can be used to change the camera settings by initially downloading system settings, updating Sunrise/Sunset data file (DeviceConfig.backup) and uploading it back to the camera VIA Web interface.

To access Web interface my obvious weapon of choice was an AppleScript. Dahua uses Prototype extensively and accessing "Import" button turned out to be impossible VIA AppleScript as button is located in dynamically created iFrame -- therefore Python is used to click on a button while setting Safari window bounds to exactly the same coordinates every time program runs.

Getting current Sunrise/Sunset data as well as updating DeviceConfig.backup file is easy enough to do using Perl.

Dahua software information:
Software Version: 2.420.0002.0.R, build : 2014-06-21
WEB Version: 3.2.4.184667
ONVIF Version: 2.4.1

AppleScript code:
View Codetell application "Safari"
make new document
delay 5
activate
delay 1
set URL of document 1 to "http://DAHUA_CAMERA_IP_ADDRESS/";
set myURL to URL of document 1
delay 3
set web_page_is_loaded to false
set myCounter to 0
set maxCounter to 30
set my_delay to 1
repeat until web_page_is_loaded is true
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" or name of window 1 contains "w.htm" then
delay my_delay
else
set web_page_is_loaded to true
end if
set myCounter to myCounter + 1
if myCounter is maxCounter then
set web_page_is_loaded to true
end if
end repeat
delay 1
do "document.getElementById('password').focus()" in document 1
end tell
delay 2

tell application "System Events"
tell process "Safari"
keystroke "CAMERA_PASSWORD_HERE"
delay 2
keystroke return
delay 2
end tell
end tell
delay 8

tell application "Safari"
do "document.getElementById('b_config').focus()" in document 1
end tell

delay 1

tell application "System Events"
tell process "Safari"
keystroke return
end tell
end tell
delay 2

tell application "Safari"
do "document.getElementById('system').focus()" in document 1
end tell

delay 1

tell application "System Events"
tell process "Safari"
keystroke return
end tell
end tell
delay 2

tell application "Safari"
set LinkPresent to ""
set AllLinks to ""
set TransMatched to ""
set exitNmbr to 5
set LinkPresent to (do "document.links.length" in document 1)
set LinkPresent to LinkPresent - 1
repeat with n from 0 to LinkPresent
set linkURL to (do "document.links[" & n & "].href" in document 1)
set linkName to (do "document.links[" & n & "].text" in document 1)
if linkName contains "Import/Export" then
do "document.links[" & n & "].focus();" in document 1
delay 1
set LinkPresent to n
exit repeat
end if
end repeat
end tell

delay 1

tell application "System Events"
tell process "Safari"
keystroke return
end tell
end tell
delay 2

-- Safari window coordinates on the next line to be adjusted accordingly
tell application "Safari" to set the bounds of the front window to {0, 22, 1650, 1024}
delay 1

-- Below is a click coordinates and could be different on your system
set Pcode to getPython("242, 245")
do shell script "python -c " & quoted form of Pcode
delay 1

tell application "System Events"
tell process "Safari"
keystroke "g" using {command down, shift down}
delay 1
keystroke "/SecuritySpy"
delay 2
keystroke return
delay 1
keystroke "DeviceConfig.backup"
delay 2
keystroke return
delay 10
keystroke "w" using {command down}
end tell
end tell
delay 2

on getPython(coord)
set PyThon to "import sys
import time
from Quartz.CoreGraphics import *

def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx, posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos = CGEventGetLocation(ourEvent);
mouseclickdn(" & coord & ");
mouseclickup(" & coord & ");
"
return PyThon
end getPython

Save AppleScript as an APP to obfuscate code from kids.
Please read Python issues notes below..

Next is a Perl script to get Sunrise/Sunset times for your region:
View Code#!/usr/bin/perl
unless($ENV{'SECURITYSESSIONID'}) {
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local:/usr/local/bin:/usr/bin/bin';
}
if(-f "/SecuritySpy/sunset.sunrise.raw.txt") {unlink "/SecuritySpy/sunset.sunrise.raw.txt";}
$brids = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.3290)';
## Replace YOUR_ZIP_CODE with your actual Zip Code
$url = "/SecuritySpy/sunset.sunrise.raw.txt http://www.wunderground.com/cgi-bin/findweather/getForecast?query=YOUR_ZIP_CODE";;
`curl --connect-timeout 30 -A '$brids' -o $url`;
unless(-f "/SecuritySpy/sunset.sunrise.raw.txt" && -f "/SecuritySpy/DeviceConfig.backup") {print "no file: /SecuritySpy/sunset.sunrise.raw.txt or Camera file\n";exit(0);}

$settings = `cat /SecuritySpy/DeviceConfig.backup`; ## THIS IS A CAMERA BACK-UP FILE

$vr = `cat /SecuritySpy/sunset.sunrise.raw.txt`;
$bd = '<body';($sh,$vr) = split(/$bd/,$vr);
$vr =~ s#<script>(.*?)</script>##gs;
$vr =~ s#<(.*?)>##gs;
$vr =~ s#(Sunrise / Set)(.*?)(\d{1,2}\:\d{2} \w{2})(.*?)(\d{1,2}\:\d{2} \w{2})##is;
$rise=$3;
$set=$5;
## Getting Hour and minutes for the sunrise. It works better if 20 minutes delay applied to the time.
$rise =~ s/(\d{1,2}\:\d{2}) (\w{2})/$1/;$ampm=$2;($hr,$min) = split(/\:/,$rise);$min++;$min--;$min+=20;
if($min > 59) {$min -= 60;$hr++;}
if($min < 1) {$min=1;}
if($min > 55) {$min=55;}
$hr++;$hr--;
$settings =~ s#("SunriseHour"\:)(\d{1,2})(,)#$1$hr$3#s;
$settings =~ s#("SunriseMinute"\:)(\d{1,2})(,)#$1$min$3#s;

if($min < 1 && $hr) {$min++;}
if($ampm =~ m/pm/i) {$hr += 12;}
open(TXT,">/SecuritySpy/sunset.sunrise.txt");print TXT "\$risehr=$hr;\t\$risemin=$min;\n";print "\$risehr=$hr;\t\$risemin=$min;\n";
## Getting Hour and minutes for the sunset. It works better if 20 minutes delay applied to the time.
$set =~ s/(\d{1,2}\:\d{2}) (\w{2})/$1/;$ampm=$2;($hr,$min) = split(/\:/,$set);$min++;$min--;$min+=20;
if($min > 59) {$min -= 60;$hr++;}
if($min < 1) {$min=1;}
if($min > 55) {$min=55;}
$hr++;$hr--;
print "HR: $hr\nMin: $min\n";
if($hr < 1 && $min) {$hr .= 'A';}
if($min < 1 && $hr) {$min++;}
if($ampm =~ m/pm/i) {$hr += 12;}
$settings =~ s#("SunsetHour"\:)(\d{1,2})(,)#$1$hr$3#s;
$settings =~ s#("SunsetMinute"\:)(\d{1,2})(,)#$1$min$3#s;
print TXT "\$sethr=$hr;\t\$setmin=$min;\n";print "\$sethr=$hr;\t\$setmin=$min;\n";
close(TXT);
open(TXB,">/SecuritySpy/DeviceConfig.backup");print TXB $settings;close(TXB);

`osascript /SecuritySpy/update.app`; ## Now run the AppleScript to upload updated back-up file
## Remove temporary data file to keep it cleaner - optional
if(-f "/SecuritySpy/sunset.sunrise.raw.txt") {unlink "/SecuritySpy/sunset.sunrise.raw.txt";}

Replace "/SecuritySpy" directory with the one you will be using.
Make sure curl is installed on your Mac. In Terminal window enter the following:
which curl
If you get path with curl program - you're fine.

Last thing to do is to setup crontab to run Perl program once in 5 or 7 days.
As an option Lingon could be used to do the crontab task. It is not a free software $5 and I am not affiliated with their website:
http://www.peterborgapps.com/lingon
it is very easy to use and worth every penny.

Mac OS X Mavericks and Yosemite may require clearing AppleScript app in FireWall in order to gain Assistive devices access.
I have no way of verifying it as newest OS I am running is Mountain Lion and have no plans on upgrading.There's no place like ~
RE: Updating Dahua IPC-HFW4300S security camera settingsPosted: Thursday, January 15, 2015 [23:01:04] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 188
Python may have issues with Quartz.CoreGraphics
Apple removed it from Python distribution. To add Python Quartz and CoreGraphics support I used PyObjC.

Getting PyObjC:
http://pythonhosted.org/pyobjc/index.html

Installing PyObjC:
http://pythonhosted.org/pyobjc/install.html

It works on Mountain Lion. Older Mac OS X versions (Tiger, Snow Leopard) work out of the box in my case.There's no place like ~
coding / applescriptPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other