Query tankerkönig API with given apikey and station

This commit is contained in:
Alexander Kobjolke 2023-11-26 21:12:12 +01:00
parent b5d61674d8
commit 6603e65131
6 changed files with 242 additions and 48 deletions

77
flake.lock generated
View file

@ -1,5 +1,21 @@
{ {
"nodes": { "nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1733328505,
"narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1659877975, "lastModified": 1659877975,
@ -15,54 +31,79 @@
"type": "github" "type": "github"
} }
}, },
"flake-utils_2": { "gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": { "locked": {
"lastModified": 1644229661, "lastModified": 1709087332,
"narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=",
"owner": "numtide", "owner": "hercules-ci",
"repo": "flake-utils", "repo": "gitignore.nix",
"rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", "rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "numtide", "owner": "hercules-ci",
"repo": "flake-utils", "repo": "gitignore.nix",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1661626419, "lastModified": 1735531152,
"narHash": "sha256-CjdPtdwH7I5Es4SjdCGuNfeulIyaM1LP0dXGWi4dyuQ=", "narHash": "sha256-As8I+ebItDKtboWgDXYZSIjGlKeqiLBvjxsQHUmAf1Q=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "a5e05d62460ff0b7627559a8b55ab421041ebf0a", "rev": "3ffbbdbac0566a0977da3d2657b89cbcfe9a173b",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-24.11",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1735286948,
"narHash": "sha256-JMRV2RI58nV1UqLXqm+lcea1/dr92fYjWU5S+Rz3fmE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "31ac92f9628682b294026f0860e14587a09ffb4b",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-24.05",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
}, },
"pre-commit-hooks": { "pre-commit-hooks": {
"inputs": { "inputs": {
"flake-utils": "flake-utils_2", "flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [ "nixpkgs": [
"nixpkgs" "nixpkgs"
] ],
"nixpkgs-stable": "nixpkgs-stable"
}, },
"locked": { "locked": {
"lastModified": 1660830093, "lastModified": 1734797603,
"narHash": "sha256-HUhx3a82C7bgp2REdGFeHJdhEAzMGCk3V8xIvfBqg1I=", "narHash": "sha256-ulZN7ps8nBV31SE+dwkDvKIzvN6hroRY8sYOT0w+E28=",
"owner": "cachix", "owner": "cachix",
"repo": "pre-commit-hooks.nix", "repo": "git-hooks.nix",
"rev": "8cb8ea5f1c7bc2984f460587fddd5f2e558f6eb8", "rev": "f0f0dc4920a903c3e08f5bdb9246bb572fcae498",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "cachix", "owner": "cachix",
"repo": "pre-commit-hooks.nix", "repo": "git-hooks.nix",
"type": "github" "type": "github"
} }
}, },

View file

@ -2,25 +2,27 @@
description = "Tankerkönig-to-MQTT gateway"; description = "Tankerkönig-to-MQTT gateway";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; pre-commit-hooks.url = "github:cachix/git-hooks.nix";
pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs"; pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
}; };
outputs = { self, nixpkgs, flake-utils, pre-commit-hooks }: outputs = { self, nixpkgs, flake-utils, pre-commit-hooks }:
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let let
ghcv = "ghc96";
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
overlay = final: prev: { overlay = final: prev: {
tk2mqtt = final.callCabal2nix "tk2mqtt" ./. { }; tk2mqtt = final.callCabal2nix "tk2mqtt" ./. { };
}; };
haskellPackages = pkgs.haskell.packages.ghc924.extend overlay; haskellPackages = pkgs.haskell.packages.${ghcv}.extend overlay;
in { in
{
packages.default = haskellPackages.tk2mqtt; packages.default = haskellPackages.tk2mqtt;
apps = { apps = {
@ -37,13 +39,14 @@
checks = { checks = {
pre-commit-check = pre-commit-hooks.lib.${system}.run { pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.; src = ./.;
settings = { ormolu.defaultExtensions = [ "GHC2021" ]; };
tools.fourmolu = haskellPackages.fourmolu;
hooks = { hooks = {
nixfmt.enable = true; nixpkgs-fmt.enable = true;
fourmolu.enable = true;
hpack.enable = true; hpack.enable = true;
hlint.enable = true; hlint.enable = true;
ormolu = {
enable = true;
settings.defaultExtensions = [ "GHC2021" ];
};
doctest = { doctest = {
enable = false; enable = false;
name = "Run documentation tests"; name = "Run documentation tests";
@ -64,14 +67,15 @@
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; [
haskellPackages.haskell-language-server haskellPackages.haskell-language-server
haskellPackages.fourmolu # haskellPackages.fourmolu
haskellPackages.hspec-discover haskellPackages.hspec-discover
haskellPackages.doctest haskellPackages.doctest
cabal-install cabal-install
ghcid ghcid
nixfmt nixfmt-rfc-style
hpack hpack
hlint hlint
yaml-language-server
]; ];
}; };
}); });

View file

@ -15,14 +15,18 @@ dependencies:
- directory - directory
- containers - containers
- net-mqtt - net-mqtt
- network-uri
- req - req
- effectful
- aeson - aeson
- text - text
# - bytestring - bytestring
- process
ghc-options: ghc-options:
- -Wall - -Wall
- -fdefer-typed-holes - -fdefer-typed-holes
- -threaded
default-extensions: default-extensions:
- BlockArguments - BlockArguments

View file

@ -1,26 +1,40 @@
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE OverloadedStrings #-}
module TK2MQTT ( module TK2MQTT
runTK2MQTT, ( runTK2MQTT,
defaultMain, defaultMain,
) where )
where
import Control.Exception (catch)
import Control.Monad (void)
import Control.Monad.IO.Class
import Data.Aeson
import Network.HTTP.Req
import Network.MQTT.Client qualified as MQTT
import Network.URI (URI, parseURI)
import System.Environment qualified as Env import System.Environment qualified as Env
import System.IO import System.IO
import Control.Exception qualified as Exception newtype APIKey = APIKey String
import System.IO.Error qualified as IOError
runTK2MQTT :: IO () newtype StationID = StationID String
runTK2MQTT = do
putStrLn "Hello"
eitherToError :: Show a => Either a b -> IO b runTK2MQTT :: URI -> APIKey -> StationID -> IO ()
eitherToError = either (Exception.throwIO . IOError.userError . show) return runTK2MQTT uri (APIKey apikey) (StationID stationId) = do
mc <- MQTT.connectURI MQTT.mqttConfig uri
result <- runReq defaultHttpConfig $ do
r <- req GET (https "creativecommons.tankerkoenig.de" /: "json" /: "prices.php") NoReqBody jsonResponse ("apikey" =: apikey <> "ids" =: stationId)
pure (responseBody r :: Value)
void $ liftIO $ MQTT.publish mc "tmp/fuel/diesel" (encode result) True
void $ liftIO $ print $ encode result
handleError :: IOError -> IO () handleError :: IOError -> IO ()
handleError e = do handleError e = do
hPutStrLn stderr $ "I ran into an issue: " <> show e hPutStrLn stderr $ "I ran into an issue: " <> show e
defaultMain :: IO () defaultMain :: IO ()
defaultMain = Exception.catch runTK2MQTT handleError defaultMain = do
[apikey, stationId] <- Env.getArgs
let (Just uri) = parseURI "mqtt://hm.felis-halfmoon.ts.net"
runTK2MQTT uri (APIKey apikey) (StationID stationId) `catch` handleError

View file

@ -1,12 +1,8 @@
module TK2MQTTSpec (spec) where module TK2MQTTSpec (spec) where
import HCat.Internal import TK2MQTT.Internal
import Test.Hspec import Test.Hspec
import Test.Hspec.QuickCheck (prop) import Test.Hspec.QuickCheck (prop)
spec :: Spec spec :: Spec
spec = do spec = pure ()
describe "parseArgs" do
it "returns the filename if given a single argument" do
True `shouldBe` True

135
tk2mqtt.cabal Normal file
View file

@ -0,0 +1,135 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.36.1.
--
-- see: https://github.com/sol/hpack
name: tk2mqtt
version: 0.0.1.0
description: Tankerkönig-to-MQTT gateway
author: Alexander Kobjolke
maintainer: alex@jakalx.net
copyright: Alexander Kobjolke 2023
license: MIT
build-type: Simple
extra-source-files:
README.org
library
exposed-modules:
TK2MQTT
TK2MQTT.Internal
other-modules:
Paths_tk2mqtt
hs-source-dirs:
src
default-extensions:
BlockArguments
OverloadedStrings
ImportQualifiedPost
ghc-options: -Wall -fdefer-typed-holes -threaded
build-depends:
aeson
, base >=4.13 && <5
, bytestring
, containers
, directory
, effectful
, net-mqtt
, network-uri
, process
, req
, text
, time
default-language: GHC2021
executable tk2mqtt
main-is: tk2mqtt.hs
other-modules:
Paths_tk2mqtt
hs-source-dirs:
app
default-extensions:
BlockArguments
OverloadedStrings
ImportQualifiedPost
ghc-options: -Wall -fdefer-typed-holes -threaded
build-depends:
aeson
, base >=4.13 && <5
, bytestring
, containers
, directory
, effectful
, net-mqtt
, network-uri
, process
, req
, text
, time
, tk2mqtt
default-language: GHC2021
test-suite doctest
type: exitcode-stdio-1.0
main-is: Doctest.hs
other-modules:
Paths_tk2mqtt
hs-source-dirs:
test/doctest
default-extensions:
BlockArguments
OverloadedStrings
ImportQualifiedPost
ghc-options: -Wall -fdefer-typed-holes -threaded
build-tool-depends:
doctest:doctest
build-depends:
aeson
, base >=4.13 && <5
, bytestring
, containers
, directory
, effectful
, net-mqtt
, network-uri
, process
, req
, text
, time
default-language: Haskell2010
test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
TK2MQTTSpec
Paths_tk2mqtt
hs-source-dirs:
test/spec
default-extensions:
BlockArguments
OverloadedStrings
ImportQualifiedPost
ghc-options: -Wall -fdefer-typed-holes -threaded
cpp-options: -DTEST
build-tool-depends:
hspec-discover:hspec-discover
build-depends:
QuickCheck
, aeson
, base >=4.13 && <5
, bytestring
, containers
, directory
, effectful
, hspec
, net-mqtt
, network-uri
, process
, quickcheck-instances
, req
, text
, time
, tk2mqtt
default-language: GHC2021