feat: mpoint增加存储表达式用于决定是否存储

This commit is contained in:
caoqianming 2025-03-18 14:15:22 +08:00
parent 70c415aa1f
commit 2238ab119d
3 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2025-03-18 06:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('enm', '0057_auto_20250211_1422'),
]
operations = [
migrations.AddField(
model_name='mpoint',
name='save_expr',
field=models.TextField(blank=True, null=True, verbose_name='存储表达式'),
),
]

View File

@ -30,6 +30,7 @@ class Mpoint(CommonBModel):
code = models.CharField("测点编号", max_length=50, unique=True)
unit = models.CharField("单位", max_length=50, null=True, blank=True)
cate = models.CharField("分类", max_length=50, null=True, blank=True)
save_expr = models.TextField("存储表达式", null=True, blank=True)
material = models.ForeignKey(Material, verbose_name="计量某种物料", on_delete=models.CASCADE, null=True, blank=True)
ep_belong = models.ForeignKey("em.equipment", verbose_name="所属设备", related_name="mp_ep_belong", on_delete=models.SET_NULL, null=True, blank=True)
mgroup = models.ForeignKey("mtm.mgroup", verbose_name="所在集合", on_delete=models.SET_NULL, null=True, blank=True)

View File

@ -50,6 +50,28 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
rval = 0
return rval
def get_can_save_from_save_expr(expr_str: str, self_val) -> bool:
"""判断是否可以保存
"""
pattern = r"\{(.*?)}"
matches = re.findall(pattern, expr_str)
for match in matches:
if match == "self":
expr_str = expr_str.replace("{self}", self_val)
else:
mpoint_data = MpointCache(match).data
current_val = mpoint_data.get("last_data", {}).get("last_val", None)
if current_val is None:
return True
else:
expr_str = expr_str.replace(f"{{{match}}}", current_val)
try:
rval = eval(expr_str)
except Exception as e:
myLogger.error(f"存储表达式计算错误: {e}, {expr_str}")
rval = False
return rval
def transfer_mpoint_val_to_ep_running_state(current_val, base_val: float, expr_str: str):
"""
将测点值转换所监测设备的运行状态值
@ -365,6 +387,11 @@ def insert_mplogx_item(code: str, val, timex: datetime, enp_mpoints_dict):
can_save = True
else:
can_save = True
if mpoint_data.get("save_expr", None):
can_save = get_can_save_from_save_expr(mpoint_data["save_expr"], val)
if can_save:
try:
MpointCache(code).set(timex, val)