123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142(*---------------------------------------------------------------------------
Copyright (c) 2022 Geocaml Developers
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)typet={net:[`Generic]Eio.Net.tyEio.Net.t}letvnet={net:>[`Generic]Eio.Net.tyEio.Net.t}moduleEndpoints=structlet(//)ab=a^"/"^bletbase="api.carbonintensity.org.uk"letintensity="/intensity"letintensity_from~fromt=letfrom=Ptime.to_rfc3339frominmatchtwith|`Fw24->intensity//from//"fw24h"|`Fw48->intensity//"fw48h"|`Pt24->intensity//"pt24h"|`Toiso->intensity//Ptime.to_rfc3339isoendmodulePeriod=structtypet={from_:string;to_:string}letfromt=t.from_letto_t=t.to_letppppft=Fmt.pfppf"%s - %s"t.from_t.to_endmoduleError=structtypet={code:string;message:string}endmoduleIntensity=structtypeindex=[`Very_low|`Low|`Moderate|`High|`Very_high]typet={period:Period.t;forecast:int;actual:intoption;index:index;}letperiodt=t.periodletforecastt=t.forecastletactualt=t.actualletindext=t.indexletindex_of_string=function|"very low"->`Very_low|"low"->`Low|"moderate"->`Moderate|"high"->`High|"very high"->`Very_high|s->invalid_argsletindex_to_string=function|`Very_low->"very low"|`Low->"low"|`Moderate->"moderate"|`High->"high"|`Very_high->"very high"letof_jsonjson=match(J.findjson["from"],J.findjson["to"])with|Somefrom_,Someto_->(letfrom_=J.to_stringfrom_inletto_=J.to_stringto_inmatch(J.findjson["intensity";"forecast"],J.findjson["intensity";"actual"],J.findjson["intensity";"index"])with|Someforecast,Someactual,Someindex->letforecast=J.to_intforecastinletactual=Option.mapJ.to_int(J.null_to_optionactual)inletindex=J.to_stringindex|>index_of_stringin{period={from_;to_};forecast;actual;index}|_->invalid_arg"JSON has malformed intensity object")|_->invalid_arg"JSON does not have keys `from' and `to'"letpp_gco2ppf=function|Somev->Fmt.pfppf"%i gCO2/kWh"v|None->Fmt.pfppf"None"letppppft=Fmt.pfppf"period: %a@.forecast: %a@.actual: %a@.index: %s@."Period.ppt.periodpp_gco2(Somet.forecast)pp_gco2t.actual(index_to_stringt.index)endmoduleFactors=structtypet={biomass:int;coal:int;dutch_imports:int;french_imports:int;gas_combined_cycle:int;gas_open_cycle:int;hydro:int;irish_imports:int;nuclear:int;oil:int;other:int;pumped_storage:int;solar:int;wind:int;}endletheaders=Http.Header.of_list[("Accept","application/json");("Host","api.carbonintensity.org.uk")]letget_intensityt=Http_client.get_json~headers~net:t.netEndpoints.(base,intensity)|>J.path["data"]|>Ezjsonm.get_listIntensity.of_json|>List.hdletget_intensity_period~period:(from,to_)t=(Http_client.get_json~headers~net:t.net@@Endpoints.(base,intensity_from~fromto_))|>J.path["data"]|>Ezjsonm.get_listIntensity.of_json(*---------------------------------------------------------------------------
Copyright (c) 2022 Patrick Ferris <patrick@sirref.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
---------------------------------------------------------------------------*)