ScreenComponent.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package cc.iotkit.screen.staticres;
  2. import cc.iotkit.common.enums.ErrCode;
  3. import cc.iotkit.common.exception.BizException;
  4. import cc.iotkit.model.screen.ScreenApi;
  5. import cc.iotkit.screen.api.ScreenApiHandle;
  6. import cc.iotkit.screen.config.ScreenConfig;
  7. import io.vertx.core.Future;
  8. import io.vertx.core.Vertx;
  9. import lombok.SneakyThrows;
  10. import lombok.extern.slf4j.Slf4j;
  11. import java.util.List;
  12. import java.util.concurrent.CountDownLatch;
  13. /**
  14. * @Author:tfd
  15. * @Date:2023/6/25 16:01
  16. */
  17. @Slf4j
  18. public class ScreenComponent {
  19. private Vertx vertx;
  20. private CountDownLatch countDownLatch;
  21. private String deployedId;
  22. private ScreenApiHandle apiHandle;
  23. private ScreenVerticle screenVerticle;
  24. public List<ScreenApi> getScreenApis() {
  25. return apiHandle.screenApis;
  26. }
  27. public void debugMode(boolean state) {
  28. apiHandle.debugMode=state;
  29. }
  30. public void create(int port, String packageName, ScreenConfig screenConfig) {
  31. vertx = Vertx.vertx();
  32. screenVerticle = new ScreenVerticle(port,packageName,screenConfig);
  33. }
  34. public void setApiHandle(ScreenApiHandle screenApiHandle) {
  35. this.apiHandle=screenApiHandle;
  36. }
  37. public void previewApis(List<ScreenApi> screenApis) {
  38. this.apiHandle.setScreenApis(screenApis);
  39. }
  40. public void publish() {
  41. try {
  42. screenVerticle.setApiHandler(apiHandle);
  43. countDownLatch = new CountDownLatch(1);
  44. Future<String> future = vertx.deployVerticle(screenVerticle);
  45. future.onSuccess((s -> {
  46. deployedId = s;
  47. countDownLatch.countDown();
  48. }));
  49. future.onFailure((e) -> {
  50. countDownLatch.countDown();
  51. log.error("publish screen failed", e);
  52. });
  53. countDownLatch.await();
  54. future.succeeded();
  55. } catch (Throwable e) {
  56. throw new BizException(ErrCode.SCREEN_PUBLISH_ERROR, e);
  57. }
  58. }
  59. @SneakyThrows
  60. public void unPublish() {
  61. screenVerticle.stop();
  62. Future<Void> future = vertx.undeploy(deployedId);
  63. future.onSuccess(unused -> log.info("unPublish screen success"));
  64. }
  65. }