-
-
Save kuribas/9826b1fb9f2907a5c40baff1d7bbcb83 to your computer and use it in GitHub Desktop.
translate json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- example JSON: | |
{ | |
"timezone": "Europe/London", | |
"inverters": { | |
"count": 397, | |
"active": 330, | |
"communicating": null | |
}, | |
"ref": "plants/P3255", | |
"updated": "2020-10-07T08:45:00Z", | |
"name": "Bowerhouse", | |
"indicators": [], | |
"longitude": -2.85366, | |
"capacity_ac": 6749, | |
"alarms": { | |
"count": 4, | |
"severity_highest": "HIGH", | |
"capacity_affected": 9313.62, | |
"open": { | |
"count": 4, | |
"acknowledged": { | |
"count": 0 | |
}, | |
"high": { | |
"count": 4, | |
"capacity_affected": 9313.62 | |
} | |
} | |
}, | |
"irradiance": 126.3, | |
"id": "P3255", | |
"capacity_dc": 9313.62, | |
"interventions": { | |
"count": 14, | |
"open": { | |
"count": 8, | |
"acknowledged": { | |
"count": 8 | |
} | |
} | |
}, | |
"latitude": 51.3516, | |
"power_ac": 1301150, | |
"data_quality": 1, | |
"last_yield_update": "2020-10-07T08:45:00Z", | |
"loggers": { | |
"count": 9, | |
"communicating": 9, | |
"not_communicating": 0 | |
} | |
} | |
-} | |
data InvertersSummary = InvertersSummary | |
{ count :: Int | |
, communicating :: Int | |
, active :: Int | |
} deriving (Show, Generic) | |
instance FromJSON InvertersSummary where | |
parseJSON = genericParseJSONStrict defaultSqaOptions | |
instance ToJSON InvertersSummary where | |
toJSON = genericToJSON defaultSqaOptions | |
data LoggersSummary = LoggersSummary | |
{ count :: Int | |
, communicating :: Int | |
, not_communicating :: Int | |
} deriving (Show, Generic) | |
instance FromJSON LoggersSummary where | |
parseJSON = genericParseJSONStrict defaultSqaOptions | |
instance ToJSON LoggersSummary where | |
toJSON = genericToJSON defaultSqaOptions | |
data AlarmsSummary = AlarmsSummary | |
{ count :: Int | |
, severity_highest :: Severity | |
, capacity_affected :: Double | |
, open_count :: Int | |
, open_acknowledged_count :: Int | |
, high_count :: Maybe Int | |
, high_capacity_affected :: Maybe Double | |
} deriving (Show, Generic) | |
alarmSummaryTrans :: SQATrans AlarmsSummary | |
alarmSummaryTrans = sqaTrans | |
[ fromField (Proxy @"open_count") ["open"] "count" | |
, fromField (Proxy @"open_acknowledged_count") ["open", "acknowledged"] | |
"count" | |
, maybeFromField (Proxy @"high_count") ["high"] "count" | |
, maybeFromField (Proxy @"high_capacity_affected") ["high"] | |
"capacity_affected" | |
] | |
instance FromJSON AlarmsSummary where | |
parseJSON = sqaPreTrans "alarmSummary" alarmSummaryTrans $ | |
genericParseJSONStrict defaultSqaOptions | |
instance ToJSON AlarmsSummary where | |
toJSON = sqaPostTrans alarmSummaryTrans $ genericToJSON defaultSqaOptions | |
data InterventionsSummary = InterventionsSummary | |
{ count :: Int | |
, open_count :: Int | |
, acknowledged_count :: Int | |
} deriving (Show, Generic) | |
interventionSummaryTrans :: SQATrans InterventionsSummary | |
interventionSummaryTrans = sqaTrans | |
[ fromField (Proxy @"open_count") ["open"] "count" | |
, fromField (Proxy @"acknowledged_count") ["acknowledged"] "count" | |
] | |
instance FromJSON InterventionsSummary where | |
parseJSON = sqaPreTrans "interventions_summary" interventionSummaryTrans $ | |
genericParseJSONStrict defaultSqaOptions | |
instance ToJSON InterventionsSummary where | |
toJSON = sqaPostTrans interventionSummaryTrans $ | |
genericToJSON defaultSqaOptions | |
data Plant = Plant | |
{ ref :: Ref Plant | |
, id_ :: Int | |
, name :: Text | |
, latitude :: Double | |
, longitude :: Double | |
, updated :: UTCTime | |
, irradiance :: Double | |
, power_ac :: Double | |
, capacity_dc :: Double | |
, inverters :: InvertersSummary | |
, alarms :: AlarmsSummary | |
, interventions :: InterventionsSummary | |
, loggers :: LoggersSummary | |
, timezone :: Text | |
} | |
deriving (Show, Generic) | |
instance FromJSON Plant where | |
parseJSON = genericParseJSONStrict defaultSqaOptions | |
instance ToJSON Plant where | |
toJSON = genericToJSON defaultSqaOptions | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment