public interface RoomRepository extends JpaRepository<Room,Integer> {
@Query("FROM Room b where b.index = :Index")
Room findByIndex(@Param("Index") String index);
}
@RestController
public class StartController {
@Autowired
private RoomService roomService;
@RequestMapping(value = "/start", method = RequestMethod.GET)
@ResponseBody
public void startArduinoScan() throws SerialPortException {
//Попробуем завести наш АрдуиноКоннектер
ArduinoConfig arduinoConfig = new ArduinoConfig();
arduinoConfig.arduinoConnection(roomService);
}
@RequestMapping(value = "/index/{index}", method = RequestMethod.GET)
@ResponseBody
public Room getRoomByIndex(@PathVariable String index){
return roomService.findByIndex(index);
}
@Configuration
@ComponentScan("com.kovalchuk.server")
@EnableJpaRepositories("com.kovalchuk.server.repository")
public class ArduinoConfig {
private static SerialPort serialPort;
public static SerialPort getSerialPort() {
return serialPort;
}
public void arduinoConnection(RoomService roomService) throws SerialPortException {
serialPort = new SerialPort("COM3");
try{
serialPort.openPort();
serialPort.setParams(9600,8,1,0);
serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
serialPort.addEventListener(new ArduinoEventsListener());
}
catch (SerialPortException ex){
System.out.println(ex);
}
}
}
@ComponentScan("com.kovalchuk.server")
@EnableJpaRepositories("com.kovalchuk.server.repository")
public class ArduinoEventsListener implements SerialPortEventListener {
private SerialPort serialPort = ArduinoConfig.getSerialPort();
@Autowired
private CommitRepository commitRepository;
@Autowired
private RoomRepository roomRepository;
@Override
public void serialEvent(SerialPortEvent event) {
try {
String buffer = serialPort.readString(15);
String[] data = buffer.split(":");
Commit commit = new Commit();
commit.setTemperature(Double.parseDouble(data[0]));
commit.setWet(data[1]);
commit.setDate(new Date());
Room room = roomRepository.findByIndex(data[2]);
commit.setRoom(room);
commitRepository.save(commit);
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}
package com.kovalchuk.server.entity;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
@Entity
@Table (name= "room")
public class Room {
@Id
@Column(name ="idRoom")
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long id;
@Column(name="Index")
private String index;
@Column(name = "Type")
private String type;
@Column(name = "Status")
private Commit status;
public Room(String index, String type, Commit status) {
this.index = index;
this.type = type;
this.status = status;
}
public Room(String index, String type) {
this.index = index;
this.type = type;
}
public Room() {
}
public Commit getStatus() {
return status;
}
public void setStatus(Commit status) {
this.status = status;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
CREATE TABLE `room` (
`idRoom` bigint(20) NOT NULL AUTO_INCREMENT,
`Index` varchar(5) NOT NULL,
`Type` varchar(5) DEFAULT NULL,
`Status` bigint(20) NOT NULL,
PRIMARY KEY (`idRoom`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
public interface RoomRepository extends JpaRepository<Room, Integer> {
Room findByIndex(String index);
}
package com.kovalchuk.server.controller;
import com.kovalchuk.server.config.arduino.ArduinoConfig;
import com.kovalchuk.server.entity.Room;
import com.kovalchuk.server.services.interf.CommitService;
import com.kovalchuk.server.services.interf.RoomService;
import jssc.SerialPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class StartController {
@Autowired
private RoomService roomService;
@Autowired
private CommitService commitService;
@RequestMapping(value = "/start", method = RequestMethod.GET)
@ResponseBody
public void startArduinoScan() throws SerialPortException {
//Попробуем завести наш АрдуиноКоннектер
ArduinoConfig arduinoConfig = new ArduinoConfig(roomService,commitService);
arduinoConfig.arduinoConnection();
}
}
package com.kovalchuk.server.config.arduino;
import com.kovalchuk.server.services.interf.CommitService;
import com.kovalchuk.server.services.interf.RoomService;
import jssc.SerialPort;
import jssc.SerialPortException;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@ComponentScan("com.kovalchuk.server")
@EnableJpaRepositories("com.kovalchuk.server.repository")
public class ArduinoConfig {
private static SerialPort serialPort;
private static RoomService roomService;
private static CommitService commitService;
public ArduinoConfig() {
}
public ArduinoConfig(RoomService roomService, CommitService commitService) {
this.roomService = roomService;
this.commitService = commitService;
}
public static RoomService getRoomService() {
return roomService;
}
public static CommitService getCommitService() {
return commitService;
}
public static SerialPort getSerialPort() {
return serialPort;
}
public void arduinoConnection() throws SerialPortException {
serialPort = new SerialPort("COM3");
try{
serialPort.openPort();
serialPort.setParams(9600,8,1,0);
serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
serialPort.addEventListener(new ArduinoEventsListener());
}
catch (SerialPortException ex){
if (serialPort.isOpened()) serialPort.closePort();
}
}
}
package com.kovalchuk.server.config.arduino;
import com.kovalchuk.server.entity.Commit;
import com.kovalchuk.server.entity.Room;
import com.kovalchuk.server.services.interf.CommitService;
import com.kovalchuk.server.services.interf.RoomService;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import java.util.Date;
@ComponentScan("com.kovalchuk.server")
@EnableJpaRepositories("com.kovalchuk.server.repository")
public class ArduinoEventsListener implements SerialPortEventListener {
private SerialPort serialPort = ArduinoConfig.getSerialPort();
private RoomService roomService = ArduinoConfig.getRoomService();
private CommitService commitService = ArduinoConfig.getCommitService();
@Override
public void serialEvent(SerialPortEvent event) {
try {
String buffer = serialPort.readString(15);
String[] data = buffer.split(":");
Commit commit = new Commit();
commit.setTemperature(Double.parseDouble(data[0]));
commit.setWet(data[1]);
commit.setDate(new Date());
Room room = roomService.findByIndex(data[2]);
commit.setRoom(room);
commitService.save(commit);
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}