Mold
Architecture Overview / Data Flow
Mold Machine
↓ (exports to file)
C:\WINCC\dati\YYYYMM.dat
↓ (monitored by)
Python Script (main.py)
↓ (detects changes via hash)
Parse CSV → JSON
↓ (POST request)
API: /api/mold/import_mold_data
↓ (validates & processes)
Database: block table
↓ (fetched by)
API: /api/block/get
↓ (displays in)
Mold In Progress Page (Tabulator table)
↓ (operator edits)
API: /api/block/save
↓ (updates)
Database: block table
Python Monitoring Script
Location: C:\Users\jmess\PycharmProjects\shelter-ent-mold-app\main.py
The Python script runs continuously on the mold machine computer, monitoring changes to the monthly mold data file.
Configuration (settings.json):
{
"site_url": "https://dev.shelter-ent.app/index.php/",
"file_path": ".\\",
"mold_file_path": ".\\",
"controller": "api/mold",
"function": "import_mold_data",
"api_key": "YOUR_API_KEY",
"sleep_time": 30,
"heartbeat_interval": 300,
"headers": [
"date", "time", "block_num", "density", "height", "depth",
"load_weight", "weight", "req_density", "regrind", "silo_density",
"block_density", "stab_t", "cycle_time", "steam_time",
"raw_silo", "raw_code", "ground_silo", "ground_code"
]
}
Key Functions
- Console QuickEdit Mode Disabler (
disable_console_quickedit): Turns off Windows Console QuickEdit mode via Win32 API (ctypes.windll.kernel32.SetConsoleMode) on launch so clicking the terminal cannot freeze execution. - Heartbeat Emission (
send_heartbeat): Posts liveness pings to{site_url}/api/mold/heartbeaton startup and everyheartbeat_intervalseconds (default 300s) during idle periods. - File Monitoring (
handle_file_change): Computes the SHA3-512 hash of the monthly.datfile every 30 seconds. When a hash change is detected, it switches to 10-second checks until the file size stabilizes before parsing. - Data Parsing (
process_block_data): Reads the tab-separated.datfile, filters unweighed or 0-weight blocks, structures data as JSON, and stores a local backup injson_files\YYYYMM.json. - API Communication (
post_data): Posts the JSON array in memory-efficient chunks of 50 blocks to{site_url}/api/mold/import_mold_datausing Bearer token authentication. - Daily Restart: Exits at 11:59 PM to allow daily log rotation and clean memory before scheduled restart.
Web Application Backend
API Controller: App\Controllers\Api\Mold
import_data()Method (POST /api/mold/import_mold_dataor/import_data):- Combines date + time into the
createdtimestamp. - Maps density string to database ID using
parse_density_string()helper (falls back to "Custom" density if no match found). - Checks for existing block via
get_block(created, block_num). - If a block doesn't exist, creates a new
Block_modelrecord. - If it exists, updates the existing record.
- Links block to lots from raw and ground silos intelligently using FIFO consumption tracking (
calculate_block_material_consumption). - Records the sync timestamp and block count in
mold_sync_status.
- Combines date + time into the
heartbeat()Method (POST /api/mold/heartbeat):- Ingests background liveness pings and optional file modification timestamps from the Python daemon.
- Updates
last_contact_atin themold_sync_statusdatabase table.
get_status()Method (GET|POST /api/mold/statusand CLIphp public/index.php api/mold/status):- Returns sync health metrics (
state,seconds_ago,date_label). Drives the Healthy (green) vs Stalled (red, after 30 minutes) badge and "Last synced" time on the Admin Dashboard Mold card.
- Returns sync health metrics (
API Controller: App\Controllers\Api\Block
get()(/api/block/get): Fetches paginated unused blocks (date_used IS NULL).save()(/api/block/save): Updates block properties from inline editing via Tabulator.flag()(/api/block/flag): Updatesflaggedstatus andcomments. Requires a comment even if an empty string.
Model: App\Models\Block_model
Table: block
Fillable Fields:
density_id,block_num,created,height,regrind,weight,depth,load_weight,raw_silo,ground_silo
Relationships:
density(): BelongsToDensity_modelcut_job(): BelongsToJob_modeleifs_blocks(): HasManyEifs_block_modelused_eifs_blocks(): HasManyEifs_block_model
Important Considerations
- Duplicate Prevention: The
get_block()helper checks for existing blocks bydate+block_num. Import always updates existing blocks rather than creating duplicates. - Density Matching:
parse_density_string()uses fuzzy matching for density labels. - Silo/Lot Linking: Blocks are automatically linked to lots in the silos they were created from in the
block_lotsjunction table.
Validation Rules
Import Validation:
- Dates must be valid
m/d/Yformat - Times must be valid
H:i:sformat - Block numbers: 1-99998
- Regrind: 0-100%
- Heights, weights must be > 0
- Silo numbers within acceptable ranges
Troubleshooting
Problem: Block appears in mold file but not in web application.
Solution:
- Check Python script console for errors.
- Verify script is running (should show "Waiting for file changes...").
- Check
logs/log-YYYY-MM-DD.txtfor error messages. - Verify API key in
settings.jsonis valid. - Check network connectivity between mold computer and web server.
- Review web application logs for validation errors.
Problem: Density shows as "Custom" when it should be specific type.
Solution:
- Check exact density string in
.datfile. - Add new variation to
parse_density_string()function inblock_helper.php. - Or update density label in database to match mold machine output.
Related Documentation
- Python Scripts - Detailed documentation on the overall architecture of Python scripts integrating with the factory hardware.