Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Simplify a ProjectX request using projectx-helper.cgi

This Ruby CGI script is for use as an alternative to having the client prepare a ProjectX XML request against projectx.cgi.

   1  
   2  #!/usr/bin/ruby
   3  # projectx-helper.cgi
   4  
   5  require 'cgi'
   6  
   7  cgi = CGI.new
   8  
   9  h = cgi.params
  10  
  11  puts "Content-Type: text/xml"
  12  puts
  13  
  14  def pparam(h,var)
  15    val = ''
  16    if not h[var].empty? then
  17      val = h[var]
  18      h.delete(var)
  19    end
  20    val
  21  end
  22  
  23  def format_param(var, val)
  24      "#{' '*8}<param var='#{var}'>#{val}</param>\r"
  25  end
  26  
  27  def pparam_remainder(h)
  28    buffer = ''
  29    h.each do |param|
  30      buffer << format_param(param[0],param[1])
  31    end
  32    buffer
  33  end
  34  
  35  xml_project =<<XML_PROJECT
  36  <project name='#{pparam(h,'project')}'>
  37    <methods>
  38      <method name='#{pparam(h,'method')}'>
  39        <params>
  40  #{pparam_remainder(h)}
  41        </params>
  42      </method>
  43    </methods>
  44  </project>
  45  XML_PROJECT
  46  
  47  puts xml_project


example input url: http://yourwebsite.com/p/projectx-helper.cgi?project=abc&method=create&path=mdynarex&xyz=123
example output:
   1  
   2  <project name='abc'>
   3    <methods>
   4      <method name='create'>
   5        <params>
   6          <param var='xyz'>123</param>
   7          <param var='path'>mdynarex</param>
   8        </params>
   9      </method>
  10    </methods>
  11  </project>


CGI Reference: Programming Ruby: The Pragmatic Programmer's Guide [rubycentral.com]

Using Bluetooth to control Asterisk calls.

Redirect an Asterisk call

   1  
   2      de61.old_status = get_status(de61.file) # found is either true or false
   3      de61.old_location = get_location(de61.file) # location is either 'at home', 'gone to bed' or 'asleep'
   4      
   5      while count < 1
   6  
   7        puts 'old status : ' + de61.old_status.to_s
   8        de61.new_status = check_device(de61.id, de61.name)
   9        puts 'new_status: ' + de61.new_status.to_s
  10  
  11  
  12        if de61.new_status and de61.old_status
  13          if de61.old_status == false
  14            puts 'connecting to the bluetooth device ...'
  15            Thread.new{`rfcomm connect 0 #{de61.id}`}
  16            sleep 12
  17          end
  18          de61.new_location = check_location(de61.id) 
  19        end
  20        
  21        puts 'old_location: ' + de61.old_location.to_s
  22        puts 'new_location: ' + de61.new_location.to_s
  23        update_status(de61.project, de61.new_status, de61.new_location) if ((de61.old_status != de61.new_status.to_s) or (de61.old_location != de61.new_location)) 
  24          
  25        mystatus = ''
  26        
  27        if  (not de61.old_status == de61.new_status) then
  28          if (de61.new_status.to_s == 'true') then
  29              mystatus = ' has entered the building'
  30              callto('SIP/line1')
  31          elsif  (de61.new_status == false) then
  32            mystatus = ' has left the building'
  33            callto('SIP/07736668666@sipgate')
  34          end
  35          puts 'updating in or out the building'
  36          ms = MyStatus.new(mystatus)
  37        elsif (de61.old_location.to_s != de61.new_location.to_s)
  38          if de61.old_location.match(/sleep/) and de61.new_location.match(/home|bed/) and not old_mystatus.match(/awoken/) then
  39            mystatus = ' has awoken'
  40          elsif (de61.old_location.match(/home|bed/)) and de61.new_location.match(/sleep/) 
  41            mystatus = ' has gone to sleep'           
  42          elsif (de61.old_location.match(/home/)) and de61.new_location.match(/bed/) 
  43            mystatus = ' has gone to bed'
  44          elsif de61.old_location.match(/bed/) and de61.new_location.match(/home/) 
  45            mystatus = ' has got out of bed'
  46          end
  47          puts 'updating home location'
  48          ms = MyStatus.new(mystatus) if mystatus != ''
  49        end
  50  
  51  
  52        de61.old_location = de61.new_location
  53        de61.new_location = 'at home'
  54        de61.old_status = de61.new_status
  55        old_mystatus = mystatus
  56        sleep 7
  57      end
  58    end
  59      
  60    def callto(dialstring)
  61      file = File.new('/etc/asterisk/ruby/call-to.txt','w')
  62      file.puts 'Dial(' + dialstring + ')'
  63      file.close
  64    end


file: extensions.conf
   1  
   2  exten => 5168666,n,ReadFile(dialplan1=/etc/asterisk/ruby/call-to.txt,130)
   3  exten => 5168666,n,Exec(${dialplan1})
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS