Design and Research of Mobile Terminal for Electric Motorcycle Based on Netty

In recent years, with the enhancing awareness of energy saving and environment protection, electric motorcycle sin the consumer market have become more and more popular. Meanwhile, the rapid development of mobile Internet and Internet of things make the intelligent terminal and intelligent hardware come into our daily life. Nowadays a large number of growth in client requires develop the server which has the performance of strong structure, extensibility, customizable for data transferring and storing. Based on the request of the need, the open source framework named Nettyis selected in this paper. Netty provides the support of communication between the mobile phone and the motorcycle. It’s the most popular NIO frameworks in the industry that has already tested by hundreds of thousands of commercial projects, so that we can control the blue tooth electronic lock, real time location, password modification, authentication on the motorcycle with the help of Android application(app).


Introduction
This paper sketches the view based on the intelligent terminal of STM32 which as the core role of the electric motorcycle.The MCU can receive and send the information via the blue tooth communication module and network communication module.The Netty application being ran on the Ali elastic computer service system.The operate system of mobile control terminal is Android.
The main functions of the server application are sending orders to client and receiving the message from clients, Protocol Analysis and storing the data into database.Nettyis the framework that support the asynchronous communication model.It has preseted the function of encoding and decoding, and supported many main protocols.Many other frameworks are short of standards and are incompetent in customization and expandability.
The instructions should be sentto control the blue tooth communication module by our intelligent terminal.The mobile phone should be operated to control the motorcycle indirectly though the Netty server.Motorcycle can communicate with server via GPRS communication module circuit.The circuit uploads the location to the server, and the server sends the new password to the motorcycle.

Create the Netty Server
Netty server was designed with reactor master-slave mode, and the connection which used to receive client's message is an independent NIO thread pool.Firstly, the instance should be created by Server Bootstrap class, and the configuration should be set up and the thread pool should be bound.There are two independent reactor thread pools, one's name is boss thread pool, other's name is worker thread pool.The former is used to receive the client connection and the latter is used to handle high numbers of read and write on the socket channel.Secondly, the channel should be bound on the server, and he channel pipeline should be created and initialized.The channel handler should be added and set up.It mainly used for handling network event.For example, the information should be encoded and decoded, the heartbeat should be added and so on.Finally, the port should be listened at the beginning.

Constitute communication protocols
The server creates port 8000 and port 9000 at the beginning.The motorcycle connect stire server via TCP protocol through the port 8000, and app connect store server via the HTTP protocol through the port 9000.
The form of the TCP transport protocol is stream in data transmission.When the server receives the message from MCU, the channel pipeline uses Delimiter Based Frame Decoder to decode the message by the separator automatically.It can also avoid sticking and unpacking effectively in the communication process.In order to reduce the stress of the server, the short links should be used between the server and clients.That is, when the client sends the information, and the server returns the information.After ten seconds, the server closes the channel and disconnects from the client automatically.The TCP communication protocols shown in Table 1.
Table1.TCP communication protocols STM32 send to the Server #(serial number),(password),(date20170419124933),(current),(volage1), (voltage 2), (voltage3), (voltage 4), (voltage 5), (voltage 6),(temperature1),( temperature2), (distance),(longitude),(latitude), (remaining electricity), (remark), #$_ The server send to stm32 #( date20170419124955),(GPS flag), (serial number),( password), \r\n HTTP is an application oriented object-protocol, which is suitable for distributed hypermedia information systems because of its simplicity and speed.When the server receives the message from the app, the channel pipeline uses Http Server Codec to decode the message.The server parses the message in the format of JSON.When the server responds to the client, the response message consists of three parts: the state line, the message header, and the response text.All of this information is encapsulated in an instance of Full Http Response, and then the message should be returned to the mobile phone .The server usually sends a response code to the client.The response code200is indicates that the client request is successful，404 indicates that the requested resource does not exist;500 indicates that the server error.At last, the HTTP connection should be closed.

Selection of thread pool and database connection
The server needs to store the data that received from the port 8000 and port 9000 into the database.This step is time-consuming.So the new Fixed Thread Pool should be used to deal with this problem.When the program submits a task, a thread is created, when the number of the client on the connection reaches the maximum that allowed by the thread pool, the thread is stopped creating.The fixed length of the thread pool can be combined with the number of clients to ensure that the server receives and stores information in a normal working state.
Mybatisis a semi-automatic mapping of the framework.It's used to connect to the database.The mapping relationship between POJO and SQL should be provided and matched manually.Mybatis is a small package, high performance, optimized, highly active Java persistence layer framework.
When Mybatis is being used, a session should be created through the SqlSession interface, which is the connection resource for the database.After that, the connection should be closed and the resources should be given back to the database connection pool.During the session, the database should be operated by SQL language.Then the relevant properties of the database connection in the configuration file should be configured.Finally the mapper should be constructed.All the process is shown in Fig 1.
Mapping is consisted of Java interface and XML files, and the java interface and the XML file should be given.In this file, a space with the same name to the interface should be defined, and then the database operation should be added in this space.At last, the type should be returned and the data should be set.When the client disconnects from the server, the server always generates an exception that the remote host is forced to close an existing connection.This exception is common, and the main reason is that the client has closed the connection, but the server is still reading the event.Therefore the server reads the client error.In order to solve this problem, the method called user Event Triggered and exception Caught should be rewritten in the processing of the server's link, in order to carry out exception capture and abnormal printing.

Server Performance Test
TCP/UDP performance testing tool is easy for us to test the server compression capability.The sending mode,target address,content,and the number of the clients should be set.After the start of the test, clients send and receive curvesthat can judge the performance of the server.The server applicationshould be ran on the Ali elasticcomputerservicesystem, and the testing toolapplicationshould be ran on the computer.After the client sends the message, the serversearches the message in the database, and then the server returnsthe messageback to the client.Ali elasticcomputerservice configuration environment is shown in Table 2.At the same time, the tool provides interval and response in two ways.The app determines whether the phone supports Bluetooth 4.0.Firstly the mBLE is instantiated by BluetoothLeClass object, and the service callback and data callback operations are performed with the mBLE.If the mBluetoothAdapterin the mBLE is null, it means this phone doesn't support Bluetooth4.0.As is shown in theFigure4,the flag bit is createdwhich named mScanning,if the state of mScanning is true, the app's activity jump into the bluetooth list interface.The name and the address of the bluetooth client are added to the client in the mLeDeviceAdapteris chosen, then the app finds the bluetoothserviceand the service's eigenvalue.Ifthe value of UUID equals the value of UUID_KEY ("0000ffe-0000-1000-8000-00805f9b34fb"), and thenthe client canbe operated bygattChar.

Fig.1 Integrated design of server 2 . 4 .
Fig.1 Integrated design of server

Fig
Fig.2 Test r

Fig. 4 Bluetooth communication flow chart 4 . 2 .
Fig.4 Bluetooth communication flow chart4.2.Authentication function.The authentication function is based on the serial number of the motorcycle and the password of the client.In this process, the motorcycle serial number and password should be inputted into the text box, and transmittedthe message to the server.The server searches the information according to

Table .
3.1.Interval mode test result.Interval mode is the mode that the clientsubmits a request after a certain time interval.The