forked from openpilot-hub/devpilot-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check login status and network availability. (openpilot-hub#42)
* Check login status and network availability. * Add warning display text. * CheckStyle adjust. * CheckStyle adjust. * check style. * checkstyle fix. * Availability check logic update. * Availability check logic update. * code refine. * code refine. --------- Co-authored-by: maozhen <[email protected]>
- Loading branch information
1 parent
e92ca18
commit 69b2078
Showing
10 changed files
with
207 additions
and
5 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
88 changes: 88 additions & 0 deletions
88
src/main/java/com/zhongan/devpilot/network/DevPilotAvailabilityChecker.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,88 @@ | ||
package com.zhongan.devpilot.network; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.zhongan.devpilot.actions.notifications.DevPilotNotification; | ||
import com.zhongan.devpilot.enums.LoginTypeEnum; | ||
import com.zhongan.devpilot.settings.state.AIGatewaySettingsState; | ||
import com.zhongan.devpilot.settings.state.AvailabilityCheck; | ||
import com.zhongan.devpilot.settings.state.DevPilotLlmSettingsState; | ||
import com.zhongan.devpilot.statusBar.DevPilotStatusBarBaseWidget; | ||
import com.zhongan.devpilot.statusBar.status.DevPilotStatusEnum; | ||
import com.zhongan.devpilot.util.LoginUtils; | ||
import com.zhongan.devpilot.util.OkhttpUtils; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
|
||
import static com.zhongan.devpilot.constant.DefaultConst.TRIAL_DEFAULT_HOST; | ||
|
||
public class DevPilotAvailabilityChecker implements Runnable { | ||
|
||
private final Project project; | ||
|
||
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); | ||
|
||
public DevPilotAvailabilityChecker(@NotNull Project project) { | ||
this.project = project; | ||
} | ||
|
||
private void checkAndNotify() { | ||
if (!LoginUtils.isLogin()) { | ||
DevPilotNotification.notLoginNotification(project); | ||
} else if (!isNetWorkAvailable()) { | ||
DevPilotNotification.networkDownNotification(project); | ||
} else { | ||
DevPilotStatusBarBaseWidget.update(project, LoginUtils.isLogin() ? DevPilotStatusEnum.LoggedIn : DevPilotStatusEnum.NotLoggedIn); | ||
} | ||
} | ||
|
||
private boolean isNetWorkAvailable() { | ||
String url = getCheckUrl(); | ||
if (StringUtils.isBlank(url)) { | ||
return false; | ||
} | ||
OkHttpClient client = OkhttpUtils.getClient(); | ||
Request request = new Request.Builder().url(url).build(); | ||
try { | ||
client.newCall(request).execute(); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
private String getCheckUrl() { | ||
var setting = DevPilotLlmSettingsState.getInstance(); | ||
var loginType = LoginTypeEnum.getLoginTypeEnum(setting.getLoginType()); | ||
switch (loginType) { | ||
case WX: | ||
return TRIAL_DEFAULT_HOST; | ||
case ZA: | ||
case ZA_TI: | ||
return AIGatewaySettingsState.getInstance().getModelBaseHost(null); | ||
default: | ||
return ""; | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (AvailabilityCheck.getInstance().getEnable()) { | ||
checkAndNotify(); | ||
} | ||
} | ||
|
||
public void checkNetworkAndLogStatus() { | ||
if (AvailabilityCheck.getInstance().getEnable()) { | ||
executor.scheduleAtFixedRate(this, 0, 2, TimeUnit.HOURS); | ||
} | ||
} | ||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/zhongan/devpilot/settings/state/AvailabilityCheck.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,37 @@ | ||
package com.zhongan.devpilot.settings.state; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.components.PersistentStateComponent; | ||
import com.intellij.openapi.components.State; | ||
import com.intellij.openapi.components.Storage; | ||
import com.intellij.util.xmlb.XmlSerializerUtil; | ||
|
||
@State(name = "DevPilot_AvailabilityCheckSettings", storages = @Storage("DevPilot_AvailabilityCheckSettings.xml")) | ||
public class AvailabilityCheck implements PersistentStateComponent<AvailabilityCheck> { | ||
|
||
private Boolean enable = true; | ||
|
||
public static AvailabilityCheck getInstance() { | ||
return ApplicationManager.getApplication().getService(AvailabilityCheck.class); | ||
} | ||
|
||
public Boolean getEnable() { | ||
return enable == null || enable; | ||
} | ||
|
||
public void setEnable(Boolean enable) { | ||
this.enable = enable; | ||
} | ||
|
||
@Override | ||
public AvailabilityCheck getState() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public void loadState(AvailabilityCheck state) { | ||
XmlSerializerUtil.copyBean(state, this); | ||
} | ||
|
||
} | ||
|
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
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