Difference between revisions of "WelcomeSwitch"

From i3Detroit
Jump to: navigation, search
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Wiring
+
THIS CODE IS OBSOLETE,
 +
NEW SCRIPT IS RUNNING IN PYTHON.
 +
PENDING AN UPDATE HERE.
  
On the camera:
+
==Wiring==
 +
 
 +
On the camera, there's a hacked-up PoE cable that does the following:
 
<pre>
 
<pre>
 
1 wht/org ethernet
 
1 wht/org ethernet
 
2 org/wht ethernet
 
2 org/wht ethernet
 
3 wht/grn ethernet
 
3 wht/grn ethernet
4 blu/wht GND
+
4 blu/wht GND, also Digital Input -
 
5 wht/blu Digital Input +
 
5 wht/blu Digital Input +
 
6 grn/wht ethernet
 
6 grn/wht ethernet
 
7 wht/brn Relay Output
 
7 wht/brn Relay Output
8 brn/wht +12V
+
8 brn/wht +12V, also Relay Input
 
</pre>
 
</pre>
  
In the box:
+
This allows the camera to be positioned away from the door by running a single cable. Without this, you'd need one for power, one for network, AND one for the I/O pins. Using a single 24ga wire for power is suboptimal, but the camera doesn't draw much power, and it hasn't been an issue yet.
Ethernet pins pass through to Ethernet input
+
 
 +
In the box above the door:
 +
Ethernet pins pass through to Ethernet input (into which is plugged the feed from the back of the space.)
 
GND from power input goes to GND from camera, also to LED cathode and SSR negative.
 
GND from power input goes to GND from camera, also to LED cathode and SSR negative.
 
+12 from power input goes to +12 from camera, also to switch contact.
 
+12 from power input goes to +12 from camera, also to switch contact.
 
Relay Output from camera goes to LED anode and SSR positive.
 
Relay Output from camera goes to LED anode and SSR positive.
Digtal Input + from camera goes to switch contact.
+
Digtal Input + from camera goes to switch contact. SSR isn't currently connected; signals are on orange wires, shrinkwrapped to prevent shorts in the time being.
 +
 
 +
(To accommodate a future local OPEN sign, provision has been made for connecting the big solid-state relay, which can switch anything we could throw at it.)
  
 
Software
 
Software
  
Proof-of-concept batch file:
+
The most important part of all this! Camera documentation: [ftp://ftp.dlink.com/Multimedia/URL%20Commands/URL%20command%20for%20DCS-5300_G_,%20DCS-2100_G_%2B,%20DCS-2000,%20DVS-301_2007_06_01.pdf]
<pre>:top
+
 
 +
 
 +
==Proof-of-concept batch file:==
 +
<pre>
 +
:top
 
del getdi.cgi
 
del getdi.cgi
wget --user=admin --password=i3 http://i3detroit.dyndns.org:5000/cgi-bin/getdi.cgi
+
wget --user=port --password=port http://i3detroit.dyndns.org:5000/cgi-bin/getdi.cgi
find "DI=H" <getdi.cgi
+
echo %errorlevel%
+
if %errorlevel% EQU 0 wget --user=admin --password=i3 http://i3detroit.dyndns.org:5000/cgi-bin/setdo.cgi?do=l --output-document=NUL
+
 
find "DI=L" <getdi.cgi
 
find "DI=L" <getdi.cgi
 
echo %errorlevel%
 
echo %errorlevel%
if %errorlevel% EQU 0 wget --user=admin --password=i3 http://i3detroit.dyndns.org:5000/cgi-bin/setdo.cgi?do=h --output-document=NUL
+
if %errorlevel% EQU 0 wget --user=port --password=port http://i3detroit.dyndns.org:5000/cgi-bin/setdo.cgi?do=l --output-document=NUL
 +
rem This turns the LED off when the switch is down.
 +
find "DI=H" <getdi.cgi
 +
echo %errorlevel%
 +
if %errorlevel% EQU 0 wget --user=port --password=port http://i3detroit.dyndns.org:5000/cgi-bin/setdo.cgi?do=h --output-document=NUL
 +
rem This turns the LED on when the switch is up.
 
sleep 1
 
sleep 1
 
goto top
 
goto top
 
</pre>
 
</pre>
  
Camera documentation: [ftp://ftp.dlink.com/Multimedia/URL%20Commands/URL%20command%20for%20DCS-5300_G_,%20DCS-2100_G_%2B,%20DCS-2000,%20DVS-301_2007_06_01.pdf]
+
Updated batch file, this is the one currently running on nate's desktop at home:
 +
<pre>
 +
echo off
 +
rem set currentstate=off
 +
set camerahost=i3detroit.dyndns.org:5000
 +
set camerauser=port
 +
set camerapass=port
 +
set twitteruser=contact@i3detroit.com
 +
set twitterpass=redacted-from-wiki
 +
 
 +
:begin
 +
del getdi.cgi
 +
wget --user=%camerauser% --password=%camerapass% --timeout=5 --tries=2 http://%camerahost%/cgi-bin/getdi.cgi
 +
find "DI=H" <getdi.cgi
 +
echo %errorlevel%
 +
if %errorlevel% EQU 0 goto itson
 +
find "DI=L" <getdi.cgi
 +
echo %errorlevel%
 +
if %errorlevel% EQU 0 goto itsoff
 +
 
 +
:loop
 +
sleep 10
 +
goto begin
 +
 
 +
:itson
 +
echo It's on!
 +
wget --user=%camerauser% --password=%camerapass% --timeout=5 http://%camerahost%/cgi-bin/setdo.cgi?do=h --output-document=NUL
 +
if "%currentstate%" EQU "on" goto loop
 +
set currentstate=on
 +
echo Switch set to ON state, %time% %date% >> log
 +
rem wget --user=%twitteruser% --password=%twitterpass% --post-data="status=The space is OPEN FOR GUESTS as of %time%, please drop in!" http://twitter.com/statuses/update.xml --output-document=NUL
 +
del video.jpg
 +
wget --user=%camerauser% --password=%camerapass% --timeout=5 http://%camerahost%/cgi-bin/video.jpg
 +
curl --form username=%twitteruser% --form password=%twitterpass% --form media=@video.jpg --form-string "message=The space is OPEN FOR GUESTS, and here's who flipped the switch! Drop in?" http://twitpic.com/api/uploadAndPost
 +
goto loop
 +
 
 +
:itsoff
 +
echo It's off!
 +
if "%currentstate%" EQU "off" goto loop
 +
set currentstate=off
 +
echo Switch set to OFF state, %time% %date% >> log
 +
wget --user=%twitteruser% --password=%twitterpass% --post-data="status=The space is closed as of %time% %date%" http://twitter.com/statuses/update.xml --output-document=NUL
 +
wget --user=%camerauser% --password=%camerapass% --timeout=5 http://%camerahost%/cgi-bin/setdo.cgi?do=l --output-document=NUL
 +
goto loop
 +
</pre>
 +
 +
==WelcomeSwitch 1.4==
 +
Replacement perl script. Running on the pfsense box in the space.
 +
<b>(OUTDATED, NEW SCRIPT BELOW)</b>
 +
<pre>
 +
#!/usr/bin/perl -w
 +
 
 +
my($camerauser, $camerapass, $twit_user, $twit_pass) = @ARGV;
 +
 
 +
my $tries = 2;
 +
my $camerahost = 'xx.xx.xx.xx';
 +
my $switchurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/getdi.cgi";
 +
my $pictureurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/video.jpg";
 +
my $twit_url = "http://twitpic.com/api/uploadAndPost";
 +
my $status_filename = 'doorbot_status';
 +
 
 +
my $open_post_text = "message=The space is OPEN FOR GUESTS, and here's who flipped the switch! Drop in?";
 +
my $close_post_text = "The space is closed as of";
 +
 
 +
my $old_switch_state = undef;
 +
 
 +
 
 +
$SIG{'INT'} = \&quit;
 +
$SIG{'QUIT'} = \&quit;
 +
 
 +
sub setOutput
 +
{
 +
        my($value) = @_;
 +
 
 +
        my $url = "http://$camerauser:$camerapass\@$camerahost/cgi-bin/setdo.cgi?do=$value";
 +
        my $result = `wget -q --tries=$tries --output-document=setdo.html $url`;
 +
 
 +
        open(my $fh, '>', $status_filename) or return;
 +
        my $status = ($value eq 'h') ? "open" : "closed";
 +
        print $fh $status;
 +
        close $fh;
 +
}
 +
 
 +
while (1)
 +
{
 +
        $cameraresponse = `curl -s $switchurl`;
 +
        chomp $cameraresponse;
 +
        # print "camera response is: $cameraresponse\n";
 +
 
 +
        $cameraresponse =~ /DI=(.)/;
 +
        $new_switch_state = $1;
 +
 
 +
        if($new_switch_state)
 +
        {
 +
                if(defined $old_switch_state and $new_switch_state eq $old_switch_state)
 +
                {
 +
                        #no change
 +
                        next;
 +
                }
 +
 
 +
                if($new_switch_state eq 'H')
 +
                {
 +
                        print "camera input is high\n";
 +
 
 +
                        #get the picture
 +
                        my $picture_file_name = 'doorpicture.jpg';
 +
                        `curl -s --output $picture_file_name $pictureurl`;
 +
 
 +
                        next if $?;
 +
 
 +
                        #make the twitter post
 +
                        my $twit_post_command = sprintf 'curl -s --form "username=%s" --form "password=%s" --form "media=@%s" --form-string "%s" %s',
 +
                                                        $twit_user, $twit_pass, $picture_file_name, $open_post_text, $twit_url;
 +
                        #print "twit_post_command " . $twit_post_command . "\n" ;
 +
 
 +
                        #print "posting picture to twitter\n";
 +
                        `$twit_post_command`;
 +
                        next if $?;
 +
 
 +
                        unlink($picture_file_name);
 +
 
 +
                        setOutput('h');
 +
                }
 +
                elsif ($new_switch_state eq 'L')
 +
                {
 +
                        print "camera input is low\n";
 +
 
 +
                        my $timestamp = scalar(localtime);
 +
 
 +
                        #make the twitter post
 +
                        my $twit_url = 'http://twitter.com/statuses/update.xml';
 +
                        my $twit_post_command =  qq(wget -q --tries=$tries --user=$twit_user --password=$twit_pass --post-data="status=$close_post_text $timestamp" $twit_url --output-document=/dev/null);
 +
                        #my $twit_post_command =  qq(curl -s --form "username=$twit_user" --form "password=$twit_pass" --form-string "status=$close_post_text $timestamp" $twit_url);
 +
                        `$twit_post_command`;
 +
                        next if $?;
 +
 
 +
                        setOutput('l');
 +
                }
 +
 
 +
                $old_switch_state = $new_switch_state;
 +
        }
 +
        else
 +
        {
 +
                #camera input is unknown
 +
        }
 +
}
 +
continue
 +
{
 +
        sleep 10;
 +
}
 +
 
 +
&quit;
 +
 
 +
 
 +
sub quit
 +
{
 +
        print "doorbot sez adios!\n";
 +
        unlink($status_filename);
 +
        exit;
 +
}
 +
</pre>
 +
 
 +
 
 +
 
 +
==Welcome Switch 1.7 *Current Build*==
 +
Running on the Debian box 10.13.0.15 <br>
 +
Current Build: myselfbot.pl <br>
 +
 
 +
To start script: <br>
 +
"/usr/bin/perl -w myselfbot.pl H &" Open <br>
 +
"/usr/bin/perl -w myselfbot.pl L &" Closed
 +
<br><br>
 +
Current Open status <br>
 +
H = Open<br>
 +
L = Closed<br>
 +
 
 +
<pre>
 +
#!/usr/bin/perl -w
 +
 
 +
 
 +
my($old_switch_state) = @ARGV;
 +
 
 +
#my($camerauser, $camerapass, $twit_user, $twit_pass) = ('port', 'port', 'Test2859258798', 'CONTACT-ADMIN');
 +
my($camerauser, $camerapass, $twit_user, $twit_pass) = ('port', 'port', 'i3detroit', 'CONTACT-ADMIN');
 +
 
 +
my $tries = 2;
 +
my $camerahost = '10.13.0.2';
 +
my $switchurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/getdi.cgi";
 +
my $pictureurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/video.jpg";
 +
my $twit_url = "http://twitpic.com/api/uploadAndPost";
 +
my $status_filename = 'doorbot_status';
 +
 
 +
my $open_post_text = "message=The space is OPEN FOR GUESTS, and here's who flipped the switch! Drop in?";
 +
my $closed_post_text = "message=The space is CLOSED, please check back later!";
 +
 
 +
 
 +
$SIG{'INT'} = \&quit;
 +
$SIG{'QUIT'} = \&quit;
 +
 
 +
sub setOutput
 +
{
 +
my($value) = @_;
 +
       
 +
my $url = "http://$camerauser:$camerapass\@$camerahost/cgi-bin/setdo.cgi?do=$value";
 +
my $result = `wget -q --tries=$tries --output-document=/dev/null $url`;
 +
 +
open(my $fh, '>', $status_filename) or return;
 +
my $status = ($value eq 'h') ? "open" : "closed";
 +
print $fh localtime() . ' ' . $status;
 +
close $fh;
 +
}
 +
 
 +
while (1)
 +
{
 +
$cameraresponse = `curl -s $switchurl`;
 +
chomp $cameraresponse;
 +
# print "camera response is: $cameraresponse\n";
 +
 
 +
$cameraresponse =~ /DI=(.)/;
 +
$new_switch_state = $1;
 +
 +
if($new_switch_state)
 +
{
 +
if(defined $old_switch_state and $new_switch_state eq $old_switch_state)
 +
{
 +
#no change
 +
next;
 +
}
 +
 +
if($new_switch_state eq 'H')
 +
{
 +
print localtime() . " camera input is high\n";
 +
 +
#get the picture
 +
my $picture_file_name = 'doorpicture.jpg';
 +
`curl -s --output $picture_file_name $pictureurl`;
 +
 +
next if $?;
 +
 
 +
#make the twitter post
 +
my $twit_post_command = sprintf 'curl -s --form "username=%s" --form "password=%s" --form "media=@%s" --form-string "%s" %s',
 +
                                $twit_user, $twit_pass, $picture_file_name, $open_post_text, $twit_url;
 +
#print "twit_post_command " . $twit_post_command . "\n" ;
 +
 +
#print "posting picture to twitter\n";
 +
`$twit_post_command`;
 +
next if $?;
 +
 +
unlink($picture_file_name);
 +
 +
setOutput('h');
 +
}
 +
elsif ($new_switch_state eq 'L')
 +
{
 +
print localtime() . " camera input is low\n";
 +
 
 +
my $timestamp = scalar(localtime);
 +
 +
my $picture_file_name2 = '/home/i3/doorbot/closed/riverview-sign-pool-closed-usg.jpg';
 +
#my $picture_file_name2 = '/home/i3/doorbot/closed.jpg';
 +
#make the twitter post
 +
my $twit_post_command2 = sprintf 'curl -s --form "username=%s" --form "password=%s" --form "media=@%s" --form-string "%s" %s',
 +
                                $twit_user, $twit_pass, $picture_file_name2, $closed_post_text, $twit_url;
 +
#print "twit_post_command2 " . $twit_post_command2 . "\n" ;
 +
 +
#print "posting picture to twitter\n";
 +
`$twit_post_command2`;
 +
 +
 
 +
#unlink($picture_file_name2);
 +
 
 +
next if $?;
 +
 +
setOutput('l');
 +
}
 +
 +
$old_switch_state = $new_switch_state;
 +
        sleep 10;
 +
}
 +
else
 +
{
 +
#camera input is unknown
 +
}
 +
}
 +
continue
 +
{
 +
sleep 2;
 +
}
 +
 
 +
&quit;
 +
 
 +
 
 +
sub quit
 +
{
 +
print localtime() . " doorbot sez adios!\n";
 +
unlink($status_filename);
 +
exit;
 +
}
 +
 
 +
</pre>
 +
[[Category:Projects]][[Category:Member Projects]][[Category:Completed Projects]][[Category:Facility Information]]

Revision as of 11:22, 28 March 2013

THIS CODE IS OBSOLETE, NEW SCRIPT IS RUNNING IN PYTHON. PENDING AN UPDATE HERE.

Contents

Wiring

On the camera, there's a hacked-up PoE cable that does the following:

1 wht/org ethernet
2 org/wht ethernet
3 wht/grn ethernet
4 blu/wht GND, also Digital Input -
5 wht/blu Digital Input +
6 grn/wht ethernet
7 wht/brn Relay Output
8 brn/wht +12V, also Relay Input

This allows the camera to be positioned away from the door by running a single cable. Without this, you'd need one for power, one for network, AND one for the I/O pins. Using a single 24ga wire for power is suboptimal, but the camera doesn't draw much power, and it hasn't been an issue yet.

In the box above the door: Ethernet pins pass through to Ethernet input (into which is plugged the feed from the back of the space.) GND from power input goes to GND from camera, also to LED cathode and SSR negative. +12 from power input goes to +12 from camera, also to switch contact. Relay Output from camera goes to LED anode and SSR positive. Digtal Input + from camera goes to switch contact. SSR isn't currently connected; signals are on orange wires, shrinkwrapped to prevent shorts in the time being.

(To accommodate a future local OPEN sign, provision has been made for connecting the big solid-state relay, which can switch anything we could throw at it.)

Software

The most important part of all this! Camera documentation: [1]


Proof-of-concept batch file:

:top
del getdi.cgi
wget --user=port --password=port http://i3detroit.dyndns.org:5000/cgi-bin/getdi.cgi
find "DI=L" <getdi.cgi
echo %errorlevel%
if %errorlevel% EQU 0 wget --user=port --password=port http://i3detroit.dyndns.org:5000/cgi-bin/setdo.cgi?do=l --output-document=NUL
rem This turns the LED off when the switch is down.
find "DI=H" <getdi.cgi
echo %errorlevel%
if %errorlevel% EQU 0 wget --user=port --password=port http://i3detroit.dyndns.org:5000/cgi-bin/setdo.cgi?do=h --output-document=NUL
rem This turns the LED on when the switch is up. 
sleep 1
goto top

Updated batch file, this is the one currently running on nate's desktop at home:

echo off
rem set currentstate=off
set camerahost=i3detroit.dyndns.org:5000
set camerauser=port
set camerapass=port
set twitteruser=contact@i3detroit.com
set twitterpass=redacted-from-wiki

:begin
del getdi.cgi
wget --user=%camerauser% --password=%camerapass% --timeout=5 --tries=2 http://%camerahost%/cgi-bin/getdi.cgi
find "DI=H" <getdi.cgi
echo %errorlevel%
if %errorlevel% EQU 0 goto itson
find "DI=L" <getdi.cgi
echo %errorlevel%
if %errorlevel% EQU 0 goto itsoff

:loop
sleep 10
goto begin

:itson
echo It's on!
wget --user=%camerauser% --password=%camerapass% --timeout=5 http://%camerahost%/cgi-bin/setdo.cgi?do=h --output-document=NUL
if "%currentstate%" EQU "on" goto loop
set currentstate=on
echo Switch set to ON state, %time% %date% >> log
rem wget --user=%twitteruser% --password=%twitterpass% --post-data="status=The space is OPEN FOR GUESTS as of %time%, please drop in!" http://twitter.com/statuses/update.xml --output-document=NUL
del video.jpg
wget --user=%camerauser% --password=%camerapass% --timeout=5 http://%camerahost%/cgi-bin/video.jpg
curl --form username=%twitteruser% --form password=%twitterpass% --form media=@video.jpg --form-string "message=The space is OPEN FOR GUESTS, and here's who flipped the switch! Drop in?" http://twitpic.com/api/uploadAndPost
goto loop

:itsoff
echo It's off!
if "%currentstate%" EQU "off" goto loop
set currentstate=off
echo Switch set to OFF state, %time% %date% >> log
wget --user=%twitteruser% --password=%twitterpass% --post-data="status=The space is closed as of %time% %date%" http://twitter.com/statuses/update.xml --output-document=NUL
wget --user=%camerauser% --password=%camerapass% --timeout=5 http://%camerahost%/cgi-bin/setdo.cgi?do=l --output-document=NUL
goto loop

WelcomeSwitch 1.4

Replacement perl script. Running on the pfsense box in the space. (OUTDATED, NEW SCRIPT BELOW)

#!/usr/bin/perl -w

my($camerauser, $camerapass, $twit_user, $twit_pass) = @ARGV;

my $tries = 2;
my $camerahost = 'xx.xx.xx.xx';
my $switchurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/getdi.cgi";
my $pictureurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/video.jpg";
my $twit_url = "http://twitpic.com/api/uploadAndPost";
my $status_filename = 'doorbot_status';

my $open_post_text = "message=The space is OPEN FOR GUESTS, and here's who flipped the switch! Drop in?";
my $close_post_text = "The space is closed as of";

my $old_switch_state = undef;


$SIG{'INT'} = \&quit;
$SIG{'QUIT'} = \&quit;

sub setOutput
{
        my($value) = @_;

        my $url = "http://$camerauser:$camerapass\@$camerahost/cgi-bin/setdo.cgi?do=$value";
        my $result = `wget -q --tries=$tries --output-document=setdo.html $url`;

        open(my $fh, '>', $status_filename) or return;
        my $status = ($value eq 'h') ? "open" : "closed";
        print $fh $status;
        close $fh;
}

while (1)
{
        $cameraresponse = `curl -s $switchurl`;
        chomp $cameraresponse;
        # print "camera response is: $cameraresponse\n";

        $cameraresponse =~ /DI=(.)/;
        $new_switch_state = $1;

        if($new_switch_state)
        {
                if(defined $old_switch_state and $new_switch_state eq $old_switch_state)
                {
                        #no change
                        next;
                }

                if($new_switch_state eq 'H')
                {
                        print "camera input is high\n";

                        #get the picture
                        my $picture_file_name = 'doorpicture.jpg';
                        `curl -s --output $picture_file_name $pictureurl`;

                        next if $?;

                        #make the twitter post
                        my $twit_post_command = sprintf 'curl -s --form "username=%s" --form "password=%s" --form "media=@%s" --form-string "%s" %s',
                                                         $twit_user, $twit_pass, $picture_file_name, $open_post_text, $twit_url;
                        #print "twit_post_command " . $twit_post_command . "\n" ;

                        #print "posting picture to twitter\n";
                        `$twit_post_command`;
                        next if $?;

                        unlink($picture_file_name);

                        setOutput('h');
                }
                elsif ($new_switch_state eq 'L')
                {
                        print "camera input is low\n";

                        my $timestamp = scalar(localtime);

                        #make the twitter post
                        my $twit_url = 'http://twitter.com/statuses/update.xml';
                        my $twit_post_command =  qq(wget -q --tries=$tries --user=$twit_user --password=$twit_pass --post-data="status=$close_post_text $timestamp" $twit_url --output-document=/dev/null);
                        #my $twit_post_command =  qq(curl -s --form "username=$twit_user" --form "password=$twit_pass" --form-string "status=$close_post_text $timestamp" $twit_url);
                        `$twit_post_command`;
                        next if $?;

                        setOutput('l');
                }

                $old_switch_state = $new_switch_state;
        }
        else
        {
                #camera input is unknown
        }
}
continue
{
        sleep 10;
}

&quit;


sub quit
{
        print "doorbot sez adios!\n";
        unlink($status_filename);
        exit;
}


Welcome Switch 1.7 *Current Build*

Running on the Debian box 10.13.0.15
Current Build: myselfbot.pl

To start script:
"/usr/bin/perl -w myselfbot.pl H &" Open
"/usr/bin/perl -w myselfbot.pl L &" Closed

Current Open status
H = Open
L = Closed

#!/usr/bin/perl -w


my($old_switch_state) = @ARGV;

#my($camerauser, $camerapass, $twit_user, $twit_pass) = ('port', 'port', 'Test2859258798', 'CONTACT-ADMIN');
my($camerauser, $camerapass, $twit_user, $twit_pass) = ('port', 'port', 'i3detroit', 'CONTACT-ADMIN');

my $tries = 2;
my $camerahost = '10.13.0.2';
my $switchurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/getdi.cgi";
my $pictureurl = "http://$camerauser:$camerapass@" . "$camerahost/cgi-bin/video.jpg";
my $twit_url = "http://twitpic.com/api/uploadAndPost";
my $status_filename = 'doorbot_status';

my $open_post_text = "message=The space is OPEN FOR GUESTS, and here's who flipped the switch! Drop in?";
my $closed_post_text = "message=The space is CLOSED, please check back later!";


$SIG{'INT'} = \&quit;
$SIG{'QUIT'} = \&quit;

sub setOutput
{
	my($value) = @_;
        
	my $url = "http://$camerauser:$camerapass\@$camerahost/cgi-bin/setdo.cgi?do=$value";
	my $result = `wget -q --tries=$tries --output-document=/dev/null $url`;
	
	open(my $fh, '>', $status_filename) or return;
	my $status = ($value eq 'h') ? "open" : "closed";
	print $fh localtime() . ' ' . $status;
	close $fh;
}

while (1)
{
	$cameraresponse = `curl -s $switchurl`;
	chomp $cameraresponse;
	# print "camera response is: $cameraresponse\n";

	$cameraresponse =~ /DI=(.)/;
	$new_switch_state = $1;
	
	if($new_switch_state)
	{
		if(defined $old_switch_state and $new_switch_state eq $old_switch_state)
		{
			#no change
			next;
		}
		
		if($new_switch_state eq 'H')
		{
			print localtime() . " camera input is high\n";
			
			#get the picture
			my $picture_file_name = 'doorpicture.jpg';
			`curl -s --output $picture_file_name $pictureurl`;
			
			next if $?;

			#make the twitter post
			my $twit_post_command = sprintf 'curl -s --form "username=%s" --form "password=%s" --form "media=@%s" --form-string "%s" %s',
			                                 $twit_user, $twit_pass, $picture_file_name, $open_post_text, $twit_url;
			#print "twit_post_command " . $twit_post_command . "\n" ;
			
			#print "posting picture to twitter\n";
			`$twit_post_command`;
			next if $?;
			
			unlink($picture_file_name);
			
			setOutput('h');
		}
		elsif ($new_switch_state eq 'L')
		{
			print localtime() . " camera input is low\n";

			my $timestamp = scalar(localtime);
			
			my $picture_file_name2 = '/home/i3/doorbot/closed/riverview-sign-pool-closed-usg.jpg';
			#my $picture_file_name2 = '/home/i3/doorbot/closed.jpg';
			#make the twitter post
			my $twit_post_command2 = sprintf 'curl -s --form "username=%s" --form "password=%s" --form "media=@%s" --form-string "%s" %s',
			                                 $twit_user, $twit_pass, $picture_file_name2, $closed_post_text, $twit_url;
			#print "twit_post_command2 " . $twit_post_command2 . "\n" ;
			
			#print "posting picture to twitter\n";
			`$twit_post_command2`;
			

			#unlink($picture_file_name2);

			next if $?;
			
			setOutput('l');
		}
		
		$old_switch_state = $new_switch_state;
        sleep 10;
	}
	else
	{
		#camera input is unknown
	}
}
continue
{	
	sleep 2;
}

&quit;


sub quit
{
	print localtime() . " doorbot sez adios!\n";
	unlink($status_filename);
	exit;
}