refactor(project): 重构项目结构并清理数据文件
- 将业务对象抽取到model包下 - 数据库实体移到model包下 - 删除wits_sample.txt示例数据文件 - 更新requirements.md文档结构
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
255
app/mqtt_mock.py
Normal file
255
app/mqtt_mock.py
Normal file
@@ -0,0 +1,255 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from config.config import build_mock_dependencies
|
||||
|
||||
|
||||
DATA_KEYS = [
|
||||
"ts",
|
||||
"wellid",
|
||||
"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",
|
||||
]
|
||||
|
||||
|
||||
def parse_broker(broker):
|
||||
if not broker:
|
||||
raise ValueError("broker is required")
|
||||
if "://" not in broker:
|
||||
broker = "tcp://" + broker
|
||||
parsed = urlparse(broker)
|
||||
host = parsed.hostname or "localhost"
|
||||
port = parsed.port or 1883
|
||||
scheme = (parsed.scheme or "tcp").lower()
|
||||
return scheme, host, port
|
||||
|
||||
|
||||
def ensure_dir(path):
|
||||
if not path:
|
||||
return
|
||||
dir_path = os.path.dirname(path)
|
||||
if dir_path:
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
|
||||
|
||||
def build_server_payload(equipment_code):
|
||||
data = {key: 0 for key in DATA_KEYS}
|
||||
data["ts"] = int(time.time() * 1000)
|
||||
data["wellid"] = ""
|
||||
return {
|
||||
"meta": {
|
||||
"equipment_code": equipment_code,
|
||||
"equipment_sn": equipment_code,
|
||||
},
|
||||
"data": data,
|
||||
}
|
||||
|
||||
|
||||
def parse_payload(raw_payload):
|
||||
try:
|
||||
return json.loads(raw_payload)
|
||||
except Exception:
|
||||
return raw_payload
|
||||
|
||||
|
||||
def build_ack_payload(topic, raw_payload):
|
||||
payload = {
|
||||
"ack": True,
|
||||
"topic": topic,
|
||||
"ts": datetime.utcnow().isoformat(timespec="seconds") + "Z",
|
||||
}
|
||||
try:
|
||||
obj = json.loads(raw_payload)
|
||||
if isinstance(obj, dict):
|
||||
meta = obj.get("meta") if isinstance(obj.get("meta"), dict) else {}
|
||||
if "equipment_code" in meta:
|
||||
payload["equipment_code"] = meta["equipment_code"]
|
||||
if "equipment_sn" in meta:
|
||||
payload["equipment_sn"] = meta["equipment_sn"]
|
||||
if "id" in obj:
|
||||
payload["id"] = obj["id"]
|
||||
payload["src"] = obj
|
||||
except Exception:
|
||||
payload["raw"] = raw_payload
|
||||
return payload
|
||||
|
||||
|
||||
def write_message(path, topic, raw_payload):
|
||||
ensure_dir(path)
|
||||
record = {
|
||||
"ts": datetime.utcnow().isoformat(timespec="seconds") + "Z",
|
||||
"topic": topic,
|
||||
"payload": parse_payload(raw_payload),
|
||||
}
|
||||
payload_obj = record["payload"]
|
||||
if isinstance(payload_obj, dict):
|
||||
data = payload_obj.get("data")
|
||||
if isinstance(data, dict) and isinstance(data.get("ts"), (int, float)):
|
||||
record["ts_iso"] = datetime.utcfromtimestamp(data["ts"] / 1000.0).isoformat(timespec="seconds") + "Z"
|
||||
with open(path, "a", encoding="utf-8") as f:
|
||||
f.write(json.dumps(record, ensure_ascii=True))
|
||||
f.write("\n")
|
||||
|
||||
|
||||
def run_mock_service(args, deps):
|
||||
mqtt_config = deps.config.mqtt
|
||||
tms_config = deps.config.tms
|
||||
tdengine_config = deps.tdengine_config
|
||||
tdengine_writer = deps.tdengine_writer
|
||||
scheme, host, port = parse_broker(mqtt_config.broker)
|
||||
|
||||
print("MQTT mock config:")
|
||||
print(f" broker: {scheme}://{host}:{port}")
|
||||
print(f" client-id: {mqtt_config.mock_client_id}")
|
||||
print(f" pub-topic (ingest): {mqtt_config.pub_topic}")
|
||||
print(f" sub-topic (forward): {mqtt_config.sub_topic}")
|
||||
print(f" ack-topic: {mqtt_config.ack_topic}")
|
||||
print(f" data-file: {deps.data_file}")
|
||||
print(f" device-code: {tms_config.device_code}")
|
||||
print(f" tdengine enabled: {tdengine_writer.enabled}")
|
||||
if tdengine_writer.enabled:
|
||||
print(f" tdengine host: {tdengine_config.base_url}")
|
||||
print(f" tdengine database: {tdengine_config.database}")
|
||||
print(f" tdengine stable: {tdengine_config.stable}")
|
||||
print(f" tdengine pool-size: {tdengine_config.pool_size}")
|
||||
print(f" mode: {args.mode}")
|
||||
|
||||
client = mqtt.Client(client_id=mqtt_config.mock_client_id, clean_session=True)
|
||||
if mqtt_config.username is not None:
|
||||
client.username_pw_set(mqtt_config.username, mqtt_config.password)
|
||||
|
||||
if scheme in ("ssl", "tls", "mqtts"):
|
||||
client.tls_set()
|
||||
|
||||
def on_connect(c, userdata, flags, rc):
|
||||
if rc == 0:
|
||||
print("Connected")
|
||||
else:
|
||||
print(f"Connect failed rc={rc}")
|
||||
return
|
||||
if args.mode in ("listen", "both") and mqtt_config.pub_topic:
|
||||
c.subscribe(mqtt_config.pub_topic)
|
||||
print(f"Subscribed: {mqtt_config.pub_topic}")
|
||||
|
||||
def on_disconnect(c, userdata, rc):
|
||||
print(f"Disconnected callback rc={rc}")
|
||||
|
||||
def on_message(c, userdata, msg):
|
||||
payload = msg.payload.decode("utf-8", errors="replace")
|
||||
print(f"RX {msg.topic}: {payload}")
|
||||
if msg.topic != mqtt_config.pub_topic:
|
||||
return
|
||||
|
||||
if deps.data_file:
|
||||
write_message(deps.data_file, msg.topic, payload)
|
||||
print(f"Wrote to file: {deps.data_file}")
|
||||
|
||||
if tdengine_writer.enabled:
|
||||
try:
|
||||
tdengine_writer.write_payload(parse_payload(payload))
|
||||
print("Wrote to TDengine")
|
||||
except Exception as exc:
|
||||
print(f"Write TDengine failed: {exc}")
|
||||
|
||||
if mqtt_config.sub_topic:
|
||||
c.publish(mqtt_config.sub_topic, payload)
|
||||
print(f"Forwarded to {mqtt_config.sub_topic}")
|
||||
|
||||
if mqtt_config.ack_topic:
|
||||
ack_payload = build_ack_payload(msg.topic, payload)
|
||||
c.publish(mqtt_config.ack_topic, json.dumps(ack_payload, ensure_ascii=True))
|
||||
print(f"TX {mqtt_config.ack_topic}: {ack_payload}")
|
||||
|
||||
client.on_connect = on_connect
|
||||
client.on_disconnect = on_disconnect
|
||||
client.on_message = on_message
|
||||
client.connect(host, port, keepalive=tms_config.keepalive)
|
||||
client.loop_start()
|
||||
|
||||
try:
|
||||
if args.mode in ("publish", "both"):
|
||||
if not mqtt_config.pub_topic:
|
||||
print("pub-topic is empty; nothing to publish")
|
||||
else:
|
||||
seq = 0
|
||||
while True:
|
||||
seq += 1
|
||||
payload = build_server_payload(tms_config.device_code)
|
||||
client.publish(mqtt_config.pub_topic, json.dumps(payload, ensure_ascii=True))
|
||||
print(f"TX {mqtt_config.pub_topic}: {payload}")
|
||||
if args.count and seq >= args.count:
|
||||
break
|
||||
time.sleep(args.interval)
|
||||
else:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
client.loop_stop()
|
||||
client.disconnect()
|
||||
print("Disconnected")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="MQTT mock service")
|
||||
ap.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
ap.add_argument("--mode", choices=["publish", "listen", "both"], default="listen")
|
||||
ap.add_argument("--interval", type=float, default=2.0, help="Publish interval (seconds)")
|
||||
ap.add_argument("--count", type=int, default=0, help="Publish count (0 = forever)")
|
||||
ap.add_argument("--data-file", default="", help="Override data-file in config")
|
||||
args = ap.parse_args()
|
||||
deps = build_mock_dependencies(args.config, data_file_override=args.data_file)
|
||||
run_mock_service(args, deps)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
193
app/mqtt_sender.py
Normal file
193
app/mqtt_sender.py
Normal file
@@ -0,0 +1,193 @@
|
||||
import argparse
|
||||
import json
|
||||
import random
|
||||
import time
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from config.config import build_sender_dependencies
|
||||
|
||||
|
||||
DATA_KEYS = [
|
||||
"ts",
|
||||
"wellid",
|
||||
"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",
|
||||
]
|
||||
|
||||
|
||||
def parse_broker(broker):
|
||||
if not broker:
|
||||
raise ValueError("broker is required")
|
||||
if "://" not in broker:
|
||||
broker = "tcp://" + broker
|
||||
parsed = urlparse(broker)
|
||||
host = parsed.hostname or "localhost"
|
||||
port = parsed.port or 1883
|
||||
scheme = (parsed.scheme or "tcp").lower()
|
||||
return scheme, host, port
|
||||
|
||||
|
||||
def rand_int(a, b):
|
||||
return random.randint(a, b)
|
||||
|
||||
|
||||
def build_random_payload(equipment_code):
|
||||
data = {key: 0 for key in DATA_KEYS}
|
||||
data["ts"] = int(time.time() * 1000)
|
||||
data["wellid"] = random.choice(["", f"WELL-{rand_int(1, 9999):04d}"])
|
||||
data["stknum"] = rand_int(0, 500)
|
||||
data["recid"] = rand_int(0, 100000)
|
||||
data["seqid"] = rand_int(0, 100000)
|
||||
data["actual_date"] = int(datetime.utcnow().strftime("%Y%m%d"))
|
||||
data["actual_time"] = int(datetime.utcnow().strftime("%H%M%S"))
|
||||
data["actcod"] = rand_int(0, 9)
|
||||
data["deptbitm"] = rand_int(0, 5000)
|
||||
data["deptbitv"] = rand_int(0, 5000)
|
||||
data["deptmeas"] = rand_int(0, 5000)
|
||||
data["deptvert"] = rand_int(0, 5000)
|
||||
data["blkpos"] = rand_int(0, 100)
|
||||
data["ropa"] = rand_int(0, 200)
|
||||
data["hkla"] = rand_int(0, 500)
|
||||
data["hklx"] = rand_int(0, 500)
|
||||
data["woba"] = rand_int(0, 200)
|
||||
data["wobx"] = rand_int(0, 200)
|
||||
data["torqa"] = rand_int(0, 200)
|
||||
data["torqx"] = rand_int(0, 200)
|
||||
data["rpma"] = rand_int(0, 300)
|
||||
data["sppa"] = rand_int(0, 5000)
|
||||
data["chkp"] = rand_int(0, 5000)
|
||||
data["spm1"] = rand_int(0, 200)
|
||||
data["spm2"] = rand_int(0, 200)
|
||||
data["spm3"] = rand_int(0, 200)
|
||||
data["tvolact"] = rand_int(0, 20000)
|
||||
data["tvolcact"] = rand_int(0, 20000)
|
||||
data["mfop"] = rand_int(0, 1000)
|
||||
data["mfoa"] = rand_int(0, 1000)
|
||||
data["mfia"] = rand_int(0, 1000)
|
||||
data["mdoa"] = rand_int(0, 1000)
|
||||
data["mdia"] = rand_int(0, 1000)
|
||||
data["mtoa"] = rand_int(0, 1000)
|
||||
data["mtia"] = rand_int(0, 1000)
|
||||
data["mcoa"] = rand_int(0, 1000)
|
||||
data["mcia"] = rand_int(0, 1000)
|
||||
data["stkc"] = rand_int(0, 200)
|
||||
data["lagstks"] = rand_int(0, 200)
|
||||
data["deptretm"] = rand_int(0, 5000)
|
||||
data["gasa"] = rand_int(0, 100)
|
||||
data["space1"] = rand_int(0, 10)
|
||||
data["space2"] = rand_int(0, 10)
|
||||
data["space3"] = rand_int(0, 10)
|
||||
data["space4"] = rand_int(0, 10)
|
||||
data["space5"] = rand_int(0, 10)
|
||||
return {
|
||||
"meta": {
|
||||
"equipment_code": equipment_code,
|
||||
"equipment_sn": equipment_code,
|
||||
},
|
||||
"data": data,
|
||||
}
|
||||
|
||||
|
||||
def run_sender(args, deps):
|
||||
mqtt_config = deps.config.mqtt
|
||||
tms_config = deps.config.tms
|
||||
scheme, host, port = parse_broker(mqtt_config.broker)
|
||||
|
||||
print("MQTT sender config:")
|
||||
print(f" broker: {scheme}://{host}:{port}")
|
||||
print(f" client-id: {mqtt_config.sender_client_id}")
|
||||
print(f" pub-topic: {mqtt_config.pub_topic}")
|
||||
print(f" interval: {args.interval}s")
|
||||
|
||||
client = mqtt.Client(client_id=mqtt_config.sender_client_id, clean_session=True)
|
||||
if mqtt_config.username is not None:
|
||||
client.username_pw_set(mqtt_config.username, mqtt_config.password)
|
||||
|
||||
if scheme in ("ssl", "tls", "mqtts"):
|
||||
client.tls_set()
|
||||
|
||||
def on_disconnect(c, userdata, rc):
|
||||
print(f"Disconnected callback rc={rc}")
|
||||
|
||||
client.on_disconnect = on_disconnect
|
||||
client.connect(host, port, keepalive=tms_config.keepalive)
|
||||
client.loop_start()
|
||||
|
||||
try:
|
||||
if not mqtt_config.pub_topic:
|
||||
print("pub-topic is empty; nothing to publish")
|
||||
return
|
||||
seq = 0
|
||||
while True:
|
||||
seq += 1
|
||||
payload = build_random_payload(tms_config.device_code)
|
||||
client.publish(mqtt_config.pub_topic, json.dumps(payload, ensure_ascii=True))
|
||||
print(f"TX {mqtt_config.pub_topic}: {payload}")
|
||||
if args.count and seq >= args.count:
|
||||
break
|
||||
time.sleep(args.interval)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
client.loop_stop()
|
||||
client.disconnect()
|
||||
print("Disconnected")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="MQTT random data sender")
|
||||
ap.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
ap.add_argument("--interval", type=float, default=3.0, help="Publish interval (seconds)")
|
||||
ap.add_argument("--count", type=int, default=0, help="Publish count (0 = forever)")
|
||||
args = ap.parse_args()
|
||||
deps = build_sender_dependencies(args.config)
|
||||
run_sender(args, deps)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
112
app/mqtt_subscriber.py
Normal file
112
app/mqtt_subscriber.py
Normal file
@@ -0,0 +1,112 @@
|
||||
import argparse
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from config.config import build_subscriber_dependencies
|
||||
|
||||
|
||||
def parse_broker(broker):
|
||||
if not broker:
|
||||
raise ValueError("broker is required")
|
||||
if "://" not in broker:
|
||||
broker = "tcp://" + broker
|
||||
parsed = urlparse(broker)
|
||||
host = parsed.hostname or "localhost"
|
||||
port = parsed.port or 1883
|
||||
scheme = (parsed.scheme or "tcp").lower()
|
||||
return scheme, host, port
|
||||
|
||||
|
||||
def print_flat_fields(title, value):
|
||||
print(title)
|
||||
if isinstance(value, dict):
|
||||
for k, v in value.items():
|
||||
print(f" {k}: {v}")
|
||||
else:
|
||||
print(f" {value}")
|
||||
|
||||
|
||||
def run_subscriber(args, deps):
|
||||
mqtt_config = deps.config.mqtt
|
||||
tms_config = deps.config.tms
|
||||
topic = args.topic or mqtt_config.sub_topic or mqtt_config.pub_topic
|
||||
if not topic:
|
||||
raise ValueError("No topic to subscribe. Set sub-topic or pub-topic, or pass --topic")
|
||||
|
||||
scheme, host, port = parse_broker(mqtt_config.broker)
|
||||
|
||||
print("MQTT subscriber config:")
|
||||
print(f" broker: {scheme}://{host}:{port}")
|
||||
print(f" client-id: {mqtt_config.subscriber_client_id}")
|
||||
print(f" topic: {topic}")
|
||||
|
||||
client = mqtt.Client(client_id=mqtt_config.subscriber_client_id, clean_session=True)
|
||||
if mqtt_config.username is not None:
|
||||
client.username_pw_set(mqtt_config.username, mqtt_config.password)
|
||||
|
||||
if scheme in ("ssl", "tls", "mqtts"):
|
||||
client.tls_set()
|
||||
|
||||
def on_connect(c, userdata, flags, rc):
|
||||
if rc == 0:
|
||||
print("Connected")
|
||||
c.subscribe(topic)
|
||||
print(f"Subscribed: {topic}")
|
||||
else:
|
||||
print(f"Connect failed rc={rc}")
|
||||
|
||||
def on_disconnect(c, userdata, rc):
|
||||
print(f"Disconnected callback rc={rc}")
|
||||
|
||||
def on_message(c, userdata, msg):
|
||||
raw_payload = msg.payload.decode("utf-8", errors="replace")
|
||||
print("=" * 80)
|
||||
print(f"RX Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print(f"Topic: {msg.topic}")
|
||||
print(f"QoS: {msg.qos}, Retain: {msg.retain}, Bytes: {len(msg.payload)}")
|
||||
try:
|
||||
obj = json.loads(raw_payload)
|
||||
print("Raw JSON:")
|
||||
print(json.dumps(obj, ensure_ascii=False, indent=2))
|
||||
if isinstance(obj, dict):
|
||||
print_flat_fields("meta:", obj.get("meta"))
|
||||
print_flat_fields("data:", obj.get("data"))
|
||||
extra_keys = [key for key in obj.keys() if key not in ("meta", "data")]
|
||||
for key in extra_keys:
|
||||
print_flat_fields(f"{key}:", obj.get(key))
|
||||
except Exception:
|
||||
print("Raw payload:")
|
||||
print(raw_payload)
|
||||
|
||||
client.on_connect = on_connect
|
||||
client.on_disconnect = on_disconnect
|
||||
client.on_message = on_message
|
||||
client.connect(host, port, keepalive=tms_config.keepalive)
|
||||
client.loop_start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
client.loop_stop()
|
||||
client.disconnect()
|
||||
print("Disconnected")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="MQTT subscriber")
|
||||
ap.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
ap.add_argument("--topic", default="", help="Override topic to subscribe")
|
||||
args = ap.parse_args()
|
||||
deps = build_subscriber_dependencies(args.config)
|
||||
run_subscriber(args, deps)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
268
app/wits_sender.py
Normal file
268
app/wits_sender.py
Normal file
@@ -0,0 +1,268 @@
|
||||
import argparse
|
||||
import random
|
||||
import socket
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from config.dependencies import build_wits_sender_dependencies
|
||||
|
||||
|
||||
BEGIN_MARK = "&&\r\n"
|
||||
END_MARK = "!!\r\n"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WitsData:
|
||||
ts: int
|
||||
wellid: str
|
||||
stknum: int
|
||||
recid: int
|
||||
seqid: int
|
||||
actual_date: float
|
||||
actual_time: float
|
||||
actual_ts: int
|
||||
actcod: int
|
||||
actod_label: str
|
||||
deptbitm: float
|
||||
deptbitv: float
|
||||
deptmeas: float
|
||||
deptvert: float
|
||||
blkpos: float
|
||||
ropa: float
|
||||
hkla: float
|
||||
hklx: float
|
||||
woba: float
|
||||
wobx: float
|
||||
torqa: float
|
||||
torqx: float
|
||||
rpma: int
|
||||
sppa: float
|
||||
chkp: float
|
||||
spm1: int
|
||||
spm2: int
|
||||
spm3: int
|
||||
tvolact: float
|
||||
tvolcact: float
|
||||
mfop: int
|
||||
mfoa: float
|
||||
mfia: float
|
||||
mdoa: float
|
||||
mdia: float
|
||||
mtoa: float
|
||||
mtia: float
|
||||
mcoa: float
|
||||
mcia: float
|
||||
stkc: int
|
||||
lagstks: int
|
||||
deptretm: float
|
||||
gasa: float
|
||||
space1: float
|
||||
space2: float
|
||||
space3: float
|
||||
space4: float
|
||||
space5: float
|
||||
|
||||
|
||||
WITS_FIELD_MAPPING = [
|
||||
(1, "wellid", "string"),
|
||||
(2, "stknum", "int"),
|
||||
(3, "recid", "int"),
|
||||
(4, "seqid", "int"),
|
||||
(5, "actual_date", "float"),
|
||||
(6, "actual_time", "float"),
|
||||
(7, "actcod", "int"),
|
||||
(8, "deptbitm", "float"),
|
||||
(9, "deptbitv", "float"),
|
||||
(10, "deptmeas", "float"),
|
||||
(11, "deptvert", "float"),
|
||||
(12, "blkpos", "float"),
|
||||
(13, "ropa", "float"),
|
||||
(14, "hkla", "float"),
|
||||
(15, "hklx", "float"),
|
||||
(16, "woba", "float"),
|
||||
(17, "wobx", "float"),
|
||||
(18, "torqa", "float"),
|
||||
(19, "torqx", "float"),
|
||||
(20, "rpma", "int"),
|
||||
(21, "sppa", "float"),
|
||||
(22, "chkp", "float"),
|
||||
(23, "spm1", "int"),
|
||||
(24, "spm2", "int"),
|
||||
(25, "spm3", "int"),
|
||||
(26, "tvolact", "float"),
|
||||
(27, "tvolcact", "float"),
|
||||
(28, "mfop", "int"),
|
||||
(29, "mfoa", "float"),
|
||||
(30, "mfia", "float"),
|
||||
(31, "mdoa", "float"),
|
||||
(32, "mdia", "float"),
|
||||
(33, "mtoa", "float"),
|
||||
(34, "mtia", "float"),
|
||||
(35, "mcoa", "float"),
|
||||
(36, "mcia", "float"),
|
||||
(37, "stkc", "int"),
|
||||
(38, "lagstks", "int"),
|
||||
(39, "deptretm", "float"),
|
||||
(40, "gasa", "float"),
|
||||
(41, "space1", "float"),
|
||||
(42, "space2", "float"),
|
||||
(43, "space3", "float"),
|
||||
(44, "space4", "float"),
|
||||
(45, "space5", "float"),
|
||||
]
|
||||
|
||||
|
||||
def rand_int(a, b):
|
||||
return random.randint(a, b)
|
||||
|
||||
|
||||
def rand_float(a, b, digits=2):
|
||||
return round(random.uniform(a, b), digits)
|
||||
|
||||
|
||||
def build_random_wits_data(device_code):
|
||||
now = datetime.now()
|
||||
ts_ms = int(time.time() * 1000)
|
||||
return WitsData(
|
||||
ts=ts_ms,
|
||||
wellid=device_code,
|
||||
stknum=rand_int(0, 500),
|
||||
recid=0,
|
||||
seqid=rand_int(1, 999999),
|
||||
actual_date=float(now.strftime("%Y%m%d")),
|
||||
actual_time=float(now.strftime("%H%M%S")),
|
||||
actual_ts=ts_ms,
|
||||
actcod=rand_int(0, 9),
|
||||
actod_label="AUTO",
|
||||
deptbitm=rand_float(0, 5000),
|
||||
deptbitv=rand_float(0, 5000),
|
||||
deptmeas=rand_float(0, 5000),
|
||||
deptvert=rand_float(0, 5000),
|
||||
blkpos=rand_float(0, 100),
|
||||
ropa=rand_float(0, 200),
|
||||
hkla=rand_float(0, 500),
|
||||
hklx=rand_float(0, 500),
|
||||
woba=rand_float(0, 200),
|
||||
wobx=rand_float(0, 200),
|
||||
torqa=rand_float(0, 200),
|
||||
torqx=rand_float(0, 200),
|
||||
rpma=rand_int(0, 300),
|
||||
sppa=rand_float(0, 5000),
|
||||
chkp=rand_float(0, 5000),
|
||||
spm1=rand_int(0, 200),
|
||||
spm2=rand_int(0, 200),
|
||||
spm3=rand_int(0, 200),
|
||||
tvolact=rand_float(0, 20000),
|
||||
tvolcact=rand_float(0, 20000),
|
||||
mfop=rand_int(0, 1000),
|
||||
mfoa=rand_float(0, 1000),
|
||||
mfia=rand_float(0, 1000),
|
||||
mdoa=rand_float(0, 1000),
|
||||
mdia=rand_float(0, 1000),
|
||||
mtoa=rand_float(0, 1000),
|
||||
mtia=rand_float(0, 1000),
|
||||
mcoa=rand_float(0, 1000),
|
||||
mcia=rand_float(0, 1000),
|
||||
stkc=rand_int(0, 200),
|
||||
lagstks=rand_int(0, 200),
|
||||
deptretm=rand_float(0, 5000),
|
||||
gasa=rand_float(0, 100),
|
||||
space1=rand_float(0, 10),
|
||||
space2=rand_float(0, 10),
|
||||
space3=rand_float(0, 10),
|
||||
space4=rand_float(0, 10),
|
||||
space5=rand_float(0, 10),
|
||||
)
|
||||
|
||||
|
||||
def format_wits_value(value, kind):
|
||||
if kind == "string":
|
||||
return str(value)
|
||||
if kind == "int":
|
||||
return str(int(value))
|
||||
return f"{float(value):.2f}"
|
||||
|
||||
|
||||
def build_wits_packet(data):
|
||||
lines = []
|
||||
for index, field_name, kind in WITS_FIELD_MAPPING:
|
||||
value = getattr(data, field_name)
|
||||
lines.append(f"{index:02d}{format_wits_value(value, kind)}")
|
||||
return BEGIN_MARK + "\r\n".join(lines) + "\r\n" + END_MARK
|
||||
|
||||
|
||||
def normalize_packet(text):
|
||||
body = text.replace("\r\n", "\n").replace("\r", "\n")
|
||||
lines = [line.rstrip() for line in body.split("\n") if line.strip()]
|
||||
if lines and lines[0] == "&&":
|
||||
lines = lines[1:]
|
||||
if lines and lines[-1] == "!!":
|
||||
lines = lines[:-1]
|
||||
return BEGIN_MARK + "\r\n".join(lines) + "\r\n" + END_MARK
|
||||
|
||||
|
||||
def load_packet_from_file(path):
|
||||
return normalize_packet(Path(path).read_text(encoding="utf-8-sig"))
|
||||
|
||||
|
||||
def send_packet(host, port, timeout, packet):
|
||||
with socket.create_connection((host, port), timeout=timeout) as sock:
|
||||
sock.sendall(packet.encode("ascii", errors="strict"))
|
||||
|
||||
|
||||
def run_wits_sender(args, deps):
|
||||
wits_config = deps.config.wits
|
||||
device_code = deps.config.tms.device_code
|
||||
source_file = args.source_file or wits_config.source_file
|
||||
host = args.host or wits_config.host
|
||||
port = args.port or wits_config.port
|
||||
timeout = args.timeout or wits_config.timeout
|
||||
|
||||
if not host or not port:
|
||||
raise ValueError("WITS target host/port is empty. Configure wits.host/wits.port or tms.server-ip/tms.server-port")
|
||||
|
||||
print("WITS sender config:")
|
||||
print(f" host: {host}")
|
||||
print(f" port: {port}")
|
||||
print(f" timeout: {timeout}s")
|
||||
print(f" source-file: {source_file or '(generated)'}")
|
||||
print(f" interval: {args.interval}s")
|
||||
print(f" count: {args.count or 'forever'}")
|
||||
|
||||
seq = 0
|
||||
try:
|
||||
while True:
|
||||
seq += 1
|
||||
if source_file:
|
||||
packet = load_packet_from_file(source_file)
|
||||
else:
|
||||
packet = build_wits_packet(build_random_wits_data(device_code))
|
||||
send_packet(host, port, timeout, packet)
|
||||
print(f"TX WITS #{seq} -> {host}:{port}")
|
||||
print(packet)
|
||||
if args.count and seq >= args.count:
|
||||
break
|
||||
time.sleep(args.interval)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="WITS TCP sender")
|
||||
ap.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
ap.add_argument("--host", default="", help="Override target host")
|
||||
ap.add_argument("--port", type=int, default=0, help="Override target port")
|
||||
ap.add_argument("--timeout", type=int, default=0, help="Override socket timeout")
|
||||
ap.add_argument("--source-file", default="", help="Send raw WITS packet from file")
|
||||
ap.add_argument("--interval", type=float, default=3.0, help="Send interval in seconds")
|
||||
ap.add_argument("--count", type=int, default=1, help="Send count (0 = forever)")
|
||||
args = ap.parse_args()
|
||||
deps = build_wits_sender_dependencies(args.config)
|
||||
run_wits_sender(args, deps)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user