{
    "name"  : "端口是否连接着非标PD",
    "precondition" : ["name", "type"],
    "problems" : {
        "no_pd" : {
            "desc"      : "【问题】检测到接口没有接PD",
            "advice"    : "【建议】重新插紧网线或者更换网线，有条件的话，更换其他PD试试"
        },
        "illegal_pd" : {
            "desc"      : "【问题】检测到非标的PD",
            "advice"    : "【建议】接口上面开启poe legacy，给非标PD供电"
        },
        "unknow" : {
            "desc"      : "【问题】未知异常",
            "advice"    : "【建议】研发支持" 
        }
    },
    "port_register" : {
        "1" : "0x0c",
        "2" : "0x0d",
        "3" : "0x0e",
        "4" : "0x0f",
        "5" : "0x0c",
        "6" : "0x0d",
        "7" : "0x0e",
        "8" : "0x0f",
        "9" : "0x0c",
        "10" : "0x0d",
        "11" : "0x0e",
        "12" : "0x0f",
        "13" : "0x0c",
        "14" : "0x0d",
        "15" : "0x0e",
        "16" : "0x0f",
        "17" : "0x0c",
        "18" : "0x0d",
        "19" : "0x0e",
        "20" : "0x0f",
        "21" : "0x0c",
        "22" : "0x0d",
        "23" : "0x0e",
        "24" : "0x0f"
    },
    "port_i2c" : {
        "1" : "0x28",
        "2" : "0x28",
        "3" : "0x28",
        "4" : "0x28",
        "5" : "0x29",
        "6" : "0x29",
        "7" : "0x29",
        "8" : "0x29",
        "9" : "0x2a",
        "10" : "0x2a",
        "11" : "0x2a",
        "12" : "0x2a",
        "13" : "0x2b",
        "14" : "0x2b",
        "15" : "0x2b",
        "16" : "0x2b",
        "17" : "0x2c",
        "18" : "0x2c",
        "19" : "0x2c",
        "20" : "0x2c",
        "21" : "0x2d",
        "22" : "0x2d",
        "23" : "0x2d",
        "24" : "0x2d"
    }
}
---------------------------code-------------------------------
def countRegLen(register):
	if register >= 0x30 && register <= 0x3e
		return 2
	end
	return 1
end

type = precondition('type')
if type != 'copper':
    add_reason('非电口类型的端口不需要进行该项诊断')
    return 'skip'
end

model = get_device_model()
if model.search(/S2928G-24P/i) == -1 && model.search(/S2928G-12P/i) == -1 && model.search(/S3760E-24P(v2.0)/i) == -1:
    add_reason('该诊断仅支持设备型号为S2928G-24P、S2928G-12P或者S3760E-24P(v2.0)的设备')
    return 'skip'
end


cli('sd')

#获取端口号
reg = /(?:\d+\/)+(\d+)/
intf_name = precondition('name')
tmp = intf_name.match(reg)
port_num = tmp[1]

port_register = get_configure_value('port_register')
port_i2c = get_configure_value('port_i2c')

cli('sh cons on')
register = port_register[port_num]
i2c = port_i2c[port_num]
regLen = countRegLen(register)
cmd = format("sh pdt read %s %s %d",i2c,register, regLen)
res = cli(cmd)
cli('sh cons on')
cli('exit')

#res = 'Ruijie(sd)#sh pdt read 0x28 0x0c 1\r\nread i2c. reg_val[0x28:0x0c]=0x06.'
reg = /^read i2c.*=(.*)\./m
val = res.match(reg)
if val == null:
    return 'success'
end
val = val[1]
val = parse_int(val, 16)
print val
if val == 6:
    add_problem('no_pd')
    return 'warn'
end

if val == 5 || val == 7:
    add_problem('illegal_pd')
    return 'warn'
end

return 'success'


