Tauri + Rust 系统信息小工具

最近看到很多推荐Tauri的文章,顺便学习下写了个小工具,用到以下知识

  • 前端 vite + vue3
  • 后端 rust

项目全部代码 https://github.com/LeoBest2/rust-tauri-example

Rust工具

后端

参照Tauri官网https://tauri.app/zh-cn/v1/guides/getting-started/setup/vite生成模板
获取系统信息使用 sysinfo 库, https://docs.rs/sysinfo/0.27.7/sysinfo/
总体Rust代码很少,主要是前端

main.rs代码

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

use serde::{Deserialize, Serialize};
use sysinfo::{System, SystemExt, CpuExt};

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[derive(Serialize, Deserialize)]
struct SystemInfo {
    memory_total: f32,
    memory_used: f32,
    cpu_used: f32,
	hostname: String
}

#[tauri::command]
fn system_info() -> SystemInfo {
    let mut sys = System::new_all();
    sys.refresh_all();
    SystemInfo {
        memory_total: sys.total_memory() as f32 / (1024 * 1024 * 1024) as f32,
        memory_used: sys.used_memory() as f32 / (1024 * 1024 * 1024) as f32,
        cpu_used: sys.global_cpu_info().cpu_usage(),
		hostname: sys.host_name().unwrap(),
    }
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![system_info])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

前端

前端用到Bootstrap样式 + echarts, 定时调用rust中system_info函数更新

<script setup>
import { onMounted, ref } from "vue";
import { invoke } from "@tauri-apps/api/tauri";
import * as echarts from 'echarts';

const memChartDom = ref(null);
const cpuChartDom = ref(null);
const sysinfo = ref({
	memory_total: 0,
	memory_used: 0,
	cpu_used: 0,
	hostname: '未初始化'
});

const memChart = ref('');
const cpuChart = ref('');

onMounted(() => {
	memChart.value = echarts.init(memChartDom.value);
	memChart.value.setOption({
		title: {
			left: 'center',
			text: '内存使用率',
			textStyle: {
				color: '#37a2da'
			},
		},
		series: [
			{
				type: 'gauge',
				axisLine: {
					lineStyle: {
						width: 30,
						color: [
							[0.3, '#67e0e3'],
							[0.7, '#37a2da'],
							[1, '#fd666d']
						]
					}
				},
				pointer: {
					itemStyle: {
						color: 'inherit'
					}
				},
				axisTick: {
					distance: -30,
					length: 8,
					lineStyle: {
						color: '#fff',
						width: 2
					}
				},
				splitLine: {
					distance: -30,
					length: 30,
					lineStyle: {
						color: '#fff',
						width: 4
					}
				},
				axisLabel: {
					color: 'inherit',
					distance: 40,
					fontSize: 20
				},
				detail: {
					valueAnimation: true,
					formatter: '{value}',
					color: 'inherit'
				},
				data: [
					{
						value: 0,
					}
				]
			}
		]
	});
	cpuChart.value = echarts.init(cpuChartDom.value);
	cpuChart.value.setOption({
		title: {
			left: 'center',
			text: 'CPU使用率',
			textStyle: {
				color: '#37a2da'
			},
		},
		series: [
			{
				type: 'gauge',
				axisLine: {
					lineStyle: {
						width: 30,
						color: [
							[0.3, '#67e0e3'],
							[0.7, '#37a2da'],
							[1, '#fd666d']
						]
					}
				},
				pointer: {
					itemStyle: {
						color: 'inherit'
					}
				},
				axisTick: {
					distance: -30,
					length: 8,
					lineStyle: {
						color: '#fff',
						width: 2
					}
				},
				splitLine: {
					distance: -30,
					length: 30,
					lineStyle: {
						color: '#fff',
						width: 4
					}
				},
				axisLabel: {
					color: 'inherit',
					distance: 40,
					fontSize: 20
				},
				detail: {
					valueAnimation: true,
					formatter: '{value}',
					color: 'inherit'
				},
				data: [
					{
						value: 0,
					}
				]
			}
		]
	});
	setInterval(() => updateCpuMem(), 3000);
})

async function updateCpuMem() {
	let result = await invoke("system_info");
	sysinfo.value = result;
	cpuChart.value.setOption({
		series: { data: [{ value: (result.cpu_used).toFixed(1) }] }
	});
	memChart.value.setOption({
		series: { data: [{ value: (result.memory_used / result.memory_total * 100).toFixed(1) }] }
	});
}

</script>

<template>
	<div class="row justify-content-center text-center mt-4">
		<table class="table table-striped-columns">
			<thead>
				<tr>
					<th scope="col">主机信息</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>主机名</td>
					<td>{{ sysinfo.hostname }}</td>
					<td>CPU使用率</td>
					<td>{{ sysinfo.cpu_used.toFixed(1) }}%</td>
				</tr>
				<tr>
					<td>已用内存</td>
					<td>{{ sysinfo.memory_used.toFixed(1) }} GB</td>
					<td>总内存</td>
					<td>{{ sysinfo.memory_total.toFixed(1) }} GB</td>
				</tr>
			</tbody>

		</table>

	</div>
	<div class="row justify-content-center">
		<div class="col-6">
			<div ref="cpuChartDom" :style="{ width: '100%', height: '400px' }"></div>
		</div>
		<div class="col-6">
			<div ref="memChartDom" :style="{ width: '100%', height: '400px' }"></div>
		</div>
	</div>
</template>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 TauriRust 结合实现接入 AI 是一种可行的方式。以下是一种简单的实现思路: 1. 选择 AI 库: 首先,选择一个适合你的需求的 AI 库。在 Rust 中,有许多成熟的 AI 库可供选择,例如 TensorFlow、PyTorch、ONNX Runtime 等。根据你的具体需求,选择一个合适的库来进行 AI 模型的加载和推理。 2. 在 Rust 中加载和使用 AI 模型: 使用选定的 AI 库,在 Rust 中加载和使用 AI 模型。这通常涉及到模型的加载、预处理输入数据、调用 AI 模型进行推理,以及处理输出结果等步骤。根据你选择的 AI 库和具体的 AI 模型,可以参考相应的文档和示例代码来实现这些功能。 3. 在 Tauri 中创建自定义 API: 在 TauriRust 代码中创建自定义 API,用于将 AI 功能封装为 Tauri 的 API。这样,你就可以在 JavaScript/TypeScript 中通过 Tauri API 调用 AI 功能。 4. 在前端代码中调用 Tauri API: 在你的前端代码(如 Angular)中,使用 Tauri 提供的方法来调用自定义的 AI API。通过调用 Tauri API,你可以将输入数据发送到 Rust 代码中进行 AI 推理,并将结果返回给前端进行展示或后续处理。 需要注意的是,具体的实现方式会因为选择的 AI 库和具体的 AI 模型而有所不同。你可以参考 Tauri 和选定 AI 库的文档、示例代码以及相关社区资源来实现 AI 功能的接入。另外,确保在 Rust 中使用 AI 库时,根据需要处理好内存管理、并发性能等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值