com.pw.ism.controllers.CommunicationController.java Source code

Java tutorial

Introduction

Here is the source code for com.pw.ism.controllers.CommunicationController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pw.ism.controllers;

import com.pw.ism.heartbeat.Heartbeat;
import com.pw.ism.heartbeat.HeartbeatRepository;
import com.pw.ism.message.Message;
import com.pw.ism.message.MessageRepository;
import com.pw.ism.message.AsiNetworkReport;
import java.time.Instant;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author NRS
 */
@RestController
public class CommunicationController {

    private final static Logger LOGGER = LoggerFactory.getLogger(CommunicationController.class.getName());

    private final MessageRepository messageRepository;

    private final HeartbeatRepository heartbeatRepository;

    @Autowired
    public CommunicationController(MessageRepository messageRepository, HeartbeatRepository heartbeatRepository) {
        this.messageRepository = messageRepository;
        this.heartbeatRepository = heartbeatRepository;
    }

    @RequestMapping(value = "/newmessage", method = RequestMethod.POST, headers = {
            "Content-type=application/json" })
    public ResponseEntity<String> newReport(@Valid @RequestBody AsiNetworkReport report,
            BindingResult bindingResults) {

        if (bindingResults.hasErrors()) {
            LOGGER.info("JSON not correct, BADREQUEST! Count: {}", bindingResults.getFieldErrorCount());
            return new ResponseEntity<>("NOK!", HttpStatus.BAD_REQUEST);
        } else {
            LOGGER.info("new post for message, customer: {}, network: {}, text: {}, sensor: {}", new Object[] {
                    report.getCustomer(), report.getNetwork(), report.getText(), report.getSensor() });
            Message message = new Message(report.getCustomer(), report.getNetwork(),
                    "Sensor ID: " + report.getSensor() + " Message: " + report.getText());
            messageRepository.save(message);
            return new ResponseEntity<>("OK", HttpStatus.OK);
        }

    }

    @RequestMapping(value = "/addheartbeat", method = RequestMethod.POST, headers = {
            "Content-type=application/json" })
    public ResponseEntity<String> addHeartbeat(@Valid @RequestBody Heartbeat hb, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            LOGGER.info("Bad request!");
            return new ResponseEntity<>("NOK!", HttpStatus.BAD_REQUEST);
        } else {
            heartbeatRepository.addHeartbeat(hb, Instant.now());
            return new ResponseEntity<>("OK", HttpStatus.OK);
        }
    }
}