gpt4 book ai didi

javascript - 将 Cyber​​power UPS 的 JSON 解析为 InfluxDB/Grafana

转载 作者:行者123 更新时间:2023-11-29 09:29:23 26 4
gpt4 key购买 nike

我正在尝试从我的 Cyber​​power UPS 代理解析 JSON(用于将数据导入 InfluxDB/Grafana)。除了电池状态之外,我能够解析我想要的所有内容。如下所示,如果 UPS 与市电断开,状态为“Discharging”,但如果已连接,则该值包含一个逗号,“Normal, Fully Charged”....我无法找到一种方法来解析数据,因为我知道值中可能有也可能没有逗号。如果值为“Discharging”,那么它工作正常,但由于 “Normal, Fully Charged” 中的逗号,grep 结果为 “Normal(显然缺少结束语)。

单个值的 JSON:

"battery":{"state":"Discharging",

带逗号和空格的 JSON 值:

"battery":{"state":"Normal, Fully Charged",

我的 curl/greb。是否可以这样做来提取一个值,该值在我想要的值中可能有逗号,也可能没有逗号?如果是这样,我做错了什么?

curl http://10.0.1.61:3052/agent/ppbe.js/init_status.js | grep -oP '(?<="battery":{"state":)[^,]*' | head -1

放电时完整的 .js 页面:

var ppbeJsObj = {
"status": {
"communicationAvaiable": true,
"onlyPhaseArch": false,
"utility": {
"state": "Blackout",
"stateWarning": true,
"voltage": "0",
"frequency": "60.00",
"voltages": null,
"currents": null,
"frequencies": null,
"powerFactors": null
},
"bypass": {
"state": "Normal",
"stateWarning": false,
"voltage": null,
"current": null,
"frequency": null,
"voltages": null,
"currents": null,
"frequencies": null,
"powerFactors": null
},
"output": {
"state": "Normal",
"stateWarning": false,
"voltage": "120.0",
"frequency": null,
"load": 58,
"watt": 522,
"current": null,
"outputLoadWarning": false,
"outlet1": null,
"outlet2": null,
"activePower": null,
"apparentPower": null,
"reactivePower": null,
"voltages": null,
"currents": null,
"frequencies": null,
"powerFactors": null,
"loads": null,
"activePowers": null,
"apparentPowers": null,
"reactivePowers": null,
"emergencyOff": null,
"batteryExhausted": null
},
"battery": {
"state": "Discharging",
"stateWarning": true,
"voltage": null,
"capacity": 99,
"runtimeFormat": 1,
"runtimeFormatWarning": false,
"runtimeHour": 0,
"runtimeMinute": 20,
"chargetimeFormat": null,
"chargetimeHour": null,
"chargetimeMinute": null,
"temperatureCelsius": null,
"highVoltage": null,
"lowVoltage": null,
"highCurrent": null,
"lowCurrent": null
},
"upsSystem": {
"state": "Normal",
"stateWarning": false,
"temperatureCelsius": null,
"temperatureFahrenheit": null,
"maintenanceBreak": null,
"systemFaultDueBypass": null,
"systemFaultDueBypassFan": null,
"originalHardwareFaultCode": "0x8080"
},
"modules": null,
"deviceId": 0
}
};

最佳答案

这应该有效:

grep -P -o '(?<="battery":{"state":")[a-zA-Z, ]+'

问候!

编辑:根据 OP 评论进行编辑。

$ cat test.txt
var ppbeJsObj={"status":{"communicationAvaiable":true,
"onlyPhaseArch":false,
"utility":{"state":"Blackout",
"stateWarning":true,
"voltage":"0",
"frequency":"60.00",
"voltages":null,
"currents":null,
"frequencies":null,
"powerFactors":null},
"bypass":{"state":"Normal",
"stateWarning":false,
"voltage":null,
"current":null,
"frequency":null,
"voltages":null,
"currents":null,
"frequencies":null,
"powerFactors":null},
"output":{"state":"Normal",
"stateWarning":false,
"voltage":"120.0",
"frequency":null,
"load":58,
"watt":522,
"current":null,
"outputLoadWarning":false,
"outlet1":null,
"outlet2":null,
"activePower":null,
"apparentPower":null,
"reactivePower":null,
"voltages":null,
"currents":null,
"frequencies":null,
"powerFactors":null,
"loads":null,
"activePowers":null,
"apparentPowers":null,
"reactivePowers":null,
"emergencyOff":null,
"batteryExhausted":null},
"battery":{"state":"Discharging",
"stateWarning":true,
"voltage":null,
"capacity":99,
"runtimeFormat":1,
"runtimeFormatWarning":false,
"runtimeHour":0,
"runtimeMinute":20,
"chargetimeFormat":null,
"chargetimeHour":null,
"chargetimeMinute":null,
"temperatureCelsius":null,
"highVoltage":null,
"lowVoltage":null,
"highCurrent":null,
"lowCurrent":null},
"upsSystem":{"state":"Normal",
"stateWarning":false,
"temperatureCelsius":null,
"temperatureFahrenheit":null,
"maintenanceBreak":null,
"systemFaultDueBypass":null,
"systemFaultDueBypassFan":null,
"originalHardwareFaultCode":"0x8080"},
"modules":null,
"deviceId":0}};

$ grep -P -o '(?<="battery":{"state":")[a-zA-Z, ]+' test.txt
Discharging

关于javascript - 将 Cyber​​power UPS 的 JSON 解析为 InfluxDB/Grafana,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48830617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com