Skip to content

Commit

Permalink
guest registar debug
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codes committed Dec 9, 2022
1 parent 1625063 commit ef22f2e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions apps/guest-registrar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function run() {
const mqtt = await createMqtt()
const restartTopic = "homeassistant/restarted"
const addGuestTopic = "homeassistant/group/guests/add"
const debugStateTopic = "guest-registrar/debug/state"
mqtt.on("message", async (topic, payload) => {
try {
logger.info(`MQTT message recieved: ${topic}`)
Expand All @@ -32,6 +33,9 @@ async function run() {
case addGuestTopic:
store.dispatch(addGuest(payload.toString()))
break
case debugStateTopic:
logger.debug(JSON.stringify(store.getState(), null, 2))
break
default:
}
} catch (error) {
Expand Down
8 changes: 7 additions & 1 deletion apps/guest-registrar/src/redux/actionCreators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AddGuestAction,
UpdateHomeAssistantWithGuestsAction,
UpdateMacsAction,
} from "./types"

const addGuest = (mac: string): AddGuestAction => ({
Expand All @@ -13,4 +14,9 @@ const updateHomeAssistantWithGuests =
type: "UPDATE_HOME_ASSISTANT_WITH_GUESTS",
})

export { addGuest, updateHomeAssistantWithGuests }
const updateMacs = (macs: string[]): UpdateMacsAction => ({
type: "UPDATE_MACS",
payload: macs,
})

export { addGuest, updateHomeAssistantWithGuests, updateMacs }
14 changes: 12 additions & 2 deletions apps/guest-registrar/src/redux/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import type { AnyAction, State } from "./types"
import { merge, uniq } from "lodash"

const defaultState: State = {}
const defaultState: State = { macs: [] }

const reducer = (state = defaultState, action: AnyAction) => state
const reducer = (state = defaultState, action: AnyAction) => {
switch (action.type) {
case "UPDATE_MACS":
const newState: State = merge({}, state)
newState.macs = uniq(newState.macs.concat(action.payload))
return newState
default:
return state
}
}

export default reducer
export { defaultState }
5 changes: 3 additions & 2 deletions apps/guest-registrar/src/redux/sagas/addGuest.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { createLogger } from "@ha/logger"
import { call } from "redux-saga/effects"
import { call, put } from "redux-saga/effects"
import {
MongoClient,
Db,
WithId,
Filter,
Document,
UpdateResult,
UpdateOptions,
} from "mongodb"
import getMongoDbClient from "../../dbClient"
import { AddGuestAction } from "../types"
import { updateMacs } from "../actionCreators"

const logger = createLogger()

Expand All @@ -27,6 +27,7 @@ function* addGuest(action: AddGuestAction) {
options: UpdateOptions,
) => Promise<UpdateResult>
>(collection.updateOne, {}, { mac: action.payload.mac }, { upsert: true })
yield put(updateMacs([action.payload.mac]))
} catch (error) {
logger.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { AsyncMqttClient, IPublishPacket } from "@ha/mqtt-client"
import { createLogger } from "@ha/logger"
import { call } from "redux-saga/effects"
import { call, put } from "redux-saga/effects"
import { MongoClient, Db, WithId, Document } from "mongodb"
import { createMqtt } from "@ha/mqtt-client"
import getMongoDbClient from "../../dbClient"
import { AddGuestAction } from "../types"
import { updateMacs } from "../actionCreators"

const logger = createLogger()

Expand Down Expand Up @@ -33,6 +34,7 @@ function* updateHomeAssistantWithGuests(action: AddGuestAction) {
qos: 1,
})
}
yield put(updateMacs(macs))
} catch (error) {
logger.error(error)
}
Expand Down
15 changes: 13 additions & 2 deletions apps/guest-registrar/src/redux/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
type State = {}
type State = {
macs: string[]
}

type AddGuestAction = {
type: "ADD_GUEST"
Expand All @@ -11,11 +13,20 @@ type UpdateHomeAssistantWithGuestsAction = {
type: "UPDATE_HOME_ASSISTANT_WITH_GUESTS"
}

type AnyAction = AddGuestAction | UpdateHomeAssistantWithGuestsAction
type UpdateMacsAction = {
type: "UPDATE_MACS"
payload: string[]
}

type AnyAction =
| AddGuestAction
| UpdateHomeAssistantWithGuestsAction
| UpdateMacsAction

export type {
AnyAction,
AddGuestAction,
State,
UpdateHomeAssistantWithGuestsAction,
UpdateMacsAction,
}

0 comments on commit ef22f2e

Please sign in to comment.