Log in

View Full Version : Enable/Disable buttons at certain times of day



NairB
05-31-2008, 04:01 AM
Folks,

I have an unusual request.

I am trying to figure out how I can disable then enable form buttons automatically using some kind of clock depending on the time of day.

To let you understand why I need this, I am running a pan/tilt webcam and I do not want the remote viewers to "control" the cam at certain times of the day/night pointing it into direct sunlight etc ruining my cam. I am currently having to manually change the button script which I forget to do sometimes. So can this be done automatically using some kind of clock?

I have "UP", "DOWN", "CENTER", "LEFT", "RIGHT" buttons on a file called index.html as seen on the webcam page CLICK HERE (http://82.11.154.234:1760/index.html)

So for example at 06:00am, the clock would disable the "UP" button then at midday the clock would enable it again automatically using some kind of clock script.

Any ideas would be great.

thanks peeps.

-NairB :)

jscheuer1
05-31-2008, 08:02 AM
As it's your camera at risk here, this would probably best be handled on the server side, like if you have PHP or asp available. That way you could even serve different pages at different times. You would want there to be no code on the page at all for moving the camera into danger at those times when it could be at risk. Because, even if a button is disabled in javascript, a clever person could look at the source code and activate the function anyway.

An even better solution would be to not have the camera situated like that. This is because, no matter what you do, a user may be able to take control of the camera via a cached page.

NairB
05-31-2008, 04:00 PM
Hi John, thanks for your reply.

I agree with your concern and I too would like to see this page protected via php but unfortunately it is written by Roborealm and I don't have control over this.

I will post this on Roborealm's forum requesting better security with the possibility of a php version of the index file.

For the time being, to stop me manually changing the buttons, a little java script of some kind was what I was looking for to maybe add the word "disabled" then remove it at a certain time as seen from this line within the code.....

<input disabled type="button" value="UP" onclick="setVariable('move=1')">

Any takers ;)

Thanks again,

NairB

jscheuer1
05-31-2008, 04:40 PM
Think about this a little more. What happens when a user leaves the page? I got the impression that the camera remains where it was left. If so, even with this disabled at times, the camera could inadvertently get left at an angle that would later burn it out.

I'm also curious (but this isn't too important here), what happens when two users try to control the camera at once.

Have you lost any cameras yet?

Getting back to using javascript for this, there is no reliable way, unless we can at least get the time from the server.

A very unreliable method that depends upon well intentioned users with accurate clocks, and upon a camera that cannot be left in a bad spot as mentioned above would be:


<input id="up_button" type="button" value="UP" onclick="setVariable('move=1')">
<script type="text/javascript" defer="defer">
<!--
var UTC_hours = new Date().getUTCHours();
if(UTC_hours > 5 && UTC_hours < 17)
document.getElementById('up_button').disabled = true;
// -->
</script>

That will kill the button for a twelve hour period, from 5am to 5pm GMT. You may change the range to suit, but remember - at different times of the year, things will be different - so make the range as broad as possible. UTC is the same as GMT, both skip changes in DST, and for user's with accurate clocks, it will be the same value regardless of their time zone. You must determine the difference between UTC time and your locale, and add/subtract the difference between your zone and UTC here (IMPORTANT):


var UTC_hours = new Date().getUTCHours() - 5;

with full awareness that if DST is used in your area, that will throw things off a little at those times, so the range should be padded by an extra hour in one direction or the other. Also sunrise and sunset varies, you will want to cover all times in the year that the camera could possibly be in jeopardy.

Maybe you should just rent a room on the other side of the street. :)

NairB
05-31-2008, 09:48 PM
Think about this a little more. What happens when a user leaves the page? I got the impression that the camera remains where it was left. If so, even with this disabled at times, the camera could inadvertently get left at an angle that would later burn it out.
Many, many thanks John for the code but let me answer your questions. The camera server is on a my PC at home and I am often away, hence I cannot always "manually" change the settings. However, I have my notebook with me and like everyone else, I remotely check the cam.

Yes, the camera will remain where it is by the remote user but No, the camera will not burn out because remotely all I need to do is click the remaining enabled buttons before the sun comes up, moving the cam out of danger. The code will disable the "up" and "center" buttons only automatically, so no one, including me can move the cam back into danger. All I need to do is click the "down" button to ensure the camera will not be stuck in an upwards position looking at the sun to burn it out. It will stay in this "down" postion until the code enables the "up" and "center" buttons again when the sun is out of the cams path and users can have full control of the cam again. Of course, this depends on me checking last thing in the evening the cam is out of danger, which I do anyway to make sure the server hasn't crashed.

I need the code because I am often not at home to "manually" change the script file on the server but now I have the code, all I need to do is click the remaining enabled "down" button remotely to protect the cam and no need to access the actual script file on the server......can you see what I mean now John.


I'm also curious (but this isn't too important here), what happens when two users try to control the camera at once.
Have you lost any cameras yet?
Well to answer this simply, both users have to fight it out who controls the cam lol....no preference I'm afraid and No, I haven't lost any cams but I do know that the CCD sensors will degrade if facing direct sunlight all the time.

Hope that answers a few questions John.

ahahahaha do you know any code that can get me a flat across the road, if you do, I will have that instead LMAO!!! :D
I will try the code when I get home to see if it works and let you know how I get on ;)

Again many many thanks John for your help and I hope you understand what I am trying to do now.

-NairB :)

NairB
05-31-2008, 10:27 PM
John,

I tried the code and it hasn't disabled the button....have I put the code in the correct position on the page?

-NairB

jscheuer1
05-31-2008, 11:56 PM
The code looks good on your page, and I'm not getting any script errors. I'm not sure what time zone you are in though. The value of -5 here:


var UTC_hours = new Date().getUTCHours() - 5;

would approximately be good for the East coast of the USA (where I am - Eastern Standard Time and Eastern Daylight Savings Time - depending upon the time of year). If you are in a different timezone (I think you probably are - it's still a little light here - looks completely dark on your page), you would need to use the offset for your timezone. What timezone ARE you in? I just tested the code, works here. I'll check again when it's light (the only time the UP button should be disabled) to see if it is working or not.

Also, since you only asked about the UP button, I only wrote for the UP button. The CENTER button would be unaffected, unless it were given its own unique id and included in the script code.

I basically understood what you were trying to do from the very start. Why was never that crucial to me in this case, but I got most of that too.

I would caution you again though, this will only be good for users with accurate settings for their computer's clock. And only if all of the math for your timezone is worked out correctly. And only if the user doesn't decide to mess with your camera. If they decide that, and they know anything about time and javascript, your camera could well be toast anyway.

NairB
06-01-2008, 03:41 AM
Hi John,

Im in the UK, so my GMT time zone is '0'. I removed the -5 from the code presuming it defaults to 0....still no change.

Hmmm, I also tried it with -0 in case it needed a value....no joy.

I then tried the code on a blank html page with no other code present on my notebook PC and it still wont work, the button is still "clickable"...strange. Is it not reading my clock on my PC perhaps??

NairB

jscheuer1
06-01-2008, 03:51 AM
Well, right now UTC_hours is 3, so the UP button should still be enabled. Wait two hours. Also, as I mentioned before, the page can be cached, so make sure you are looking at it with the most recent code before judging whether or not it works.

NairB
06-01-2008, 04:03 AM
John,

I removed the -5 thinking that because I am GMT zero, it should work. Im unsure now what my code should read.


<input id="up_button" type="button" value="UP" onclick="setVariable('move=1')">
<script type="text/javascript" defer="defer">
<!--
var UTC_hours = new Date().getUTCHours();
if(UTC_hours > 5 && UTC_hours < 17)
document.getElementById('up_button').disabled = true;
// -->
</script>

Should I leave it without the -5?

It is now gone 5am here in UK, should my button be disabled now??

NairB

jscheuer1
06-01-2008, 04:11 AM
We still need to include the CENTER button, and I just realized that we should do this not just on page load, but every minute or so. And, since it looks like it is already getting light there, I changed the values a little:


<tr>
<td colspan=2 align=center>

<input id="up_button" type="button" value="UP" onclick="setVariable('move=1')">


</td>
</tr>
<tr>
<td align=center>
<input type="button" value="LEFT" onclick="setVariable('move=4')">
<input id="center_button" type="button" value="CENTER" onclick="setVariable('move=16')">
<script type="text/javascript" defer="defer">
<!--
var enableDisable = function(){
var UTC_hours = new Date().getUTCHours();
if(UTC_hours >= 3 && UTC_hours < 17)
document.getElementById('up_button').disabled =
document.getElementById('center_button').disabled = true;
else
document.getElementById('up_button').disabled =
document.getElementById('center_button').disabled = false;
}
setInterval(enableDisable, 1000*60);
enableDisable();
// -->
</script>
<input type="button" value="RIGHT" onclick="setVariable('move=8')">
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="button" value="DOWN" onclick="setVariable('move=2')">
</td>
</tr>

NairB
06-01-2008, 04:34 AM
SUCCESS, I got it working. Whoopeee. :D

John, I added +1 to the code and it works for my desired time to "disable" 5am.

EDIT - I also added an '=' sign after the 5 as I seen this in your new code!!!!

I have set it to "enable" again at 10am when the sun is further west out of view.

Here is what your code looks like john that works for me...


<input id="up_button" type="button" value="UP" onclick="setVariable('move=1')">
<script type="text/javascript" defer="defer">
<!--
var UTC_hours = new Date().getUTCHours() + 1;
if(UTC_hours >= 5 && UTC_hours < 10)
document.getElementById('up_button').disabled = true;
// -->
</script>

Also, I have manipulated the code to do the "CENTER" button too and that works also...yipeee....


<input id="center_button" type="button" value="CENTER" onclick="setVariable('move=1')">
<script type="text/javascript" defer="defer">
<!--
var UTC_hours = new Date().getUTCHours() + 1;
if(UTC_hours >= 5 && UTC_hours < 10)
document.getElementById('center_button').disabled = true;
// -->
</script>

So at 5am both "UP" & "CENTER" buttons will disable, then at 10am both enable.....great.

I adjusted my PC clock to check both works.

I'm very happy with this....thanks very much John :D

A very happy NairB :D

NairB
06-01-2008, 04:57 AM
Oh I just noticed your new code above my last post....we must have posted at the same time lol.

I will try your new code to see how that works.

EDIT - GREAT I am now using the new code John and it works just fine.

Many thanks again friend.

NairB

NairB
06-04-2008, 02:25 AM
Hi John,

I thought I would update you. I was worried that even although the disable code will work locally, that someone in another time zone could still have full control of my cam in another timezone, as you warned.....so I got thinking...:confused:

I have cam up with another method to protect my cam. The biggest threat is first thing in the morning as the sun rises to the right of the cam above the church. So I have written code where as the remote viewer clicks "right", the cam will also move "down" after 5 seconds. Each time "right" is clicked, the cam will move "down" automatically taking the cam out of direct sunlight danger.


<tr>

<td colspan=2 align=center>

<script language="JavaScript">
<!-- hide

function timer() {
setTimeout("setVariable('move=2')", 5000);
}

// -->
</script>

<input disabled type="button" value="UP" onclick="setVariable('move=1')">
</td>
</tr>

<tr>
<td align=center>
<input type="button" value="LEFT" onclick="setVariable('move=4')">
<input type="button" value="CENTER" onclick="setVariable('move=16')">
<input type="button" value="RIGHT" onclick="timer(setVariable('move=8'))">
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="button" value="DOWN" onclick="setVariable('move=2')">
</td>
</tr>

See for yourself....its probably the best way to do this although I can never use the " UP" button. At least my remote viewers can still see the sky to the left of center as some want to "weather watch" lol.....a crazy bunch they are lol. :D

Thanks again,

NairB

jscheuer1
06-04-2008, 07:06 AM
I'm all for anything you can do to protect that camera. As I said before about the code I wrote, it will work as long as the user's clock is accurate. What I didn't spell out though is that since it converts their time to UTC time - it doesn't matter what timezone they are in, as long as they are all well intentioned.

Even with your updated code, a user may enter commands directly into the browser's address bar, and thereby do whatever they like with the camera.

NairB
06-04-2008, 05:53 PM
I'm all for anything you can do to protect that camera. As I said before about the code I wrote, it will work as long as the user's clock is accurate. What I didn't spell out though is that since it converts their time to UTC time - it doesn't matter what timezone they are in, as long as they are all well intentioned.

Even with your updated code, a user may enter commands directly into the browser's address bar, and thereby do whatever they like with the camera.

Oh so even if folk in the east coast of US look at my cam here in UK, the buttons will be "disabled"?? If so I will use both methods....its better than none John don't you agree.

I imagine most folks in the world have fairly accurate clocks John, whether they have good intentions is another matter.

I will put your code back on with the timer code I added and re-enable the "UP" button again. ;)

-NairB