Borealis-Legacy/data_collector.py

63 lines
1.5 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.
"""
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": "0/0",
"mp": "0/0",
"fp": "0/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_max = 100
mp_current = 30 + counter % 5
mp_max = 50
fp_current = 20 # fixed, for example
fp_max = 20
exp_val = round(10.0 + (counter * 0.1), 4)
latest_data = {
"hp": f"{hp_current}/{hp_max}",
"mp": f"{mp_current}/{mp_max}",
"fp": f"{fp_current}/{fp_max}",
"exp": f"{exp_val:.4f}"
}
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)