You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

143 lines
4.1 KiB

import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import router from "@/router";
const settingStore = defineStore("settingStore", {
state: (): any => {
return {
title: "虚拟仿真实验项目",
qw: 0, // 气温
zl: 0, // 质量
srmj: 0, // 散热面积
jrgl: 0, // 加热功率
wdsz: 0, // 温度数值
ip: "", // ip
port: "", // 端口
cssd: 0, // 初始湿度
jsgl: 0, // 加湿功率
jsmj: 0, // 加湿面积
sdsz: 0, // 湿度数值
falg: false,
timer: null,
experimentPreservation:
Boolean(localStorage.getItem("experimentPreservation")) || false, // 是否设计好实验
saveRoute: Boolean(localStorage.getItem("saveRoute")) || false, // 是否保存路由
stepIds: null,
wenduCode: null,
shiduCode: null,
isRunning: false,
isRunCode: Boolean(localStorage.getItem("isRunCode")) || false,
shiyanData: [],
};
},
actions: {
setTitle(name: any) {
this.title = name;
},
setQw(value: number) {
console.log(value);
this.qw = value;
},
setValue(value: number | boolean, name: string) {
this[name] = value;
},
openHeating() {
this.flag = !this.flag; // 切换状态
if (this.flag) {
// 开启加热
this.timer = setInterval(() => {
this.qw++;
if (this.qw >= 100) {
clearInterval(this.timer);
}
}, 500);
} else {
// 关闭加热
clearInterval(this.timer);
}
},
calculateTemperature() {
this.qw <= 0 ? (this.qw = 1) : this.qw;
const a = (100000 * this.zl * this.srmj) / this.jrgl;
let time = 0;
let currentTemp = this.qw;
const interval = setInterval(() => {
currentTemp = (1 - Math.exp(-time / a)) * 100 + this.qw;
console.log(`时间: ${time}s, 温度: ${currentTemp.toFixed(2)}°C`);
this.qw = currentTemp >= 100 ? 100 : currentTemp;
if (currentTemp >= 100) {
console.log("温度达到 100°C, 停止加热!");
clearInterval(interval);
}
time += 1; // 每秒增加 1s
}, 1000);
},
simulateHeatingAndHumidifying() {
// const router = useRouter();
if (this.isRunning) return; // 如果已经在运行,则直接返回
this.isRunning = true;
this.shiyanData = [];
// 计算时间常数
const a = (100000 * this.zl * this.srmj) / this.jrgl;
const b = (10000 * this.jsmj) / this.jsgl;
let time = 0;
let currentTemp = this.qw;
let currentHumidity = this.cssd;
const interval = setInterval(() => {
// 计算温度
currentTemp = (1 - Math.exp(-time / a)) * 100 + this.qw;
if (currentTemp > 100) currentTemp = 100;
// 计算湿度
currentHumidity = (1 - Math.exp(-time / b)) * 100 + this.cssd;
if (currentHumidity > 100) currentHumidity = 100;
console.log(
`时间: ${time}s, 温度: ${currentTemp.toFixed(
2
)}°C, 湿度: ${currentHumidity}%RH`
);
this.shiyanData.push({
time,
currentTemp,
currentHumidity,
});
this.qw = currentTemp >= 100 ? 100 : currentTemp;
this.cssd =
currentHumidity >= 100 ? 100 : Number(currentHumidity.toFixed(2));
// 停止加热 & 加湿
if (currentTemp >= 100 && currentHumidity >= 100) {
console.log("温度和湿度均达到上限,停止模拟!");
ElMessage.warning("温度和湿度均达到上限,停止模拟");
if (this.isRunCode) {
router.push({
path: "/experimentalData",
});
} else {
router.push({
path: "/",
query: {
idea: true,
},
});
}
clearInterval(interval);
this.isRunning = false; // 复位标志,允许再次触发
}
time += 1; // 每秒增加 1s
}, 1000);
},
},
});
export default settingStore;