15 changed files with 308 additions and 113 deletions
-
5src/main/java/a8k/BoditechA800Application.java
-
106src/main/java/a8k/OS.java
-
3src/main/java/a8k/app/channel/iflytophald/type/protocol/A8kEcode.java
-
4src/main/java/a8k/app/controler/api/v1/app/setting/DeviceInfoControler.java
-
6src/main/java/a8k/app/controler/api/v1/app/setting/DeviceSettingController.java
-
7src/main/java/a8k/app/controler/api/v1/app/setting/NetworkSettingController.java
-
3src/main/java/a8k/app/dao/type/db/NetworkSetting.java
-
2src/main/java/a8k/app/i18n/Internationalization.java
-
103src/main/java/a8k/app/service/os/OSNetworkMgrService.java
-
2src/main/java/a8k/app/service/os/OSTimeMgrService.java
-
33src/main/java/a8k/app/service/setting/AppNetworkSettingService.java
-
9src/main/java/a8k/app/service/statemgr/GStateMgrService.java
-
2src/main/java/a8k/app/type/GState.java
-
4src/main/java/a8k/iflyutils/netplan/NetplanConfigFactory.java
@ -0,0 +1,103 @@ |
|||
package a8k.app.service.os; |
|||
|
|||
import a8k.OS; |
|||
import a8k.app.channel.iflytophald.type.protocol.A8kEcode; |
|||
import a8k.app.dao.NetworkSettingDao; |
|||
import a8k.app.dao.type.db.NetworkSetting; |
|||
import a8k.app.service.statemgr.GStateMgrService; |
|||
import a8k.app.type.exception.AppException; |
|||
import a8k.iflyutils.netplan.NetplanConfigFactory; |
|||
import a8k.iflyutils.netplan.exception.NetSettingIllegalException; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.context.ApplicationListener; |
|||
import org.springframework.context.event.ContextRefreshedEvent; |
|||
import org.springframework.scheduling.annotation.EnableScheduling; |
|||
import org.springframework.scheduling.annotation.Scheduled; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.io.FileWriter; |
|||
import java.io.IOException; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
@EnableScheduling |
|||
public class OSNetworkMgrService implements ApplicationListener<ContextRefreshedEvent> { |
|||
private final NetworkSettingDao networkSettingDao; |
|||
private final GStateMgrService gStateMgrService; |
|||
|
|||
public NetworkSetting getNetworkSetting() { |
|||
return networkSettingDao.get(); |
|||
} |
|||
|
|||
|
|||
public void setNetworkSetting(NetworkSetting setting) throws AppException { |
|||
try { |
|||
doSetNetworkSetting(setting); |
|||
} catch (NetSettingIllegalException e) { |
|||
throw AppException.of(A8kEcode.NETWORK_PARAMETER_ERROR, e.getMessage()); |
|||
} catch (IOException e) { |
|||
throw AppException.of(A8kEcode.CODEERROR, e.getMessage()); |
|||
} |
|||
networkSettingDao.update(setting); |
|||
log.info("Network configuration updated: {}", setting); |
|||
} |
|||
|
|||
public void doSetNetworkSetting(NetworkSetting setting) throws NetSettingIllegalException, IOException, AppException { |
|||
if (OS.isRunOnWindows()) { |
|||
return; |
|||
} |
|||
NetplanConfigFactory netplanConfigFactory = new NetplanConfigFactory(); |
|||
String netplanConfigContent = ""; |
|||
switch (setting.networkMode) { |
|||
case STATIC_IP -> netplanConfigContent = netplanConfigFactory.newStaticEthernetConfig( |
|||
OS.getEthName(), |
|||
setting.staticIPConfig.ip, |
|||
setting.staticIPConfig.netmask, |
|||
setting.staticIPConfig.gateway, |
|||
setting.staticIPConfig.dnsList); |
|||
|
|||
case DYNAMIC_IP -> netplanConfigContent = netplanConfigFactory.newDynamicEthernetConfig( |
|||
OS.getEthName(), setting.dynamicIpConfig.autoDns, setting.dynamicIpConfig.dnsList); |
|||
} |
|||
|
|||
//参考规范 https://iflytop1.feishu.cn/wiki/I2v5whcqEitWeRkFZtKcuC08nxe |
|||
try (FileWriter writer = new FileWriter("/etc/netplan/00_netplan_config.yaml")) { |
|||
//向/etc/netplan/00_netplan_config.yaml写入配置文件 |
|||
writer.write(netplanConfigContent); |
|||
} |
|||
|
|||
try { |
|||
OS.executeLinuxCommand("netplan apply", 5, TimeUnit.SECONDS); |
|||
} catch (InterruptedException e) { |
|||
throw new AppException(A8kEcode.SYS_CMD_EXEC_OVERTIME); |
|||
} |
|||
log.info("Network configuration applied successfully: {}", setting); |
|||
} |
|||
|
|||
@Scheduled(fixedDelay = 1000) |
|||
void updateLocalIp() { |
|||
gStateMgrService.updateLocalIp(OS.getLocalIp()); |
|||
} |
|||
|
|||
/** |
|||
* 在应用启动时设置网络配置 |
|||
*/ |
|||
@Override public void onApplicationEvent(ContextRefreshedEvent event) { |
|||
if (OS.isRunOnWindows()) { |
|||
return; |
|||
} |
|||
|
|||
NetworkSetting setting = networkSettingDao.get(); |
|||
try { |
|||
doSetNetworkSetting(setting); |
|||
setting.firstRun = false; |
|||
networkSettingDao.update(setting); |
|||
log.info("Network setting applied on startup: {}", setting); |
|||
} catch (Exception e) { |
|||
log.error("setting network failed: {}", e.getMessage()); |
|||
} |
|||
} |
|||
} |
@ -1,33 +0,0 @@ |
|||
package a8k.app.service.setting; |
|||
|
|||
import a8k.app.dao.NetworkSettingDao; |
|||
import a8k.app.dao.type.db.NetworkSetting; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class AppNetworkSettingService { |
|||
|
|||
private final NetworkSettingDao networkSettingDao; |
|||
|
|||
public NetworkSetting getNetworkSetting() { |
|||
return networkSettingDao.get(); |
|||
} |
|||
|
|||
public void setNetworkSetting(NetworkSetting setting) { |
|||
networkSettingDao.update(setting); |
|||
log.info("Network configuration updated: {}", setting); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue