Commit 74e04ce0 authored by 0Tyler's avatar 0Tyler

RemoteService

parent a5e04465
This diff is collapsed.
...@@ -8,7 +8,6 @@ import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; ...@@ -8,7 +8,6 @@ import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.p3p.*; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.p3p.*;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService; import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService; import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
...@@ -21,27 +20,18 @@ public class DefaultData implements ApplicationRunner { ...@@ -21,27 +20,18 @@ public class DefaultData implements ApplicationRunner {
private DeviceService deviceService; private DeviceService deviceService;
private PrivacyPolicyReportService privacyPolicyReportService; private PrivacyPolicyReportService privacyPolicyReportService;
// private SensitiveUserService sensitiveUserService;
@Autowired @Autowired
public DefaultData(DeviceService deviceService, public DefaultData(DeviceService deviceService,
PrivacyPolicyReportService privacyPolicyReportService, PrivacyPolicyReportService privacyPolicyReportService) {
SensitiveUserService sensitiveUserService) {
this.deviceService = deviceService; this.deviceService = deviceService;
this.privacyPolicyReportService = privacyPolicyReportService; this.privacyPolicyReportService = privacyPolicyReportService;
// this.sensitiveUserService = sensitiveUserService;
} }
@Override @Override
public void run(ApplicationArguments args) { public void run(ApplicationArguments args) {
// User user = User.builder()
// .account("test")
// .password("test")
// .build();
// sensitiveUserService.add(user);
Device oxygenDevice = Device.builder() Device oxygenDevice = Device.builder()
.udn("a1252c49-4188-4e6d-a32e-66604c664fb8") .udn("a1252c49-4188-4e6d-a32e-66604c664fb8")
.name("指尖式血氧機") .name("指尖式血氧機")
...@@ -119,7 +109,4 @@ public class DefaultData implements ApplicationRunner { ...@@ -119,7 +109,4 @@ public class DefaultData implements ApplicationRunner {
privacyPolicyReportService.add(oxygenPrivacyPolicyReport); privacyPolicyReportService.add(oxygenPrivacyPolicyReport);
} }
} }
\ No newline at end of file
...@@ -43,10 +43,6 @@ public class DeviceController { ...@@ -43,10 +43,6 @@ public class DeviceController {
.orElseGet(()->ResponseEntity.noContent().build()); .orElseGet(()->ResponseEntity.noContent().build());
} }
// @GetMapping
// public ResponseEntity<Iterable<Device>> readDevicesByUser(User user) {
// }
@PostMapping("/privacy") @PostMapping("/privacy")
public ResponseEntity<PrivacyPolicyReport> addPrivacyPolicyReport(@RequestBody PrivacyPolicyReport privacyPolicy) { public ResponseEntity<PrivacyPolicyReport> addPrivacyPolicyReport(@RequestBody PrivacyPolicyReport privacyPolicy) {
return privacyPolicyReportService.add(privacyPolicy) return privacyPolicyReportService.add(privacyPolicy)
......
...@@ -2,7 +2,6 @@ package edu.prlab.tyler.iotgateway.cloud.controllers; ...@@ -2,7 +2,6 @@ package edu.prlab.tyler.iotgateway.cloud.controllers;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService; import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -15,13 +14,10 @@ import java.util.Optional; ...@@ -15,13 +14,10 @@ import java.util.Optional;
@RestController @RestController
public class GatewayController { public class GatewayController {
private PrivacyChoiceService privacyChoiceService; private PrivacyChoiceService privacyChoiceService;
private SensitiveUserService sensitiveUserService;
@Autowired @Autowired
public GatewayController(PrivacyChoiceService privacyChoiceService, public GatewayController(PrivacyChoiceService privacyChoiceService) {
SensitiveUserService sensitiveUserService) {
this.privacyChoiceService = privacyChoiceService; this.privacyChoiceService = privacyChoiceService;
this.sensitiveUserService = sensitiveUserService;
} }
@PostMapping("/choice") @PostMapping("/choice")
......
package edu.prlab.tyler.iotgateway.cloud.repositories;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SensitiveUserRepository extends JpaRepository<User, Long> {
}
package edu.prlab.tyler.iotgateway.cloud.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.repositories.SensitiveUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class SensitiveUserService {
private SensitiveUserRepository repository;
@Autowired
public SensitiveUserService(SensitiveUserRepository repository) {
this.repository = repository;
}
public Optional<User> add(User pojo) {
return Optional.of(repository.save(pojo));
}
}
...@@ -16,7 +16,6 @@ public class DefaultData implements ApplicationRunner { ...@@ -16,7 +16,6 @@ public class DefaultData implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
SensitiveUser defaultUser = userService.registerNewUser(SensitiveUser.builder().account("testUser0").password("testUser0").build()).get(); userService.registerNewUser(SensitiveUser.builder().account("testUser0").password("testUser0").build()).get();
System.out.println(defaultUser.toString());
} }
} }
...@@ -4,15 +4,12 @@ import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; ...@@ -4,15 +4,12 @@ import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.model.PrivacyChoiceResponse; import edu.prlab.tyler.iotgateway.gateway.model.PrivacyChoiceResponse;
import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import edu.prlab.tyler.iotgateway.gateway.services.DeviceService; import edu.prlab.tyler.iotgateway.gateway.services.DeviceService;
import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService; import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController @RestController
public class GatewayController { public class GatewayController {
......
...@@ -3,6 +3,7 @@ package edu.prlab.tyler.iotgateway.gateway.services; ...@@ -3,6 +3,7 @@ package edu.prlab.tyler.iotgateway.gateway.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
...@@ -11,12 +12,14 @@ import org.springframework.stereotype.Service; ...@@ -11,12 +12,14 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier;
@Service @Service
public class RemoteService { public class RemoteService {
private RestTemplate template; private RestTemplate template;
private String cloudAddress; private String cloudAddress;
@Autowired
public RemoteService(RestTemplate template) { public RemoteService(RestTemplate template) {
this.template = template; this.template = template;
} }
...@@ -27,51 +30,38 @@ public class RemoteService { ...@@ -27,51 +30,38 @@ public class RemoteService {
} }
public Optional<Device> readDevice(String udn) { public Optional<Device> readDevice(String udn) {
try { return tryCatch(() -> template.exchange(cloudAddress + "/device/" + udn, HttpMethod.GET, null,
return Optional.of(template.exchange(cloudAddress + "/device/" + udn, HttpMethod.GET, null, Device.class).getBody()); Device.class).getBody(), "Read device error");
} catch (Exception e) {
System.out.println("Read device error");
return Optional.empty();
}
} }
public Optional<Iterable<Device>> readDevices() { public Optional<Iterable<Device>> readDevices() {
try { return tryCatch(() -> template.exchange(cloudAddress + "/device", HttpMethod.GET, null,
return Optional.of(template.exchange(cloudAddress + "/device", HttpMethod.GET, null,
new ParameterizedTypeReference<Iterable<Device>>() { new ParameterizedTypeReference<Iterable<Device>>() {
}).getBody()); }).getBody(), "Read devices error");
} catch (Exception e) {
System.out.println("Read devices error");
return Optional.empty();
}
} }
public Optional<PrivacyPolicyReport> readRelatedPrivacyPolicies(String udn) { public Optional<PrivacyPolicyReport> readRelatedPrivacyPolicies(String udn) {
try { return tryCatch(() -> template.exchange(cloudAddress + "/privacy/" + udn, HttpMethod.GET, null,
return Optional.of(template.exchange(cloudAddress + "/privacy/" + udn, HttpMethod.GET, null, PrivacyPolicyReport.class).getBody(), "Read privacyPolicyReport error");
PrivacyPolicyReport.class).getBody());
} catch (Exception e) {
System.out.println("Read privacyPolicyReport error");
return Optional.empty();
}
} }
public Optional<PrivacyChoice> setPrivacyChoice(PrivacyChoice choice) { public Optional<PrivacyChoice> setPrivacyChoice(PrivacyChoice choice) {
try { return tryCatch(() -> template.exchange(cloudAddress + "/choice", HttpMethod.POST,
return Optional.of(template.exchange(cloudAddress + "/choice", HttpMethod.POST, new HttpEntity(choice), PrivacyChoice.class).getBody()); new HttpEntity(choice), PrivacyChoice.class).getBody(), "Set privacyChoice error");
} catch (Exception e) {
System.out.println("Set privacyChoice error");
return Optional.empty();
}
} }
public Optional<Iterable<PrivacyChoice>> readPrivacyChoices() { public Optional<Iterable<PrivacyChoice>> readPrivacyChoices() {
try { return tryCatch(() -> template.exchange(cloudAddress + "/choice", HttpMethod.GET, null,
return Optional.of(template.exchange(cloudAddress + "/choice", HttpMethod.GET, null,
new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { new ParameterizedTypeReference<Iterable<PrivacyChoice>>() {
}).getBody()); }).getBody(), "Read privacyChoice error");
}
public <T> Optional<T> tryCatch(Supplier<T> supplier, String errorMessage) {
try {
return Optional.of(supplier.get());
} catch (Exception e) { } catch (Exception e) {
System.out.println("Read privacyChoice error"); System.out.println(errorMessage);
e.printStackTrace();
return Optional.empty(); return Optional.empty();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment