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,
"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
- File Monitoring (
handle_file_change): Computes the SHA3-512 hash of the mold file every 30 seconds. When the hash changes, it waits for the file to stop changing, parses the file, and exports it to the web application. - Data Parsing (
process_block_data): Reads the tab-separated.datfile, structures data as JSON, and stores a local backup injson_files\YYYYMM.json. - API Communication (
post_data): Posts the JSON array to{site_url}/api/mold/import_mold_datausing Bearer token authentication. - Startup Behavior: Runs an immediate export on script start, begins monitoring for file changes, and outputs: "DO NOT CLOSE THIS WINDOW (minimize it)".
Web Application Backend
API Controller: App\Controllers\Api\Mold
import_data() Method (/api/mold/import_mold_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).
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.