diff --git a/.gitignore b/.gitignore index 8ca4a74..cf073b0 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ build/ /config/config-typescript/* /config/editor/* /docs/* +/docker/ha/sbc2ha.jar diff --git a/app/src/main/java/com/dfi/sbc2ha/Sbc2ha.java b/app/src/main/java/com/dfi/sbc2ha/Sbc2ha.java index c0d1566..d6c58e6 100644 --- a/app/src/main/java/com/dfi/sbc2ha/Sbc2ha.java +++ b/app/src/main/java/com/dfi/sbc2ha/Sbc2ha.java @@ -65,7 +65,7 @@ private Sbc2ha(long startTime) { public static void main(String[] args) { try { long startTime = System.currentTimeMillis(); - log.info("App starting {}", Version.VERSION); + log.info("App starting {}", Version.getVersion()); configFile = getConfigFile(args); sbc2ha = new Sbc2ha(startTime); diff --git a/app/src/main/java/com/dfi/sbc2ha/Version.java b/app/src/main/java/com/dfi/sbc2ha/Version.java index 84d8e71..4ef73f4 100644 --- a/app/src/main/java/com/dfi/sbc2ha/Version.java +++ b/app/src/main/java/com/dfi/sbc2ha/Version.java @@ -1,8 +1,13 @@ package com.dfi.sbc2ha; public class Version { - public static final String VERSION = "java-0.0.6"; - public static String getVersion(){ - return Version.class.getPackage().getImplementationVersion(); + private static final String VERSION = "java-0.0.7"; + + public static String getVersion() { + String implementationVersion = Version.class.getPackage().getImplementationVersion(); + if (implementationVersion == null) { + return VERSION; + } + return implementationVersion; } } diff --git a/app/src/main/java/com/dfi/sbc2ha/components/platform/ha/autodiscovery/message/Availability.java b/app/src/main/java/com/dfi/sbc2ha/components/platform/ha/autodiscovery/message/Availability.java index 8553170..75d7e96 100644 --- a/app/src/main/java/com/dfi/sbc2ha/components/platform/ha/autodiscovery/message/Availability.java +++ b/app/src/main/java/com/dfi/sbc2ha/components/platform/ha/autodiscovery/message/Availability.java @@ -1,6 +1,9 @@ package com.dfi.sbc2ha.components.platform.ha.autodiscovery.message; import com.dfi.sbc2ha.Version; +import com.dfi.sbc2ha.components.platform.bus.ModbusFactory; +import com.dfi.sbc2ha.components.platform.ha.autodiscovery.HaDeviceType; +import com.dfi.sbc2ha.components.platform.ha.autodiscovery.SbcDeviceType; import com.dfi.sbc2ha.components.sensor.modbus.ModbusSensor; import com.dfi.sbc2ha.config.sbc2ha.definition.actuator.ActuatorConfig; import com.dfi.sbc2ha.config.sbc2ha.definition.actuator.CoverConfig; @@ -16,16 +19,16 @@ import com.dfi.sbc2ha.config.sbc2ha.definition.sensor.modbus.ModbusSensorDefinition; import com.dfi.sbc2ha.config.sbc2ha.definition.sensor.modbus.Register; import com.dfi.sbc2ha.config.sbc2ha.definition.sensor.oneWire.therm.DS18B20; -import com.dfi.sbc2ha.components.platform.bus.ModbusFactory; -import com.dfi.sbc2ha.components.platform.ha.autodiscovery.HaDeviceType; -import com.dfi.sbc2ha.components.platform.ha.autodiscovery.SbcDeviceType; import com.dfi.sbc2ha.services.state.sensor.ButtonState; +import com.dfi.sbc2ha.web.Server; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import lombok.extern.slf4j.Slf4j; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.*; import java.util.stream.Collectors; @@ -330,7 +333,15 @@ public DeviceAvailability(String topic, String model) { identifiers.add(topic); this.model = model; name = topic; - swVersion = Version.VERSION; + swVersion = Version.getVersion(); + + try { + InetAddress localHost = InetAddress.getLocalHost(); + configurationUrl = "http://" + localHost.getHostAddress() + ":" + Server.getPort(); + } catch (UnknownHostException ignored) { + + } + } } diff --git a/app/src/main/java/com/dfi/sbc2ha/components/platform/mqtt/MqttPlatform.java b/app/src/main/java/com/dfi/sbc2ha/components/platform/mqtt/MqttPlatform.java index da63a0b..955079b 100644 --- a/app/src/main/java/com/dfi/sbc2ha/components/platform/mqtt/MqttPlatform.java +++ b/app/src/main/java/com/dfi/sbc2ha/components/platform/mqtt/MqttPlatform.java @@ -66,7 +66,7 @@ private void prepareAutodiscovery() { manager.addDisplayLine("prepare autodiscovery"); Availability.setTopicPrefix(mqttConfig.getTopicPrefix()); Availability.setModel("boneIO Relay Board"); - Availability.setVersion(Version.VERSION); + Availability.setVersion(Version.getVersion()); } } diff --git a/app/src/main/java/com/dfi/sbc2ha/components/platform/oled/Display.java b/app/src/main/java/com/dfi/sbc2ha/components/platform/oled/Display.java index 28629d1..5fdc395 100644 --- a/app/src/main/java/com/dfi/sbc2ha/components/platform/oled/Display.java +++ b/app/src/main/java/com/dfi/sbc2ha/components/platform/oled/Display.java @@ -272,7 +272,7 @@ private void drawUptime(List lines) { //g2d.drawString("java", 73, 15); g2d.setFont(fontSmall); - g2d.drawString("version: " + Version.VERSION, 3, 30); + g2d.drawString("version: " + Version.getVersion(), 3, 30); try { g2d.drawString((String) lines.get(0), 3, 42); diff --git a/app/src/main/java/com/dfi/sbc2ha/web/Server.java b/app/src/main/java/com/dfi/sbc2ha/web/Server.java index a72dbea..d72e178 100644 --- a/app/src/main/java/com/dfi/sbc2ha/web/Server.java +++ b/app/src/main/java/com/dfi/sbc2ha/web/Server.java @@ -35,12 +35,14 @@ public class Server { + public static final int PORT = 8080; private final String configFile; private final EventBus eventBus = EventBus.getDefault(); public Server(String configFile) { this.configFile = configFile; - port(8080); + int port = getPort(); + port(port); staticFiles.location("/editor"); webSocket("/ws/device", DeviceWebsocket.class); webSocket("/ws/logs", LogWebsocket.class); @@ -64,7 +66,11 @@ public Server(String configFile) { }); } - private Route getConfigJson() { + public static int getPort() { + return PORT; + } + + private Route getConfigJson() { return (request, res) -> { String filePath = ConfigProvider.getCachedFileName(configFile); byte[] bytes = Files.readAllBytes(Paths.get(filePath)); @@ -90,17 +96,10 @@ private Route renderIndex() { private Route convert() { return (request, response) -> { try { - - - - - List uploadFiles = getUploadFiles(request); if (uploadFiles.size() == 1) { return ConverterService.convert(uploadFiles.get(0)); } - - } catch (RuntimeException e) { response.status(HttpStatus.BAD_REQUEST_400); return e.getMessage(); @@ -115,8 +114,6 @@ private Route writeCache() { String inputFileName = "cache"; Path config = getUploadFile(request, inputFileName); ConfigService.writeCache(config); - - } catch (Exception e) { response.status(HttpStatus.BAD_REQUEST_400); return e.getMessage(); @@ -218,10 +215,8 @@ private List getUploadFiles(Request req) throws IOException, ServletExcept File uploadDir = new File("/tmp/upload"); uploadDir.mkdir(); - req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp")); - HttpServletRequest raw = req.raw(); Collection parts = raw.getParts(); diff --git a/config/editor/src/app/modules/config-editor/component/edit/edit.component.ts b/config/editor/src/app/modules/config-editor/component/edit/edit.component.ts index 0ab2ef7..d360b66 100644 --- a/config/editor/src/app/modules/config-editor/component/edit/edit.component.ts +++ b/config/editor/src/app/modules/config-editor/component/edit/edit.component.ts @@ -14,138 +14,156 @@ import {UrlHelper} from "../../../../shared/url-helper"; @Component({ - selector: 'app-edit', - templateUrl: './edit.component.html', - styleUrls: ['./edit.component.scss'] + selector: 'app-edit', + templateUrl: './edit.component.html', + styleUrls: ['./edit.component.scss'] }) -export class EditComponent implements OnInit{ - @ViewChild(MatAccordion) accordion: MatAccordion = {} as MatAccordion - - public appConfig: AppConfig; - - constructor(private route: ActivatedRoute, - private router: Router, - private es: ExtensionsService, - private httpClient: HttpClient, - private yamlPipe: YamlPipe, - private jsonPipe: JsonDPipe, - readonly snackBar: MatSnackBar - ) { - if (es.getConfig()) { - this.appConfig = es.getConfig() - } else { - this.appConfig = new AppConfig(); - } +export class EditComponent implements OnInit { + @ViewChild(MatAccordion) accordion: MatAccordion = {} as MatAccordion + + public appConfig: AppConfig; + + constructor(private route: ActivatedRoute, + private router: Router, + private es: ExtensionsService, + private httpClient: HttpClient, + private yamlPipe: YamlPipe, + private jsonPipe: JsonDPipe, + readonly snackBar: MatSnackBar + ) { + if (es.getConfig()) { + this.appConfig = es.getConfig() + } else { + this.appConfig = new AppConfig(); } + } + + getConfig() { + return this.es.getConfig(); + } + + ngOnInit() { + this.route.queryParams + .subscribe((params) => { + if (isConfig(params)) { + if (this.es.getConfig() == null) { + this.es.getCurrentConfig().subscribe(value => { + this.es.setConfig(value); + this.appConfig = value; + }) + } + + } else if (isCreator(params)) { + this.appConfig.extensionBoards = { + vendor: params.v, + inputBoard: params.i, + outputBoard: params.o + } + this.es.setConfig(this.appConfig); + } else if (isSaved(params)) { + this.es.loadConfig(params.uuid) + } + let config = this.es.getConfig(); + if (config) { + this.appConfig = config + } + }); + } - getConfig() { - return this.es.getConfig(); + uploadToCache() { + if (environment.webOnly!) { + this.webOnly(); + return } - ngOnInit() { - this.route.queryParams - .subscribe((params) => { - if (isConfig(params)) { - if (this.es.getConfig() == null) { - this.es.getCurrentConfig().subscribe(value => { - this.es.setConfig(value); - this.appConfig = value; - }) - } - - } else if (isCreator(params)) { - this.appConfig.extensionBoards = { - vendor: params.v, - inputBoard: params.i, - outputBoard: params.o - } - this.es.setConfig(this.appConfig); - } else if (isSaved(params)) { - this.es.loadConfig(params.uuid) - } - let config = this.es.getConfig(); - if(config) { - this.appConfig = config - } - }); - } + const jsonConfig = this.jsonPipe.transform(this.es.getConfig()) - uploadToCache() { - if(environment.webOnly!){ - this.webOnly(); - return - } + let fileBits: BlobPart[] = [jsonConfig] + let fileName: string = "cache"; - const jsonConfig = this.jsonPipe.transform(this.es.getConfig()) + const fileToUpload = new File(fileBits, fileName); - let fileBits: BlobPart[] = [jsonConfig] - let fileName: string = "cache"; + const endpoint = UrlHelper.getApiUrl() + 'write/cache'; + const formData: FormData = new FormData(); + formData.append('cache', fileToUpload, fileToUpload.name); + return this.httpClient + .post(endpoint, formData) + .subscribe({ + next: value => { + this.router.navigate(["/settings"]) + }, + error: err => { + this.snackBar.open(err.error) + } + }); + } - const fileToUpload = new File(fileBits, fileName); + uploadToArgument() { + if (environment.webOnly!) { + this.webOnly(); + return + } - const endpoint = UrlHelper.getApiUrl()+'write/cache'; - const formData: FormData = new FormData(); - formData.append('cache', fileToUpload, fileToUpload.name); - return this.httpClient - .post(endpoint, formData) - .subscribe({ - next: value => { - this.router.navigate(["/logs"]) - }, - error: err => { - this.snackBar.open(err.error) - } - }); + const jsonConfig = this.jsonPipe.transform(this.es.getConfig()) - } + let fileBits: BlobPart[] = [jsonConfig] + let fileName: string = "cache"; - uploadToArgument() { - if(environment.webOnly!){ - this.webOnly(); - return - }else { + const fileToUpload = new File(fileBits, fileName); + const endpoint = UrlHelper.getApiUrl() + 'write/config'; + const formData: FormData = new FormData(); + formData.append('cache', fileToUpload, fileToUpload.name); + return this.httpClient + .post(endpoint, formData) + .subscribe({ + next: value => { + this.router.navigate(["/settings"]) + }, + error: err => { + this.snackBar.open(err.error) } - } + }); + } - addLog($event: boolean) { - this.appConfig.logger = { - logs: new Map(), - default: LogLevel.INFO, - } + addLog($event: boolean) { + this.appConfig.logger = { + logs: new Map(), + default: LogLevel.INFO, } + } - downloadYaml() { - const data = this.yamlPipe.transform(this.getConfig()); - const theFile = new Blob([data], { type: "text/yaml" }); - saveAs(theFile,"config.yaml") - } + downloadYaml() { + const data = this.yamlPipe.transform(this.getConfig()); + const theFile = new Blob([data], {type: "text/yaml"}); + saveAs(theFile, "config.yaml") + } - webOnly() { - this.snackBar.open("Log viewer unavailable in webOnly preview", "Ok, i understand", {panelClass: 'error'}) - } + webOnly() { + this.snackBar.open("Log viewer unavailable in webOnly preview", "Ok, i understand", {panelClass: 'error'}) + } } interface EditorParamsCreator { - v: string; - i: string; - o: string; + v: string; + i: string; + o: string; } interface EditorParamsConfig { - config: "current" + config: "current" } interface EditorParamsSaved { - uuid: string; + uuid: string; } const isCreator = function (object: any): object is EditorParamsCreator { - return 'v' in object; + return 'v' in object; } const isConfig = function (object: any): object is EditorParamsConfig { - return 'config' in object; + return 'config' in object; } const isSaved = function (object: any): object is EditorParamsSaved { - return 'uuid' in object; + return 'uuid' in object; } diff --git a/docker/ha/Dockerfile b/docker/ha/Dockerfile index 26880c7..649a8bf 100644 --- a/docker/ha/Dockerfile +++ b/docker/ha/Dockerfile @@ -4,9 +4,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates telnet iputils-ping curl netbase net-tools iproute2 wget jq \ && rm -rf /var/lib/apt/lists/* -WORKDIR /app - -COPY sbc2ha.jar ./ +WORKDIR /data +RUN mkdir /app +COPY sbc2ha.jar /app COPY sbc.yaml ./config.yaml COPY run.sh / diff --git a/docker/ha/run.sh b/docker/ha/run.sh index 3d4d30a..f540a53 100755 --- a/docker/ha/run.sh +++ b/docker/ha/run.sh @@ -1,4 +1,4 @@ PIGPIOD_HOST=$(jq -r .pigpio_host /data/options.json) export PIGPIOD_HOST -java -Ddiozero.devicefactory=com.diozero.internal.provider.pigpioj.PigpioJDeviceFactory -DusePwmFadingLed=true -jar sbc2ha.jar /app/config.yaml +java -Ddiozero.devicefactory=com.diozero.internal.provider.pigpioj.PigpioJDeviceFactory -DusePwmFadingLed=true -jar /app/sbc2ha.jar /data/config.yaml diff --git a/pom.xml b/pom.xml index 2e29782..06d25e1 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ 16 16 UTF-8 - 0.0.6 + 0.0.7 1.4.0-dafik 0.5.0