-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/io/mapsmessaging/rest/api/impl/lora/LoRaDeviceApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright [ 2020 - 2024 ] [Matthew Buckton] | ||
* | ||
* 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 io.mapsmessaging.rest.api.impl.lora; | ||
|
||
import static io.mapsmessaging.rest.api.Constants.URI_PATH; | ||
|
||
import io.mapsmessaging.config.lora.LoRaDeviceConfig; | ||
import io.mapsmessaging.network.io.impl.lora.device.LoRaDevice; | ||
import io.mapsmessaging.network.io.impl.lora.device.LoRaDeviceManager; | ||
import io.mapsmessaging.rest.api.impl.BaseRestApi; | ||
import io.mapsmessaging.rest.data.lora.LoRaDeviceInfo; | ||
import io.mapsmessaging.rest.data.lora.LoRaListResponse; | ||
import io.mapsmessaging.rest.responses.BaseResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.ws.rs.*; | ||
import jakarta.ws.rs.core.MediaType; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Tag(name = "LoRa Device Management") | ||
@Path(URI_PATH) | ||
public class LoRaDeviceApi extends BaseRestApi { | ||
|
||
@GET | ||
@Path("/device/lora") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public LoRaListResponse getAllLoRaDevices() { | ||
checkAuthentication(); | ||
LoRaDeviceManager deviceManager = LoRaDeviceManager.getInstance(); | ||
List<LoRaDeviceInfo> deviceInfos = new ArrayList<>(); | ||
for(LoRaDevice device : deviceManager.getDevices()) { | ||
deviceInfos.add(createInfo(device)); | ||
} | ||
return new LoRaListResponse(request, deviceInfos); | ||
} | ||
|
||
@GET | ||
@Path("/device/lora/{deviceName}") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public LoRaDeviceInfo getLoRaDevice(@PathParam("deviceName") String deviceName) { | ||
checkAuthentication(); | ||
LoRaDeviceManager deviceManager = LoRaDeviceManager.getInstance(); | ||
LoRaDeviceConfig deviceConfig = null; | ||
LoRaDeviceInfo deviceInfo = new LoRaDeviceInfo(); | ||
if (deviceName != null && !deviceName.isEmpty()) { | ||
List<LoRaDevice> lookup = deviceManager.getDevices().stream() | ||
.filter(device -> deviceName.equals(device.getName())) | ||
.collect(Collectors.toList()); | ||
if(!lookup.isEmpty()) { | ||
deviceInfo = createInfo(lookup.get(0)); | ||
} | ||
} | ||
|
||
if (deviceConfig == null) { | ||
response.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
} | ||
return deviceInfo; | ||
} | ||
|
||
@POST | ||
@Path("/device/lora") | ||
@Consumes({MediaType.APPLICATION_JSON}) | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public BaseResponse addLoRaDevice(LoRaDeviceInfo newDevice) { | ||
checkAuthentication(); | ||
LoRaDeviceManager deviceManager = LoRaDeviceManager.getInstance(); | ||
return new BaseResponse(request); | ||
// response.setStatus(500); | ||
// return new BaseResponse(request); | ||
} | ||
|
||
@DELETE | ||
@Path("/device/lora/{deviceId}") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public BaseResponse deleteLoRaDevice(@PathParam("deviceId") String deviceId) { | ||
checkAuthentication(); | ||
LoRaDeviceManager deviceManager = LoRaDeviceManager.getInstance(); | ||
// if (deviceManager.deleteDevice(UUID.fromString(deviceId))) | ||
return new BaseResponse(request); | ||
// } | ||
// response.setStatus(500); | ||
// return new BaseResponse(request); | ||
} | ||
|
||
private LoRaDeviceInfo createInfo(LoRaDevice device) { | ||
LoRaDeviceInfo deviceInfo = new LoRaDeviceInfo(); | ||
LoRaDeviceConfig loRaDeviceConfig = device.getConfig(); | ||
deviceInfo.setName(device.getName()); | ||
deviceInfo.setCs(loRaDeviceConfig.getCs()); | ||
deviceInfo.setIrq(loRaDeviceConfig.getIrq()); | ||
deviceInfo.setCadTimeout(loRaDeviceConfig.getCadTimeout()); | ||
deviceInfo.setPower(loRaDeviceConfig.getPower()); | ||
deviceInfo.setFrequency(loRaDeviceConfig.getFrequency()); | ||
deviceInfo.setRst(loRaDeviceConfig.getRst()); | ||
deviceInfo.setRadio(loRaDeviceConfig.getRadio()); | ||
deviceInfo.setBytesReceived(device.getBytesReceived().sum()); | ||
deviceInfo.setBytesSent(device.getBytesSent().sum()); | ||
deviceInfo.setPacketsReceived(device.getPacketsReceived().sum()); | ||
deviceInfo.setPacketsSent(device.getPacketsSent().sum()); | ||
return deviceInfo; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/io/mapsmessaging/rest/data/lora/LoRaDeviceInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright [ 2020 - 2024 ] [Matthew Buckton] | ||
* | ||
* 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 io.mapsmessaging.rest.data.lora; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@ToString | ||
@NoArgsConstructor | ||
public class LoRaDeviceInfo { | ||
|
||
private String name; | ||
private String radio; | ||
|
||
private int cs; | ||
private int irq; | ||
private int rst; | ||
private int power; | ||
private int cadTimeout; | ||
private float frequency; | ||
|
||
private long bytesSent; | ||
private long bytesReceived; | ||
private long packetsSent; | ||
private long packetsReceived; | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/io/mapsmessaging/rest/data/lora/LoRaListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright [ 2020 - 2024 ] [Matthew Buckton] | ||
* | ||
* 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 io.mapsmessaging.rest.data.lora; | ||
|
||
import io.mapsmessaging.rest.responses.BaseResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.List; | ||
import lombok.Getter; | ||
|
||
public class LoRaListResponse extends BaseResponse { | ||
|
||
@Getter | ||
private final List<LoRaDeviceInfo> data; | ||
|
||
|
||
public LoRaListResponse(HttpServletRequest request, List<LoRaDeviceInfo> data) { | ||
super(request); | ||
this.data = data; | ||
} | ||
} |