dmt

source code for the kunsthal art installation
git clone git://parazyd.org/dmt.git
Log | Files | Refs | README | LICENSE

freepbx_glue.py (2025B)


      1 #!/usr/bin/env python3
      2 # See LICENSE file for copyright and license details.
      3 """
      4 FreePBX/Asterisk log parser and handler
      5 """
      6 
      7 import sys
      8 import requests
      9 
     10 import globalvars
     11 from config import (CALLER_ID, RECEIVER_ID, STATION_API)
     12 
     13 
     14 def handle_line(logline):
     15     """
     16     Parses a single log line to make an according call
     17     """
     18     if not logline:
     19         return
     20 
     21     parsed = logline.split()
     22     if len(parsed) < 7:
     23         return
     24 
     25     # We wait for a ring.
     26     if not globalvars.weareringing and not globalvars.wehaveanswered:
     27         if parsed[4].split('-')[0] == RECEIVER_ID:
     28             # stackno = parsed[4].split('-')[1]
     29             if parsed[6] == 'ringing':
     30                 globalvars.weareringing = True
     31                 print('We are ringing')
     32                 return
     33 
     34     # When a call is answered, the receiver stackno is +1 in hex than
     35     # the caller's.
     36 
     37     # The phone is ringing.
     38     if globalvars.weareringing:
     39         if parsed[3] == 'app_dial.c:' and parsed[5] == 'answered':
     40             globalvars.weareringing = False
     41             globalvars.wehaveanswered = True
     42             print('We have answered')
     43             resp = requests.get(STATION_API+'/callanswered')
     44             if resp.status_code != 200:
     45                 print('Something went wrong with the API call.')
     46             return
     47 
     48     # The phone has been picked up.
     49     if globalvars.wehaveanswered:
     50         if parsed[3] == 'pbx.c:':
     51             if parsed[6].startswith('Hangup("' + CALLER_ID):
     52                 globalvars.wehaveanswered = False
     53                 print('We hung up.')
     54                 print('---')
     55                 resp = requests.get(STATION_API+'/callended')
     56                 if resp.status_code != 200:
     57                     print('Something went wrong with the API call.')
     58                 return
     59 
     60 
     61 def main():
     62     """
     63     Main routine.
     64     Reads standard input line by line.
     65     """
     66     print('Tailing logfile...')
     67     while True:
     68         line = sys.stdin.readline()
     69         handle_line(line)
     70 
     71 
     72 if __name__ == '__main__':
     73     main()