package com.cesca.sensors.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.cesca.sensors.entity.DHTSensor;

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

@Controller
public class DHTSensorSerialMonitorService implements SerialPortEventListener {

	private SerialPort serialPort;
	private String output = "";

	private Logger log = Logger.getLogger(this.getClass());

	public DHTSensorSerialMonitorService() {

		this.serialPort = new SerialPort("COM5");
		try {
			serialPort.openPort();
			serialPort.setParams(9600, 8, 1, 0);// Set params.
			serialPort.addEventListener(this);// Add
		} catch (SerialPortException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Autowired
	public DHTSensorService dhtSensorService;

	@Override
	public void serialEvent(SerialPortEvent event) {
		log.debug("Event received");

		if (event.isRXCHAR()) {

			try {
				String serialOutput = this.serialPort.readString();
				serialOutput.replaceAll("null", "");
				this.output += serialOutput;

				String[] lines = output.split("\n\r");

				String lineToBeProcessed = null;

				for (String line : lines) {

					log.info(line.length());
					if (line.length() == 11) {
						lineToBeProcessed = line;
					}
				}

				if (lineToBeProcessed != null) {
					this.processSerialLine(lineToBeProcessed);
				}

			} catch (SerialPortException e) { // TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			log.debug("Event is not RXCHAR");
			log.debug("Event : " + event.toString());
		}
	}

	private void processSerialLine(String lineToBeProcessed) {
		log.info("Processing: " + lineToBeProcessed);

		String[] values = lineToBeProcessed.split(" ");

		long collectedTimeStamp = System.currentTimeMillis();
		float humidity = Float.parseFloat(values[0] + "f");
		float temperature = Float.parseFloat(values[1] + "f");

		DHTSensor dht = new DHTSensor(collectedTimeStamp, humidity, temperature);

		this.dhtSensorService.addDHTSensorData(dht);

		this.output = "";
	}

}
@@ -1240,17 +1240,16 @@
 %5Cn%5Cr%22);%0A
-%0A
 %09%09%09%09Stri
@@ -1314,71 +1314,162 @@
 ) %7B%0A
-%0A
 %09%09%09%09%09
-log.info(line.length());%0A%09%09%09%09%09if (line.length() == 11
+String%5B%5D values = line.split(%22 %22);%0A%09%09%09%09%09if ( (line.length() == 11) && (values.length == 2)) %7B%0A%09%09%09%09%09%09if (values%5B0%5D.length() == values%5B1%5D.length()
 ) %7B%0A
+%09
 %09%09%09%09
@@ -1501,17 +1501,31 @@
 e;%0A%09%09%09%09%09
-%7D
+%09%7D%0A%09%09%09%09%09%7D%09%09%09%09%09%09
 %0A%09%09%09%09%7D%0A%0A
@@ -2147,17 +2147,70 @@
 + %22f%22);%0A
+%09%09
 %0A
+%09%09if ( (humidity %3C 100) && (temperature %3C 100) )%7B%0A%09
 %09%09DHTSen
@@ -2276,24 +2276,25 @@
 rature);%0A%0A%09%09
+%09
 this.dhtSens
@@ -2325,16 +2325,23 @@
 ta(dht);
+%09%09%09%0A%09%09%7D
 %0A%0A%09%09this

Add Serial output sanitizer