# application/inverter/routes.py from flask import Blueprint, request, jsonify, g, current_app from werkzeug.security import generate_password_hash from .. import db from ..auth.decorators import token_required from application.inverter.utils import Utilities inverter_bp = Blueprint('inverter', __name__) utils = Utilities() @inverter_bp.route('/', methods=['GET']) @token_required def inverter_list(): try: result = utils.get_inverter_list() if result: return jsonify({ 'success': True, 'data': result }) else: return jsonify(success=False, message="Failed to register inverter"), 500 except Exception as e: # log the error here return jsonify(success=False, message=str(e)), 500 @inverter_bp.route('/register', methods=['POST']) @token_required def register_inverter(): try: data = request.get_json() user_id = g.current_user['id'] username = data.get('username') password = data.get('password') inverter_id = data.get('inverter_id') if not all([username, password, inverter_id]): return jsonify(success=False, message="Missing required fields"), 400 check_inverter = utils.check_inverter_exists(user_id) if not check_inverter: return jsonify(success=False, error="Inverter already exists", code="INVERTER_EXISTS"), 400 result = utils.save_inverter_details(username, password, inverter_id, user_id) if result: return jsonify(success=True, message="Inverter registered successfully"), 201 else: return jsonify(success=False, message="Failed to register inverter"), 500 except Exception as e: # log the error here return jsonify(success=False, message=str(e)), 500 @inverter_bp.route('/update', methods=['POST']) @token_required def update_inverter(): try: data = request.get_json() user_id = g.current_user['id'] username = data.get('username') password = data.get('password') inverter_id = data.get('inverter_id') if not all([username, password, inverter_id]): return jsonify(success=False, message="Missing required fields"), 400 result = utils.update_credentials(username, password, inverter_id, user_id) if result: return jsonify(success=True, message="Inverter credentials updated"), 200 else: return jsonify(success=False, message="Update failed or no rows affected"), 400 except Exception as e: # log the error here return jsonify(success=False, message=str(e)), 500