Skip to content

Commit

Permalink
Add ability for WS client to publish MQTT messages to topics (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddellacosta authored Oct 24, 2023
1 parent d8be1cd commit 6e2d1ef
Show file tree
Hide file tree
Showing 32 changed files with 2,793 additions and 391 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ You can now take this and drop it in your `compose.yml` file for docker-compose.

### Testing

Running tests:
Running main application test suite:

```bash
# watchexec is handy:
Expand All @@ -138,6 +138,17 @@ $ cabal test --test-show-details=always --test-options '--color=always -l -p Uni
$ cabal test --test-show-details=always --test-options '--color=always -l -p Integration'
```

Frontend tests:

```bash
$ npm run test
$ npm run test:watch

# the above are wrappers for
$ npx spago test
$ watchexec -w src -w test -- npx spago test
```


## License/Copyright

Expand Down
24 changes: 13 additions & 11 deletions automation-service.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ common shared
default-language: GHC2021

build-depends:
aeson ^>= 2.0.3.0
aeson ^>= 2.1.2.1
, astro ^>= 0.4.3.0
, base ^>= 4.16.3.0
, bytestring ^>= 0.11.3.1
, connection ^>= 0.3.1
, containers ^>= 0.6.5.1
, cron ^>= 0.7.0
, dhall ^>= 1.41.1
, fast-logger ^>= 3.1.1
, fast-logger ^>= 3.2.2
, hashable ^>= 1.4.2.0
, hslua ^>= 2.2.1
, hslua-marshalling ^>= 2.2.1
, hslua-packaging ^>= 2.2.1
, hslua ^>= 2.3.0
, hslua-marshalling ^>= 2.3.0
, hslua-packaging ^>= 2.3.0
, http-client ^>= 0.7.13.1
, http-client-tls ^>= 0.3.6.1
, lens ^>= 5.1.1
, lens ^>= 5.2.3
, lens-aeson ^>= 1.2.2
, mtl ^>= 2.2.2
, net-mqtt ^>= 0.8.2.1
Expand All @@ -51,19 +51,20 @@ common shared
, safe ^>= 0.3.19
, scotty ^>= 0.12.1
, sqlite-simple ^>= 0.4.18.2
-- had to downgrade from 2.0.2 because of other bumped
-- dependencies?
, text ^>= 1.2.5.0
, time ^>= 1.11.1.1
, tls ^>= 1.5.8
-- would like to upgrade ASAP
-- , tls ^>= 1.6.0
-- v1.6.0 disables SSL3
, tls ^>= 1.6.0
, transformers ^>= 0.5.6.2
, tz ^>= 0.1.3.6
, tzdata ^>= 0.2.20230322.0
, unliftio ^>= 0.2.23.0
, unliftio-core ^>= 0.2.1.0
, unordered-containers ^>= 0.2.19.1
, uuid ^>= 1.3.15
, vector ^>= 0.12.3.1
, vector ^>= 0.13.0.0
, wai ^>= 3.2.3
, wai-middleware-static ^>= 0.9.2
, wai-websockets ^>= 3.0.1.2
Expand Down Expand Up @@ -91,6 +92,7 @@ library
, Service.Automations
, Service.Automations.Gold
, Service.Automations.HTTP
, Service.Automations.HTTPDefault
, Service.Automations.LuaScript
, Service.Automations.StateManager
, Service.Daemon
Expand Down Expand Up @@ -140,7 +142,7 @@ test-suite automation-service-tests
, Test.Unit.Service.TimeHelpers
build-depends:
automation-service
, hspec ^>= 2.9.7
, hspec ^>= 2.10.10
, QuickCheck ^>= 2.14.2
, tasty ^>= 1.4.2.3
, tasty-hspec ^>= 1.2.0.1
Expand Down
1 change: 1 addition & 0 deletions cabal.project.local
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
constraints: lua +system-lua +pkg-config +apicheck
ignore-project: False
tests: True
2 changes: 2 additions & 0 deletions config.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ in { mqttBroker =
{ uri =
"mqtts://automation-service:wvwSWIXIKnpiI6qyR8Z7ojg9BH4RWPbHY6SHTGvLWd2AeYPifH@mosquitto:8883"
, automationServiceTopic = "automation-service/set"
, statusTopic = "automation-service/status"
, caCertPath = Some "ca.crt"
, clientCertPath = Some "client.crt"
, clientKeyPath = Some "client.key"
Expand All @@ -19,5 +20,6 @@ in { mqttBroker =
, logLevel = LogLevel.Debug
, luaScriptPath = "lua-automations/"
, dbPath = "automationState.db"
, httpPort = 8080
}
: ./config/Config.dhall
1 change: 1 addition & 0 deletions config/Config.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
, logLevel : ./LogLevel.dhall
, luaScriptPath : Text
, dbPath : Text
, httpPort : Natural
}
74 changes: 11 additions & 63 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@
description = "automation-service";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
# for PureScript 0.15.11
nixpkgs.url = "nixpkgs/9957cd48326fe8dbd52fdc50dd2502307f188b0d";
flake-utils.url = "github:numtide/flake-utils";
easy-purescript-nix.url = "github:justinwoo/easy-purescript-nix";
};

outputs = { self, nixpkgs, flake-utils, easy-purescript-nix }:
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachSystem ["x86_64-linux"] (system:
with nixpkgs.legacyPackages.${system};
let
t = lib.trivial;
hl = haskell.lib;
haskellPackages = haskell.packages.ghc927;
easy-ps = easy-purescript-nix.packages.${system};

name = "automation-service";

project = devTools:
let
addBuildTools = (t.flip hl.addBuildTools) (devTools ++ [
easy-ps.purs-0_15_10
easy-ps.spago
easy-ps.purescript-language-server
easy-ps.purs-tidy
pkgs.nodejs_20
pkgs.purescript
pkgs.nodejs_18
pkgs.esbuild
zlib
]);
Expand Down Expand Up @@ -59,6 +55,7 @@
];
};

# should I bump this? - 2023-10-19
nixFromDockerHub = pkgs.dockerTools.pullImage {
imageName = "nixos/nix";
imageDigest = "sha256:31b808456afccc2a419507ea112e152cf27e9bd2527517b0b6ca8639cc423501";
Expand Down
10 changes: 7 additions & 3 deletions src/Service/AutomationName.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Service.AutomationName
( AutomationName(..)
, Port(..)
, parseAutomationName
, parseAutomationNameText
, serializeAutomationName
Expand All @@ -14,10 +15,15 @@ import Data.List (uncons)
import Data.Text (Text)
import qualified Data.Text as T
import GHC.Generics (Generic)
import Numeric.Natural (Natural)

newtype Port = Port Natural
deriving (Generic, Eq, Ord, Show, Enum, Integral, Num, Real, Hashable, FromJSON)

data AutomationName
= Gold
| HTTP
| HTTP Port
| HTTPDefault
| LuaScript FilePath
| Null
| StateManager
Expand All @@ -39,9 +45,7 @@ serializeAutomationName = \case
parseAutomationName :: String -> Maybe AutomationName
parseAutomationName = \case
"Gold" -> Just Gold
"HTTP" -> Just HTTP
"Null" -> Just Null
"StateManager" -> Just StateManager
maybeLuaScript -> do
let filepath = filter (/= '"') maybeLuaScript
(firstChar, _remainder) <- uncons filepath
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Automations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Service.Automation (Automation, nullAutomation)
import Service.AutomationName (AutomationName (..))
import Service.Automations.Gold (goldAutomation)
import Service.Automations.HTTP (httpAutomation)
import Service.Automations.HTTPDefault (defaultHttpAutomation)
import Service.Automations.LuaScript (luaAutomation)
import Service.Automations.StateManager (stateManagerAutomation)
import Service.Env (Env)
Expand All @@ -23,6 +24,7 @@ findAutomation
findAutomation = \case
Null -> nullAutomation
Gold -> goldAutomation
HTTP -> httpAutomation
HTTPDefault -> defaultHttpAutomation
HTTP port -> httpAutomation port
LuaScript filePath -> luaAutomation filePath
StateManager -> stateManagerAutomation
Loading

0 comments on commit 6e2d1ef

Please sign in to comment.