news 2026/4/23 1:28:18

Cordova与OpenHarmony施肥记录管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Cordova与OpenHarmony施肥记录管理

欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。

施肥管理系统概述

施肥记录管理系统用于记录和追踪植物的施肥历史。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个完整的施肥管理系统,包括施肥记录的创建、查询、统计和提醒功能。这个系统需要考虑不同类型肥料的管理和施肥周期的计算。

施肥记录数据模型

classFertilizerType{constructor(id,name,npkRatio,description){this.id=id;this.name=name;this.npkRatio=npkRatio;// 氮磷钾比例this.description=description;}}classFertilizingRecord{constructor(plantId,fertilizerType,amount,notes){this.id='fert_'+Date.now();this.plantId=plantId;this.fertilizerType=fertilizerType;this.amount=amount;// 克this.date=newDate();this.notes=notes;}}classFertilizingManager{constructor(){this.records=[];this.fertilizerTypes=[];this.initDefaultFertilizers();this.loadFromStorage();}initDefaultFertilizers(){this.fertilizerTypes=[newFertilizerType('fert_1','通用肥','10-10-10','适合大多数植物'),newFertilizerType('fert_2','高氮肥','20-10-10','促进叶片生长'),newFertilizerType('fert_3','高磷肥','10-20-10','促进开花结果'),newFertilizerType('fert_4','高钾肥','10-10-20','增强抗性')];}addFertilizingRecord(plantId,fertilizerType,amount,notes){constrecord=newFertilizingRecord(plantId,fertilizerType,amount,notes);this.records.push(record);this.saveToStorage();returnrecord;}}

这个施肥记录数据模型定义了FertilizerType、FertilizingRecord和FertilizingManager类。FertilizerType类定义了肥料类型及其NPK比例,FertilizingRecord类记录每次施肥的详细信息,FertilizingManager类管理所有施肥记录和肥料类型。

与OpenHarmony数据库的集成

functionsaveFertilizingRecordToDatabase(record){cordova.exec(function(result){console.log("施肥记录已保存到数据库");},function(error){console.error("保存失败:",error);},"DatabasePlugin","saveFertilizingRecord",[{id:record.id,plantId:record.plantId,fertilizerType:record.fertilizerType,amount:record.amount,date:record.date.toISOString(),notes:record.notes}]);}functionloadFertilizingRecordsFromDatabase(){cordova.exec(function(result){console.log("施肥记录已从数据库加载");fertilizingManager.records=result.map(rec=>{constrecord=newFertilizingRecord(rec.plantId,rec.fertilizerType,rec.amount,rec.notes);record.id=rec.id;record.date=newDate(rec.date);returnrecord;});renderFertilizingRecords();},function(error){console.error("加载失败:",error);},"DatabasePlugin","loadFertilizingRecords",[]);}

这段代码展示了如何与OpenHarmony的数据库进行交互。saveFertilizingRecordToDatabase函数将施肥记录保存到数据库,loadFertilizingRecordsFromDatabase函数从数据库加载所有施肥记录。通过这种方式,我们确保了施肥数据的持久化存储。

施肥记录列表展示

functionrenderFertilizingRecords(plantId){constplant=plants.find(p=>p.id===plantId);if(!plant)return;constrecords=fertilizingManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));constcontainer=document.getElementById('page-container');container.innerHTML=`<div class="fertilizing-records-container"> <h2>${plant.name}的施肥记录</h2> <button class="add-record-btn" onclick="showAddFertilizingRecordDialog('${plantId}')"> ➕ 添加施肥记录 </button> </div>`;if(records.length===0){container.innerHTML+='<p class="empty-message">还没有施肥记录</p>';return;}constrecordsList=document.createElement('div');recordsList.className='records-list';records.forEach(record=>{constfertilizerType=fertilizingManager.fertilizerTypes.find(f=>f.id===record.fertilizerType);constrecordItem=document.createElement('div');recordItem.className='record-item';recordItem.innerHTML=`<div class="record-info"> <p class="record-date">${record.date.toLocaleString('zh-CN')}</p> <p class="fertilizer-type">🌾 肥料:${fertilizerType?.name||'未知'}</p> <p class="fertilizer-npk">NPK比例:${fertilizerType?.npkRatio||'N/A'}</p> <p class="record-amount">用量:${record.amount}克</p>${record.notes?`<p class="record-notes">备注:${record.notes}</p>`:''}</div> <div class="record-actions"> <button onclick="editFertilizingRecord('${record.id}')">编辑</button> <button onclick="deleteFertilizingRecord('${record.id}')">删除</button> </div>`;recordsList.appendChild(recordItem);});container.appendChild(recordsList);}

这个函数负责渲染施肥记录列表。它显示了特定植物的所有施肥记录,包括日期、肥料类型、NPK比例和用量。用户可以通过"编辑"和"删除"按钮管理记录。这种设计提供了清晰的记录展示。

添加施肥记录对话框

functionshowAddFertilizingRecordDialog(plantId){constdialog=document.createElement('div');dialog.className='modal-dialog';letfertilizerOptions='';fertilizingManager.fertilizerTypes.forEach(fert=>{fertilizerOptions+=`<option value="${fert.id}">${fert.name}(${fert.npkRatio})</option>`;});dialog.innerHTML=`<div class="modal-content"> <h3>添加施肥记录</h3> <form id="add-fertilizing-form"> <div class="form-group"> <label>肥料类型</label> <select id="fertilizer-type" required> <option value="">请选择肥料类型</option>${fertilizerOptions}</select> </div> <div class="form-group"> <label>用量 (克)</label> <input type="number" id="fertilizer-amount" min="0" required> </div> <div class="form-group"> <label>施肥日期</label> <input type="datetime-local" id="fertilizing-date" required> </div> <div class="form-group"> <label>备注</label> <textarea id="fertilizing-notes"></textarea> </div> <div class="form-actions"> <button type="submit">保存</button> <button type="button" onclick="closeDialog()">取消</button> </div> </form> </div>`;document.getElementById('modal-container').appendChild(dialog);constnow=newDate();document.getElementById('fertilizing-date').value=now.toISOString().slice(0,16);document.getElementById('add-fertilizing-form').addEventListener('submit',function(e){e.preventDefault();constfertilizerType=document.getElementById('fertilizer-type').value;constamount=parseFloat(document.getElementById('fertilizer-amount').value);constdate=newDate(document.getElementById('fertilizing-date').value);constnotes=document.getElementById('fertilizing-notes').value;constrecord=newFertilizingRecord(plantId,fertilizerType,amount,notes);record.date=date;fertilizingManager.records.push(record);fertilizingManager.saveToStorage();saveFertilizingRecordToDatabase(record);closeDialog();renderFertilizingRecords(plantId);showToast('施肥记录已添加');});}

这个函数创建并显示添加施肥记录的对话框。用户可以选择肥料类型、输入用量、日期和备注。提交后,新记录会被添加到fertilizingManager中,并保存到数据库。这种设计提供了灵活的记录输入方式。

施肥统计功能

classFertilizingStatistics{constructor(fertilizingManager){this.fertilizingManager=fertilizingManager;}getTotalFertilizingCount(plantId){returnthis.fertilizingManager.records.filter(r=>r.plantId===plantId).length;}getAverageFertilizerAmount(plantId){constrecords=this.fertilizingManager.records.filter(r=>r.plantId===plantId);if(records.length===0)return0;consttotal=records.reduce((sum,r)=>sum+r.amount,0);returntotal/records.length;}getMostUsedFertilizer(plantId){constrecords=this.fertilizingManager.records.filter(r=>r.plantId===plantId);constfertilizerCounts={};records.forEach(record=>{fertilizerCounts[record.fertilizerType]=(fertilizerCounts[record.fertilizerType]||0)+1;});constmostUsed=Object.keys(fertilizerCounts).reduce((a,b)=>fertilizerCounts[a]>fertilizerCounts[b]?a:b);returnthis.fertilizingManager.fertilizerTypes.find(f=>f.id===mostUsed);}getFertilizingFrequency(plantId,days=30){constrecords=this.fertilizingManager.records.filter(r=>r.plantId===plantId);constcutoffDate=newDate();cutoffDate.setDate(cutoffDate.getDate()-days);constrecentRecords=records.filter(r=>newDate(r.date)>cutoffDate);return(recentRecords.length/days*7).toFixed(2);// 每周施肥次数}}

这个FertilizingStatistics类提供了施肥的统计功能。getTotalFertilizingCount返回施肥总次数,getAverageFertilizerAmount计算平均用量,getMostUsedFertilizer返回最常用的肥料,getFertilizingFrequency计算施肥频率。这些统计信息可以帮助用户了解施肥规律。

施肥提醒功能

functioncheckFertilizingReminders(){plants.forEach(plant=>{constlastFertilizingDate=getLastFertilizingDate(plant.id);constfertilizingInterval=plant.fertilizingInterval||14;// 默认14天if(!lastFertilizingDate){sendFertilizingReminder(plant.id,plant.name,'从未施过肥');return;}constdaysSinceFertilizing=Math.floor((newDate()-newDate(lastFertilizingDate))/(24*60*60*1000));if(daysSinceFertilizing>=fertilizingInterval){sendFertilizingReminder(plant.id,plant.name,`${daysSinceFertilizing}天未施肥`);}});}functionsendFertilizingReminder(plantId,plantName,message){cordova.exec(function(result){console.log("施肥提醒已发送");},function(error){console.error("提醒发送失败:",error);},"NotificationPlugin","sendReminder",[{title:`${plantName}需要施肥`,message:message,plantId:plantId,type:'fertilizing'}]);}functiongetLastFertilizingDate(plantId){constrecords=fertilizingManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));returnrecords.length>0?records[0].date:null;}setInterval(checkFertilizingReminders,60*60*1000);// 每小时检查一次

这段代码实现了施肥提醒功能。checkFertilizingReminders函数检查所有植物,如果某个植物超过设定的施肥间隔,就发送提醒。通过NotificationPlugin,我们可以向用户发送系统通知。这个功能帮助用户不会忘记给植物施肥。

肥料库存管理

classFertilizerInventory{constructor(){this.inventory=newMap();// 肥料ID -> 库存量}addFertilizerStock(fertilizerTypeId,quantity){constcurrent=this.inventory.get(fertilizerTypeId)||0;this.inventory.set(fertilizerTypeId,current+quantity);}useFertilizer(fertilizerTypeId,quantity){constcurrent=this.inventory.get(fertilizerTypeId)||0;if(current<quantity){returnfalse;// 库存不足}this.inventory.set(fertilizerTypeId,current-quantity);returntrue;}getFertilizerStock(fertilizerTypeId){returnthis.inventory.get(fertilizerTypeId)||0;}}

这个FertilizerInventory类管理肥料的库存。通过addFertilizerStock添加库存,useFertilizer消耗库存,getFertilizerStock查询库存。这个功能帮助用户管理肥料的库存情况。

总结

施肥记录管理系统是植物养护应用的重要功能。通过合理的数据模型设计、与OpenHarmony系统的集成和各种统计分析功能,我们可以创建一个功能完整的施肥管理系统,帮助用户科学地管理植物的施肥。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/19 8:59:22

纸质档案存隐患?档案宝全生命周期电子化管理

在企业日常运营中&#xff0c;档案管理是不可或缺的基础工作&#xff0c;合同文件、财务凭证、人事资料、项目报告等各类档案承载着组织的核心信息&#xff0c;其管理质量直接影响企业运营效率与风险防控能力。然而&#xff0c;传统纸质档案管理模式长期以来存在诸多难以解决的…

作者头像 李华
网站建设 2026/4/17 7:40:14

Python+AI 打造每日新闻简报应用(聚合热搜 + 智能摘要 + 语音播报)

一、教程概述 本教程将带你从零搭建一款 AI 驱动的每日新闻简报应用「Briefy」&#xff0c;核心功能包括聚合多平台热搜、AI 智能摘要、语音播报&#xff0c;最终实现 “5 分钟掌握全网热点” 的高效信息获取工具。适合有 Python 基础、对 AI 应用开发感兴趣的开发者&#xff…

作者头像 李华
网站建设 2026/4/17 10:39:54

JUnit 5 中的 @ClassTemplate 实战指南

当你在本地、测试环境和 CI 中跑同一组测试时&#xff0c;是否遇到过这样的困惑&#xff1a;同一段业务逻辑在不同配置、不同 Locale 下的表现不尽相同&#xff0c;但你又不想为每种场景复制一堆几乎一样的测试类&#xff1f;如果把所有分支逻辑都塞进一个测试方法里&#xff0…

作者头像 李华
网站建设 2026/4/20 9:24:20

Parasoft Jtest 如何用 JSON 文件驱动Java 测试自动化

在金融、汽车、医疗等对可靠性与合规性要求较高的行业&#xff0c;Java 应用中的代码缺陷可能直接导致资金损失、服务中断或监管处罚。Parasoft Jtest 是一款企业级 Java 自动化测试平台&#xff0c;支持静态代码分析、智能单元测试生成、代码覆盖率评估以及合规规则检查。其内…

作者头像 李华
网站建设 2026/4/21 3:37:36

固定头尾、中间滚动?用Flex + vh轻松搞定三栏布局

固定头尾、中间滚动&#xff1f;用Flex vh轻松搞定三栏布局固定头尾、中间滚动&#xff1f;用Flex vh轻松搞定三栏布局引言&#xff1a;为什么页面头尾固定这么让人头疼CSS Flex 布局快速上手指南——从“ Flex 是谁”到“ Flex 是我兄弟”1. 激活 Flex 模式2. 主轴与交叉轴—…

作者头像 李华
网站建设 2026/4/18 10:40:13

微电网恒功率PQ控制策略下的LCL并网仿真研究

微电网恒功率PQ控制&#xff0c;LCL并网仿真最近在搞微电网并网控制时发现个有意思的事——并网逆变器的PQ控制策略和LCL滤波器配合使用时&#xff0c;参数整定能把人绕晕。今天咱们就手撕个MATLAB仿真&#xff0c;看看这个经典组合到底怎么玩。先说说控制逻辑的核心&#xff1…

作者头像 李华