Spaces:
Sleeping
Sleeping
File size: 8,036 Bytes
c5c8950 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | import os
import json
import time
import random
import threading
from datetime import datetime, timedelta
from flask import Flask, render_template, jsonify, request, send_from_directory
from werkzeug.utils import secure_filename
from werkzeug.exceptions import RequestEntityTooLarge
app = Flask(__name__)
# --- Configuration ---
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB limit
app.config['UPLOAD_FOLDER'] = 'uploads'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# --- Core Memory / Storage ---
DATA_FILE = 'maintenance_data.json'
# --- Simulation Logic ---
class EquipmentSimulator:
def __init__(self, eq_id, name, type_, **kwargs):
self.id = eq_id
self.name = name
self.type = type_
self.status = kwargs.get('status', 'Running')
self.health_score = kwargs.get('health_score', 100.0)
self.run_hours = kwargs.get('run_hours', 0.0)
self.temperature = kwargs.get('temperature', 45.0)
self.vibration = kwargs.get('vibration', 0.5)
self.pressure = kwargs.get('pressure', 100.0)
self.rpm = kwargs.get('rpm', 3000)
self.last_update = time.time()
self.maintenance_history = kwargs.get('maintenance_history', [])
self.rul_prediction = kwargs.get('rul_prediction', 1000)
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'type': self.type,
'status': self.status,
'health_score': self.health_score,
'run_hours': self.run_hours,
'temperature': self.temperature,
'vibration': self.vibration,
'pressure': self.pressure,
'rpm': self.rpm,
'maintenance_history': self.maintenance_history,
'rul_prediction': self.rul_prediction
}
def update(self):
now = time.time()
delta = (now - self.last_update)
self.last_update = now
if self.status == 'Stopped':
self.temperature = max(25.0, self.temperature - 0.5)
self.vibration = 0.0
self.rpm = 0
return
# Simulate running accumulation
sim_hours = delta * 1.0
self.run_hours += sim_hours
# Degradation logic
decay_rate = 0.01
if self.temperature > 80: decay_rate *= 2
if self.vibration > 2.0: decay_rate *= 3
self.health_score = max(0, self.health_score - (decay_rate * sim_hours))
# Sensor Simulation with Random Walk
self.temperature += random.uniform(-0.5, 0.6) + (0.05 if self.health_score < 80 else 0)
self.vibration += random.uniform(-0.05, 0.06) + (0.01 if self.health_score < 60 else 0)
self.pressure += random.uniform(-1, 1)
self.rpm += random.uniform(-10, 10)
# Anomaly Injection (Random spikes)
if random.random() < 0.05:
self.vibration += 0.5
# Status Logic
if self.health_score < 30:
self.status = 'Critical'
elif self.health_score < 70:
self.status = 'Warning'
else:
self.status = 'Running'
# RUL Calculation
self.rul_prediction = self.health_score * 10
def perform_maintenance(self):
self.health_score = 100.0
self.status = 'Running'
self.temperature = 45.0
self.vibration = 0.5
self.maintenance_history.append({
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'type': 'Preventive',
'cost': 500
})
# --- Persistence Helpers ---
def load_data():
if os.path.exists(DATA_FILE):
try:
with open(DATA_FILE, 'r') as f:
data = json.load(f)
return {
k: EquipmentSimulator(k, v['name'], v['type'], **v)
for k, v in data.items()
}
except Exception as e:
print(f"Error loading data: {e}")
# Default data
return {
'EQ-001': EquipmentSimulator('EQ-001', 'CNC-Alpha', 'CNC Machine'),
'EQ-002': EquipmentSimulator('EQ-002', 'Pump-Beta', 'Hydraulic Pump'),
'EQ-003': EquipmentSimulator('EQ-003', 'Turbine-Gamma', 'Gas Turbine'),
'EQ-004': EquipmentSimulator('EQ-004', 'Conveyor-Delta', 'Belt System'),
'EQ-005': EquipmentSimulator('EQ-005', 'Robot-Arm-X', 'Robotic Arm'),
'EQ-006': EquipmentSimulator('EQ-006', 'Generator-Z', 'Backup Generator')
}
def save_data():
try:
data = {k: v.to_dict() for k, v in fleet.items()}
with open(DATA_FILE, 'w') as f:
json.dump(data, f, indent=2)
except Exception as e:
print(f"Error saving data: {e}")
# Initialize Fleet
fleet = load_data()
# --- Routes ---
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/status')
def get_status():
data = []
total_cost = 0
risk_score = 0
for eq in fleet.values():
eq.update()
data.append({
'id': eq.id,
'name': eq.name,
'type': eq.type,
'status': eq.status,
'health': round(eq.health_score, 1),
'sensors': {
'temp': round(eq.temperature, 1),
'vibration': round(eq.vibration, 2),
'pressure': round(eq.pressure, 0),
'rpm': int(eq.rpm)
},
'rul': int(eq.rul_prediction),
'run_hours': round(eq.run_hours, 1)
})
total_cost += sum(m['cost'] for m in eq.maintenance_history)
if eq.health_score < 50: risk_score += 1
return jsonify({
'fleet': data,
'timestamp': datetime.now().strftime('%H:%M:%S'),
'metrics': {
'total_maintenance_cost': total_cost,
'fleet_risk_level': 'High' if risk_score > 1 else 'Low' if risk_score == 0 else 'Medium'
}
})
@app.route('/api/maintain', methods=['POST'])
def maintain_equipment():
eq_id = request.json.get('id')
if eq_id in fleet:
fleet[eq_id].perform_maintenance()
save_data() # Save after maintenance
return jsonify({'success': True, 'message': f'Maintenance performed on {eq_id}'})
return jsonify({'success': False, 'message': 'Equipment not found'}), 404
@app.route('/api/export', methods=['GET'])
def export_data():
export_data = {
'generated_at': datetime.now().isoformat(),
'fleet_status': [
{
'id': eq.id,
'name': eq.name,
'history': eq.maintenance_history
} for eq in fleet.values()
]
}
return jsonify(export_data)
@app.route('/api/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'success': False, 'message': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'success': False, 'message': 'No selected file'}), 400
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify({'success': True, 'message': f'File {filename} uploaded successfully'})
return jsonify({'success': False, 'message': 'Upload failed'}), 500
# --- Error Handlers ---
@app.errorhandler(404)
def not_found(e):
if request.path.startswith('/api/'):
return jsonify({'error': 'Not found'}), 404
return "Page Not Found", 404
@app.errorhandler(500)
def server_error(e):
if request.path.startswith('/api/'):
return jsonify({'error': 'Internal Server Error'}), 500
return "Internal Server Error", 500
@app.errorhandler(RequestEntityTooLarge)
def handle_file_too_large(e):
return jsonify({'success': False, 'message': 'File is too large'}), 413
if __name__ == '__main__':
port = int(os.environ.get('PORT', 7860))
app.run(host='0.0.0.0', port=port, debug=True)
|