- 将主应用改为同时启动MQTT订阅入库和WITS数据发送两个服务 - 实现WITS发送器的自动重连机制和连接状态管理 - 添加日志记录到log/app.log和错误日志到log/error.log - 更新WITS通道映射定义并支持字符串类型的日期时间字段 - 修改数据入库逻辑以支持空值处理和类型转换容错 - 移除命令行子命令模式,改为配置文件驱动的参数设置 - 添加.vscode和log目录到.gitignore忽略列表
122 lines
3.0 KiB
Python
122 lines
3.0 KiB
Python
import logging
|
|
import re
|
|
|
|
from model import DrillingRealtimeData
|
|
|
|
|
|
DB_COLUMNS = [
|
|
"ts",
|
|
"stknum",
|
|
"recid",
|
|
"seqid",
|
|
"actual_date",
|
|
"actual_time",
|
|
"actcod",
|
|
"deptbitm",
|
|
"deptbitv",
|
|
"deptmeas",
|
|
"deptvert",
|
|
"blkpos",
|
|
"ropa",
|
|
"hkla",
|
|
"hklx",
|
|
"woba",
|
|
"wobx",
|
|
"torqa",
|
|
"torqx",
|
|
"rpma",
|
|
"sppa",
|
|
"chkp",
|
|
"spm1",
|
|
"spm2",
|
|
"spm3",
|
|
"tvolact",
|
|
"tvolcact",
|
|
"mfop",
|
|
"mfoa",
|
|
"mfia",
|
|
"mdoa",
|
|
"mdia",
|
|
"mtoa",
|
|
"mtia",
|
|
"mcoa",
|
|
"mcia",
|
|
"stkc",
|
|
"lagstks",
|
|
"deptretm",
|
|
"gasa",
|
|
"space1",
|
|
"space2",
|
|
"space3",
|
|
"space4",
|
|
"space5",
|
|
]
|
|
|
|
INT_COLUMNS = {"stknum", "recid", "seqid", "actcod", "rpma", "spm1", "spm2", "spm3", "mfop", "stkc", "lagstks"}
|
|
|
|
|
|
def sanitize_identifier(value, fallback):
|
|
cleaned = re.sub(r"[^A-Za-z0-9_]", "_", str(value or ""))
|
|
if not cleaned:
|
|
cleaned = fallback
|
|
if cleaned[0].isdigit():
|
|
cleaned = f"t_{cleaned}"
|
|
return cleaned.lower()
|
|
|
|
|
|
def sql_quote(value):
|
|
return "'" + str(value).replace("'", "''") + "'"
|
|
|
|
|
|
class DrillingRealtimeORM:
|
|
def __init__(self, database, stable="drilling_realtime_st", default_device_code="GJ-304-0088"):
|
|
self.database = database
|
|
self.stable = stable
|
|
self.default_device_code = default_device_code or "GJ-304-0088"
|
|
|
|
def build_insert_sql(self, payload):
|
|
if not isinstance(payload, dict):
|
|
raise ValueError("payload is not JSON object")
|
|
entity = DrillingRealtimeData.from_payload(payload)
|
|
meta = payload.get("meta") if isinstance(payload.get("meta"), dict) else {}
|
|
equipment_code = (
|
|
str(self.default_device_code).strip()
|
|
or str(meta.get("equipment_code", "")).strip()
|
|
or str(meta.get("equipment_sn", "")).strip()
|
|
or entity.wellid
|
|
or "GJ-304-0088"
|
|
)
|
|
table_name = sanitize_identifier(f"drilling_realtime_{equipment_code}", "drilling_realtime_default")
|
|
values = []
|
|
for column in DB_COLUMNS:
|
|
raw = getattr(entity, column, None)
|
|
if raw is None:
|
|
logging.debug("Column %s is None, default=0", column)
|
|
if column in INT_COLUMNS or column == "ts":
|
|
values.append(str(to_int(raw)))
|
|
else:
|
|
values.append(str(to_float(raw)))
|
|
columns_sql = ", ".join([f"`{column}`" for column in DB_COLUMNS])
|
|
values_sql = ", ".join(values)
|
|
return (
|
|
f"INSERT INTO `{self.database}`.`{table_name}` USING `{self.database}`.`{self.stable}` "
|
|
f"TAGS ({sql_quote(equipment_code)}) ({columns_sql}) VALUES ({values_sql})"
|
|
)
|
|
|
|
|
|
def to_int(value, default=0):
|
|
try:
|
|
if value is None or value == "":
|
|
return default
|
|
return int(value)
|
|
except Exception:
|
|
return default
|
|
|
|
|
|
def to_float(value, default=0.0):
|
|
try:
|
|
if value is None or value == "":
|
|
return default
|
|
return float(value)
|
|
except Exception:
|
|
return default |