In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what functions have been added to MQTT X v1.4.2". In daily operation, I believe many people have doubts about what functions have been added to MQTT X v1.4.2. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what new features have been added to MQTT X v1.4.2". Next, please follow the editor to study!
Script function
After v1.4.2, MQTT X has added a scripting function that allows users to edit scripts to customize the conversion of Payload sent and received in MQTT connections. Can be used to simulate a custom test field. The main scripting language is JavaScript. The following will briefly introduce two simple test case scenarios to introduce the script function, it should be noted: in v1.4.2 version, the script function belongs to the open testing function, the use of process, security and functionality need to be further optimized and improved. Users are also welcome to discuss in detail in the GitHub issue area of MQTT X. we will review and reply carefully.
In the editing script function, the global contains only one execute API, and users need to write a custom function, which receives a value parameter, that is, Payload, in which the value can be customized and converted. Finally, the custom written function can be executed by passing the function as a parameter to execute.
Example one
Cooperate with the timing sending function to simulate the temperature and humidity data reporting.
For example, when users use EMQ X, they need to use the rules engine feature to save data to the database. After the configuration is complete, you can connect to EMQ X using MQTT X and test it using the scripting feature. Assuming that the user needs to save the reported temperature and humidity data in a JSON format, we can use the following script to simulate the data.
/ * * Simulated temperature and humidity reporting * @ return Return a simulated temperature and humidity JSON data-{"temperature": 23, "humidity": 40} * @ param value, MQTT Payload-{} * / function random (min Max) {return Math.round (Math.random () * (max-min)) + min} function handlePayload (value) {let _ value = value if (typeof value = 'string') {_ value = JSON.parse (value)} _ value.temperature = random (10,30) _ value.humidity = random (20,40) return JSON.stringify (_ value, null, 2)} execute (handlePayload)
At this point, you can copy this code into the code editing box of the script page, click the Save button in the upper right corner, and set the script name to TempAndHum and save it. We enter a {} in the Input input box as the initial data. Click the test button to view the execution results in the Output box, and if the results meet expectations, you will be able to use the script normally.
We created a new connection using the free public MQTT server provided by EMQ X, which is based on EMQ X's MQTT Internet of things cloud platform. The server access information is as follows:
Broker: broker.emqx.io
TCP Port: 1883
Websocket Port: 8083
After the connection is successful, click the drop-down function menu in the upper right corner, select use script, in the pop-up window, select the TempAndHum script that has just been saved, and then select the application type to send, click the confirm button to open the script function.
After opening the script, we continue to set the scheduled sending function. Also click the drop-down menu in the upper right corner, and select the scheduled message. Here we set the sending frequency to 1 second, and click to confirm the scheduled message function.
When you are ready, you can enter the initial Payload and the Topic you want to send to. After clicking on a successful message, you can see that MQTT X will automatically send the simulation data once a second.
This avoids the trouble of users to manually input and modify the data, and the simulation data can be controlled when using the script, and the simulation data interval can be set in the random function of the script. If there are visual chart testing requirements for the saved data or need to add a certain amount of data to test, it is more convenient and friendly.
Example two
Converts the timestamp in the received Payload into normal time.
In some test scenarios, the Payload received by a user may contain timestamp information. If you need to observe and test time-sensitive data, you may need to copy the data, and then convert the timestamp into time, which is more troublesome. At this time, you can use the script to automatically convert the received data, which is convenient for users to observe the data information. We can use the following script to convert the data. It is still assumed that the data received is of type JSON and contains the time field.
/ * Convert timestamp to normal time. * @ return Return the UTC time-{"time": "2020-12-17 14:18:07"} * @ param value MQTT Payload-{"time": 1608185887} * / function handleTimestamp (value) {let _ value = value if (typeof value = 'string') {_ value = JSON.parse (value)} / / East Eight District needs an additional 8 hours const date = new Date (_ value.time * 1000 + 8 * 3600 * 1000) _ value.time = date.toJSON (). Substr (0,19). Replace (' tweets,') return JSON.stringify (_ value, null, 2)} execute (handleTimestamp)
At this point, you can copy this code into the code editing box of the script page, click the Save button in the upper right corner, and set the script name to Time and save it. We enter a {"time": 1608365158} in the Input input box as the initial data. Click the test button to view the execution results in the Output box, and if the results meet expectations, you will be able to use the script normally.
At this point, we still create a new connection and open the script using the method described above. Note that when selecting the application type, you need to select when receiving.
When scripting is enabled, we add a Topic for testtopic/time, and then we send a Payload with timestamp information to that Topic. Then looking at the received Payload information, you can see that the timestamp has been automatically converted to normal time.
At this point, the study of "what features have been added to MQTT X v1.4.2" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.