Commit 1f108d72 authored by wubi's avatar wubi

添加初始化检测逻辑

parent 17b292fd
...@@ -8,7 +8,9 @@ const sudoPrompt = require('sudo-prompt'); ...@@ -8,7 +8,9 @@ const sudoPrompt = require('sudo-prompt');
const os = require('os'); const os = require('os');
const Store = require('electron-store'); const Store = require('electron-store');
const psTree = require('ps-tree'); const psTree = require('ps-tree');
const {SerialPort}=require('serialport') const {SerialPort}=require('serialport');
const { exit } = require('process');
//const sudoPrompt = require('sudo-prompt');
app.commandLine.appendSwitch('ignore-certificate-errors'); app.commandLine.appendSwitch('ignore-certificate-errors');
app.commandLine.appendSwitch('allow-insecure-localhost'); app.commandLine.appendSwitch('allow-insecure-localhost');
// 配置和状态管理 // 配置和状态管理
...@@ -90,7 +92,7 @@ const createWindow = () => { ...@@ -90,7 +92,7 @@ const createWindow = () => {
}); });
state.uiWindow.webContents.openDevTools() //state.uiWindow.webContents.openDevTools()
state.uiWindow.loadFile(path.join(__dirname, 'web/index.html')); state.uiWindow.loadFile(path.join(__dirname, 'web/index.html'));
}; };
...@@ -118,6 +120,30 @@ function registerNodredShortcut() { ...@@ -118,6 +120,30 @@ function registerNodredShortcut() {
} }
} }
// 注册全局快捷键并打开指定页面
function registerProtocolShortcut() {
const ret = globalShortcut.register('Control+Shift+T', () => {
console.log('Ctrl+Shift+T pressed');
// 创建新窗口或导航到指定页面
const newWindow = new BrowserWindow({
width: 1400,
height: 900,
});
// 打开你的指定页面(例如:special.html)
//newWindow.loadFile('special.html');
// 或者使用URL:
//newWindow.webContents.openDevTools()
newWindow.loadURL('http://192.168.101.113:1910');
});
if (!ret) {
console.error('Shortcut registration failed');
}
}
// 用户数据目录和配置文件管理 // 用户数据目录和配置文件管理
...@@ -173,6 +199,28 @@ function handleAction(event, action, callback) { ...@@ -173,6 +199,28 @@ function handleAction(event, action, callback) {
} }
} }
async function checkDialoutPermission() {
return new Promise((resolve, reject) => {
// 使用 'groups' 命令来获取当前用户的组列表
exec('groups', (error, stdout, stderr) => {
if (error) {
console.error(`执行 'groups' 命令失败: ${error}`);
// 如果命令执行失败,通常意味着没有权限,或者发生了其他错误
return resolve(false);
}
// 'groups' 命令的输出是包含所有组名的字符串,例如:"user sudo dialout"
// 我们将其拆分成一个数组,以便于检查
const userGroups = stdout.toString().trim().split(/\s+/);
// 检查数组中是否包含 'dialout'
const hasPermission = userGroups.includes('dialout');
resolve(hasPermission);
});
});
}
function setToken(event, token) { function setToken(event, token) {
console.log("setToken:", token); console.log("setToken:", token);
state.currentToken = token; state.currentToken = token;
...@@ -329,6 +377,11 @@ function stopServices() { ...@@ -329,6 +377,11 @@ function stopServices() {
} }
} }
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 应用生命周期管理 // 应用生命周期管理
app.whenReady().then(async () => { app.whenReady().then(async () => {
await initConfigFile(); await initConfigFile();
...@@ -357,11 +410,55 @@ app.whenReady().then(async () => { ...@@ -357,11 +410,55 @@ app.whenReady().then(async () => {
// 创建窗口 // 创建窗口
createWindow(); createWindow();
// 启动服务 // 启动服务
startNodeRed(); startNodeRed();
startMQTT(); startMQTT();
await sleep(2000);
//检测dialout权限
let perm = await checkDialoutPermission();
console.log("dialout:",perm);
if(!perm){
const options = {
type: 'question',
buttons: ['是', '否'], // 按钮选项
defaultId: 0, // 默认选中的按钮索引
title: '权限配置',
message: '当前用户无被测设备访问权限是否获取?',
detail: '注意:执行此操作可能需要管理员权限,配置成功后自动重启系统!',
};
// showMessageBox 返回一个 Promise,我们使用 await 等待它完成
const response = await dialog.showMessageBox(null, options);
console.log("response:",response.response);
if(response.response == 1){
app.quit();
}else{
username = os.userInfo().username;
//用户获取权限
getpermission = `usermod -aG dialout ${username} && reboot`
console.log(getpermission)
// const option = {
// name: 'iot-test' // Your application name
// };
sudoPrompt.exec(getpermission, {name:"iot test"}, (error, stdout, stderr) => {
if (error) {
console.error(error);
return;
}
// 打印 stdout 和 stderr 的输出
console.log('stdout:', stdout);
console.log('stderr:', stderr);
});
}
}
// 注册全局快捷键(开发模式) // 注册全局快捷键(开发模式)
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
globalShortcut.register('CommandOrControl+R', () => { globalShortcut.register('CommandOrControl+R', () => {
...@@ -373,7 +470,11 @@ app.whenReady().then(async () => { ...@@ -373,7 +470,11 @@ app.whenReady().then(async () => {
}); });
} }
registerNodredShortcut(); registerNodredShortcut();
registerProtocolShortcut();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment