[
    {
        "id": "452525118dba30af",
        "type": "function",
        "z": "6e6e08c3c76c7987",
        "name": "Get ROI Images",
        "func": "const url = msg.payload.image_url;\nconst ipRegex = /http:\\/\\/(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/;\nconst match = url.match(ipRegex);\nconst ipAddress = match[1];\nglobal.set('ip',ipAddress)\nconst baseUrl = `http://${ipAddress}/postgrest/captures?select=id%2Cpath%2Ccaptured_at%2Ctriggered_by%2Crecipe%3Arecipes%28id%2Cname%2Crecipe_type%29%2Ctrigger_id%2Cacq_mode_at_capture%2Cblock_aligner_training_instances%28block_instance%28recipes%28id%2Cname%29%29%29%2Cblock_classification_training_images%28block_instance%28recipes%28id%2Cname%29%29%29%2Cblock_segmentation_training_images%28block_instance%28recipes%28id%2Cname%29%29%29%2Ccapture_result%28pass_or_fail%2Cuser_metadata%29%2Calignment_result%28angle%2Ccenter_loc%2Cconfidence%29%2Cclassification_result%28id%2Cconfidence%2Cblock_instance%28recipe%3Arecipes%28id%2Cname%29%29%2Croi_result%28fabricjs_object%2Calignment_result%28affine_matrix%29%2Cinspection_region%28id%2Cname%2Cinspection_type%28id%2Cname%29%29%29%2Cclass%3Ablock_classification_classes%28id%2Clabel_name%2Ccolor%29%29%2Csegmentation_result%28id%2Cimage_score%2Cconfidence_score_threshold%2Cmask_stats%2Clookup_table_dl2mask%2Cblock_instance%28recipe%3Arecipes%28id%2Cname%29%29%2Croi_result%28fabricjs_object%2Calignment_result%28affine_matrix%29%2Cinspection_region%28id%2Cname%2Cinspection_type%28id%2Cname%29%29%29%29`;\nconst latestResult = '&order=id.desc&limit=1';\n\nmsg.url = baseUrl + latestResult;\nreturn msg;\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 260,
        "wires": [
            [
                "1652659627cf1fc7"
            ]
        ]
    },
    {
        "id": "1652659627cf1fc7",
        "type": "http request",
        "z": "6e6e08c3c76c7987",
        "name": "",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 630,
        "y": 260,
        "wires": [
            [
                "c7e9a68db1a235ee"
            ]
        ]
    },
    {
        "id": "c7e9a68db1a235ee",
        "type": "function",
        "z": "6e6e08c3c76c7987",
        "name": "Store recent images",
        "func": "// Get or initialize the map of ROI name -> image URL\nlet imageMap = global.get('imageMap') || {};\n\n// Base URL for the images\nconst baseUrl = global.get('ip');\n\n// Get the most recent capture only\nconst captures = msg.payload;\n\nif (Array.isArray(captures) && captures.length > 0) {\n    const latestCapture = captures[0];\n    const classificationResults = latestCapture.classification_result;\n\n    if (classificationResults && classificationResults.length > 0) {\n        classificationResults.forEach(result => {\n            const roiName = result.roi_result?.inspection_region?.name || `ROI_${result.id}`;\n            const imageUrl = `http://${baseUrl}/edge/haystack/cropped_roi/${result.id}`;\n            imageMap[roiName] = imageUrl; // Overwrite with latest\n        });\n    }\n\n    // Save the updated map\n    global.set('imageMap', imageMap);\n\n    // Return just the latest image URLs\n    msg.payload = Object.values(imageMap);\n    return msg;\n} else {\n    node.error('No valid capture data provided', msg);\n    return null;\n}\n",
        "outputs": 1,
        "timeout": 5,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 820,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "2023e1c1b59a88fb",
        "type": "change",
        "z": "6e6e08c3c76c7987",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload.position",
                "pt": "msg",
                "to": "position",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 230,
        "y": 260,
        "wires": [
            [
                "452525118dba30af"
            ]
        ]
    }
]

