Page 309 - Asterisk™: The Future of Telephony
P. 309

If the pin entered matches, we validate the login with the valid_login extension. First
               we utilize the CHANNEL variable to figure out which phone device we’re calling from. The
               CHANNEL variable is usually populated with something such as: SIP/desk_1-ab4034c, so
               we make use of the CUT() function to first pull off the SIP/ portion of the string and
               assign that to LOCATION. We then strip off the -ab4034c part of the string, discard it, and
               assign the remainder of desk_1 to the LOCATION variable.

                   exten => valid_login,1,NoOp()
                   ; CUT off the channel technology and assign to the LOCATION variable
                   exten => valid_login,n,Set(LOCATION=${CUT(CHANNEL,/,2)})
                   ; CUT off the unique identifier and save the remainder to the LOCATION variable
                   exten => valid_login,n,Set(LOCATION=${CUT(LOCATION,-,1)})
               We  utilize  yet  another  custom  function,  HOTDESK_CHECK_PHONE_LOGINS(),  created  in
               func_odbc.conf to check if any other users were previously logged in to this phone and
               had forgotten to log out. If the number of previously logged in users was greater than
               0 (and should only ever be 1, but we check for more anyway and reset those, too), it
               runs the logic in the logout_login extension.
               If no previous agents were logged in, we update the login status for this user with the
               HOTDESK_STATUS() function:

                   exten => valid_login,n,Set(ARRAY(USERS_LOGGED_IN)=${HOTDESK_CHECK_PHONE_
                   LOGINS(${LOCATION})})
                   exten => valid_login,n,GotoIf($[${USERS_LOGGED_IN} > 0]?logout_login,1)
                   exten => valid_login,n(set_login_status),NoOp()

                   ; Set the status for the phone to '1' and where we're logged into
                   ; NOTE: we need to escape the comma here because the Set() application has arguments
                   exten => valid_login,n,Set(HOTDESK_STATUS(${E})=1\,${LOCATION})
                   exten => valid_login,n,GotoIf($[${ODBCROWS} < 1]?error,1)
                   exten => valid_login,n,Playback(agent-loginok)
                   exten => valid_login,n,Hangup()
               We create a write function in func_odbc.conf like so:
                   [STATUS]
                   prefix=HOTDESK
                   dsn=asterisk
                   write=UPDATE ast_hotdesk SET status = '${VAL1}', location = '${VAL2}' WHERE extension
                   = '${ARG1}'
               The syntax is very similar to the read syntax discussed earlier in the chapter, but there
               are a few new things here, so let’s discuss them before moving on.
               The first thing you may have noticed is that we now have both ${VALx} and ${ARGx}
               variables in our SQL statement. These contain the values we pass to the function from
               the dialplan. In this case, we have two VAL variables, and a single ARG variable that were
               set from the dialplan via this statement:
                   Set(HOTDESK_STATUS(${E})=1\,${LOCATION})





                                                        Getting Funky with func_odbc: Hot-Desking | 281
   304   305   306   307   308   309   310   311   312   313   314