57.5. LUA SCRIPT examples

57.5.1. Restart ADSL USB modem using LUA SCRIPT

This script checks the IP status (in this case ip-25) every 60 seconds (POLLPERIOD) and if it finds it DOWN twice, it will restart the USB modem (in this case DSL-1).

--@author Ion Buraga
--version 1.4
----------------------------------


local abilis = require "abilis"
-- Poll period in seconds
local POLLPERIOD = 60

local Ipres = "ip-25"
local lowres = "DSL-1"
local ClientN = "Customer"
local Sede = "site 1"
local mailuser = "tem@antek.it"


function get_abilis_prompt()
	local Success = false
	local Error = "NO ERROR"
	local StateString = "UNKNOWN"

	local CpStream = abilis.cp.open()
	if CpStream == nil then
		print("CP stream cannot be opened !!!")
		Error "ERROR - CP STREAM CANNOT BE OPENED"
		return Success, Error, StateString
	end

	local LineCounter = 0

	if CpStream:write("d d "..Ipres.." | state") then
		local CpResponse = CpStream:read("l")
		while CpResponse do
			LineCounter = LineCounter + 1
			print(CpResponse)

			local First, Last = string.find(CpResponse, "STATE:")
			if First ~= nil and Last ~= nil then

				local First_2, Last_2 = string.find(CpResponse, "LINK")
				if First_2 ~= nil and Last_2 ~= nil then

					Success = true
					StateString = string.sub(CpResponse, Last + 1, First_2 - 1)

					CpStream:close()
					return Success, Error, StateString
				end
			end

		CpResponse = CpStream:read("l")
		end
		Error = "ERROR - PROMPT NOT FOUND"
	else
		Error = "ERROR - WRITING 'D D IP-X' FAILED"
	end

	CpStream:close()
	return Success, Error, StateString

end



function cp(cmd) -- start of function 'cp'
    local s = abilis.cp.open() -- opening of CP stream channel
    if s then -- if successfully opened...
        if s:write(cmd) then -- ...write a command there
            local asw = s:read("l") -- read the line of the result of the written command
            while asw do -- repeat until the end of listing is reached
                print(asw) -- print line
                asw = s:read("l") -- read another line
            end
        end
        s:close() -- close CP stream channel
    end
end -- end of function 'cplist'

local function log_cp_command(LogFile, CpCommand)
	
	local CpStream = abilis.cp.open()
	if CpStream == nil then
		print("CP stream cannot be opened !!!")	
		return
	end

	if CpStream:write(CpCommand) then
		local CpResp = CpStream:read("l")
		while CpResp do
			LogFile:write(CpResp,"\r\n")

			CpResp = CpStream:read("l")
		end
	else
	end

	CpStream:close()

end

local function get_status()

	local Success, Error, StateString = get_abilis_prompt()

	if Success then
		print("State of "..Ipres.."  is "..StateString)
State2 = StateString
	else
		print("Abilis prompt loading failed ! Error: ".. Error)
	end
end





local function verify_state()

	 get_status()

	if string.find(State2, "DOWN") then

	abilis.cpx.sleep(60)

	get_status()



	

	if string.find(State2, "DOWN") then
dt = os.date("*t")
	local AbilisLogFile =
		"Abilis_Log_"
		..dt.year..dt.month..dt.day..
		"_"..dt.hour..dt.min..dt.sec..".txt"

	local file = io.open(AbilisLogFile, "wb")
	if file == nil then
		print("File "..AbilisLogFile.." not found !!!")	
		return
	end

	log_cp_command(file, "d usbdev" )
        log_cp_command(file, "d d "..Ipres.."" )
        log_cp_command(file, "d dse "..lowres.."" )
	
	file:close()

		-- abilis.cpx.sendmail(<from>, <to>, <subject>, <message>)
		mail_retval = abilis.cpx.sendmail(
			""..mailuser..""
			,""..mailuser..""
			,""..ClientN..""..Sede.." report mail"
			,""..Ipres.."  down"
			,AbilisLogFile
			)
 	cp("debug res:"..lowres.." lsn:254")
if mail_retval == 0 then
		print("*")
		print("* Mail with LOG SENT SUCCESFULLY (retval:"..mail_retval..")")

		-- delete the file to don't consume space on system disk
		os.remove(AbilisLogFile)
	else
		print("*")
		print("* Mail with LOG FAILED (retval:"..mail_retval..")")
		print("* Check log file on system disk instead - filename: "..AbilisLogFile)

		-- intentionally don't delete the file to check it via FTP or telnet
	end

		end	
end
	


end

local function verify_state_after_pollperiod()
    while true do
       
        verify_state()
        abilis.cpx.sleep(POLLPERIOD)
    end
end

--
local function main()
	
	verify_state_after_pollperiod()
	
end

-- Execute the main function
main()

57.5.2. LUA Script to pilot digital outputs of RIO device

To pilot digital outputs of RIO device using a LUA Script, see Section 81.2.1, “Example of LUA Script to pilot digital outputs of RIO device”.

57.5.3. LUA Script to insert a command on CP by call

This script insert a command on CP (command line of abilis) after a call. It can be useful to activate a specific user preference, change a parameter in the configuration, do a restart, etc.

--- commandbycall.lua Script
--
-- This script insert a command on CP (command line of abilis)  after a call

-- author Ion Buraga <buraga@antek.it>
--version 1.0
------------------------------------------------------------

-- 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 = "comandbycall.lua"

-- Name of DISA/APP service
local DISASERVICE = "entryphone"

-- Poll period in seconds
local POLLPERIOD = 0.5
local abilis = require "abilis"
local CMD = "s p ip-1 descr:BBB"

------------------------------------------------------------
--- 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

-------------------------------------------------------
local function command() -- start of function 'cp'
    local CpStream = abilis.cp.open() -- opening of CP stream channel
    if CpStream == nil then 
	print("CP stream cannot be opened !!!")
	else
        if CpStream:write(""..CMD.."") then 
			print(""..CMD.."")

			else 
			log("ERROR inserting CMD")
					        
        end
        CpStream:close() -- close CP stream channel
    end
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

------------------------------------------------------------
--- 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
--
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))
		command()
	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




local function main()
	log(SCRIPTNAME .. " script started")

	handle_incoming_calls()
	
	log(SCRIPTNAME .. " script ended")

end

-- Execute the main function
main()


[Note]Note

For CTI configuration (DISA service/user, CTIR routing), please see Section 81.2, “Example of I/O management via LUA Script”.