[{"id":"d282d9f17d9c6066","type":"tab","label":"フロー 1","disabled":false,"info":"","env":[]},{"id":"db4dd7aaffde654c","type":"LoRa Input","z":"d282d9f17d9c6066","name":"","devEUI":"","extendedField":"","x":100,"y":60,"wires":[["88855779998b3ffb"]]},{"id":"88855779998b3ffb","type":"Device Filter","z":"d282d9f17d9c6066","name":"VS373","eui":"24E124806F258113","x":150,"y":120,"wires":[["c0ed6f5e725736f7","196979796b660995"]]},{"id":"c0ed6f5e725736f7","type":"debug","z":"d282d9f17d9c6066","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":340,"y":120,"wires":[]},{"id":"196979796b660995","type":"function","z":"d282d9f17d9c6066","name":"VS373 decode","func":"/**\n * Payload Decoder\n *\n * Copyright 2025 Milesight IoT\n *\n * @product VS373\n */\nvar RAW_VALUE = 0x00;\n// fPortは通常85ですが、ここではデータ本体(bytes)のみを使用します\nvar fPort = 85;\nvar payloadRaw = msg.payload;\nvar bytes = Buffer.from(payloadRaw, 'base64');\n/* eslint no-redeclare: \"off\" */\n/* eslint-disable */\n// Chirpstack v4\nfunction decodeUplink(input) {\n    var decoded = milesightDeviceDecode(input.bytes);\n    return { data: decoded };\n}\n\n// Chirpstack v3\nfunction Decode(fPort, bytes) {\n    return milesightDeviceDecode(bytes);\n}\n\n// The Things Network\nfunction Decoder(bytes, port) {\n    return milesightDeviceDecode(bytes);\n}\n/* eslint-enable */\n\nfunction milesightDeviceDecode(bytes) {\n    var decoded = {};\n\n    for (var i = 0; i < bytes.length;) {\n        var channel_id = bytes[i++];\n        var channel_type = bytes[i++];\n\n        // IPSO VERSION\n        if (channel_id === 0xff && channel_type === 0x01) {\n            decoded.ipso_version = readProtocolVersion(bytes[i]);\n            i += 1;\n        }\n        // HARDWARE VERSION\n        else if (channel_id === 0xff && channel_type === 0x09) {\n            decoded.hardware_version = readHardwareVersion(bytes.slice(i, i + 2));\n            i += 2;\n        }\n        // FIRMWARE VERSION\n        else if (channel_id === 0xff && channel_type === 0x0a) {\n            decoded.firmware_version = readFirmwareVersion(bytes.slice(i, i + 2));\n            i += 2;\n        }\n        // DEVICE STATUS\n        else if (channel_id === 0xff && channel_type === 0x0b) {\n            decoded.device_status = readDeviceStatus(bytes[i]);\n            i += 1;\n        }\n        // LORAWAN CLASS\n        else if (channel_id === 0xff && channel_type === 0x0f) {\n            decoded.lorawan_class = readLoRaWANClass(bytes[i]);\n            i += 1;\n        }\n        // PRODUCT SERIAL NUMBER\n        else if (channel_id === 0xff && channel_type === 0x16) {\n            decoded.sn = readSerialNumber(bytes.slice(i, i + 8));\n            i += 8;\n        }\n        // TSL VERSION\n        else if (channel_id === 0xff && channel_type === 0xff) {\n            decoded.tsl_version = readTslVersion(bytes.slice(i, i + 2));\n            i += 2;\n        }\n        // DETECTION TARGET (v1.0.1)\n        else if (channel_id === 0x03 && channel_type === 0xf8) {\n            decoded.detection_status = readDetectionStatus(bytes[i]);\n            decoded.target_status = readTargetStatus(bytes[i + 1]);\n            decoded.use_time_now = readUInt16LE(bytes.slice(i + 2, i + 4));\n            decoded.use_time_today = readUInt16LE(bytes.slice(i + 4, i + 6));\n            i += 6;\n        }\n        // DETECTION TARGET (v1.0.2)\n        else if (channel_id === 0x07 && channel_type === 0xb0) {\n            decoded.detection_status = readDetectionStatus(bytes[i]);\n            decoded.target_status = readTargetStatus(bytes[i + 1]);\n            decoded.use_time_now = readUInt24LE(bytes.slice(i + 2, i + 5));\n            decoded.use_time_today = readUInt24LE(bytes.slice(i + 5, i + 8));\n            i += 8;\n        }\n        // REGION OCCUPANCY (v1.0.1)\n        else if (channel_id === 0x04 && channel_type === 0xf9) {\n            // for the old firmware, the occupancy status is 0: occupied, 1: vacant\n            // for the new firmware, the occupancy status is 0: vacant, 1: occupied\n            decoded.region_1_occupancy = readOccupancyStatus(bytes[i] === 1 ? 0 : 1);\n            decoded.region_2_occupancy = readOccupancyStatus(bytes[i + 1] === 1 ? 0 : 1);\n            decoded.region_3_occupancy = readOccupancyStatus(bytes[i + 2] === 1 ? 0 : 1);\n            decoded.region_4_occupancy = readOccupancyStatus(bytes[i + 3] === 1 ? 0 : 1);\n            i += 4;\n        }\n        // REGION TYPE (v1.0.2)\n        else if (channel_id === 0x09 && channel_type === 0xb2) {\n            for (var j = 0; j <= 5; j++) {\n                var region_chn_name = \"region_\" + (j + 1) + \"_type\";\n                decoded[region_chn_name] = readRegionType(bytes[i + j]);\n            }\n            i += 6;\n        }\n        // REGION OCCUPY(v1.0.2)\n        else if (channel_id === 0x0a && channel_type === 0xb3) {\n            var region_count = readUInt8(bytes[i]);\n            var data = readUInt32LE(bytes.slice(i + 1, i + 5));\n            for (var j = 0; j < region_count; j++) {\n                var region_chn_name = \"region_\" + (j + 1) + \"_occupancy\";\n                decoded[region_chn_name] = readOccupancyStatus((data >>> j) & 0x01);\n            }\n            i += 5;\n        }\n        // OUT OF BED (v1.0.1)\n        else if (channel_id === 0x05 && channel_type === 0xfa) {\n            decoded.region_1_out_of_bed_time = readUInt16LE(bytes.slice(i, i + 2));\n            decoded.region_2_out_of_bed_time = readUInt16LE(bytes.slice(i + 2, i + 4));\n            decoded.region_3_out_of_bed_time = readUInt16LE(bytes.slice(i + 4, i + 6));\n            decoded.region_4_out_of_bed_time = readUInt16LE(bytes.slice(i + 6, i + 8));\n            i += 8;\n        }\n        // OUT OF BED (v1.0.2) - REGION 1-3\n        else if (channel_id === 0x0b && channel_type === 0xb4) {\n            decoded.region_1_out_of_bed_time = readUInt24LE(bytes.slice(i, i + 3));\n            decoded.region_2_out_of_bed_time = readUInt24LE(bytes.slice(i + 3, i + 6));\n            decoded.region_3_out_of_bed_time = readUInt24LE(bytes.slice(i + 6, i + 9));\n            i += 9;\n        }\n        // OUT OF BED (v1.0.2) - REGION 4-6\n        else if (channel_id === 0x0c && channel_type === 0xb4) {\n            decoded.region_4_out_of_bed_time = readUInt24LE(bytes.slice(i, i + 3));\n            decoded.region_5_out_of_bed_time = readUInt24LE(bytes.slice(i + 3, i + 6));\n            decoded.region_6_out_of_bed_time = readUInt24LE(bytes.slice(i + 6, i + 9));\n            i += 9;\n        }\n        // ALARM\n        else if (channel_id === 0x06 && channel_type === 0xfb) {\n            var event = {};\n            event.alarm_id = readUInt16LE(bytes.slice(i, i + 2));\n            event.alarm_type = readAlarmType(bytes[i + 2]);\n            event.alarm_status = readAlarmStatus(bytes[i + 3]);\n            // EVENT TYPE: OUT OF BED\n            var alarm_type = readUInt8(bytes[i + 2]);\n            // out_of_bed, bradynea, tachypnea\n            if (alarm_type === 3 || alarm_type === 6 || alarm_type === 7) {\n                event.region_id = readUInt8(bytes[i + 4]);\n            }\n            i += 5;\n            decoded.events = decoded.events || [];\n            decoded.events.push(event);\n        }\n        // BREATHING DETECTION\n        else if (channel_id === 0x08 && channel_type === 0xb1) {\n            decoded.respiratory_status = readBreathStatus(bytes[i]);\n            decoded.respiratory_rate = readUInt16LE(bytes.slice(i + 1, i + 3)) / 100;\n            i += 3;\n        }\n        // HISTORY DATA\n        else if (channel_id === 0x20 && channel_type === 0xce) {\n            var data = {};\n            data.timestamp = readUInt32LE(bytes.slice(i, i + 4));\n            data.alarm_id = readUInt16LE(bytes.slice(i + 4, i + 6));\n            data.alarm_type = readAlarmType(bytes[i + 6]);\n            data.alarm_status = readAlarmStatus(bytes[i + 7]);\n            var alarm_type = readUInt8(bytes[i + 6]);\n            // EVENT TYPE: OUT OF BED\n            if (alarm_type === 3 || alarm_type === 6 || alarm_type === 7) {\n                data.region_id = readUInt8(bytes[i + 8]);\n            }\n            i += 9;\n            decoded.history = decoded.history || [];\n            decoded.history.push(data);\n        }\n        // DOWNLINK RESPONSE\n        else if (channel_id === 0xfe || channel_id === 0xff) {\n            var result = handle_downlink_response(channel_type, bytes, i);\n            decoded = Object.assign(decoded, result.data);\n            i = result.offset;\n        } else if (channel_id === 0xf8 || channel_id === 0xf9) {\n            var result = handle_downlink_response_ext(channel_id, channel_type, bytes, i);\n            decoded = Object.assign(decoded, result.data);\n            i = result.offset;\n        } else {\n            break;\n        }\n    }\n\n    return decoded;\n}\n\nfunction handle_downlink_response(channel_type, bytes, offset) {\n    var decoded = {};\n\n    switch (channel_type) {\n        case 0x04:\n            decoded.confirm_mode_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x10:\n            decoded.reboot = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x11:\n            decoded.timestamp = readUInt32LE(bytes.slice(offset, offset + 4));\n            offset += 4;\n            break;\n        case 0x28:\n            decoded.report_status = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x2f:\n            decoded.led_indicator_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x35:\n            decoded.d2d_key = bytesToHexString(bytes.slice(offset, offset + 8));\n            offset += 8;\n            break;\n        case 0x3e:\n            decoded.buzzer_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x40:\n            decoded.adr_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x42:\n            decoded.wifi_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x64:\n            decoded.release_alarm = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x69:\n            decoded.retransmit_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x6a:\n            var interval_type = readUInt8(bytes[offset]);\n            switch (interval_type) {\n                case 0:\n                    decoded.retransmit_interval = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n                    break;\n                case 1:\n                    decoded.resend_interval = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n                    break;\n            }\n            offset += 3;\n            break;\n        case 0x84:\n            decoded.d2d_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x8e:\n            // ignore the first byte\n            decoded.report_interval = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n            offset += 3;\n            break;\n        case 0x96:\n            var config = readD2DMasterConfig(bytes.slice(offset, offset + 8));\n            offset += 8;\n\n            decoded.d2d_master_config = decoded.d2d_master_config || [];\n            decoded.d2d_master_config.push(config);\n            break;\n        default:\n            throw new Error(\"unknown downlink response\");\n    }\n\n    return { data: decoded, offset: offset };\n}\n\nfunction handle_downlink_response_ext(code, channel_type, bytes, offset) {\n    var decoded = {};\n\n    switch (channel_type) {\n        case 0x48:\n            var region_id = readUInt8(bytes[offset]) + 1;\n            var region_name = \"region_\" + region_id;\n            decoded.delete_region = {};\n            decoded.delete_region[region_name] = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x49:\n            var region_settings = readRegionSettings(bytes.slice(offset, offset + 9));\n            offset += 9;\n            decoded.region_settings = decoded.region_settings || [];\n            decoded.region_settings.push(region_settings);\n            break;\n        case 0x4a:\n            var region_detection_settings = readRegionDetectionSettings(bytes.slice(offset, offset + 5));\n            offset += 5;\n            decoded.region_detection_settings = decoded.region_detection_settings || [];\n            decoded.region_detection_settings.push(region_detection_settings);\n            break;\n        case 0x4b:\n            var bed_detection_settings = readBedDetectionSettings(bytes.slice(offset, offset + 9));\n            offset += 9;\n            decoded.bed_detection_settings = decoded.bed_detection_settings || [];\n            decoded.bed_detection_settings.push(bed_detection_settings);\n            break;\n        case 0x4c:\n            var d2d_slave_config = readD2DSlaveConfig(bytes.slice(offset, offset + 5));\n            offset += 5;\n            decoded.d2d_slave_config = decoded.d2d_slave_config || [];\n            decoded.d2d_slave_config.push(d2d_slave_config);\n            break;\n        case 0x4e:\n            decoded.digital_output = readDigitalOutput(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x4d:\n            decoded.wifi_ssid_hidden = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x4f:\n            decoded.detection_region_settings = readDetectionRegion(bytes.slice(offset, offset + 12));\n            offset += 12;\n            break;\n        case 0x50:\n            decoded.detection_settings = readDetectionSettings(bytes.slice(offset, offset + 2));\n            offset += 2;\n            break;\n        case 0x51:\n            decoded.fall_detection_settings = readFallDetectionSettings(bytes.slice(offset, offset + 6));\n            offset += 6;\n            break;\n        case 0x52:\n            decoded.dwell_detection_settings = readDwellDetectionSettings(bytes.slice(offset, offset + 3));\n            offset += 3;\n            break;\n        case 0x53:\n            decoded.motion_detection_settings = readMotionDetectionSettings(bytes.slice(offset, offset + 3));\n            offset += 3;\n            break;\n        case 0x56:\n            decoded.existence_detection_settings = readExistenceDetectionSettings(bytes.slice(offset, offset + 2));\n            offset += 2;\n            break;\n        case 0x85:\n            decoded.rejoin_config = {};\n            decoded.rejoin_config.enable = readEnableStatus(bytes[offset]);\n            decoded.rejoin_config.max_count = readUInt8(bytes[offset + 1]);\n            offset += 2;\n            break;\n        case 0x86:\n            decoded.data_rate = readUInt8(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x87:\n            decoded.tx_power_level = readUInt8(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x91:\n            decoded.time_sync_config = {};\n            decoded.time_sync_config.mode = readTimeSyncMode(bytes[offset]);\n            decoded.time_sync_config.timestamp = readUInt32LE(bytes.slice(offset + 1, offset + 5));\n            offset += 5;\n            break;\n        case 0xb1:\n            decoded.sleep_detection_config = {};\n            decoded.sleep_detection_config.enable = readEnableStatus(bytes[offset]);\n            decoded.sleep_detection_config.start_time = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n            decoded.sleep_detection_config.end_time = readUInt16LE(bytes.slice(offset + 3, offset + 5));\n            decoded.sleep_detection_config.out_of_bed_enable = readEnableStatus(bytes[offset + 5]);\n            decoded.sleep_detection_config.out_of_bed_time = readUInt8(bytes[offset + 6]);\n            offset += 7;\n            break;\n        case 0xb2:\n            decoded.respiratory_detection_config = {};\n            decoded.respiratory_detection_config.enable = readEnableStatus(bytes[offset]);\n            decoded.respiratory_detection_config.min = readUInt8(bytes[offset + 1]);\n            decoded.respiratory_detection_config.max = readUInt8(bytes[offset + 2]);\n            offset += 3;\n            break;\n        case 0xb3:\n            decoded.ai_fall_detection_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0xb4:\n            decoded.confirm_fall_alarm = {};\n            decoded.confirm_fall_alarm.alarm_id = readUInt16LE(bytes.slice(offset, offset + 2));\n            decoded.confirm_fall_alarm.action = readConfirmAlarmType(bytes[offset + 2]);\n            offset += 3;\n            break;\n        case 0xb5:\n            decoded.trigger_digital_output_config = {};\n            decoded.trigger_digital_output_config.enable = readEnableStatus(bytes[offset]);\n            decoded.trigger_digital_output_config.fall = readEnableStatus(bytes[offset + 1]);\n            decoded.trigger_digital_output_config.lying = readEnableStatus(bytes[offset + 2]);\n            decoded.trigger_digital_output_config.out_of_bed = readEnableStatus(bytes[offset + 3]);\n            decoded.trigger_digital_output_config.dwell = readEnableStatus(bytes[offset + 4]);\n            decoded.trigger_digital_output_config.motionless = readEnableStatus(bytes[offset + 5]);\n            offset += 6;\n            break;\n        default:\n            throw new Error(\"unknown downlink response\");\n    }\n\n    if (hasResultFlag(code)) {\n        var result_value = readUInt8(bytes[offset]);\n        offset += 1;\n\n        if (result_value !== 0) {\n            var request = decoded;\n            decoded = {};\n            decoded.device_response_result = {};\n            decoded.device_response_result.channel_type = channel_type;\n            decoded.device_response_result.result = readResultStatus(result_value);\n            decoded.device_response_result.request = request;\n        }\n    }\n\n    return { data: decoded, offset: offset };\n}\n\nfunction hasResultFlag(code) {\n    return code === 0xf8;\n}\n\nfunction readResultStatus(status) {\n    var status_map = { 0: \"success\", 1: \"forbidden\", 2: \"invalid parameter\" };\n    return getValue(status_map, status);\n}\n\nfunction readProtocolVersion(bytes) {\n    var major = (bytes & 0xf0) >> 4;\n    var minor = bytes & 0x0f;\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readHardwareVersion(bytes) {\n    var major = (bytes[0] & 0xff).toString(16);\n    var minor = (bytes[1] & 0xff) >> 4;\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readFirmwareVersion(bytes) {\n    var major = (bytes[0] & 0xff).toString(16);\n    var minor = (bytes[1] & 0xff).toString(16);\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readTslVersion(bytes) {\n    var major = bytes[0] & 0xff;\n    var minor = bytes[1] & 0xff;\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readSerialNumber(bytes) {\n    var temp = [];\n    for (var idx = 0; idx < bytes.length; idx++) {\n        temp.push((\"0\" + (bytes[idx] & 0xff).toString(16)).slice(-2));\n    }\n    return temp.join(\"\");\n}\n\nfunction readLoRaWANClass(type) {\n    var lorawan_class_map = {\n        0: \"Class A\",\n        1: \"Class B\",\n        2: \"Class C\",\n        3: \"Class CtoB\",\n    };\n    return getValue(lorawan_class_map, type);\n}\n\nfunction readDeviceStatus(status) {\n    var device_status_map = { 0: \"off\", 1: \"on\" };\n    return getValue(device_status_map, status);\n}\n\nfunction readDetectionStatus(status) {\n    var detection_status_map = {\n        0: \"normal\",\n        1: \"vacant\",\n        2: \"in_bed\",\n        3: \"out_of_bed\",\n        4: \"fall\",\n    };\n    return getValue(detection_status_map, status);\n}\n\nfunction readBreathStatus(status) {\n    var breath_status_map = {\n        1: \"no_data_input\",\n        2: \"normal\",\n        3: \"tachypnea\",\n        4: \"bradypnea\",\n        5: \"undetectable\",\n    };\n    return getValue(breath_status_map, status);\n}\n\nfunction readTargetStatus(status) {\n    var target_status_map = {\n        0: \"normal\",\n        1: \"motionless\",\n        2: \"abnormal\",\n        3: \"lying_down\",\n    };\n    return getValue(target_status_map, status);\n}\n\nfunction readOccupancyStatus(status) {\n    var occupancy_status_map = {\n        0: \"vacant\",\n        1: \"occupied\",\n    };\n    return getValue(occupancy_status_map, status);\n}\n\nfunction readAlarmType(type) {\n    var alarm_type_map = {\n        0: \"fall\",\n        1: \"motionless\",\n        2: \"dwell\",\n        3: \"out_of_bed\",\n        4: \"occupied\",\n        5: \"vacant\",\n        6: \"bradynea\",\n        7: \"tachypnea\",\n        8: \"lying_down\",\n    };\n    return getValue(alarm_type_map, type);\n}\n\nfunction readAlarmStatus(status) {\n    var alarm_status_map = {\n        1: \"alarm_triggered\",\n        2: \"alarm_deactivated\",\n        3: \"alarm_ignored\",\n        4: \"respiratory_status\",\n    };\n    return getValue(alarm_status_map, status);\n}\n\nfunction readEnableStatus(status) {\n    var status_map = { 0: \"disable\", 1: \"enable\" };\n    return getValue(status_map, status);\n}\n\nfunction readYesNoStatus(status) {\n    var yes_no_status_map = { 0: \"no\", 1: \"yes\" };\n    return getValue(yes_no_status_map, status);\n}\n\nfunction readDigitalOutput(status) {\n    var digital_output_map = { 0: \"low\", 1: \"high\" };\n    return getValue(digital_output_map, status);\n}\n\nfunction readDetectionRegion(bytes) {\n    var detection_region = {};\n    detection_region.x_min = readInt16LE(bytes.slice(0, 2));\n    detection_region.x_max = readInt16LE(bytes.slice(2, 4));\n    detection_region.y_min = readInt16LE(bytes.slice(4, 6));\n    detection_region.y_max = readInt16LE(bytes.slice(6, 8));\n    detection_region.z_max = readUInt16LE(bytes.slice(8, 10));\n    detection_region.install_height = readUInt16LE(bytes.slice(10, 12));\n    return detection_region;\n}\n\nfunction readDetectionSettings(bytes) {\n    var detection_settings = {};\n    detection_settings.mode = readDetectionMode(bytes[0]);\n    detection_settings.sensitivity = readDetectionSensitivity(bytes[1]);\n    return detection_settings;\n}\n\nfunction readDetectionMode(type) {\n    var detection_mode_map = { 0: \"default\", 1: \"bedroom\", 2: \"bathroom\", 3: \"public\" };\n    return getValue(detection_mode_map, type);\n}\n\nfunction readDetectionSensitivity(type) {\n    var detection_sensitivity_map = { 0: \"low\", 1: \"high\", 2: \"medium\", 3: \"custom\" };\n    return getValue(detection_sensitivity_map, type);\n}\n\nfunction readFallDetectionSettings(bytes) {\n    var fall_detection_settings = {};\n    fall_detection_settings.confirm_time = readUInt16LE(bytes.slice(0, 2));\n    fall_detection_settings.delay_report_time = readUInt16LE(bytes.slice(2, 4));\n    fall_detection_settings.alarm_duration = readUInt16LE(bytes.slice(4, 6));\n    return fall_detection_settings;\n}\n\nfunction readDwellDetectionSettings(bytes) {\n    var dwell_detection_settings = {};\n    dwell_detection_settings.enable = readEnableStatus(bytes[0]);\n    dwell_detection_settings.dwell_time = readUInt16LE(bytes.slice(1, 3));\n    return dwell_detection_settings;\n}\n\nfunction readMotionDetectionSettings(bytes) {\n    var motion_detection_settings = {};\n    motion_detection_settings.enable = readEnableStatus(bytes[0]);\n    motion_detection_settings.motionless_time = readUInt8(bytes[2]);\n    return motion_detection_settings;\n}\n\nfunction readExistenceDetectionSettings(bytes) {\n    var existence_detection_settings = {};\n    existence_detection_settings.exist_confirm_time = readUInt8(bytes[0]);\n    existence_detection_settings.leaved_confirm_time = readUInt8(bytes[1]);\n    return existence_detection_settings;\n}\n\nfunction readRegionSettings(bytes) {\n    var region_settings = {};\n    region_settings.region_id = readUInt8(bytes[0]) + 1;\n    region_settings.x_min = readInt16LE(bytes.slice(1, 3));\n    region_settings.x_max = readInt16LE(bytes.slice(3, 5));\n    region_settings.y_min = readInt16LE(bytes.slice(5, 7));\n    region_settings.y_max = readInt16LE(bytes.slice(7, 9));\n    return region_settings;\n}\n\nfunction readRegionDetectionSettings(bytes) {\n    var region_detection_settings = {};\n    region_detection_settings.region_id = readUInt8(bytes[0]) + 1;\n    region_detection_settings.fall_detection_enable = readEnableStatus(bytes[1]);\n    region_detection_settings.dwell_detection_enable = readEnableStatus(bytes[2]);\n    region_detection_settings.motion_detection_enable = readEnableStatus(bytes[3]);\n    region_detection_settings.region_type = readRegionType(bytes[4]);\n    return region_detection_settings;\n}\n\nfunction readRegionType(type) {\n    var region_type_map = { 0: \"custom\", 1: \"bed\", 2: \"door\", 3: \"ignore\", 255: \"unset\" };\n    return getValue(region_type_map, type);\n}\n\nfunction readBedDetectionSettings(bytes) {\n    var bed_detection_settings = {};\n    bed_detection_settings.bed_id = readUInt8(bytes[0]) + 1;\n    bed_detection_settings.enable = readEnableStatus(bytes[1]);\n    bed_detection_settings.start_time = readUInt16LE(bytes.slice(2, 4));\n    bed_detection_settings.end_time = readUInt16LE(bytes.slice(4, 6));\n    bed_detection_settings.bed_height = readUInt16LE(bytes.slice(6, 8));\n    bed_detection_settings.out_of_bed_time = readUInt16LE(bytes.slice(8, 10));\n    return bed_detection_settings;\n}\n\nfunction readD2DMasterConfig(bytes) {\n    var offset = 0;\n    var config = {};\n    config.mode = readD2DMode(readUInt8(bytes[offset]));\n    config.enable = readEnableStatus(bytes[offset + 1]);\n    config.lora_uplink_enable = readEnableStatus(bytes[offset + 2]);\n    config.d2d_cmd = readD2DCommand(bytes.slice(offset + 3, offset + 5));\n    config.time = readUInt16LE(bytes.slice(offset + 5, offset + 7));\n    config.time_enable = readEnableStatus(bytes[offset + 7]);\n    return config;\n}\n\nfunction readD2DCommand(bytes) {\n    return (\"0\" + (bytes[1] & 0xff).toString(16)).slice(-2) + (\"0\" + (bytes[0] & 0xff).toString(16)).slice(-2);\n}\n\nfunction readD2DMode(type) {\n    var d2d_mode_map = { 0: \"occupied\", 1: \"vacant\", 2: \"fall\", 3: \"out_of_bed\", 4: \"motionless\", 5: \"dwell\" };\n    return getValue(d2d_mode_map, type);\n}\n\nfunction readD2DSlaveConfig(bytes) {\n    var d2d_slave_config = {};\n    d2d_slave_config.mode = readD2DMode(readUInt8(bytes[0]));\n    d2d_slave_config.d2d_cmd = readD2DCommand(bytes.slice(1, 3));\n    d2d_slave_config.control_type = readD2DControlType(bytes[3]);\n    d2d_slave_config.action_type = readD2DActionType(bytes[4]);\n    return d2d_slave_config;\n}\n\nfunction readD2DControlType(type) {\n    var d2d_control_type_map = { 1: \"button\" };\n    return getValue(d2d_control_type_map, type);\n}\n\nfunction readD2DActionType(type) {\n    var d2d_action_type_map = { 1: \"alarm_deactivate\", 2: \"wifi_on\", 3: \"wifi_off\" };\n    return getValue(d2d_action_type_map, type);\n}\n\nfunction readConfirmAlarmType(type) {\n    var confirm_alarm_type_map = { 2: \"dismiss\", 3: \"ignore\" };\n    return getValue(confirm_alarm_type_map, type);\n}\n\nfunction readTimeSyncMode(type) {\n    var time_sync_mode_map = { 0: \"sync_from_gateway\", 1: \"manual_sync\" };\n    return getValue(time_sync_mode_map, type);\n}\n\n/* eslint-disable */\nfunction readUInt8(bytes) {\n    return bytes & 0xff;\n}\n\nfunction readInt8(bytes) {\n    var ref = readUInt8(bytes);\n    return ref > 0x7f ? ref - 0x100 : ref;\n}\n\nfunction readUInt16LE(bytes) {\n    var value = (bytes[1] << 8) + bytes[0];\n    return value & 0xffff;\n}\n\nfunction readInt16LE(bytes) {\n    var ref = readUInt16LE(bytes);\n    return ref > 0x7fff ? ref - 0x10000 : ref;\n}\n\nfunction readUInt24LE(bytes) {\n    var value = (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];\n    return value & 0xffffff;\n}\n\nfunction readInt24LE(bytes) {\n    var ref = readUInt24LE(bytes);\n    return ref > 0x7fffff ? ref - 0x1000000 : ref;\n}\n\nfunction readUInt32LE(bytes) {\n    var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];\n    return (value & 0xffffffff) >>> 0;\n}\n\nfunction readInt32LE(bytes) {\n    var ref = readUInt32LE(bytes);\n    return ref > 0x7fffffff ? ref - 0x100000000 : ref;\n}\n\nfunction readFloatLE(bytes) {\n    var bits = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];\n    var sign = bits >>> 31 === 0 ? 1.0 : -1.0;\n    var e = (bits >>> 23) & 0xff;\n    var m = e === 0 ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;\n    var f = sign * m * Math.pow(2, e - 150);\n    return f;\n}\n\nfunction bytesToHexString(bytes) {\n    var temp = [];\n    for (var i = 0; i < bytes.length; i++) {\n        temp.push((\"0\" + (bytes[i] & 0xff).toString(16)).slice(-2));\n    }\n    return temp.join(\"\");\n}\n\nfunction getValue(map, key) {\n    if (RAW_VALUE) return key;\n\n    var value = map[key];\n    if (!value) value = \"unknown\";\n    return value;\n}\n\n//if (!Object.assign) {\nObject.defineProperty(Object, \"assign\", {\n    enumerable: false,\n    configurable: true,\n    writable: true,\n    value: function (target) {\n        \"use strict\";\n        if (target == null) {\n            throw new TypeError(\"Cannot convert first argument to object\");\n        }\n\n        var to = Object(target);\n        for (var i = 1; i < arguments.length; i++) {\n            var nextSource = arguments[i];\n            if (nextSource == null) {\n                continue;\n            }\n            nextSource = Object(nextSource);\n\n            var keysArray = Object.keys(Object(nextSource));\n            for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {\n                var nextKey = keysArray[nextIndex];\n                var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);\n                if (desc !== undefined && desc.enumerable) {\n                    // concat array\n                    if (Array.isArray(to[nextKey]) && Array.isArray(nextSource[nextKey])) {\n                        to[nextKey] = to[nextKey].concat(nextSource[nextKey]);\n                    } else {\n                        to[nextKey] = nextSource[nextKey];\n                    }\n                }\n            }\n        }\n        return to;\n    },\n});\n//}\n// ---------------------------------------------------------\n// 3. デコード実行と出力\n// ---------------------------------------------------------\n\n// デコード結果をmsg.payloadに上書き\nmsg.payload = Decode(fPort, bytes);\n\n// 識別用に機種名をセット(任意)\nmsg.topic = \"Milesight VS373\";\nreturn msg;","outputs":1,"noerr":69,"initialize":"","finalize":"","libs":[],"x":240,"y":180,"wires":[["194af53ae885457b"]]},{"id":"8995eb8bdcfde1bb","type":"inject","z":"d282d9f17d9c6066","name":"Test: Fall (0x06 0xFB 0x01 0x00 0x00 0x01)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"events\":[{\"alarm_id\":1,\"alarm_type\":0,\"alarm_status\":1}]}","payloadType":"json","x":250,"y":240,"wires":[["194af53ae885457b"]]},{"id":"b85da489417dab65","type":"inject","z":"d282d9f17d9c6066","name":"Test: Motionless","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"events\":[{\"alarm_id\":2,\"alarm_type\":1,\"alarm_status\":1}]}","payloadType":"json","x":250,"y":280,"wires":[["194af53ae885457b"]]},{"id":"5684dc924c3579c9","type":"inject","z":"d282d9f17d9c6066","name":"Test: Dwell","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"events\":[{\"alarm_id\":3,\"alarm_type\":2,\"alarm_status\":1}]}","payloadType":"json","x":250,"y":320,"wires":[["194af53ae885457b"]]},{"id":"21a476bdf38f411c","type":"inject","z":"d282d9f17d9c6066","name":"Test: Out of Bed","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"events\":[{\"alarm_id\":4,\"alarm_type\":3,\"alarm_status\":1,\"region_id\":1}]}","payloadType":"json","x":250,"y":360,"wires":[["194af53ae885457b"]]},{"id":"431e42ab10545da8","type":"inject","z":"d282d9f17d9c6066","name":"Test: Occupied","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"events\":[{\"alarm_id\":5,\"alarm_type\":4,\"alarm_status\":1}]}","payloadType":"json","x":250,"y":400,"wires":[["194af53ae885457b"]]},{"id":"7719791ed49feb66","type":"inject","z":"d282d9f17d9c6066","name":"Test: Vacant","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"events\":[{\"alarm_id\":6,\"alarm_type\":5,\"alarm_status\":1}]}","payloadType":"json","x":250,"y":440,"wires":[["194af53ae885457b"]]},{"id":"b5a33692b6ca22a0","type":"inject","z":"d282d9f17d9c6066","name":"Test: Raw bytes (Fall) 06 FB 01 00 00 01","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[6,251,1,0,0,1,0]","payloadType":"json","x":250,"y":480,"wires":[["194af53ae885457b"]]},{"id":"194af53ae885457b","type":"function","z":"d282d9f17d9c6066","name":"VS373 Alarm Detect & Format","func":"// =============================================\n// VS373 Alarm Detector\n// Alarm uplink: channel_id=0x06, channel_type=0xFB\n// Byte3 (alarm_type): 0=Fall 1=Motionless 2=Dwell\n//                     3=Out of Bed 4=Occupied 5=Vacant\n// RAW_VALUE=0(文字列) / RAW_VALUE=1(数値) の両方に対応\n// =============================================\n\n// RAW_VALUE=1 (数値) の場合\nvar ALARM_TYPE_NUM = {\n    0: \"Fall\", 1: \"Motionless\", 2: \"Dwell\",\n    3: \"Out of Bed\", 4: \"Occupied\", 5: \"Vacant\"\n};\n// RAW_VALUE=0 (文字列) の場合\nvar ALARM_TYPE_STR = {\n    \"fall\": \"Fall\", \"motionless\": \"Motionless\", \"dwell\": \"Dwell\",\n    \"out_of_bed\": \"Out of Bed\", \"occupied\": \"Occupied\", \"vacant\": \"Vacant\"\n};\nvar ALARM_STATUS_NUM = {\n    1: \"Alarm Triggered\", 2: \"Alarm Deactivated\",\n    3: \"Alarm Ignored\",   4: \"Respiratory Status\"\n};\nvar ALARM_STATUS_STR = {\n    \"alarm_triggered\": \"Alarm Triggered\",\n    \"alarm_deactivated\": \"Alarm Deactivated\",\n    \"alarm_ignored\": \"Alarm Ignored\",\n    \"respiratory_status\": \"Respiratory Status\"\n};\n\nfunction formatDateTime() {\n    var now = new Date();\n    var p = function(n) { return (\"0\" + n).slice(-2); };\n    return now.getFullYear() + \"/\" + p(now.getMonth() + 1) + \"/\" + p(now.getDate()) +\n           \" \" + p(now.getHours()) + \":\" + p(now.getMinutes()) + \":\" + p(now.getSeconds());\n}\n\nfunction resolveAlarmType(at) {\n    if (typeof at === \"number\") return ALARM_TYPE_NUM[at] || null;\n    if (typeof at === \"string\") return ALARM_TYPE_STR[at] || null;\n    return null;\n}\n\nfunction resolveAlarmStatus(as) {\n    if (typeof as === \"number\") return ALARM_STATUS_NUM[as] || \"\";\n    if (typeof as === \"string\") return ALARM_STATUS_STR[as] || as;\n    return \"\";\n}\n\nvar payload = msg.payload;\nvar alarmName = null;\nvar statusName = \"\";\nvar regionId = null;\nvar alarmId = null;\n\n// --- Case 1: デコード済みpayload (events配列) ---\n// RAW_VALUE=0: alarm_type=\"fall\"(文字列)  RAW_VALUE=1: alarm_type=0(数値)\nif (payload && Array.isArray(payload.events) && payload.events.length > 0) {\n    for (var i = 0; i < payload.events.length; i++) {\n        var ev = payload.events[i];\n        var resolved = resolveAlarmType(ev.alarm_type);\n        if (resolved !== null) {\n            alarmName = resolved;\n            alarmId = ev.alarm_id;\n            statusName = resolveAlarmStatus(ev.alarm_status);\n            regionId = (typeof ev.region_id !== \"undefined\") ? ev.region_id : null;\n            break;\n        }\n    }\n}\n\n// --- Case 2: 生バイト列 (Buffer / Array / HEX文字列) ---\n// パケット形式: [0x06][0xFB][alarm_id_lo][alarm_id_hi][alarm_type][alarm_status]([region_id])\nif (alarmName === null) {\n    var bytes = null;\n    if (Buffer.isBuffer(payload)) {\n        bytes = Array.from(payload);\n    } else if (Array.isArray(payload)) {\n        bytes = payload;\n    } else if (payload && Array.isArray(payload.bytes)) {\n        bytes = payload.bytes;\n    } else if (typeof payload === \"string\" && /^[0-9a-fA-F\\s]+$/.test(payload.trim())) {\n        var hex = payload.replace(/\\s/g, \"\");\n        bytes = [];\n        for (var k = 0; k < hex.length; k += 2) {\n            bytes.push(parseInt(hex.substr(k, 2), 16));\n        }\n    }\n\n    if (bytes && bytes.length >= 5) {\n        for (var j = 0; j <= bytes.length - 5; j++) {\n            if (bytes[j] === 0x06 && bytes[j + 1] === 0xfb) {\n                alarmId = (bytes[j + 3] << 8) | bytes[j + 2];\n                var typeVal = bytes[j + 4];\n                alarmName = ALARM_TYPE_NUM[typeVal] || null;\n                statusName = ALARM_STATUS_NUM[bytes[j + 5]] || \"\";\n                regionId = (typeVal === 3 && bytes.length > j + 6) ? bytes[j + 6] : null;\n                break;\n            }\n        }\n    }\n}\n\n// --- アラーム検出時に出力 ---\nif (alarmName !== null) {\n    var regionInfo = (regionId !== null) ? \" (Region \" + regionId + \")\" : \"\";\n\n    // 重複排除: 同じalarm_idまたは同じアラーム種別が60秒以内に来た場合はスキップ\n    var dedupKey = alarmName + \"|\" + (alarmId || \"\") + \"|\" + statusName;\n    var lastKey = context.get(\"lastKey\") || \"\";\n    var lastTime = context.get(\"lastTime\") || 0;\n    var now = Date.now();\n    if (dedupKey === lastKey && (now - lastTime) < 60000) {\n        node.status({ fill: \"yellow\", shape: \"ring\", text: \"dup skip: \" + alarmName });\n        return null;\n    }\n    context.set(\"lastKey\", dedupKey);\n    context.set(\"lastTime\", now);\n\n    var dt = formatDateTime();\n\n    msg.payload = alarmName + regionInfo;\n    msg.alarm_status = statusName;\n    msg.datetime = dt;\n    msg.region_id = regionId;\n    msg.alarm_id = alarmId;\n    msg.topic = \"VS373/Alarm\";\n\n    node.status({ fill: \"red\", shape: \"dot\", text: alarmName + \" @ \" + dt });\n    return msg;\n}\n\n// アラームでないアップリンク\nnode.status({ fill: \"grey\", shape: \"ring\", text: \"No alarm in payload\" });\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":360,"wires":[["02dc27e9d90687f0","3a54e69764a7152c"]]},{"id":"02dc27e9d90687f0","type":"ui_text","z":"d282d9f17d9c6066","group":"ui_grp_vs373_alarm","order":1,"width":6,"height":3,"name":"VS373 Alarm Display","label":"VS373 Alarm","format":"<div style='text-align:center'><h2 style='color:red;margin:4px 0'>{{msg.payload}}</h2><p style='font-size:1.1em;margin:4px 0'>{{msg.alarm_status}}</p><p style='color:#888;font-size:0.9em;margin:4px 0'>{{msg.datetime}}</p></div>","layout":"row-center","className":"","style":false,"font":"","fontSize":"","color":"#000000","x":790,"y":340,"wires":[]},{"id":"3a54e69764a7152c","type":"debug","z":"d282d9f17d9c6066","name":"Alarm Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":400,"wires":[]},{"id":"ui_grp_vs373_alarm","type":"ui_group","name":"Alarm Status","tab":"ui_tab_vs373_alarm","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"ui_tab_vs373_alarm","type":"ui_tab","name":"VS373 Alarm","icon":"dashboard","disabled":false,"hidden":false}]