113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Flyff HP Current Node (Final Combined Version)
|
|
- Polls the API at http://127.0.0.1:5000/data
|
|
- Outputs only the "hp_current" value as a string
|
|
- Uses color (126, 36, 57) for its output port
|
|
- Displays "hp_current" in a text field labeled "Value"
|
|
- Avoids "list indices must be integers" by retrieving the port with self.outputs().get('value')
|
|
"""
|
|
|
|
import time
|
|
import requests
|
|
import traceback
|
|
from OdenGraphQt import BaseNode
|
|
|
|
class FlyffHPCurrentNode(BaseNode):
|
|
__identifier__ = 'bunny-lab.io.flyff_hp_current_node'
|
|
NODE_NAME = 'Flyff - HP Current'
|
|
|
|
def __init__(self):
|
|
super(FlyffHPCurrentNode, self).__init__()
|
|
|
|
# 1) Add a text input property named "value" for UI display
|
|
self.add_text_input('value', 'Value', text='N/A')
|
|
|
|
# 2) Add an output port also named "value"
|
|
self.add_output('value', color=(126, 36, 57))
|
|
|
|
# Start in "disconnected" state
|
|
self._api_down = True
|
|
self._last_api_attempt = 0.0
|
|
self._retry_interval = 5.0
|
|
self._last_error_printed = 0.0
|
|
|
|
# Default node title
|
|
self.set_name("Flyff - HP Current (API Disconnected)")
|
|
|
|
def process_input(self):
|
|
"""
|
|
Called periodically by the global timer in borealis.py
|
|
"""
|
|
current_time = time.time()
|
|
if self._api_down and (current_time - self._last_api_attempt < self._retry_interval):
|
|
return
|
|
|
|
self._last_api_attempt = current_time
|
|
|
|
try:
|
|
response = requests.get("http://127.0.0.1:5000/data", timeout=1)
|
|
status_code = response.status_code
|
|
print(f"[DEBUG] FlyffHPCurrentNode: HTTP Status Code = {status_code}")
|
|
|
|
if status_code == 200:
|
|
# Attempt to parse JSON
|
|
try:
|
|
data = response.json() or {}
|
|
except ValueError:
|
|
data = {}
|
|
|
|
# If data is a list, ignore or convert to {}
|
|
if isinstance(data, list):
|
|
data = {}
|
|
|
|
# Mark node as connected
|
|
self._api_down = False
|
|
self.set_name("Flyff - HP Current (API Connected)")
|
|
|
|
# Retrieve hp_current (default "N/A" if missing)
|
|
new_value = data.get("hp_current", "N/A")
|
|
print(f"[DEBUG] FlyffHPCurrentNode: hp_current = {new_value}")
|
|
|
|
# Convert to string
|
|
new_value_str = str(new_value)
|
|
|
|
# 3) Update the text input property so the user sees it
|
|
self.set_property('value', new_value_str)
|
|
|
|
# 4) Transmit to downstream nodes
|
|
self.transmit_data(new_value_str)
|
|
|
|
else:
|
|
# Non-200 => disconnected
|
|
self._handle_api_error(f"HTTP {status_code} from FlyffHPCurrentNode")
|
|
self._api_down = True
|
|
|
|
except Exception as e:
|
|
tb = traceback.format_exc()
|
|
self._handle_api_error(f"Exception in FlyffHPCurrentNode: {e}\nTraceback:\n{tb}")
|
|
self._api_down = True
|
|
|
|
def transmit_data(self, data):
|
|
"""
|
|
Sends 'data' to any connected node via the "value" port.
|
|
(Uses self.outputs().get('value') instead of self.output('value'))
|
|
"""
|
|
output_port = self.outputs().get('value')
|
|
if output_port and output_port.connected_ports():
|
|
for connected_port in output_port.connected_ports():
|
|
connected_node = connected_port.node()
|
|
if hasattr(connected_node, 'receive_data'):
|
|
try:
|
|
connected_node.receive_data(data, source_port_name='value')
|
|
except Exception as e:
|
|
print(f"[ERROR] Error transmitting data to {connected_node}: {e}")
|
|
|
|
def _handle_api_error(self, msg):
|
|
current_time = time.time()
|
|
if (current_time - self._last_error_printed) >= self._retry_interval:
|
|
print(f"[ERROR] {msg}")
|
|
self._last_error_printed = current_time
|
|
|
|
self.set_name("Flyff - HP Current (API Disconnected)")
|