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

标签 javascript json bash influxdb grafana

我正在尝试从我的 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/

相关文章:

javascript - jquery 两次选择的 select 选项无法正常工作

javascript - Onmouseout 事件不起作用

直接调用函数比通过 bash 脚本调用它花费更多的时间

linux - 如何使用任何流编辑器(如 sed 或 awk)将条目附加到多行条目的末尾

linux - 脚本不在 bg 中运行

javascript - 在 AngularJS 中将 JSON 对象保存为文件

javascript - 如何找出点击事件在哪里被捕获?

json - 如何在mongodb中导入json文件?

json - Playframework JSON 解析 - 空指针异常 - 当数组存在时

java - Jackson:为 Xml 和 JSON 格式配置实体