The previous article introduced the power monitoring platform, which is based on THE TB Internet of Things platform. TB is an open source Internet of things platform, although the function is very powerful, but after all is written by foreigners, in the process of using you will find a lot of habits and domestic system are different. For example, THE REST interface exposed by TB is often not further encapsulated, and the format output is messy. If it is directly used by the front end, it will lead to disharmony. Similarly, if TB websocket interface is directly used, the front-end resolution is also very complicated. How do you block out these differences?

Ask questions:

How to format REST interfaces?

For example, the data format of TB is as follows:

{"token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI4OTMzOTM5NTNAcXEuY29tIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiIwMmNkN GVhMC1mZDRkLTExZWEtYTg3ZC1iYjQyNTAyMjE0MmUiLCJmaXJzdE5hbWUiOiLkuIkiLCJsYXN0TmFtZSI6IuW8oCIsImVuYWJsZWQiOnRydWUsImlzUHVib GljIjpmYWxzZSwidGVuYW50SWQiOiI1NTBjYWNkMC1mZDRiLTExZWEtYTg3ZC1iYjQyNTAyMjE0MmUiLCJjdXN0b21lcklkIjoiMTM4MTQwMDAtMWRkMi0xM Ahisf - noEU99 WIyLTgwODAtODA4MDgwODA4MDgwIiwiaXNzIjoidGhpbmdzYm9hcmQuaW8iLCJpYXQiOjE2MTc2MjA4ODcsImV4cCI6MTYxNzcxMDg4N30. 1 3G4m5jO8m8MiASdBM1AW1D-a40W3EV920OpIaLW1S5f7fm3C-QHrqZBneFMKeWYA_NDLcE-xA","refreshToken":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIi OiI4OTMzOTM5NTNAcXEuY29tIiwic2NvcGVzIjpbIlJFRlJFU0hfVE9LRU4iXSwidXNlcklkIjoiMDJjZDRlYTAtZmQ0ZC0xMWVhLWE4N2QtYmI0MjUwMjIx NDJlIiwiaXNQdWJsaWMiOmZhbHNlLCJpc3MiOiJ0aGluZ3Nib2FyZC5pbyIsImp0aSI6ImFlZWNkYTQ4LWUwMzItNGZiOC1iMjk0LTg1M2Y1YzNjZmZkMCIs ImlhdCI6MTYxNzYyMDg4NywiZXhwIjoxNjE4MjI1Njg3fQ.jwX3cqAHXjcSdkU1ctDr6qhU8fgl0pvBt9LZlE7x-N0GRSqvfdyK8c2FJCmXifzRGBPekKze_ 1ZRTbRDdHoJ6w"}Copy the code

And we normally need data input, should be like this:

{"success":true,"resultCode":"00","resultMsg":"","result":{"token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI4OTMzOTM5NTNAcXEuY29 tIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiIwMmNkNGVhMC1mZDRkLTExZWEtYTg3ZC1iYjQyNTAyMjE0MmUiLCJmaXJzdE5hbWUiOiL kuIkiLCJsYXN0TmFtZSI6IuW8oCIsImVuYWJsZWQiOnRydWUsImlzUHVibGljIjpmYWxzZSwidGVuYW50SWQiOiI1NTBjYWNkMC1mZDRiLTExZWEtYTg3ZC1 iYjQyNTAyMjE0MmUiLCJjdXN0b21lcklkIjoiMTM4MTQwMDAtMWRkMi0xMWIyLTgwODAtODA4MDgwODA4MDgwIiwiaXNzIjoidGhpbmdzYm9hcmQuaW8iLCJ PYXQiOjE2MTc2MjA4ODcsImV4cCI6MTYxNzcxMDg4N30. 1 ahisf - noEU993G4m5jO8m8MiASdBM1AW1D - a40W3EV920OpIaLW1S5f7fm3C - QHrqZBneFMKeW YA_NDLcE-xA","refreshToken":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI4OTMzOTM5NTNAcXEuY29tIiwic2NvcGVzIjpbIlJFRlJFU0hfVE9LRU4iXS widXNlcklkIjoiMDJjZDRlYTAtZmQ0ZC0xMWVhLWE4N2QtYmI0MjUwMjIxNDJlIiwiaXNQdWJsaWMiOmZhbHNlLCJpc3MiOiJ0aGluZ3Nib2FyZC5pbyIsIm p0aSI6ImFlZWNkYTQ4LWUwMzItNGZiOC1iMjk0LTg1M2Y1YzNjZmZkMCIsImlhdCI6MTYxNzYyMDg4NywiZXhwIjoxNjE4MjI1Njg3fQ.jwX3cqAHXjcSdkU 1ctDr6qhU8fgl0pvBt9LZlE7x-N0GRSqvfdyK8c2FJCmXifzRGBPekKze_1ZRTbRDdHoJ6w"}}Copy the code

How do we convert TB from the original format to the format that we need?

Solution:

  • Solve the problem by modifying TB source code

Modify TB source code, change up very simple, but it will lead to TB platform built-in management background is not available. Because the TB front end itself calls these interfaces, special attention should be paid to compatibility with the TB platform. In other words, the TB platform should ensure that there are both output parameter formats. Another approach, which is more complex, especially for back-end developers, is to modify the way the TB front end parses data. In short, modify the source code, the original system is more intrusive, higher coupling degree.

  • Resolve the problem using the Kettle data platform

I’ve talked about the power of the Kettle platform in my previous blog, which is a great way to solve this data format conversion problem. This is a brief introduction. For details, see the Kettle Column’s blog.

Presentation:

Create a kettle file and convert data formats in the kettle file.

Watch the result node being parsed and converted through the JS script.

Upload the conversion file to the Kettle platform.

Configure services on the Kettle platform.

The following log shows how to obtain the token interface.

View log details.

Conclusion:

This paper discusses the usability of REST interface of TB platform, and finally selects and optimizes kettle platform, which perfectly solves the problem of data format conversion. This is only for rest interfaces. How can I resolve this problem if the Kettle platform also supports transparent transmission of the WebSocket protocol? The next article describes how the Kettle platform converts Websocket communication.