## 4 Methodology and Prototype Implementation
### Implementation of the Decentralized EMS  
The automated decentralized EMS is responsible for managing the charging process, while considering user inputs and the restrictions posed by the DSMgr. In order to do so, the EMS receives global parameters from the aEMP and commands from the consumer, optimizes the charging strategies, and sends control signals to the charging station. To be able to collect user inputs, the EMS includes a web interface for interacting with the inhabitant of the building; i.e., the consumer. Via the web interface, the consumer can start and stop the charging process, and give the necessary information for the cost optimization; that is, the time span until the EV leaves for the next ride and the desired energy to be charged. Furthermore, the automated EMS receives daily updated time-variable electricity prices for the next 72 h, and real-time smart grid traffic light signals from the aEMP. Depending on the grid state, which is reflected in the traffic light phase, the EMS must adjust the power to the requested level. Taking the stated parameters into consideration, the automated EMS determines the charging strategy exhibiting minimal costs by solving an optimization problem. Due to the simple problem structure, it is possible to compute the optimal charging strategy with the help of a heuristic. The optimal solution is found by sorting the time steps by the associated price and repeatedly scheduling charging powers until the desired amount of energy is reached. As the focus of this paper lies on the SMGW infrastructure rather than the approaches for DSM and energy management, we refer to the source code provided with this paper for further information regarding this heuristic. The strategy is updated constantly to respond to new grid states, and the EV charging station is controlled accordingly. By sending suitable time-variable electricity prices to the consumer, the presented optimization enables the DSMgr to exploit the flexibility of the charging station.  
The individual components of the EMS, that is, the optimization and scheduling logic, the user interface, and the communication handler (ComHandler), exchange information via a message broker. As message broker, Mosquito ([[https://mosquitto.org/](https://mosquitto.org/)]([https://mosquitto.org/](https://mosquitto.org/))) is used, which implements the \"MQTT\" protocol [39]. MQTT provides a publish and subscribe mechanism for exchanging information. Data are published to so-called topics, and each subscriber of the specific topic receives the data. The protocol is used internally, and externally for the communication with the aEMP, using the offered bridge function of MQTT. Yet, the ComHandler module is needed as an additional interface between Mosquito and the SMGW. Depending on the HKS3-5 used, the ComHandler has to be adjusted. For HKS3, the ComHandler encrypts the MQTT message within the TLS protocol, wraps it in the SOCKSv5 protocol, and sends the message to the SMGW. From the aEMP, it receives messages via the SMGW, decrypts the data, and sends the decrypted MQTT message internally to the message broker. Furthermore, the ComHandler ensures that the connection to the aEMP remains open. If the connection is disrupted, the ComHandler automatically tries to restart the connection by requesting a new initiation of the transparent data tunnel. For HKS4 and HKS5, the ComHandler encrypts the message within the TLS protocol and sends it directly to the SMGW. However, in HKS4 and HKS5 the ComHandler embodies a TLS server and can only communicate with the SMGW if a connection is opened by the SMGW.