Skip to main content

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/heartbeat on startup and every heartbeat_interval seconds (default 300s) during idle periods.
  • File Monitoring (handle_file_change): Computes the SHA3-512 hash of the monthly .dat file 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 .dat file, filters unweighed or 0-weight blocks, structures data as JSON, and stores a local backup in json_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_data using 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_data or /import_data):
    • Combines date + time into the created timestamp.
    • 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_model record.
    • 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.
  • heartbeat() Method (POST /api/mold/heartbeat):
    • Ingests background liveness pings and optional file modification timestamps from the Python daemon.
    • Updates last_contact_at in the mold_sync_status database table.
  • get_status() Method (GET|POST /api/mold/status and CLI php 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.

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): Updates flagged status and comments. 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(): BelongsTo Density_model
  • cut_job(): BelongsTo Job_model
  • eifs_blocks(): HasMany Eifs_block_model
  • used_eifs_blocks(): HasMany Eifs_block_model

Important Considerations​

  • Duplicate Prevention: The get_block() helper checks for existing blocks by date + 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_lots junction table.

Validation Rules​

Import Validation:

  • Dates must be valid m/d/Y format
  • Times must be valid H:i:s format
  • 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:

  1. Check Python script console for errors.
  2. Verify script is running (should show "Waiting for file changes...").
  3. Check logs/log-YYYY-MM-DD.txt for error messages.
  4. Verify API key in settings.json is valid.
  5. Check network connectivity between mold computer and web server.
  6. Review web application logs for validation errors.

Problem: Density shows as "Custom" when it should be specific type.

Solution:

  1. Check exact density string in .dat file.
  2. Add new variation to parse_density_string() function in block_helper.php.
  3. Or update density label in database to match mold machine output.
  • Python Scripts - Detailed documentation on the overall architecture of Python scripts integrating with the factory hardware.