news 2026/5/1 6:47:05

Cordova与OpenHarmony修剪记录系统

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Cordova与OpenHarmony修剪记录系统

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

修剪管理系统概述

修剪是植物养护的重要环节,它促进植物的生长和塑形。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个完整的修剪记录系统,包括修剪记录的创建、查询、统计和提醒功能。这个系统需要记录修剪的类型、位置和效果。

修剪记录数据模型

classPruningType{constructor(id,name,description,purpose){this.id=id;this.name=name;this.description=description;this.purpose=purpose;// 修剪目的}}classPruningRecord{constructor(plantId,pruningType,location,notes){this.id='prune_'+Date.now();this.plantId=plantId;this.pruningType=pruningType;this.location=location;// 修剪位置this.date=newDate();this.notes=notes;this.branchesRemoved=0;// 移除的枝条数}}classPruningManager{constructor(){this.records=[];this.pruningTypes=[];this.initDefaultPruningTypes();this.loadFromStorage();}initDefaultPruningTypes(){this.pruningTypes=[newPruningType('prune_1','轻修剪','移除枯死或病弱枝条','清理'),newPruningType('prune_2','中度修剪','塑形和促进分枝','塑形'),newPruningType('prune_3','重度修剪','大幅度修剪以促进新生长','更新'),newPruningType('prune_4','摘心','移除顶端生长点','促进分枝')];}addPruningRecord(plantId,pruningType,location,notes){constrecord=newPruningRecord(plantId,pruningType,location,notes);this.records.push(record);this.saveToStorage();returnrecord;}}

这个修剪记录数据模型定义了PruningType、PruningRecord和PruningManager类。PruningType类定义了修剪类型及其目的,PruningRecord类记录每次修剪的详细信息,PruningManager类管理所有修剪记录。

与OpenHarmony数据库的集成

functionsavePruningRecordToDatabase(record){cordova.exec(function(result){console.log("修剪记录已保存到数据库");},function(error){console.error("保存失败:",error);},"DatabasePlugin","savePruningRecord",[{id:record.id,plantId:record.plantId,pruningType:record.pruningType,location:record.location,date:record.date.toISOString(),notes:record.notes,branchesRemoved:record.branchesRemoved}]);}functionloadPruningRecordsFromDatabase(){cordova.exec(function(result){console.log("修剪记录已从数据库加载");pruningManager.records=result.map(rec=>{constrecord=newPruningRecord(rec.plantId,rec.pruningType,rec.location,rec.notes);record.id=rec.id;record.date=newDate(rec.date);record.branchesRemoved=rec.branchesRemoved;returnrecord;});renderPruningRecords();},function(error){console.error("加载失败:",error);},"DatabasePlugin","loadPruningRecords",[]);}

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

修剪记录列表展示

functionrenderPruningRecords(plantId){constplant=plants.find(p=>p.id===plantId);if(!plant)return;constrecords=pruningManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));constcontainer=document.getElementById('page-container');container.innerHTML=`<div class="pruning-records-container"> <h2>${plant.name}的修剪记录</h2> <button class="add-record-btn" onclick="showAddPruningRecordDialog('${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=>{constpruningType=pruningManager.pruningTypes.find(p=>p.id===record.pruningType);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="pruning-type">✂️ 修剪类型:${pruningType?.name||'未知'}</p> <p class="pruning-purpose">目的:${pruningType?.purpose||'N/A'}</p> <p class="pruning-location">位置:${record.location}</p> <p class="branches-removed">移除枝条:${record.branchesRemoved}条</p>${record.notes?`<p class="record-notes">备注:${record.notes}</p>`:''}</div> <div class="record-actions"> <button onclick="editPruningRecord('${record.id}')">编辑</button> <button onclick="deletePruningRecord('${record.id}')">删除</button> </div>`;recordsList.appendChild(recordItem);});container.appendChild(recordsList);}

这个函数负责渲染修剪记录列表。它显示了特定植物的所有修剪记录,包括日期、修剪类型、目的、位置和移除的枝条数。用户可以通过"编辑"和"删除"按钮管理记录。这种设计提供了清晰的记录展示。

添加修剪记录对话框

functionshowAddPruningRecordDialog(plantId){constdialog=document.createElement('div');dialog.className='modal-dialog';letpruningTypeOptions='';pruningManager.pruningTypes.forEach(type=>{pruningTypeOptions+=`<option value="${type.id}">${type.name}-${type.purpose}</option>`;});dialog.innerHTML=`<div class="modal-content"> <h3>添加修剪记录</h3> <form id="add-pruning-form"> <div class="form-group"> <label>修剪类型</label> <select id="pruning-type" required> <option value="">请选择修剪类型</option>${pruningTypeOptions}</select> </div> <div class="form-group"> <label>修剪位置</label> <input type="text" id="pruning-location" placeholder="例如: 顶端、左侧枝条" required> </div> <div class="form-group"> <label>移除枝条数</label> <input type="number" id="branches-removed" min="0" value="0"> </div> <div class="form-group"> <label>修剪日期</label> <input type="datetime-local" id="pruning-date" required> </div> <div class="form-group"> <label>备注</label> <textarea id="pruning-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('pruning-date').value=now.toISOString().slice(0,16);document.getElementById('add-pruning-form').addEventListener('submit',function(e){e.preventDefault();constpruningType=document.getElementById('pruning-type').value;constlocation=document.getElementById('pruning-location').value;constbranchesRemoved=parseInt(document.getElementById('branches-removed').value);constdate=newDate(document.getElementById('pruning-date').value);constnotes=document.getElementById('pruning-notes').value;constrecord=newPruningRecord(plantId,pruningType,location,notes);record.date=date;record.branchesRemoved=branchesRemoved;pruningManager.records.push(record);pruningManager.saveToStorage();savePruningRecordToDatabase(record);closeDialog();renderPruningRecords(plantId);showToast('修剪记录已添加');});}

这个函数创建并显示添加修剪记录的对话框。用户可以选择修剪类型、输入位置、枝条数、日期和备注。提交后,新记录会被添加到pruningManager中,并保存到数据库。

修剪统计功能

classPruningStatistics{constructor(pruningManager){this.pruningManager=pruningManager;}getTotalPruningCount(plantId){returnthis.pruningManager.records.filter(r=>r.plantId===plantId).length;}getTotalBranchesRemoved(plantId){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);returnrecords.reduce((sum,r)=>sum+r.branchesRemoved,0);}getMostCommonPruningType(plantId){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);consttypeCounts={};records.forEach(record=>{typeCounts[record.pruningType]=(typeCounts[record.pruningType]||0)+1;});constmostCommon=Object.keys(typeCounts).reduce((a,b)=>typeCounts[a]>typeCounts[b]?a:b);returnthis.pruningManager.pruningTypes.find(p=>p.id===mostCommon);}getPruningFrequency(plantId,days=30){constrecords=this.pruningManager.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);// 每周修剪次数}}

这个PruningStatistics类提供了修剪的统计功能。getTotalPruningCount返回修剪总次数,getTotalBranchesRemoved计算移除的总枝条数,getMostCommonPruningType返回最常用的修剪类型,getPruningFrequency计算修剪频率。这些统计信息可以帮助用户了解修剪规律。

修剪提醒功能

functioncheckPruningReminders(){plants.forEach(plant=>{constlastPruningDate=getLastPruningDate(plant.id);constpruningInterval=plant.pruningInterval||30;// 默认30天if(!lastPruningDate){sendPruningReminder(plant.id,plant.name,'从未修剪过');return;}constdaysSincePruning=Math.floor((newDate()-newDate(lastPruningDate))/(24*60*60*1000));if(daysSincePruning>=pruningInterval){sendPruningReminder(plant.id,plant.name,`${daysSincePruning}天未修剪`);}});}functionsendPruningReminder(plantId,plantName,message){cordova.exec(function(result){console.log("修剪提醒已发送");},function(error){console.error("提醒发送失败:",error);},"NotificationPlugin","sendReminder",[{title:`${plantName}需要修剪`,message:message,plantId:plantId,type:'pruning'}]);}functiongetLastPruningDate(plantId){constrecords=pruningManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));returnrecords.length>0?records[0].date:null;}setInterval(checkPruningReminders,60*60*1000);// 每小时检查一次

这段代码实现了修剪提醒功能。checkPruningReminders函数检查所有植物,如果某个植物超过设定的修剪间隔,就发送提醒。通过NotificationPlugin,我们可以向用户发送系统通知。

修剪效果追踪

classPruningEffectTracking{constructor(){this.effectRecords=newMap();// 修剪ID -> 效果数据}recordPruningEffect(pruningRecordId,newGrowthDays,growthRate){this.effectRecords.set(pruningRecordId,{recordedDate:newDate(),newGrowthDays:newGrowthDays,// 新生长出现的天数growthRate:growthRate// 生长速率});}getPruningEffect(pruningRecordId){returnthis.effectRecords.get(pruningRecordId);}}

这个PruningEffectTracking类用于追踪修剪的效果。通过记录修剪后新生长出现的时间和生长速率,用户可以评估修剪的效果。

总结

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

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

Cordova与OpenHarmony换盆记录管理

欢迎大家加入开源鸿蒙跨平台开发者社区&#xff0c;一起共建开源鸿蒙跨平台生态。 换盆管理系统概述 换盆是植物生长过程中的重要环节&#xff0c;它为植物提供更多的生长空间和新鲜的土壤。在Cordova框架与OpenHarmony系统的结合下&#xff0c;我们需要实现一个完整的换盆记录…

作者头像 李华
网站建设 2026/4/30 18:36:11

SVG 多边形

SVG 多边形 SVG(可缩放矢量图形)是一种基于可缩放矢量图形的图形和图像的文件格式,它可以用于网页设计、动画制作以及图表展示等领域。在SVG中,多边形是一个非常基础且强大的图形元素。本文将详细介绍SVG多边形的定义、属性、创建方法以及在实际应用中的使用技巧。 一、S…

作者头像 李华
网站建设 2026/5/1 6:29:24

10_C 语言进阶之避坑指南:浮点数与精度损失—— 不可思议的 “量化误差”

C 语言进阶之避坑指南:浮点数与精度损失—— 不可思议的 “量化误差” 浮点数是 C 语言中处理小数、科学计数法数值的核心类型,看似简单的float和double,却暗藏大量容易被忽视的陷阱 —— 从精度丢失导致的计算错误,到浮点数比较的逻辑漏洞,再到嵌入式环境下的浮点运算支…

作者头像 李华
网站建设 2026/4/26 4:59:13

通信系统仿真:通信系统基础理论_(2).信号与系统

信号与系统 信号的分类 在通信系统中,信号是信息传递的基本载体。根据不同的特性,信号可以分为多种类型。了解信号的分类是进行通信系统仿真和分析的基础。 连续时间信号与离散时间信号 连续时间信号是指在时间上连续变化的信号,可以用数学函数 x(t)x(t)x(t) 表示,其中…

作者头像 李华
网站建设 2026/5/1 6:29:06

网易Java面试被问:偏向锁在什么场景下反而降低性能?如何关闭?

偏向锁性能问题详解 一、偏向锁的工作原理与性能隐患 1. 偏向锁设计初衷 java // 偏向锁的核心思想&#xff1a;大多数情况下锁不存在竞争 public class BiasedLockDesign {/*假设场景&#xff1a;单线程重复获取同一把锁无锁 -> 偏向锁 -> 轻量级锁 -> 重量级锁偏…

作者头像 李华
网站建设 2026/5/1 6:28:17

【ros2】ROS2中激光雷达/IMU/深度相机配置与RViz可视化全指南

文章目录 激光雷达/IMU/深度相机配置与RViz可视化全指南 一、文档概述 二、前置准备 2.1 环境与依赖 2.2 功能包与目录结构 三、传感器配置步骤(URDF/Xacro) 3.1 激光雷达(LiDAR)配置 3.1.1 编写Xacro配置文件 3.1.2 关键参数说明 3.2 惯性测量单元(IMU)配置 3.2.1 在Xac…

作者头像 李华