Borealis-Legacy/data_collector.py

72 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""
Collector Process:
- Runs the OCR engine.
- Updates OCR data every 0.5 seconds.
- Exposes the latest data via an HTTP API using Flask.
This version splits the HP, MP, and FP values into 'current' and 'total' before
sending them via the API, so the Character Status Node can ingest them directly.
"""
import time
import threading
from flask import Flask, jsonify
app = Flask(__name__)
# Global variable to hold the latest stats (HP, MP, FP, EXP)
latest_data = {
"hp_current": 0,
"hp_total": 0,
"mp_current": 0,
"mp_total": 0,
"fp_current": 0,
"fp_total": 0,
"exp": 0.0000
}
def ocr_collector():
"""
This function simulates the OCR process.
Replace the code below with your actual OCR logic.
"""
global latest_data
counter = 0
while True:
# Simulate updating stats:
hp_current = 50 + counter % 10
hp_total = 100
mp_current = 30 + counter % 5
mp_total = 50
fp_current = 20 # fixed, for example
fp_total = 20
exp_val = round(10.0 + (counter * 0.1), 4)
latest_data = {
"hp_current": hp_current,
"hp_total": hp_total,
"mp_current": mp_current,
"mp_total": mp_total,
"fp_current": fp_current,
"fp_total": fp_total,
"exp": exp_val
}
counter += 1
time.sleep(0.5)
@app.route('/data')
def get_data():
"""Return the latest OCR data as JSON."""
return jsonify(latest_data)
if __name__ == '__main__':
# Start the OCR collector in a background thread.
collector_thread = threading.Thread(target=ocr_collector)
collector_thread.daemon = True
collector_thread.start()
# Run the Flask app on localhost:5000.
app.run(host="127.0.0.1", port=5000)