81.2. Example of I/O management via LUA Script

The Abilis permits to pilot Output digital line also making phone calls. For that we use a LUA script joined to DISA resource that allows to open Gate#1 connected to Digital Output 1 verifying the calling number.

[Note]Note

This advanced configuration normally is realized by Second Level Abilis support. Bellow are reported only the resource configurations and a short logical description.

  1. The Abilis verify to receive a call to the called number 0213456789 from the calling numbers recognized in list CallingOK, in this case is sent a call to the DISA Service with called Number 9x. For example, if the called number is 0213456781 the output called number from CTIR routing will be 91 and will use Digital Output 1, if called number will be 0213456782 the Digital Output 2 will be used and so on. Last digit of the called number and correspond with Digital Output number.

    [13:47:55] ABILIS_CPX: d ctir pr:3
    
    Last change: 18/08/2021 13:53:09
    
    ---+------+-----------------+---------+--------------------+--------------------
    PR |[DESCR]
       |BCI   |POI |SR      |GI |OUT      |CDI                 |CDO
    ACT|NEXT        |LAST       |EEC |T301|CGI                 |CGO
    EDT|SP    |SC   |DJ   |MJ   |FMDJ|FMMJ|SDI                 |SDO
       |SPOUT |SCOUT|DJOUT|MJOUT|LCS |LCST|SGI                 |SGO
       |                        |BCO      |RGI                 |RGO
       |FMRLY |FAXSP|MODSP|FMLVL|ECM |UDT |IG  |OG  |SG        |DL  |DH
       |CODERS
       |CODERSOUT
       |TI1 .. TI5
    --------------------------------------------------------------------------------
    3   VOICE  PBX  *        G0  Disa      021345678?           9?                  
        NO           ANY         NO   Dft  'CallingOK'          *                   
        64000  Sys   Sys   Sys   Sys  Sys  *                    *                   
    --------------------------------------------------------------------------------
    
    [13:47:55] ABILIS_CPX:d list:callingok 
    
    LIST:CallingOK            - IN                - Ref-Numb:1     Items-Numb:3     
         --------------------------------------------------------------------------
         3331233333              3342323233              3353435253              
    
    
  2. The DISA resource, configured like below, receive the call with called number 9x. The DISA service:entryphone refuse the call but recognized the number that is motorized by LUA Script.

    [13:47:16] ABILIS_CPX: d p ctidisad ctidisa service
    
    RES:CtiDisa -------------------------------------------------------------------
    Run    DESCR:Direct_Inward_System_Access
           LOG:NO          ACT:YES        sesnum:10       RESTRICTED-CALLING:NO
           PROVIDE-SG:NO   CALL-ABORT:**  CALL-REPEAT:**  PIN-START:*  PIN-END:#
           NPOO-CT:SYS     CTIP-TYPE:NET-PRIVATE          BUSY-NOCHAN:YES
           WDIR:C:\APP\DISA\
    
    [13:47:19] ABILIS_CPX: d ctidisa user
    
    Total number of users:1
    
    -------------------------------------------------------------------------------
    USER:Anonymous
      PIN:     #                |MAX-CALLS: NOMAX |PERMIT: SERVICE
      SERVICE: AUTO             |CB:        NO    |DENY:   #
      CGI:   -
    -------------------------------------------------------------------------------
    
    [13:47:22] ABILIS_CPX: d ctidisa service:entryphone
    
    Total number of services:2
    
    -------------------------------------------------------------------------------
    SERVICE:entryphone        DESCR:
      MODE:        APP                    |INTRO-STATE:   CREQ  |INTRO:
      MAX-CALLS:   NOMAX                  |CONN-DELAY:    0     |WELCOME:
      BC:          Speech                 |DIAL-TOUT:     30    |INITDIAL:
      CB-CGO:      #                                            |BYE:      #
      CDI:         9*
      CGI:         #
    -------------------------------------------------------------------------------
    
  3. To open GATE#1 connected to D-OUT:1 Mfio is configured to generate a Pulse of 250 ms.

    [Note]Note

    This is only a protective measure since this pulse time is also indicated in the script "local PULSEDURATION = 0.25".

    [13:46:44] ABILIS_CPX: d p mfio
    
    RES:MfIo ----------------------------------------------------------------------
    Run    DESCR:Abilis_Multi_Function_IO_Interface
           LOG:NO        ACT:YES
           -------+-------------------- Digital Input lines -----------------------
           D-IN:  | [DESCR:]
           -------+----------------------------------------------------------------
           -------+-------------------- Digital Output lines ----------------------
           D-OUT: | [DESCR:]
                  | MODE:    PULSE-T:    PULSE-GAP:
    -------+----------------------------------------------------------------
           1      | GATE#1
                  | PULSE    250          1000
           2      | GATE#2
                  | PULSE    250          1000
           3      | PULSE    90000        1000
           ------------------------------------------------------------------------   
  4. The script ephone-cd.lua must be uploaded using Z-modem or FTP, or the web interface, and activated at startup.

    [Tip]Tip

    To manage the Lua script by web interface follows the chapter Section 72.23, “LUA scripts”.

    [Tip]Tip

    The script ephone-cd.lua can also be imported from the script examples. Please see the chapter: Section 72.23, “LUA scripts”. GPIORESTYPE and GPIORESINST must be checked before starting it.

    [13:46:52] ABILIS_CPX: d p script
    
    RES:Script --------------------------------------------------------------------
    Run    DESCR:Scripting_service
           LOG:NO        ACT:YES    bg-sesnum:4    fg-sesnum:4
           wdir:C:\APP\SCRIPT\
           - Background scripts ---------------------------------------------------
           id:0  startup:YES  file:ephone-cd.lua
           id:1  startup:NO   file:
           id:2  startup:NO   file:
           id:3  startup:NO   file:
    
  5. The script ephone-cd.lua verify the last digit of the called number and pilot the corresponding Digital Output. In this example the called number is 91 then the driven Digital Output is D-OUT: 1. LUA Script ephone-cd.lua:

    ------------------------------------------------------------
    --- ephone-cd.lua Script
    --
    -- This script waits for incoming call and makes a 250 ms
    -- pulse on the selected digital output line depending on
    -- the called number.
    --
    -- @author Tomas Klinkovsky <tklink@antek.it>
    ------------------------------------------------------------
    
    local abilis = require "abilis"
    
    ------------------------------------------------------------
    
    -- call state
    local CLOSED = 0
    local CALLING = 1
    local ALERTING = 2
    local ACTIVE = 3
    local CLOSING = 4
    local LISTENING = 5
    -- play state
    --local SILENT = 0,
    --local PLAYING = 1,
    
    ------------------------------------------------------------
    
    -- Name of this script, show in log
    local SCRIPTNAME = "ephone-cd.lua"
    -- Name of DISA/APP service
    local DISASERVICE = "entryphone"
    -- GPIO resource type
    local GPIORESTYPE = "MFIO"
    -- GPIO resource instance
    local GPIORESINST = 1
    -- GPIO pulse duration
    local PULSEDURATION = 0.25
    -- Poll period in seconds
    local POLLPERIOD = 0.5
    
    ------------------------------------------------------------
    --- Display log message
    -- The message is preceded with an automatically generated
    -- timestamp.
    -- @parammsg Message to be logged
    --
    local function log(msg)
    	local dt = os.date("%Y-%m-%d %X")
    	local sep = " "
    	print(dt .. sep .. msg)
    end
    
    ------------------------------------------------------------
    --- Handle of selected GPIO device
    --
    local GPIOHANDLE = abilis.gio.get_device_handle(GPIORESTYPE, GPIORESINST)
    
    ------------------------------------------------------------
    --- Generate 250 ms pulse on selected GPIO line
    -- @param line Digital output line number
    --
    local function pulse_line(line)
    	abilis.gio.set_digital_output(GPIOHANDLE, line, 1)
    	abilis.cpx.sleep(PULSEDURATION)
    	abilis.gio.set_digital_output(GPIOHANDLE, line, 0)
    end
    
    ------------------------------------------------------------
    --- Wait for incoming call in DISA/APP
    -- @param handle DISA/APP handle
    --
    local function wait_for_incoming_call(handle)
    	assert(handle > 0)
    
    	while true do
    		local state = abilis.disa.get_state(handle)
    		if state == CLOSED then
    			return false
    		elseif state ~= LISTENING then
    			return true
    		else
    			abilis.cpx.sleep(POLLPERIOD)
    		end
    	end
    end
    
    ------------------------------------------------------------
    --- Map last digit of called number to GPIO line
    --
    local MAP = {
    	["1"] = 1,
    	["2"] = 2,
    	["3"] = 3,
    	["4"] = 4,
    	["5"] = 5,
    	["6"] = 6,
    	["7"] = 7,
    	["8"] = 8,
    }
    
    ------------------------------------------------------------
    --- Convert called number to GPIO line (or nil).
    -- @param number Called phone number
    -- @return GPIO line number
    --
    local function get_line_for_number(number)
    	if number ~= nil then
    		local last_digit = number:sub(-1)
    		return MAP[last_digit]
    	else
    		return nil
    	end
    end
    
    ------------------------------------------------------------
    --- Convert value to string.
    -- Nil is converted to empty string, other values are
    -- returned unchanged.
    -- @param x String or nil
    -- @return String
    local function tostr(x)
    	if x ~= nil then
    		return x
    	else
    		return ""
    	end
    end
    
    ------------------------------------------------------------
    --- Handle incoming call.
    -- Wait for incoming call, obtain called number, find the
    -- corresponding GPIO line, and generate pulse there.
    -- @param handle DISA/APP handle
    --
    local function handle_incoming_call(handle)
    	assert(handle > 0)
    
    	if wait_for_incoming_call(handle) then
    		local cg, cd = abilis.disa.get_info(handle)
    		log("incoming call CG:" .. tostr(cg) .. " CD:" .. tostr(cd))
    		local line = get_line_for_number(cd)
    		if line ~= nil then
    			log("generating pulse on line " .. line)
    			pulse_line(line)
    		else
    	 	log("unsupported called number")
    		end
    	end
    	log("closing call")
    	abilis.disa.close(handle)
    end
    
    ------------------------------------------------------------
    --- Handle incoming calls.
    -- Listen on selected DISA/APP service, and process
    -- incoming call. And again, forever.
    --
    local function handle_incoming_calls()
    	while true do
    		log("waiting for incoming call")
    		local handle = abilis.disa.listen(DISASERVICE)
    		if handle ~= nil then
    			handle_incoming_call(handle)
    		else
    			log("listen function failed")
    			break
    		end
    	end
    end
    
    ------------------------------------------------------------
    --- Create session object, and handle incoming calls from
    -- DISA/APP.
    --
    local function main()
    	log(SCRIPTNAME .. " script started")
    	handle_incoming_calls()
    	log(SCRIPTNAME .. " script ended")
    end
    
    -- Execute the main function
    main()
    
    
[Note]Note

This script can also be used for an GPIO device, it's necessary to modify only this line "local GPIORESTYPE = "GPIO"".

81.2.1. Example of LUA Script to pilot digital outputs of RIO device

The Abilis permits to pilot Output digital line and Relay switch ports making phone calls. For that we use a LUA script joined to DISA MODE:APP resource that allows to open Gate#1 connected to Relay switch ports 1-A, 1-B. By enabling the digital output line (ON) a contact is closed that short-circuits the respective A and B pins.

[Note]Note

This advanced configuration normally is realized by Second Level Abilis support. Bellow are reported only the resource configurations and a short logical description.

  1. The Abilis verify to receive a call to the called number 40x, for example 401 to pilot D-OUT:1. The called number 402 will be used to pilot D-OUT:2 and so on.

    [13:47:55] ABILIS_CPX: d ctir pr:3
    
    Last change: 01/02/2021 14:20:26 CET
    
    ---+-----+-----------------+---------+--------------------+--------------------
    PR |[DESCR]
       |TYPE |POI/SR   [SR]    |POO/DS   |CDI                 |CDO
    ACT|NEXT       |LAST       |EEC |T301|CGI                 |CGO
    EDT|SP   |SC   |DJ   |MJ   |FMDJ|FMMJ|SDI                 |SDO
       |SPOUT|SCOUT|DJOUT|MJOUT|LCS |LCST|SGI                 |SGO
       |                       |BCO      |RGI                 |RGO
       |FMRLY|FAXSP|MODSP|FMLVL|ECM |UDT |IG  |OG  |SG        |DL  |DH
    |CODERS
       |CODERSOUT
       |TI1 .. TI5
    --------------------------------------------------------------------------------
    3   [Management_I/O]
        VOICE  **   #        #   Disa      40?                  *                   
        NO           ANY         NO   Dft  *                    *                   
        64000  Sys   Sys   Sys   Sys  Sys  *                    *                   
    --------------------------------------------------------------------------------
    
    [Note]Note

    A list of allowed calling numbers CallingOK can be further configured, as in the example above.

  2. The DISA resource configured like below, receive the call with called number 40x. The DISA service:rioapp refuses the call but recognized the number "X" that is motorized by LUA Script.

    [13:47:16] ABILIS_CPX: d p ctidisa
    
    RES:CtiDisa -------------------------------------------------------------------
    Run    DESCR:Direct_Inward_System_Access
           LOG:NO          ACT:YES        sesnum:10       RESTRICTED-CALLING:NO
           PROVIDE-SG:YES  CALL-ABORT:**  CALL-REPEAT:**  PIN-START:*  PIN-END:#
           NPOO-CT:SYS     CTIP-TYPE:NET-PRIVATE          BUSY-NOCHAN:YES
           WDIR:C:\APP\DISA\
    
    [13:47:19] ABILIS_CPX: d ctidisa user
    
    Total number of users:1
    
    -------------------------------------------------------------------------------
    USER:Anonymous
      PIN:     #                |MAX-CALLS: NOMAX |PERMIT: SERVICE
      SERVICE: AUTO             |CB:        NO    |DENY:   #
      CGI:   -
    -------------------------------------------------------------------------------
    
    [13:47:22] ABILIS_CPX: d ctidisa service:rioapp
    
    Total number of services:2
    
    -------------------------------------------------------------------------------
    SERVICE:rioapp            DESCR:
      MODE:        APP                    |INTRO-STATE:   CREQ  |INTRO:      
      MAX-CALLS:   NOMAX                  |CONN-DELAY:    0     |WELCOME:  
      BC:          Speech                 |DIAL-TOUT:     30    |INITDIAL: 
      CB-CGO:      #                                            |BYE:      #
      CDI:         40*                    
      CGI:         #
    -------------------------------------------------------------------------------
  3. To open Gate#1 connected to D-OUT:1 RIO device is configured to generate a Pulse of 500 ms.

    [Note]Note

    This is only a protective measure since this pulse time is also indicated in the script "local PULSEDURATION = 0.5".

    [13:46:44] ABILIS_CPX: d p rio
    
    RES:Rio-1 ---------------------------------------------------------------------
    Run    DESCR:RIO
           LOG:DS                  ACT:YES
           - IpConf parameters (Read Only) ---------------------------------------
           MAC:B8-27-EB-12-F7-FB   IPRES:Ip-1    
           IPADD:192.168.001.222   MASK:255.255.255.000   GW:192.168.001.001
           -------+-------------------- Digital Input lines -----------------------
           D-IN:  | [DESCR:]
           -------+----------------------------------------------------------------
           -------+-------------------- Digital Output lines ----------------------
           D-OUT: | [DESCR:]
                  | MODE:    PULSE-T:    PULSE-GAP:    START: DFT:
           -------+----------------------------------------------------------------
           1      | Gate#1
                  | PULSE    500         500           LAST   OFF
           -------+-------------------- Analog Input lines ------------------------
           A-IN:  | [DESCR:]
                  | UNIT:    MIN:    MAX:    ALM-LO: ALM-HI: HYST:   UPD:    POLL:
           -------+----------------------------------------------------------------
           -------+-------------------- Analog Output lines -----------------------
           A-OUT: | [DESCR:]
                  | UNIT:    MIN:    MAX:    ALM-LO: ALM-HI: HYST:   UPD:    POLL:
                  |          CUT-LO: CUT-HI: START:  DFT:
           -------+----------------------------------------------------------------
  4. The script ephone-cd.lua must be uploaded using Z-modem or FTP, or the web interface, and activated at startup.

    [Tip]Tip

    To manage the Lua script by web interface follows the chapter Section 72.23, “LUA scripts”.

    [Tip]Tip

    The script ephone-cd.lua can also be imported from the script examples. Please see the chapter: Section 72.23, “LUA scripts”. GPIORESTYPE and GPIORESINST must be checked before starting it.

    [13:46:52] ABILIS_CPX: d p script
    
    RES:Script --------------------------------------------------------------------
    Run    DESCR:Scripting_service
           LOG:NO        ACT:YES    bg-sesnum:4    fg-sesnum:4
           wdir:C:\APP\SCRIPT\
           - Background scripts ---------------------------------------------------
           id:0  startup:YES  file:ephone-cd.lua
           id:1  startup:NO   file:
           id:2  startup:NO   file:
           id:3  startup:NO   file:
  5. The script ephone-cd.lua verify the last digit of the called number and pilot the corresponding Digital Output. In this example the called number is 401 then the driven D-OUT:1. LUA Script ephone-cd.lua:

    --- ephone-cd.lua Script
    --
    -- This script waits for incoming call and makes a 500 ms
    -- pulse on the selected digital output line depending on
    -- the called number.
    -- author Ion Buraga <buraga@antek.it>
    
    local abilis = require "abilis"
    
    ------------------------------------------------------------
    
    -- call state
    local CLOSED = 0
    local CALLING = 1
    local ALERTING = 2
    local ACTIVE = 3
    local CLOSING = 4
    local LISTENING = 5
    -- play state
    --local SILENT = 0,
    --local PLAYING = 1,
    
    ------------------------------------------------------------
    
    -- Name of this script, show in log
    local SCRIPTNAME = "gate.lua"
    -- Name of DISA/APP service
    local DISASERVICE = "rioapp"
    -- GPIO resource type
    local GPIORESTYPE = "RIO"
    -- GPIO resource instance
    local GPIORESINST = 1
    -- GPIO pulse duration
    local PULSEDURATION = 0.5
    -- Poll period in seconds
    local POLLPERIOD = 0.5
    
    ------------------------------------------------------------
    --- Display log message
    -- The message is preceded with an automatically generated
    -- timestamp.
    -- @parammsg Message to be logged
    --
    local function log(msg)
    	local dt = os.date("%Y-%m-%d %X")
    	local sep = " "
    	print(dt .. sep .. msg)
    end
    
    ------------------------------------------------------------
    --- Handle of selected GPIO device
    --
    local GPIOHANDLE = abilis.gio.get_device_handle(GPIORESTYPE, GPIORESINST)
    
    ------------------------------------------------------------
    --- Generate 250 ms pulse on selected GPIO line
    -- @param line Digital output line number
    --
    local function pulse_line(line)
    	abilis.gio.set_digital_output(GPIOHANDLE, line, 1)
    	abilis.cpx.sleep(PULSEDURATION)
    	abilis.gio.set_digital_output(GPIOHANDLE, line, 0)
    end
    
    ------------------------------------------------------------
    --- Wait for incoming call in DISA/APP
    -- @param handle DISA/APP handle
    --
    local function wait_for_incoming_call(handle)
    	assert(handle > 0)
    
    	while true do
    		local state = abilis.disa.get_state(handle)
    		if state == CLOSED then
    			return false
    		elseif state ~= LISTENING then
    			return true
    		else
    			abilis.cpx.sleep(POLLPERIOD)
    		end
    	end
    end
    
    ------------------------------------------------------------
    --- Map last digit of called number to GPIO line
    --
    local MAP = {
    	["1"] = 1,
    	["2"] = 2,
    	["3"] = 3,
    	["4"] = 4,
    	["5"] = 5,
    	["6"] = 6,
    	["7"] = 7,
    	["8"] = 8,
    }
    
    ------------------------------------------------------------
    --- Convert called number to GPIO line (or nil).
    -- @param number Called phone number
    -- @return GPIO line number
    --
    local function get_line_for_number(number)
    	if number ~= nil then
    		local last_digit = number:sub(-1)
    		return MAP[last_digit]
    	else
    		return nil
    	end
    end
    
    ------------------------------------------------------------
    --- Convert value to string.
    -- Nil is converted to empty string, other values are
    -- returned unchanged.
    -- @param x String or nil
    -- @return String
    local function tostr(x)
    	if x ~= nil then
    		return x
    	else
    		return ""
    	end
    end
    
    ------------------------------------------------------------
    --- Handle incoming call.
    -- Wait for incoming call, obtain called number, find the
    -- corresponding GPIO line, and generate pulse there.
    -- @param handle DISA/APP handle
    --
    local function handle_incoming_call(handle)
    	assert(handle > 0)
    
    	if wait_for_incoming_call(handle) then
    		local cg, cd = abilis.disa.get_info(handle)
    		log("incoming call CG:" .. tostr(cg) .. " CD:" .. tostr(cd))
    		local line = get_line_for_number(cd)
    		if line ~= nil then
    			log("generating pulse on line " .. line)
    			pulse_line(line)
    		else
    	 	log("unsupported called number")
    		end
    	end
    	log("closing call")
    	abilis.disa.close(handle)
    end
    
    ------------------------------------------------------------
    --- Handle incoming calls.
    -- Listen on selected DISA/APP service, and process
    -- incoming call. And again, forever.
    --
    local function handle_incoming_calls()
    	while true do
    		log("waiting for incoming call")
    		local handle = abilis.disa.listen(DISASERVICE)
    		if handle ~= nil then
    			handle_incoming_call(handle)
    		else
    			log("listen function failed")
    			break
    		end
    	end
    end
    
    ------------------------------------------------------------
    --- Create session object, and handle incoming calls from
    -- DISA/APP.
    --
    local function main()
    	log(SCRIPTNAME .. " script started")
    	handle_incoming_calls()
    	log(SCRIPTNAME .. " script ended")
    end
    
    -- Execute the main function
    main()
    
[Note]Note

This script can also be used for an RVS device, it's necessary to modify only this line "local GPIORESTYPE = "RVS"".

[Note]Note

For RVS-2 or RIO-2, in the script, you will only need to modify "local GPIORESINST = 2".