To begin with, I set up the LabVIEW block diagram as usual, with the Recipe Puppy API, HTTP Get and JSON Hack. Since the user should be able to put multiple items into search (ingredients and food keyword), I had to figure out a way to get multiple strings into HTTP Get. Using Concatenate Strings function, I was able to get multiple strings and convert them into a single output, simply concatenate. To separate each input, or parameters as in the API example, I used the relevant string constants (,), (&), (q=) that would also go into the concatenate string function in order, while having string body controls in between for user input. Since I created controls in the String format for food keyword and the ingredients, on the front panel, the user would be able to type any relevant word and then see the results.
The first challenge was figuring out how to be able to extract specific items from the JSON string that contains multiple items (URL, Recipe title and Ingredients). Searching online, I was able to find an interesting function called Unflatten from JSON (which I would use in the place of JSON hack) and learned about the "path" input wired to Unflatten from JSON, which communicates with data type and values attached to "type/defaults" nod of the function to which I would unflatten the JSON string. So in order to establish a connection with path and the items in JSON string (URL, Recipe title and Ingredients) I created a cluster constant of 4 elements (string constants) and labeled them each with relevant values inside the JSON string and further wired to "types/defaults".
Since path uses an array of strings to identify the item, where each element in the array references either the name of a cluster element or an integer index of an array, I first created a constant and wired it to path node, typing the string value "results" so that the function would get the data after the word "results" from the body response from HTTP request that contained various titles of recipes and relevant information based on user input. Figuring out how to display the specific part of the JSON string after "results" and then finding how to change between recipes in numbers was challenging and complex for me in the beginning, so it took some time. In order to be able to switch between recipes I had to change the string value of the cluster elements manually on the block diagram, which was not practical and not displayed on the front panel.
To the right is the cluster constant that goes into Unflatten from JSON as an input, wired to types/defaults node.
Constant going into path shows my initial attempt to display recipe numbers. 0 at the rightside of results defines the order of the output, recipe number.
Since the necessary information we need is after "results"on the JSON string, we parse it from there.
When I created a string control (changed the constant into control) and wired it to the path node to get the user input regarding recipe numbers, it was not user-friendly to enter the number of the recipe in a string format, so I then created a number control. Using Format into String function, I was able to format the numerical data into string to wire into Build Array Function that would append elements to an n-dimensional array to further connect to the path node. I also wired "results" as a constant as I did previously (credit: Briana:). The user was able to switch between numbers on the front panel.
Note: The order in which I put elements into Build Array Function would matter and affect the running of the code. (results should be wired first).
As another challenge, I had to extract each element one by one separately, so I searched for a specific function to do that. I could have simply used an indicator but then I would not be able to edit each output, which would become useful in the future steps. The function Unbundle by Name was helpful for separating the output values as it returns the cluster elements that I specified in the cluster constant. The function was very practical since it automatically took in and displayed the cluster elements by name and I was able to sort them out in order by right clicking "add element".
I created body indicators for each element to display on the front panel, as seen above. Note: I also wanted to include thumbnail but I was not able to display the pictures, not the jpg url.
I created tags of the type String to connect to LabVIEW and SystemLink controls, and further display on my SystemLink Dashboard. In addition to tags for the title, url and the ingredients ( 2 tags because the user may or may not put 2 ingredient values), I included input tags since I wanted the user to be able type on the dashboard without going to LabVIEW back and forth.
SystemLink Read is a very practical, useful, and a simple-to-use function, however I faced some challenges when trying to connect SystemLink Read to the Build Array Function (in addition to the number control as an input and results constant) and further into Unflatten from JSON function. I kept receiving the error "The path cannot be found in the JSON string" and discovered that "path" does not accept those three inputs at once. I further learned that in order for Unflatten JSON to work, I should have one selector working at a time, so that's why Unflatten JSON would not accept two different inputs. (credit: Briana :) In order to troubleshoot, on Briana's advice, I duplicated my code and created slightly different diagrams for SystemLink and LabVIEW.
As I did with the numeric control, I wanted the user to be able to switch between recipe number in a convenient way, so I used Format into String Function again in order to wire the numeric input out of SystemLink Read as a string format into Build Array Function, in addition to "results."
As a further step, I multiplied SystemLink Read functions to wire into initial Concatenate String function in order for the user to put values of ingredients and food keyword, via SystemLink Dashboard. I realized that LabVIEW code and SystemLink Code could potentially have different inputs and outputs, depending on the user. So rather than displaying both of them on the front panel, I preferred this approach.
I wired SystemLink Write functions, having the date type string, to the string outputs at the right end of the code in order to display on the Dashboard.
Since SystemLink Dashboard does not have the feature for text wrapping, I had to figure out how to cut the relatively longer "ingredients" output, ideally into two pieces so that one could go under another on the Dashboard. I first created another tag value "ingredients 2" to assign into the second SystemLink Write Function.
Then, on LabVIEW, I found two different functions that can split the string, Search/Split function and Match Pattern. I tried both of them and decided to use Match Pattern function was more effective since I can wire both "before substring" and "after substring". I made a rough estimate of 70 characters as an offset which turned out to be ideal, to move on to the second line. As a regular expression, I put space, , as a constant, because I did not want to cut any letters when going to the second line, second tag value on the Dashboard.