simple dial plan with various options

Catalog of dial plans
gbonnet
Site Admin
Posts: 680
Joined: Wed Jul 11, 2007 2:58 pm
Location: Bologna
Contact:

Post by gbonnet » Tue Sep 09, 2008 9:43 pm

You're a Ruby guru ! Thanks for your explanations.

I don't know what I messed up last time :roll:
Blueface [url=http://www.blueface.ie/]Phone[/url] Service

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Wed Sep 10, 2008 4:32 am

I wrote my first line in Ruby about 2 months ago, so I'm not qualified for a guru.
I don't know what I messed up last time
Probably it was a bad choice of words on my side. Your "t.hour + offset" approach is correct and works, but only while time of day is the only thing you need to know in order to make a decision ("to ring or not to ring", for example) :-) Once you need to know the day of week (of month, etc) as well, you should adjust TIME object to local time before calculation of the day.

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Wed Sep 10, 2008 7:26 am

In addition: if you need to be more precise (say, accept calls from 11:23 to 23:20), here is your lines:

Code: Select all

if (1123..2320) === (t.hour*100 + t.min)
 ... process incoming call ...
end

gbonnet
Site Admin
Posts: 680
Joined: Wed Jul 11, 2007 2:58 pm
Location: Bologna
Contact:

Post by gbonnet » Wed Sep 10, 2008 8:02 am

MikeTelis wrote:I wrote my first line in Ruby about 2 months ago, so I'm not qualified for a guru.
impressive then !

Thanks again
Blueface [url=http://www.blueface.ie/]Phone[/url] Service

dalrun
Posts: 7
Joined: Fri Aug 29, 2008 10:26 pm
Contact:

Post by dalrun » Wed Sep 10, 2008 6:15 pm

This thread has morphed a bit. I had tired the original (days don't matter to me) with Mike's suggested format. IIRC, I used the following and voicemail started kicking in at 16:00 (00:00 GMT) because 0 - 8 = -8.

Code: Select all

        t = Time.now
	localtime  = t.hour - 8 # GMT-8 
	if (9..22) === localtime # 9am-10pm
		sys.Dial("#{sys.Username}@local",15) if sys.IsAvailable() 
	end
	sys.Respond(480, "Not available") 

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Wed Sep 10, 2008 7:32 pm

dalrun,

yep, that's right! We overlooked this thing... :(

Anyway, my recent suggestion should work fine. Try this:

Code: Select all

t = Time.now - (8*60*60) # current local time GMT-8
sys.Log(t.strftime('Local time: %c')) # make sure our calculations work!
sys.Dial("#{sys.Username}@local",15) if (9..22) === t.hour && sys.IsAvailable()
sys.Respond(480, "Not available")

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Wed Sep 10, 2008 8:04 pm

I decided to dig a bit deeper and check how Time strftime method is implemented in IronRuby. Here is a simple program I wrote for this occasion:

Code: Select all

#Ruby

def prt(time,chr)
  s = time.strftime("%#{chr}")
  sys.Log("%#{chr}: <#{s}>") unless s.empty? || s == chr
end

t = Time.now

sys.Log('========= strftime format directives ===========')
20.upto(127) {|x| prt(t,x.chr)}
It produced the following:

Code: Select all

DialPlan 20:54:39:622: ========= strftime format directives ===========
DialPlan 20:54:39:669: %A: <Wednesday>
DialPlan 20:54:39:669: %B: <September>
DialPlan 20:54:39:669: %H: <20>
DialPlan 20:54:39:669: %I: <08>
DialPlan 20:54:39:669: %M: <54>
DialPlan 20:54:39:669: %S: <39>
DialPlan 20:54:39:669: %U: <36>
DialPlan 20:54:39:669: %W: <36>
DialPlan 20:54:39:669: %X: <20:54>
DialPlan 20:54:39:669: %Y: <2008>
DialPlan 20:54:39:669: %Z: <+01:00>
DialPlan 20:54:39:669: %a: <Wed>
DialPlan 20:54:39:669: %b: <Sep>
DialPlan 20:54:39:669: %c: <10/09/2008 20:54>
DialPlan 20:54:39:669: %d: <10>
DialPlan 20:54:39:669: %j: <254>
DialPlan 20:54:39:669: %m: <09>
DialPlan 20:54:39:669: %w: <3>
DialPlan 20:54:39:669: %x: <10/09/2008>
DialPlan 20:54:39:669: %y: <08>
As you can see, the most important things are already implemented. One that's missing is %p (AM/PM) but it's not very significant. Time.isdst method also works :)

teddy_b
Posts: 65
Joined: Fri Aug 15, 2008 3:56 am

Post by teddy_b » Wed Sep 10, 2008 8:28 pm

Another thing...

I think the above calculations work only because MSS servers are coincidentally are in GMT time zone. To make it independent of the server's time zone, it could be changed like this:

Code: Select all

offset = -5  # My timezone offset - GMT-5
t = Time.now.utc + (offset * 3600)  # adjusting GMT to local time
t = t + (1 * 3600) if Time.now.localtime.isdst # add 1 hr if DST is in effect
sys.Log(t.strftime('Local time: %c'))
Unfortunately this is not perfect either, and can cause 1 hr discrepancy if DST rules in server's time zone are different from local time zone rules. This is because it can only determine if DST is in effect in the server's time zone. But the discrepancy only occurs during the short period when DST is in effect in one zone and not in effect in another zone. If you're in Europe where the DST rules are the same across all zones, this should cause no issues for you.

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Thu Sep 11, 2008 5:56 am

Unfortunately this is not perfect either...
Exactly! I think that a perfect solution would be in getting Time.local to work with user's time zone instead of local computer's time zone. Each MSS user would have associated time zone settings and Time.local would take these settings and use them as a base for local time calculations.

In the meantime, we need to stick to some temporary workarounds. I prefer to hardcode offset rather than calculate it each time. Yes, I rely on fact that MSS servers are in UTC, but so what? :wink:

User avatar
TheFug
Posts: 914
Joined: Sat Oct 06, 2007 8:23 am
Location: The Netherlands, North-Holland

Post by TheFug » Thu Apr 23, 2009 4:49 pm

dalrun wrote:This thread has morphed a bit. I had tired the original (days don't matter to me) with Mike's suggested format. IIRC, I used the following and voicemail started kicking in at 16:00 (00:00 GMT) because 0 - 8 = -8.

Code: Select all

        t = Time.now
	localtime  = t.hour - 8 # GMT-8 
	if (9..22) === localtime # 9am-10pm
		sys.Dial("#{sys.Username}@local",15) if sys.IsAvailable() 
	end
	sys.Respond(480, "Not available") 
You only have to watch out for one thing, the end time in (14..17),
means that the time period is from 14:00 to 17:59, because the hour digit changes one minute before it becomes 18:00 hours.
I 've found an somewhat cleaner version, with the help of jvwelzen,

Code: Select all

t = Time.now + (1*60*60)  # Get current time and adjust to local time GMT+1 no DST needed
 sys.Log(t.strftime('local time time: %c'))
 tijd = t.strftime('%H:%M')
sys.Log("Tijd : #{tijd}")
 if (1..5) === t.wday && ('06:35'..'17:15') === tijd # next lines will be executed when this condition is valid
sys.Dial("900316xxxxxxxx@TPAD" ,25) 
You should not cross the midnight hour, with this method, you have to split that up, or find a routine to do that, jvwelzen has found an solution for that.
Thanks, The Fug.

gear: my ISP's Zyxel Modem/Router in bridge, Sitecom WL309 Router, Siemens Gigaset 301D

Post Reply