# Phase 1 Quick Start Guide

## What Was Implemented

✅ **Scenario 1: Auto-Push for Missing Versions**
- When a device sends a status message with missing version fields
- System automatically pushes predefined parameters and configs
- Uses template-based approach: Active template first, then Default

---

## Files to Review

### Core Implementation
1. **AutoPushService** → `lib/da_product_app/terminal_management/auto_push_service.ex`
   - Main logic for missing version detection
   - Template lookup and parameter resolution
   - MQTT command building and sending

2. **MQTT Handler** → `lib/da_product_app/mqtt/handler.ex`
   - Updated to extract vendor, model, versions
   - Triggers auto-push for missing versions
   - Stores versions in terminal table

3. **Schemas** 
   - `lib/da_product_app/terminal_management/tms_terminal.ex` - Added version fields
   - `lib/da_product_app/parameter_management/parameter_push_log.ex` - Added config tracking

4. **Migrations**
   - `priv/repo/migrations/20250131000001_add_version_columns_to_tms_terminals.exs`
   - `priv/repo/migrations/20250131000002_extend_parameter_push_logs_for_configs.exs`

5. **Seeds**
   - `priv/repo/seeds/device_setup_parameters.exs` - Creates Device Setup category parameters

---

## Deployment Steps

### Step 1: Run Migrations
```bash
cd /home/prem/mercurypay/tmsuat
mix ecto.migrate
```

Check the database:
```bash
mysql -u root -pdataaegis123 shukria_transactions -e "DESC tms_terminals;" | grep version
```

Expected output:
```
parameter_config_version        varchar(255)    YES
emv_config_version              varchar(255)    YES
keys_config_version             varchar(255)    YES
```

### Step 2: Seed Device Setup Parameters
```bash
mix run priv/repo/seeds/device_setup_parameters.exs
```

Verify seeding:
```bash
mysql -u root -pdataaegis123 shukria_transactions -e "SELECT key, name FROM parameter_definitions WHERE key IN ('emv_config_version', 'keys_config_version', 'application_version');"
```

### Step 3: Restart the Application
```bash
# Stop current server (Ctrl+C if running in foreground)
mix phx.server
```

---

## Testing Scenario 1

### Send MQTT Message via mosquitto_pub:

```bash
mosquitto_pub \
  -h 20.233.19.152 \
  -p 8883 \
  --cafile /path/to/ca.crt \
  -t "tms/status/61250904380091" \
  -m '{
    "oid": "org_123",
    "sn": "61250904380091",
    "vendor": "MoreFun",
    "model": "MF919",
    "uploadTime": "2025-01-31T15:00:00Z",
    "org.device": [
      {"itemkey": "status", "value": "online", "timestamp": "2025-01-31T15:00:00Z", "message": "Status"},
      {"itemkey": "parameter_config", "value": "", "timestamp": "2025-01-31T15:00:00Z", "message": "Parameter Config"},
      {"itemkey": "emv_config", "value": null, "timestamp": "2025-01-31T15:00:00Z", "message": "EMV Config"},
      {"itemkey": "keys_config", "value": "", "timestamp": "2025-01-31T15:00:00Z", "message": "Keys Config"},
      {"itemkey": "application", "value": null, "timestamp": "2025-01-31T15:00:00Z", "message": "Application"}
    ]
  }'
```

### Monitor Logs:

```bash
tail -f logs/dev.log
```

Look for:
```
AutoPushService: Triggering auto-push for 61250904380091 (MoreFun MF919) - missing version
AutoPushService: Created push log X for parameters (template: Y)
AutoPushService: Sending parameter push to 61250904380091
AutoPushService: Parameter push sent successfully
```

### Check Database:

```bash
# Check if terminal was updated with versions
mysql -u root -pdataaegis123 shukria_transactions -e "SELECT serial_number, vendor, model, parameter_config_version, emv_config_version, keys_config_version FROM tms_terminals WHERE serial_number = '61250904380091';"

# Check if push logs were created
mysql -u root -pdataaegis123 shukria_transactions -e "SELECT config_type, device_vendor, device_model, trigger_reason, status FROM parameter_push_logs WHERE device_model = 'MF919' ORDER BY created_at DESC LIMIT 10;"
```

---

## Key Metrics to Monitor

1. **Auto-Push Trigger Success**
   - Check `parameter_push_logs` for entries with `trigger_reason = 'missing_version'`
   - Should have 4 entries per device: 1 for parameters + 3 for configs

2. **MQTT Command Delivery**
   - Check device logs for received push commands
   - Verify command format matches device expectations

3. **Terminal State Updates**
   - Version fields should be populated in `tms_terminals`
   - Check: `vendor`, `model`, `parameter_config_version`, etc.

---

## Troubleshooting

### Issue: Auto-push not triggered
**Check:**
- Are version fields actually NULL/EMPTY in the message?
- Is template found for vendor+model combination?
- Check logs for: `No template found for...`

### Issue: MQTT command not sent
**Check:**
- Is MQTT broker connection active?
- Check `DaProductApp.MQTT.publish/3` return value in logs
- Verify topic and payload format

### Issue: Terminal not updated
**Check:**
- Is handler receiving the message?
- Check handler logs: `Received status update for terminal...`
- Verify database connection is working

---

## Phase 2 Preview

Next phase will add:
- Version comparison logic
- Scheduled version tracking
- Auto-push for outdated versions (Scenario 2)

All version data is now ready for Phase 2!

---

**Last Updated:** 2025-01-31
**Phase 1 Status:** ✅ COMPLETE
**Ready for Phase 2:** ✅ YES
