Make a Custom File Reader
This is good example of how we can usehttps://interfaceware.atlassian.net/wiki/spaces/EC/pages/2091581477 by building a custom file reader component which has a simple interface for a unique purpose rather than using a general purpose no code solution - see https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2681077787.
Here’s a scenario:
A large lab needs to have a file reading component which can feed files from different customers based on the prefix of the file, i.e. some files start with JJ99, others XYZ etc. depending on the source.
Let’s do it!
This component generates some test files to make it easier to learn how to make a custom file reader.
You can usehttps://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684453620 to import the component.
Copy paste the URL from here:
git@bitbucket.org:interfaceware/file_tutorial.gitRefer to https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2679931435 to see how it's done if you have not done this before.
To simplify the tutorial we have set these up a ClientList with the filename prefix Ids. Click on the config.json and you should see:
See https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2500263941 to understand how this was created.
Navigate to MatchRules.lua in the https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2686124034. This is the function we need to alter:
function MatchFile(FileName)
return true
endThe file reader component is deliberately simple and uses separation of concerns to make it more obvious how to modify the code to meet new needs. We need this function to return true when the matching criteria are met.
Change the matching to add in two lines so we can see the filenames we need to filter on.
function MatchFile(FileName)
local JustFileName = FILfilename(FileName)
trace(JustFileName)
return true
endThese are the concepts we have used:
Create a second function, APPidList, to extract the ClientList prefixes from the custom fields and update MatchFile to call APPidList.
You can replace the contents of the MatchRules.lua file with this new code:
function APPidList()
local IdList = component.fields()["ClientList"]
return IdList
end
function MatchFile(FileName)
local IdList = APPidList()
trace(IdList)
local JustFileName = FILfilename(FileName)
trace(JustFileName)
return true
endConcepts used:
Writing a https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2685304936
https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684846443
Using APP as a prefix, to maintain a common naming convention
https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2500263941
We can quickly do this by adding one line to AppidList after line 2:
IdList = IdList:split(",")You should see something like:
Concepts used:
Create a third function, APPmatchPrefix, to determine if the filename matches the prefix. You can copy the code below:
function APPmatchPrefix(Prefix, FileName)
local Part = FileName:sub(1, #Prefix)
trace(Part, Prefix)
return (Part == Prefix)
endSee the screenshot below and add a line to MatchFile to call and invoke AppmatchPrefix.
Concepts used:
A https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2685304936
https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684813341 to get the length of the prefix
https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2685337684
https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2685665315
Let’s modify the code to go through the entire list of prefixes:
for i = 1, #IdList do
if APPmatchPrefix(IdList[i], JustFileName) then
return true
end
end
return false -- we didn't match anythingAnd this is what you should see:
Concepts used:
https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684355428 and go to the component card for https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2650931201.
You’re done! The next steps are how you could further refine the code
This means refining the APPidList function.
That would involve looping through the list and removing white space from each ID:
Concepts:
Add intuitive https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684322170 for your component to increase visibility into processing and adding meaningful messages to help with troubleshooting.
You can see an example of this in main.lua on line 25:
iguana.log("Polling every "..Polltime.." seconds");Try it for yourself!
See https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2689335358 for a quick explanation on the double-dot notation.
Add https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684485918 for your component to have it display real time updates at the bottom of the component card as its running.
This component already creates custom status on lines 31-36 of main.lua.
Try it for yourself!
There is no limit to how you can streamline the usage of a custom component - if that helps address a bottleneck, then it is well worth it.