Skip to content

Commit

Permalink
add DeviceInfo and Location for the android Device. update DB structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
richkmeli committed Mar 21, 2021
1 parent f4da53a commit efcc21f
Show file tree
Hide file tree
Showing 35 changed files with 592 additions and 155 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
volumes:
#- ./out/artifacts/ROOT/ROOT.war:/usr/local/tomcat/webapps/ROOT.war:z
#- ./out/artifacts/Richkware_Manager_Server/Richkware-Manager-Server.war:/usr/local/tomcat/webapps/Richkware-Manager-Server.war:z
# TODO RELEASE: update version
#- ./target/Richkware-Manager-Server :/usr/local/tomcat/webapps/Richkware-Manager-Server_EXP:z
- ./target/Richkware-Manager-Server.war :/usr/local/tomcat/webapps/Richkware-Manager-Server.war:z

Expand Down
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Dependencies -->
<jframework.version>1.2.12</jframework.version>
<jframework.version>1.2.13</jframework.version>
<!-- Webjars dependencies -->
<!-- <webjars-locator-core.version>0.45</webjars-locator-core.version>-->
<webjars-bootstrap.version>4.0.0</webjars-bootstrap.version>
Expand Down Expand Up @@ -109,8 +109,9 @@

<dependencies>
<dependency>
<!-- <groupId>com.github.richkmeli.JFramework</groupId>-->
<!-- <artifactId>auth-core</artifactId>-->
<groupId>com.github.richkmeli.JFramework</groupId>
<!-- <artifactId>JFramework</artifactId>-->
<artifactId>auth-core</artifactId>
<version>${jframework.version}</version>
</dependency>
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/it/richkmeli/rms/data/LoadDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,39 @@ CommandLineRunner initDatabase(AuthDatabaseSpringManager authDatabaseSpringManag
authDatabaseSpringManager.addUser(adminUser);
}

// add login entry
User adminUser2 = new User("[email protected]", "00000000", true);
if (!authDatabaseSpringManager.isUserPresent(adminUser2.getEmail())) {
authDatabaseSpringManager.addUser(adminUser2);
}


// test
User user = new User(RandomStringGenerator.generateAlphanumericString(4) + "@example.com", "00000000", false);
User user1 = new User("[email protected]", "00000000", false);
authDatabaseSpringManager.addUser(user);
authDatabaseSpringManager.removeUser(user.getEmail());
authDatabaseSpringManager.addUser(user1);
if(!authDatabaseSpringManager.isUserPresent(user1.getEmail())) {
authDatabaseSpringManager.addUser(user1);
}
// user1 will be deleted later in RMC test phase


Device device = new Device(RandomStringGenerator.generateAlphanumericString(4), "43.34.43.34", "40", "20-10-18", "ckeroivervioeon", "[email protected]", "", "","iid","loc");
Device device1 = new Device(RandomStringGenerator.generateAlphanumericString(4), "43.34.43.34", "40", "20-10-18", "ckeroivervioeon", "[email protected]", "", "","iid","loc");
User u1 = authDatabaseSpringManager.findUserByEmail("[email protected]");
User u2 = authDatabaseSpringManager.findUserByEmail("[email protected]");
Device device = new Device(RandomStringGenerator.generateAlphanumericString(4), "43.34.43.34", "40", "20-10-18", "ckeroivervioeon", u1, "", "", "iid", null, null);
Device device1 = new Device(RandomStringGenerator.generateAlphanumericString(4), "43.34.43.34", "40", "20-10-18", "ckeroivervioeon", u2, "", "", "iid", null, null);
deviceDatabaseSpringManager.addDevice(device);
deviceDatabaseSpringManager.addDevice(device1);
deviceDatabaseSpringManager.removeDevice(device.getName());
deviceDatabaseSpringManager.removeDevice(device1.getName());

Rmc rmc = new Rmc(adminUser.getEmail(), "test_rmc_ID");
Rmc rmc2 = new Rmc("[email protected]", "test_rmc_ID" + RandomStringGenerator.generateAlphanumericString(4));
Rmc rmc = new Rmc(u1, "test_rmc_ID");
Rmc rmc2 = new Rmc(u2, "test_rmc_ID" + RandomStringGenerator.generateAlphanumericString(4));

rmcDatabaseSpringManager.addRMC(rmc);
rmcDatabaseSpringManager.addRMC(rmc2);

authDatabaseSpringManager.removeUser(rmc.getAssociatedUser());
authDatabaseSpringManager.removeUser(rmc.getAssociatedUser().getEmail());
assert rmcDatabaseSpringManager.checkRmc(rmc.getRmcId());
authDatabaseSpringManager.removeUser(user1.getEmail());
assert rmcDatabaseSpringManager.checkRmc(rmc2.getRmcId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
// }
//
//
// // TODO ORM aggiungi foreign keys
// // _TODO ORM aggiungi foreign keys
// public List<Device> getUserDevices(String user) throws AuthDatabaseException {
// List<Device> devices = readAll(Device.class);
// if (devices != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.richkmeli.jframework.auth.data.exception.AuthDatabaseException;
import it.richkmeli.rms.data.entity.device.model.Device;
import it.richkmeli.rms.data.entity.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -14,14 +15,18 @@
@Component
public class DeviceDatabaseSpringManager implements DeviceDatabaseModel {
private static DeviceRepository deviceRepository;
private static LocationRepository locationRepository;
private static DeviceInfoRepository deviceInfoRepository;

@Autowired
public DeviceDatabaseSpringManager(DeviceRepository deviceRepository) {
public DeviceDatabaseSpringManager(DeviceRepository deviceRepository, LocationRepository locationRepository, DeviceInfoRepository deviceInfoRepository) {
this.deviceRepository = deviceRepository;
this.locationRepository = locationRepository;
this.deviceInfoRepository = deviceInfoRepository;
}

public static DeviceDatabaseSpringManager getInstance() {
return new DeviceDatabaseSpringManager(deviceRepository);
return new DeviceDatabaseSpringManager(deviceRepository, locationRepository, deviceInfoRepository);
}

@Override
Expand All @@ -41,22 +46,26 @@ public Device addDevice(Device device) throws AuthDatabaseException {

@Override
public Device editDevice(Device device) throws AuthDatabaseException {

return deviceRepository.save(device);
}

@Override
public void removeDevice(String device) throws AuthDatabaseException {
deviceRepository.deleteById(device);
public void removeDevice(String name) throws AuthDatabaseException {
Device device = deviceRepository.findDeviceByName(name);
if(device != null) {
deviceRepository.delete(device);
}
}

@Override
public Device getDevice(String name) throws AuthDatabaseException {
return deviceRepository.findById(name).orElse(null);
return deviceRepository.findDeviceByName(name);
}

@Override
public String getEncryptionKey(String name) throws AuthDatabaseException {
Device device = deviceRepository.findById(name).orElse(null);
Device device = deviceRepository.findDeviceByName(name);
if (device != null) {
return device.getEncryptionKey();
} else {
Expand All @@ -66,7 +75,7 @@ public String getEncryptionKey(String name) throws AuthDatabaseException {

@Override
public boolean editCommands(String deviceName, String commands) throws AuthDatabaseException {
Device device = deviceRepository.findById(deviceName).orElse(null);
Device device = deviceRepository.findDeviceByName(deviceName);
if (device != null) {
device.setCommands(commands);
deviceRepository.save(device);
Expand All @@ -78,7 +87,7 @@ public boolean editCommands(String deviceName, String commands) throws AuthDatab

@Override
public String getCommands(String deviceName) throws AuthDatabaseException {
Device device = deviceRepository.findById(deviceName).orElse(null);
Device device = deviceRepository.findDeviceByName(deviceName);
if (device != null) {
return device.getCommands();
} else {
Expand All @@ -88,7 +97,7 @@ public String getCommands(String deviceName) throws AuthDatabaseException {

@Override
public boolean setCommandsOutput(String deviceName, String commandsOutput) throws AuthDatabaseException {
Device device = deviceRepository.findById(deviceName).orElse(null);
Device device = deviceRepository.findDeviceByName(deviceName);
if (device != null) {
device.setCommandsOutput(commandsOutput);
deviceRepository.save(device);
Expand All @@ -100,7 +109,7 @@ public boolean setCommandsOutput(String deviceName, String commandsOutput) throw

@Override
public String getCommandsOutput(String deviceName) throws AuthDatabaseException {
Device device = deviceRepository.findById(deviceName).orElse(null);
Device device = deviceRepository.findDeviceByName(deviceName);
if (device != null) {
return device.getCommandsOutput();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package it.richkmeli.rms.data.entity.device;

import it.richkmeli.rms.data.entity.device.model.Device;
import it.richkmeli.rms.data.entity.device.model.DeviceInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface DeviceInfoRepository extends JpaRepository<DeviceInfo, String> {

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package it.richkmeli.rms.data.entity.device;

import it.richkmeli.rms.data.entity.device.model.Device;
import it.richkmeli.rms.data.entity.device.model.Location;
import it.richkmeli.rms.data.entity.user.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Locale;


@Repository
public interface DeviceRepository extends JpaRepository<Device, String> {

List<Device> findDevicesByAssociatedUser_Email(String email);

boolean existsDeviceByName(String name);
void deleteDeviceByName(String name);
Device findDeviceByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package it.richkmeli.rms.data.entity.device;

import it.richkmeli.rms.data.entity.device.model.Device;
import it.richkmeli.rms.data.entity.device.model.Location;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface LocationRepository extends JpaRepository<Location, String> {

}
82 changes: 59 additions & 23 deletions src/main/java/it/richkmeli/rms/data/entity/device/model/Device.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package it.richkmeli.rms.data.entity.device.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import it.richkmeli.rms.data.entity.user.model.User;
import it.richkmeli.rms.util.GeoLocation;
import org.hibernate.validator.constraints.Length;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
Expand All @@ -25,30 +24,47 @@ public class Device {
@Length(max = 32)
private String encryptionKey;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name="user_email")
private User associatedUser;
@Length(max = 1000)
private String commands;
@Length(max = 1000)
private String commandsOutput;
@Length(max = 64)
private String installationId;
@Length(max = 200)
private String location;
@OneToOne(mappedBy = "device", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}/*, orphanRemoval = true*/)
@PrimaryKeyJoinColumn
@JsonBackReference
private Location location;
@OneToOne(mappedBy = "device", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}/*, orphanRemoval = true*/)
@PrimaryKeyJoinColumn
@JsonBackReference
private DeviceInfo deviceInfo;

public Device() {
}

public Device(String name, String ip, String serverPort, String lastConnection, String encryptionKey, String associatedUser, String commands, String commandsOutput, String installationId, String location) {
public Device(String name, String ip, String serverPort, String lastConnection, String encryptionKey, User associatedUser, String commands, String commandsOutput, String installationId, Location location, DeviceInfo deviceInfo) {
this.name = name;
this.ip = ip;
this.serverPort = serverPort;
this.lastConnection = lastConnection;
this.encryptionKey = encryptionKey;
this.associatedUser = new User(associatedUser);
this.associatedUser = associatedUser;
this.commands = commands;
this.commandsOutput = commandsOutput;
this.installationId = installationId;
this.location = location;
if (this.location != null) {
this.location.setId(name);
this.location.setDevice(this);
}
this.deviceInfo = deviceInfo;
if (this.deviceInfo != null) {
this.deviceInfo.setId(name);
this.deviceInfo.setDevice(this);
}
}

public String getInstallationId() {
Expand All @@ -59,14 +75,6 @@ public void setInstallationId(String installationId) {
this.installationId = installationId;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -107,18 +115,14 @@ public void setEncryptionKey(String encryptionKey) {
this.encryptionKey = encryptionKey;
}

public String getAssociatedUser() {
return associatedUser.getEmail();
public User getAssociatedUser() {
return associatedUser;
}

public void setAssociatedUser(User associatedUser) {
this.associatedUser = associatedUser;
}

public void setAssociatedUser(String associatedUser) {
this.associatedUser = new User(associatedUser);
}

public String getCommands() {
return commands;
}
Expand All @@ -135,6 +139,37 @@ public void setCommandsOutput(String commandsOutput) {
this.commandsOutput = commandsOutput;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

public DeviceInfo getDeviceInfo() {
return deviceInfo;
}

public void setDeviceInfo(DeviceInfo deviceInfo) {
this.deviceInfo = deviceInfo;
}

// called by the serialization, location is not serialized due to JsonBackReference
public String getLocationAsPosition() {
return GeoLocation.getPositionFromCoordinates(location.getLongitude(), location.getLatitude(), location.getAltitude());
}

// called by the serialization, location is not serialized due to JsonBackReference
public String getAssociatedUserEmail() {
return associatedUser.getEmail();
}

// called by the serialization, location is not serialized due to JsonBackReference
public String getDeviceInfoDevName() {
return deviceInfo.getDevName();
}

@Override
public String toString() {
String output = "";
Expand All @@ -147,7 +182,8 @@ public String toString() {
+ getCommands() + ", "
+ getCommandsOutput() + ", "
+ getInstallationId() + ", "
+ getLocation()
+ getLocation() + ", "
+ getDeviceInfo()
+ "}";

return output;
Expand Down
Loading

0 comments on commit efcc21f

Please sign in to comment.