Skip to content

Commit

Permalink
[java-matter-controller] Convert from java to kotlin phase I (#25384)
Browse files Browse the repository at this point in the history
* [java-matter-controller] Convert from java to kotlin phase I

* Address review comments

* Move constants outside the class instance
  • Loading branch information
yufengwangca authored and pull[bot] committed Feb 26, 2024
1 parent 3902b26 commit 8906388
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 292 deletions.
25 changes: 19 additions & 6 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ import("//build_overrides/chip.gni")
import("${chip_root}/build/chip/java/rules.gni")
import("${chip_root}/build/chip/tools.gni")

java_binary("java-matter-controller") {
output_name = "java-matter-controller"
java_library("java") {
output_name = "JavaMatterController.jar"
deps = [
"${chip_root}/src/controller/java",
"${chip_root}/src/setup_payload/java",
"${chip_root}/third_party/java_deps:annotation",
]

sources = [
"java/src/com/matter/controller/Main.java",
"java/src/com/matter/controller/commands/common/Argument.java",
"java/src/com/matter/controller/commands/common/ArgumentType.java",
"java/src/com/matter/controller/commands/common/Command.java",
Expand All @@ -37,9 +36,6 @@ java_binary("java-matter-controller") {
"java/src/com/matter/controller/commands/common/IPAddress.java",
"java/src/com/matter/controller/commands/common/MatterCommand.java",
"java/src/com/matter/controller/commands/common/RealResult.java",
"java/src/com/matter/controller/commands/discover/DiscoverCommand.java",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java",
"java/src/com/matter/controller/commands/pairing/CloseSessionCommand.java",
"java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.java",
"java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.java",
Expand All @@ -66,6 +62,23 @@ java_binary("java-matter-controller") {
javac_flags = [ "-Xlint:deprecation" ]
}

kotlin_binary("java-matter-controller") {
output_name = "java-matter-controller"
deps = [
":java",
"${chip_root}/third_party/java_deps:kotlin-stdlib",
]

sources = [
"java/src/com/matter/controller/Main.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt",
]

kotlinc_flags = [ "-Xlint:deprecation" ]
}

group("default") {
deps = [ ":java-matter-controller" ]
}
4 changes: 2 additions & 2 deletions examples/java-matter-controller/Manifest.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Main-Class: com.matter.controller.Main
Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/CHIPController.jar ../lib/third_party/connectedhomeip/src/setup_payload/java/SetupPayloadParser.jar ../lib/third_party/connectedhomeip/third_party/java_deps/stub_src/Android.jar ../lib/third_party/connectedhomeip/third_party/java_deps/json-20220924.jar ../lib/third_party/connectedhomeip/third_party/java_deps/jsr305-3.0.2.jar
Main-Class: com.matter.controller.MainKt
Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/CHIPController.jar ../lib/third_party/connectedhomeip/src/setup_payload/java/SetupPayloadParser.jar ../lib/third_party/connectedhomeip/third_party/java_deps/stub_src/Android.jar ../lib/third_party/connectedhomeip/third_party/java_deps/json-20220924.jar ../lib/third_party/connectedhomeip/third_party/java_deps/jsr305-3.0.2.jar ../lib/third_party/connectedhomeip/third_party/java_deps/kotlin-stdlib-1.8.10.jar

53 changes: 48 additions & 5 deletions examples/java-matter-controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You need to have the following two software installed on your Ubuntu system:
1. Java Runtime Environment (JRE)
2. Java Development Kit (JDK)

```
```shell
java -version
```

Expand All @@ -36,20 +36,63 @@ sudo apt install default-jre Install Java default JRE
After installing the JRE, let us check if we have the Java Development Kit
installed on our system or not.

```
```shell
javac -version
```

The above output shows that I need to install the Java compiler or the JDK on my
system. You can install it through the following command as root:

```
```shell
sudo apt install default-jdk
```

### Linux
You also need to install kotlin compiler on your Ubuntu system:

kotlin compiler version 1.8.10 or above is needed to compile
java-matter-controller, if you already have lower version kotlin compiler
installed on your Ubuntu from apt,
you need to remove the Kotlin compiler package, run the following command:

```shell
sudo apt-get remove kotlin
```

Wait for the removal process to complete. Once it's done, the Kotlin compiler
will be removed from your system.

(Optional) If you want to remove any configuration files associated with Kotlin,
run the following command:

```shell
sudo apt-get purge kotlin
```

Install kotlin compiler 1.8.10 or above, such as
[kotlin-compiler-1.8.10-url](https://github.com/JetBrains/kotlin/releases/download/v1.8.10/kotlin-compiler-1.8.10.zip)

```shell
cd /usr/lib \
&& sudo wget -q [kotlin-compiler-1.8.10-url] \
&& sudo unzip kotlin-compiler-*.zip \
&& sudo rm kotlin-compiler-*.zip \
&& sudo rm -f kotlinc/bin/*.bat
```

Add a directory to PATH permanently by editing the `.bashrc` file located in the
Home directory. Follow these steps:

1. Open the `.bashrc` file using a text editor.
2. Go to the end of the file.
3. Paste the export syntax at the end of the file.

```shell
export PATH="/usr/lib/kotlinc/bin:$PATH"
```

### Linux

```shell
export JAVA_PATH=[JDK path]
```

Expand Down Expand Up @@ -81,6 +124,6 @@ The Java executable file `java-matter-controller` will be generated at

Run the java-matter-controller

```
```shell
java -Djava.library.path=../lib/jni -jar java-matter-controller
```

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.matter.controller

import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ControllerParams
import com.matter.controller.commands.common.*
import com.matter.controller.commands.discover.*
import com.matter.controller.commands.pairing.*

private fun getDiscoveryCommands(
controller: ChipDeviceController,
credentialsIssuer: CredentialsIssuer
): List<Command> {
return listOf(
DiscoverCommand(controller, credentialsIssuer),
DiscoverCommissionablesCommand(controller, credentialsIssuer),
DiscoverCommissionersCommand(controller, credentialsIssuer),
)
}

private fun getPairingCommands(
controller: ChipDeviceController,
credentialsIssuer: CredentialsIssuer
): List<Command> {
return listOf(
UnpairCommand(controller, credentialsIssuer),
PairCodeCommand(controller, credentialsIssuer),
PairCodePaseCommand(controller, credentialsIssuer),
PairCodeWifiCommand(controller, credentialsIssuer),
PairCodeThreadCommand(controller, credentialsIssuer),
PairAddressPaseCommand(controller, credentialsIssuer),
PairAlreadyDiscoveredCommand(controller, credentialsIssuer),
PairOnNetworkCommand(controller, credentialsIssuer),
PairOnNetworkShortCommand(controller, credentialsIssuer),
PairOnNetworkLongCommand(controller, credentialsIssuer),
PairOnNetworkVendorCommand(controller, credentialsIssuer),
PairOnNetworkCommissioningModeCommand(controller, credentialsIssuer),
PairOnNetworkCommissionerCommand(controller, credentialsIssuer),
PairOnNetworkDeviceTypeCommand(controller, credentialsIssuer),
PairOnNetworkInstanceNameCommand(controller, credentialsIssuer),
)
}

fun main(args: Array<String>) {
val controller = ChipDeviceController(
ControllerParams.newBuilder()
.setUdpListenPort(0)
.setControllerVendorId(0xFFF1)
.setCountryCode("US")
.build()
)
val credentialsIssuer = CredentialsIssuer()
val commandManager = CommandManager()

commandManager.register("discover", getDiscoveryCommands(controller, credentialsIssuer))
commandManager.register("pairing", getPairingCommands(controller, credentialsIssuer))

try {
commandManager.run(args)
} catch (e: Exception) {
println("Run command failed with exception: " + e.message)
System.exit(1)
}
controller.shutdownCommissioning()
}
Loading

0 comments on commit 8906388

Please sign in to comment.