Skip to content

Commit

Permalink
improve mobile device log、command and update command
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed Dec 13, 2023
1 parent 6c91cf8 commit 365b672
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public MobDeviceHisDataResponse getHisDataPoints(MobileConfigDomain config, Stri
params.put("datastream_id", dataStreamIds);
params.put("start", start);
params.put("end", end);
params.put("limit", limit <= 0 ? "50" : limit + "");
params.put("limit", limit + "");
entity.setParams(params);
HttpResponseEntity response = HttpRequestExecutor.executeGet(entity);
if (StringUtils.isNotBlank(response.getBody())) {
Expand All @@ -112,7 +112,8 @@ public MobDeviceHisDataResponse getHisDataPoints(MobileConfigDomain config, Stri


public MobCachedCommandResponse getCachedCommandList(MobileConfigDomain config, String imei,
String startTime, String pageNo) {
String startTime, String endTime,
Integer pageNo, Integer pageSize) {
MobCachedCommandResponse mobCachedCommandResponse = new MobCachedCommandResponse();
try {
HttpRequestEntity entity = new HttpRequestEntity();
Expand All @@ -124,8 +125,9 @@ public MobCachedCommandResponse getCachedCommandList(MobileConfigDomain config,
Map<String, String> params = new HashMap<>();
params.put("imei", imei);
params.put("start", startTime);
params.put("page", pageNo);
params.put("per_page", "20");
params.put("end", endTime);
params.put("page", pageNo + "");
params.put("per_page", pageSize + "");
entity.setParams(params);
HttpResponseEntity response = HttpRequestExecutor.executeGet(entity);
if (StringUtils.isNotBlank(response.getBody())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
import iot.technology.client.toolkit.nb.service.processor.MobProcessContext;
import org.apache.commons.cli.*;

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,12 @@
import iot.technology.client.toolkit.nb.service.mobile.domain.action.data.MobCachedCommandItem;
import iot.technology.client.toolkit.nb.service.mobile.domain.action.data.MobCachedCommandResponse;
import iot.technology.client.toolkit.nb.service.processor.MobProcessContext;
import org.apache.commons.cli.*;

import java.util.Arrays;
import java.util.List;

/**
* <p>
* 1、command imei : print today's command for the device
* <p>
* 2、command imei pageNo : print today's first page command for the device
* <p>
* 3、command imei start pageNo : print startTime pageNo command for the device
* <p>
*
* @author mushuwei
*/
public class MobCommandDataDeviceProcessor extends TkAbstractProcessor implements TkProcessor {
Expand All @@ -58,57 +51,98 @@ public void handle(ProcessContext context) {
MobProcessContext mobProcessContext = (MobProcessContext) context;
MobileConfigDomain mobileConfigDomain = mobProcessContext.getMobileConfigDomain();

List<String> arguArgs = List.of(context.getData().split(" "));
if (arguArgs.size() < 2 || arguArgs.size() > 4) {
StringBuilder sb = new StringBuilder();
sb.append(String.format(ColorUtils.redError("argument:%s is illegal"), context.getData()))
.append(StringUtils.lineSeparator());
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
}
String imei = arguArgs.get(1);
String pageNo = "1";
Options options = new Options();
Option imeiOption = new Option("imei", true, "the device imei");
Option startTimeOption = new Option("st","startTime", true, "start time of search device command data list");
Option endTimeOption = new Option("et","endTime", true, "end time of search device command data list");
Option pageNoOption = new Option("pn", "pageNo", true, "the pageNo of device command data list");
Option pageSizeOption = new Option("ps", "pageSize", true, "the pageSize of device command data list");

options.addOption(imeiOption)
.addOption(pageNoOption)
.addOption(pageSizeOption)
.addOption(startTimeOption)
.addOption(endTimeOption);

String imei = "";
Integer pageNo = 1;
Integer pageSize = 20;
String startTime = DateUtils.getCurrentDayStartTimeForMob();
// command imei
if (arguArgs.size() == 2) {
}
if (arguArgs.size() == 3) {
String pageNoStr = arguArgs.get(2);
if (!validateParam(pageNoStr)) {
String endTime = DateUtils.getCurrentDayEndTimeForMob();
String consoleStartTime = "";
String consoleEndTime = "";
try {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, convertCommandData(context.getData()));
// the imei option
if (!cmd.hasOption(imeiOption)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("pageNo is not a number"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.redError("imei is required")).append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
}
pageNo = pageNoStr;
}
if (arguArgs.size() == 4) {
String startTimeStr = arguArgs.get(2);
if (!validateParam(startTimeStr)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("the time format is incorrect, correct time format:2019-02-01T00:01:01"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
imei = cmd.getOptionValue(imeiOption);
// the startTime option
if (cmd.hasOption(startTimeOption)) {
consoleStartTime = cmd.getOptionValue(startTimeOption);
if (!DateUtils.mobileTimePattern(consoleStartTime)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("the time format is incorrect, correct time format:2019-02-01T00:01:01"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
}
startTime = consoleStartTime;
}
startTime = startTimeStr;
String pageNoStr = arguArgs.get(3);
if (!validateParam(pageNoStr)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("pageNo is not a number"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
// the endTime option
if (cmd.hasOption(endTimeOption)) {
consoleEndTime = cmd.getOptionValue(endTimeOption);
if (!DateUtils.mobileTimePattern(consoleEndTime)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("the time format is incorrect, correct time format:2019-02-01T00:01:01"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
}
endTime = consoleEndTime;
}
pageNo = pageNoStr;

// pageNo option
if (cmd.hasOption(pageNoOption)) {
String pageNoStr = cmd.getOptionValue(pageNoOption);
if (!validateParam(pageNoStr)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("pageNo is not a number")).append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
}
pageNo = Integer.parseInt(pageNoStr);
}
// pageSize Option
if (cmd.hasOption(pageSizeOption)) {
String pageSizeStr = cmd.getOptionValue(pageSizeOption);
if (!validateParam(pageSizeStr)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("pageSize is not a number")).append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help command"));
System.out.println(sb);
return;
}
pageSize = Integer.parseInt(pageSizeStr);
}

} catch (ParseException e) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("command parse failed!")).append(StringUtils.lineSeparator);
System.out.println(sb);
}
MobCachedCommandResponse
mobCachedCommandResponse = mobileDeviceDataService.getCachedCommandList(mobileConfigDomain, imei, startTime, pageNo);
mobCachedCommandResponse = mobileDeviceDataService.getCachedCommandList(
mobileConfigDomain, imei, startTime, endTime, pageNo, pageSize);
if (mobCachedCommandResponse.isSuccess()
&& mobCachedCommandResponse.getData() != null
&& !mobCachedCommandResponse.getData().getItems().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@
import java.util.List;

/**
* usage:
* <p>
* 1、list: print first page device list
* <p>
* 2、list pageNo: print pageNo device list
* <p>
* 3、list searchValue pageNo: print searchValue pageNo device list
* <p>
*
* @author mushuwei
*/
public class MobListDeviceProcessor extends TkAbstractProcessor implements TkProcessor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import iot.technology.client.toolkit.nb.service.mobile.domain.action.data.*;
import iot.technology.client.toolkit.nb.service.mobile.domain.action.device.MobQueryDeviceByImeiResponse;
import iot.technology.client.toolkit.nb.service.processor.MobProcessContext;
import org.apache.commons.cli.*;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -60,64 +61,77 @@ public void handle(ProcessContext context) {
MobProcessContext mobProcessContext = (MobProcessContext) context;
MobileConfigDomain mobileConfigDomain = mobProcessContext.getMobileConfigDomain();

List<String> arguArgs = List.of(context.getData().split(" "));
if (arguArgs.size() > 5 || arguArgs.size() < 2) {
StringBuilder sb = new StringBuilder();
sb.append(String.format(ColorUtils.redError("argument:%s is illegal"), context.getData()))
.append(StringUtils.lineSeparator());
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
}
int limit = 50;
Options options = new Options();
Option imeiOption = new Option("imei", true, "the device imei");
Option limitOption = new Option("limit", true, "limit of the device log data list");
Option startTimeOption = new Option("startTime", true, "start time of search device log data list");
Option endTimeOption = new Option("endTime", true, "end time of search device log data list");

options.addOption(imeiOption)
.addOption(limitOption)
.addOption(startTimeOption)
.addOption(endTimeOption);

String imei = "";
String startTime = "";
String endTime = "";
if (arguArgs.size() == 2) {
imei = arguArgs.get(1);
}
if (arguArgs.size() == 3) {
imei = arguArgs.get(1);
String limitStr = arguArgs.get(2);
if (!validateParam(limitStr)) {
Integer limit = 50;
String startTime = DateUtils.getCurrentDayStartTimeForMob();
String endTime = DateUtils.getCurrentDayEndTimeForMob();
String consoleStartTime = "";
String consoleEndTime = "";

try {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, convertCommandData(context.getData()));

if (!cmd.hasOption(imeiOption)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("limit is not a number"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.redError("imei is required")).append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
}
limit = Integer.parseInt(limitStr);
}
if (arguArgs.size() == 4 || arguArgs.size() == 5) {
imei = arguArgs.get(1);
startTime = arguArgs.get(2);
endTime = arguArgs.get(3);
if (!DateUtils.mobileTimePattern(startTime) || !DateUtils.mobileTimePattern(endTime)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("the time format is incorrect, correct time format:2019-02-01T00:01:01"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
imei = cmd.getOptionValue(imeiOption);
if (cmd.hasOption(limitOption)) {
String limitStr = cmd.getOptionValue(limitOption);
if (!validateParam(limitStr)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("limit is not a number")).append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
}
limit = Integer.parseInt(limitStr);
}
}
if (arguArgs.size() == 5) {
imei = arguArgs.get(1);
String limitStr = arguArgs.get(4);
if (!validateParam(limitStr)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("limit is not a number"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
if (cmd.hasOption(startTimeOption)) {
consoleStartTime = cmd.getOptionValue(startTimeOption);
if (!DateUtils.mobileTimePattern(consoleStartTime)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("the time format is incorrect, correct time format:2019-02-01T00:01:01"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
}
startTime = consoleStartTime;
}
if (cmd.hasOption(endTimeOption)) {
consoleEndTime = cmd.getOptionValue(endTimeOption);
if (!DateUtils.mobileTimePattern(consoleEndTime)) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("the time format is incorrect, correct time format:2019-02-01T00:01:01"))
.append(StringUtils.lineSeparator);
sb.append(ColorUtils.blackBold("detail usage please enter: help log"));
System.out.println(sb);
return;
}
endTime = consoleEndTime;
}
limit = Integer.parseInt(limitStr);
}
startTime = StringUtils.isBlank(startTime) ? DateUtils.getCurrentDayStartTimeForMob() : startTime;
endTime = StringUtils.isBlank(endTime) ? DateUtils.getCurrentDayEndTimeForMob() : endTime;

} catch (ParseException e) {
StringBuilder sb = new StringBuilder();
sb.append(ColorUtils.redError("command parse failed!")).append(StringUtils.lineSeparator);
System.out.println(sb);
}
MobQueryDeviceByImeiResponse response = mobileDeviceService.queryDeviceByImei(mobileConfigDomain, imei);
if (response.isSuccess() && Objects.nonNull(response.getData().getId())) {
String deviceId = response.getData().getId();
Expand All @@ -141,8 +155,7 @@ public void handle(ProcessContext context) {
datastreams.stream().map(MobDeviceLatestDataStreamsBody::getId).collect(Collectors.toList());
String dataStreamIdStr = String.join(",", dataStreamIds);
MobDeviceHisDataResponse deviceHisDataResponse =
mobileDeviceDataService.getHisDataPoints(mobileConfigDomain, deviceId, dataStreamIdStr, startTime, endTime,
limit);
mobileDeviceDataService.getHisDataPoints(mobileConfigDomain, deviceId, dataStreamIdStr, startTime, endTime, limit);
if (deviceHisDataResponse.isSuccess()) {
int count = deviceHisDataResponse.getData().getCount();
System.out.println(
Expand Down
Loading

0 comments on commit 365b672

Please sign in to comment.