Merge branch 'dev' of lab.si.usi.ch:sa4-2020/the-sanmarinoes/backend into thermostat-feature

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-15 13:54:59 +02:00
commit 74b32cea83
12 changed files with 150 additions and 176 deletions

View File

@ -1,5 +1,6 @@
#Sun Apr 12 12:33:03 CEST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

View File

@ -9,32 +9,35 @@
<h1>Waiting for authentication...</h1> <h1>Waiting for authentication...</h1>
</div> </div>
<script> <script>
let malusa = document.getElementById("malusa"); let malusa = document.getElementById("malusa");
let connection = new WebSocket("ws://localhost:8080/sensor-socket"); let token = localStorage.getItem("token");
if (!token) {
token = prompt("insert authentication token");
localStorage.setItem("token", token);
}
let connection = new WebSocket("ws://localhost:8080/sensor-socket?token=" + token);
console.log("***CREATED WEBSOCKET"); console.log("***CREATED WEBSOCKET");
let authentica
connection.onopen = function(evt) { connection.onopen = function(evt) {
console.log("***ONOPEN", evt); malusa.innerHTML = "<h1>Socket is now authenticated!</h1>" +
connection.send(JSON.stringify({token: prompt("insert authentication token")})); "<img src='https://maggioni.xyz/astley.gif'>";
}; };
connection.onmessage = function(evt) { connection.onmessage = function(evt) {
console.log("***ONMESSAGE", evt); console.log("***ONMESSAGE", evt);
let data = JSON.parse(evt.data); let data = JSON.parse(evt.data);
if (data.authenticated) {
malusa.innerHTML = "<h1>Socket is now authenticated!</h1>" +
"<img src='https://maggioni.xyz/astley.gif'>";
} else if (data.authenticated === false) {
malusa.innerHTML = "<h1>Authentication error</h1>";
} else {
malusa.innerHTML += "<p><pre>" + JSON.stringify(JSON.parse(evt.data), null, 2) + "</pre></p>"; malusa.innerHTML += "<p><pre>" + JSON.stringify(JSON.parse(evt.data), null, 2) + "</pre></p>";
}
}; };
connection.onerror = function(evt) { connection.onerror = function(evt) {
console.error("***ONERROR", evt); console.error("***ONERROR", evt);
}; };
</script> </script>
</body> </body>
</html> </html>

View File

@ -77,14 +77,14 @@ public class ButtonDimmerController
@PostMapping("/{id}/lights") @PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addLight( public Set<? extends OutputDevice> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return addOutput(inputId, lightId); return addOutput(inputId, lightId);
} }
@DeleteMapping("/{id}/lights") @DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeLight( public Set<? extends OutputDevice> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return removeOutput(inputId, lightId); return removeOutput(inputId, lightId);
} }

View File

@ -2,6 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
@ -14,11 +16,11 @@ import java.util.Set;
public abstract class InputDeviceConnectionController< public abstract class InputDeviceConnectionController<
I extends InputDevice, O extends OutputDevice> { I extends InputDevice, O extends OutputDevice> {
private class IOPair { private class Connection {
private final I input; private final I input;
private final O output; private final List<O> output;
private IOPair(I input, O output) { private Connection(I input, List<O> output) {
this.input = input; this.input = input;
this.output = output; this.output = output;
} }
@ -46,31 +48,39 @@ public abstract class InputDeviceConnectionController<
this.connector = connector; this.connector = connector;
} }
private IOPair checkConnectionIDs(Long inputId, Long outputId) throws NotFoundException { private Connection checkConnectionIDs(Long inputId, List<Long> outputs)
throws NotFoundException {
final I input = final I input =
inputRepository inputRepository
.findById(inputId) .findById(inputId)
.orElseThrow(() -> new NotFoundException("input device")); .orElseThrow(() -> new NotFoundException("input device"));
final O output = final List<O> outputDevices = new ArrayList<>();
for (final Long outputId : outputs) {
outputDevices.add(
outputReposiory outputReposiory
.findById(outputId) .findById(outputId)
.orElseThrow(() -> new NotFoundException("output device")); .orElseThrow(() -> new NotFoundException("output device")));
return new IOPair(input, output); }
return new Connection(input, outputDevices);
} }
/** /**
* Implements the output device connection creation (add) route * Implements the output device connection creation (add) route
* *
* @param inputId input device id * @param inputId input device id
* @param outputId output device id * @param outputId output device id list
* @return the list of output devices attached to the input device of id inputId * @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid * @throws NotFoundException if inputId or outputId are not valid
*/ */
protected Set<? extends OutputDevice> addOutput(Long inputId, Long outputId) protected Set<? extends OutputDevice> addOutput(Long inputId, List<Long> outputId)
throws NotFoundException { throws NotFoundException {
final IOPair pair = checkConnectionIDs(inputId, outputId); final Connection pair = checkConnectionIDs(inputId, outputId);
connector.connect(pair.input, pair.output, true);
outputReposiory.save(pair.output); for (final O o : pair.output) {
connector.connect(pair.input, o, true);
}
outputReposiory.saveAll(pair.output);
return pair.input.getOutputs(); return pair.input.getOutputs();
} }
@ -78,15 +88,19 @@ public abstract class InputDeviceConnectionController<
* Implements the output device connection destruction (remove) route * Implements the output device connection destruction (remove) route
* *
* @param inputId input device id * @param inputId input device id
* @param outputId output device id * @param outputId output device id list
* @return the list of output devices attached to the input device of id inputId * @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid * @throws NotFoundException if inputId or outputId are not valid
*/ */
protected Set<? extends OutputDevice> removeOutput(Long inputId, Long outputId) protected Set<? extends OutputDevice> removeOutput(Long inputId, List<Long> outputId)
throws NotFoundException { throws NotFoundException {
final IOPair pair = checkConnectionIDs(inputId, outputId); final Connection pair = checkConnectionIDs(inputId, outputId);
connector.connect(pair.input, pair.output, false);
outputReposiory.save(pair.output); for (final O o : pair.output) {
connector.connect(pair.input, o, false);
}
outputReposiory.saveAll(pair.output);
return pair.input.getOutputs(); return pair.input.getOutputs();
} }
} }

View File

@ -70,14 +70,14 @@ public class KnobDimmerController
@PostMapping("/{id}/lights") @PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addLight( public Set<? extends OutputDevice> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return addOutput(inputId, lightId); return addOutput(inputId, lightId);
} }
@DeleteMapping("/{id}/lights") @DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeLight( public Set<? extends OutputDevice> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return removeOutput(inputId, lightId); return removeOutput(inputId, lightId);
} }

View File

@ -53,7 +53,7 @@ public class MotionSensorController {
sensor.setDetected(detected); sensor.setDetected(detected);
final MotionSensor toReturn = motionSensorService.save(sensor); final MotionSensor toReturn = motionSensorService.save(sensor);
sensorSocketEndpoint.broadcast(sensor, motionSensorService.findUser(sensor.getId())); sensorSocketEndpoint.queueDeviceUpdate(sensor, motionSensorService.findUser(sensor.getId()));
return toReturn; return toReturn;
} }

View File

@ -83,14 +83,14 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
@PostMapping("/{id}/lights") @PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addSwitchable( public Set<? extends OutputDevice> addSwitchable(
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) @PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
throws NotFoundException { throws NotFoundException {
return addOutput(inputId, switchableId); return addOutput(inputId, switchableId);
} }
@DeleteMapping("/{id}/lights") @DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeSwitchable( public Set<? extends OutputDevice> removeSwitchable(
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) @PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
throws NotFoundException { throws NotFoundException {
return removeOutput(inputId, switchableId); return removeOutput(inputId, switchableId);
} }

View File

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
/** /**
@ -9,6 +10,9 @@ import javax.persistence.Entity;
@Entity @Entity
public class KnobDimmer extends Dimmer { public class KnobDimmer extends Dimmer {
@Column
Integer intensity = 0;
public KnobDimmer() { public KnobDimmer() {
super("knobDimmer"); super("knobDimmer");
} }
@ -19,6 +23,7 @@ public class KnobDimmer extends Dimmer {
* @param intensity the intensity (must be from 0 to 100) * @param intensity the intensity (must be from 0 to 100)
*/ */
public void setLightIntensity(int intensity) { public void setLightIntensity(int intensity) {
this.intensity = intensity;
for (DimmableLight dl : getOutputs()) { for (DimmableLight dl : getOutputs()) {
dl.setIntensity(intensity); dl.setIntensity(intensity);
} }

View File

@ -64,6 +64,12 @@ public class UpdateTasks {
public void smartPlugConsumptionFakeUpdate() { public void smartPlugConsumptionFakeUpdate() {
smartPlugRepository.updateTotalConsumption(SmartPlug.AVERAGE_CONSUMPTION_KW); smartPlugRepository.updateTotalConsumption(SmartPlug.AVERAGE_CONSUMPTION_KW);
final Collection<SmartPlug> c = smartPlugRepository.findByOn(true); final Collection<SmartPlug> c = smartPlugRepository.findByOn(true);
c.forEach(s -> sensorSocketEndpoint.broadcast(s, sensorRepository.findUser(s.getId()))); c.forEach(s -> sensorSocketEndpoint.queueDeviceUpdate(s, sensorRepository.findUser(s.getId())));
}
/** Sends device updates through sensor socket in batch every one second */
@Scheduled(fixedDelay = 1000)
public void socketFlush() {
sensorSocketEndpoint.flushDeviceUpdates();
} }
} }

View File

@ -45,7 +45,7 @@ public class SensorService {
sensor.setValue(value); sensor.setValue(value);
final Sensor toReturn = sensorRepository.save(sensor); final Sensor toReturn = sensorRepository.save(sensor);
endpoint.broadcast(sensor, sensorRepository.findUser(sensor.getId())); endpoint.queueDeviceUpdate(sensor, sensorRepository.findUser(sensor.getId()));
return toReturn; return toReturn;
} }

View File

@ -1,95 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.jsonwebtoken.ExpiredJwtException;
import java.io.IOException;
import java.util.Map;
import java.util.function.BiConsumer;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/** Generates MessageHandlers for unauthenticated socket sessions */
@Component
public class AuthenticationMessageListener {
private Gson gson = GsonConfig.gson();
private JWTTokenUtils jwtTokenUtils;
private UserRepository userRepository;
@Autowired
public AuthenticationMessageListener(
JWTTokenUtils jwtTokenUtils, UserRepository userRepository) {
this.jwtTokenUtils = jwtTokenUtils;
this.userRepository = userRepository;
}
/**
* Generates a new message handler to handle socket authentication
*
* @param session the session to which authentication must be checked
* @param authorizedSetter function to call once user is authenticated
* @return a new message handler to handle socket authentication
*/
MessageHandler.Whole<String> newHandler(
final Session session, BiConsumer<User, Session> authorizedSetter) {
return new MessageHandler.Whole<>() {
@Override
public void onMessage(final String message) {
if (message == null) {
acknowledge(false);
return;
}
String token;
String username;
try {
token = gson.fromJson(message, JsonObject.class).get("token").getAsString();
username = jwtTokenUtils.getUsernameFromToken(token);
} catch (ExpiredJwtException e) {
System.err.println(e.getMessage());
acknowledge(false);
return;
} catch (Throwable ignored) {
System.out.println("Token format not valid");
acknowledge(false);
return;
}
final User user = userRepository.findByUsername(username);
if (user == null || jwtTokenUtils.isTokenExpired(token)) {
System.out.println("Token not valid");
acknowledge(false);
return;
}
// Here user is authenticated
session.removeMessageHandler(this);
// Add user-session pair in authorized list
authorizedSetter.accept(user, session);
// update client to acknowledge authentication
acknowledge(true);
}
private void acknowledge(boolean success) {
try {
session.getBasicRemote()
.sendText(gson.toJson(Map.of("authenticated", success)));
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
}

View File

@ -2,67 +2,87 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import javax.websocket.*; import javax.websocket.*;
import com.google.gson.JsonObject;
import io.jsonwebtoken.ExpiredJwtException;
import org.hibernate.annotations.common.reflection.XProperty;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** Endpoint of socket at URL /sensor-socket used to update the client with sensor information */ /**
* Endpoint of socket at URL /sensor-socket used to update the client with sensor information
*/
@Component @Component
public class SensorSocketEndpoint extends Endpoint { public class SensorSocketEndpoint extends Endpoint {
private Gson gson = GsonConfig.gson(); private Gson gson = GsonConfig.gson();
private AuthenticationMessageListener authenticationMessageListener; private UserRepository userRepository;
private Set<Session> unauthorizedClients = Collections.synchronizedSet(new HashSet<>()); private JWTTokenUtils jwtTokenUtils;
private Multimap<User, Session> authorizedClients = private Multimap<User, Session> authorizedClients =
Multimaps.synchronizedMultimap(HashMultimap.create()); Multimaps.synchronizedMultimap(HashMultimap.create());
private final Map<User, Map<Long, Device>> messages = new HashMap<>();
@Autowired @Autowired
public SensorSocketEndpoint(AuthenticationMessageListener authenticationMessageListener) { public SensorSocketEndpoint(UserRepository userRepository, JWTTokenUtils jwtTokenUtils) {
this.authenticationMessageListener = authenticationMessageListener; this.jwtTokenUtils = jwtTokenUtils;
this.userRepository = userRepository;
} }
/** /**
* Returns a synchronized set of socket sessions not yet authorized with a token * Queues a single device update for a certain user to be sent
* * @param device the device update to be sent
* @return a synchronized set of socket sessions not yet authorized with a token * @param u the user the device belongs
*/ */
public Set<Session> getUnauthorizedClients() { public void queueDeviceUpdate(Device device, User u) {
return unauthorizedClients; synchronized (messages) {
messages.putIfAbsent(u, new HashMap<>());
messages.get(u).put(device.getId(), device);
}
} }
/** /**
* Returns a synchronized User to Session multimap with authorized sessions * Sends all device updates queued to be sent in a unique WebSocket message
*
* @return a synchronized User to Session multimap with authorized sessions
*/ */
public Multimap<User, Session> getAuthorizedClients() { public void flushDeviceUpdates() {
return authorizedClients; synchronized (messages) {
for (Map.Entry<User, Map<Long, Device>> batchForUser : messages.entrySet()) {
broadcast(batchForUser.getKey(), batchForUser.getValue().values());
batchForUser.getValue().clear();
}
}
} }
/** /**
* Given a message and a user, broadcasts that message in json to all associated clients and * Given a collection of messages and a user, broadcasts that message in json to all
* returns the number of successful transfers * associated clients
* *
* @param message the message to send * @param messages the message batch to send
* @param u the user to which to send the message * @param u the user to which to send the message
* @return number of successful transfer
*/ */
public void broadcast(Object message, User u) { private void broadcast(User u, Collection<?> messages) {
if (messages.isEmpty()) return;
final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u)); final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u));
for (Session s : sessions) { for (Session s : sessions) {
try { try {
if (s.isOpen()) { if (s.isOpen()) {
s.getBasicRemote().sendText(gson.toJson(message)); s.getBasicRemote().sendText(gson.toJson(messages));
} else { } else {
authorizedClients.remove(u, s); authorizedClients.remove(u, s);
} }
@ -80,13 +100,33 @@ public class SensorSocketEndpoint extends Endpoint {
*/ */
@Override @Override
public void onOpen(Session session, EndpointConfig config) { public void onOpen(Session session, EndpointConfig config) {
unauthorizedClients.add(session); final List<String> tokenQuery = session.getRequestParameterMap().get("token");
session.addMessageHandler( User u;
authenticationMessageListener.newHandler( if (!tokenQuery.isEmpty() && (u = checkToken(tokenQuery.get(0))) != null) {
session, authorizedClients.put(u, session);
(u, s) -> { } else {
unauthorizedClients.remove(s); try {
authorizedClients.put(u, s); session.close();
})); } catch (IOException ignored) {
}
}
}
private User checkToken(String protocolString) {
String username;
try {
username = jwtTokenUtils.getUsernameFromToken(protocolString);
} catch (Throwable ignored) {
System.out.println("Token format not valid");
return null;
}
final User user = userRepository.findByUsername(username);
if (user != null && !jwtTokenUtils.isTokenExpired(protocolString)) {
return user;
} else {
return null;
}
} }
} }