当前位置: 首页 > news >正文

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系统的集成和各种统计分析功能,我们可以创建一个功能完整的施肥管理系统,帮助用户科学地管理植物的施肥。

http://www.jsqmd.com/news/100946/

相关文章:

  • Solidity入门(8)-库合约Library
  • 还买啥USB网卡~直接开启RNDIS就行
  • DApp开发暴风指南:7天从零到上线,手把手教你用代码撬动Web3流量红利
  • 冥想第一千七百三十五天(1735)
  • 零碳园区应急能源基础架构规划:备用电源与清洁能源联动配置
  • 2026年EOR名义雇主服务优势TOP8对比榜单,助力全球化布局与用工优化
  • 【课程设计/毕业设计】基于SpringBoot的在线天气查询系统基于springboot天气预报查询系统【附源码、数据库、万字文档】
  • 2025 全国最新水池布厂家TOP5 评测!云南等地优质企业权威榜单发布,赋能现代设施农业 - 全局中转站
  • 【MongoDB实战】5.1 聚合管道基础:理解阶段(Stage)概念
  • 实用指南:(113页PPT)西门子制造业研发工艺协同平台及制造平台整体规划(附下载方式)
  • Vue低代码平台实测红黑榜:别让“伪效率“消耗你的团队
  • 单目实时3D识别
  • 【计算机毕设】移动互联时代新闻编辑力探析(系统配套LW+开题报告+任务书)
  • 构建高效测试体系:测试文档编写规范详解
  • 简单的创建一个Spring Boot网页
  • 【课程设计/毕业设计】基于SpringBoot+Vue茶叶销售系统的设计与实现基于Java语言的茶叶销售系统的前端设计与实现【附源码、数据库、万字文档】
  • 通俗易懂的理解 LLM 知识入门浅析:核心原理、LLM是怎么学习的、LLM的局限性、Transformer 架构、注意力机制、如何训练一个 LLM
  • 如何降低对标注数据的依赖,实现多病种检测与病灶精准定位?请看此文
  • 长沙美食小吃攻略|五一广场 和 太平老街:不是来旅游,是来“吃服”的! - 资讯焦点
  • 第四章算法作业
  • 播放器视频后处理实践(二)氛围模式
  • 版本升级|Origin 2026 科学绘图与数据分析软件
  • 基于改进A*算法融合DWA算法的机器人路径规划MATLAB仿真程序(含注释) 包含传统A*算法...
  • 【课程设计/毕业设计】基于springboot/javaEE的二手手机交易平台的设计与实现基于javaEE的二手手机交易平台的设计与实现【附源码、数据库、万字文档】
  • 基于AI数字人系统源码的低成本开发方案与实践经验
  • K-Means聚类+PCA降维:高维数据聚类的最优组合实战指南
  • SQL 调优全解:从 20 秒到 200 ms 的 6 步实战笔记(附脚本)
  • AI一周重要会议和活动(12.15-12.22)
  • Nano Banana Pro:设计师的威胁,还是创意领域的新伙伴?
  • BioSIM 抗人 IL-1b 抗体SIM0362:多种应用兼容性,适应多样化实验需求