This week, we added a new tool to the command line API. This new tool is called ysh : it's the shell for Yoctopuce modules. It's an interactive tool enabling you to run any command on any module. Let's see what improvements it brings...
This new ysh tool enables you to use all the functions of a Yoctopuce module. Indeed, the same executable can send command to the temperature sensor of a Yocto-Meteo but also to its humidity sensor. Before, you had to use the YTemperature tool and the YHumidity tool. This new tool is the union of all the tools contained in the command line API.
The ysh tool also enables you to run a sequence of commands from a text file. Here is the syntax of a command to be run.
- The <function> is the type of function on which you are going to work (YRelay, YSensor, YModule, and so on...).
- The [target] is the name of the function to which the method applies. You can also use the "all" alias to target all the compatible functions. Some methods don't require a target.
- The <method> is the method you want to run. Almost all functions available in the classic programming APIs are available here as well.
- The [parameters] are, rather logically, the parameters of the method.
If you have already used our command line API, you can notice that the syntax is similar to that of the other tools. You simply need to add the type of function that you are going to use as first argument. This function name corresponds to the name of the tool which was used before. In this way, it's very easy to transform your scripts to use ysh .
For example, the script
YLightSensor all get_currentValue
using the YRelay and YLightSensor tools can be modified to use ysh like this:
ysh YLightSensor all get_currentValue
Now that we have seen the syntax of a command, let's have a look at the different solutions to pass commands to run to ysh.
The first method is, as we have seen it above, to pass the command directly as an argument when running ysh.
The tool initializes the connection with the module, runs the command, and completes its execution. This working mode is strictly identical to traditional tools. The only advantage is that there is only one executable (ysh) that you need to install.
The second method is to run ysh interactively. In this mode, ysh displays a prompt and runs the commands that the user types in. To run ysh in interactive mode, you only need to run it without arguments:
> YRelay RELAYHI1-00055.relay1 pulse 20
> YLightSensor all get_currentValue
> _
The third method is to give as parameter a text file, containing commands to be run. This method is very interesting because ysh initializes the connection with the modules only once at startup. Then, all the commands in the file are run one after the other. Therefore, running a file containing two commands is much faster than running twice ysh with a command as argument. Above all, it allows you to run two commands together almost instantaneously, which can be very useful if you want to synchronize several modules.
The format of the file to give as an argument is very simple: each line is interpreted as a command and lines starting with the "#" character are ignored.
YRelay RELAYHI1-00055.relay1 pulse 20
YLightSensor all get_currentValue
The --file option enables you to give the path of the command file.
The last method is to use a standard input (stdin). The ysh tool can run the commands it receives on the standard input, and thus can be conjointly used with other system commands such as grep, cat, and so on...
Here we are, we had a look at the new features of the command line API. This new tool will certainly be appreciated by users who do not have great programming background but who are used to automate complex tasks using bash scripts.