Skip to content

Commit

Permalink
Added get and set device state functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpetro committed Mar 3, 2024
1 parent a0d86e5 commit 5bc4cd6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/irrigation-events/irrigation-events.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { DeviceState } from '@/enums/device-state.interface'
import { MakerApiEventDto } from './dto/maker-api-event.dto'
import { IrrigationEvent } from './interfaces/irrigation-event.interface'
import { parseISO } from 'date-fns'
import { DeviceStates } from '@/maker-api/interfaces/device-states.interface'
import { IrrigationEventViewmodel } from './dto/irrigation-event-viewmodel.dto'

describe('IrrigationEventsController', () => {
Expand Down Expand Up @@ -185,7 +184,7 @@ describe('IrrigationEventsController', () => {
},
] as IrrigationEvent[])
mockGetEventsAfterEnd.mockResolvedValue([])
mockGetAllDeviceDetails.mockResolvedValue({ 42: DeviceState.ON } as DeviceStates)
mockGetAllDeviceDetails.mockResolvedValue({ 42: DeviceState.ON })
const dateSpy = jest.spyOn(Date, 'now').mockImplementation(() => new Date('2024-01-01T11:30:00.000Z').getTime())
const startTimestamp = '2024-01-01T00:00:00.000Z'
const endTimestamp = '2024-01-02T00:00:00.000Z'
Expand Down
5 changes: 0 additions & 5 deletions src/maker-api/interfaces/device-states.interface.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/maker-api/interfaces/maker-device-details.interface.ts

This file was deleted.

23 changes: 21 additions & 2 deletions src/maker-api/maker-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import EnvironmentVariables from '@/environment-variables'
import { Injectable, OnModuleInit } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import axios, { AxiosInstance } from 'axios'
import { MakerDeviceDetails } from './interfaces/maker-device-details.interface'
import { DeviceStates } from './interfaces/device-states.interface'
import { DeviceState } from '@/enums/device-state.interface'

interface DeviceStates {
[deviceId: number]: DeviceState | undefined
}

interface MakerDeviceDetails {
id: string
attributes: {
switch: DeviceState
}
}

@Injectable()
export class MakerApiService implements OnModuleInit {
Expand All @@ -24,4 +34,13 @@ export class MakerApiService implements OnModuleInit {
const data = response.data ?? []
return Object.fromEntries(data.map((device) => [parseInt(device.id, 10), device.attributes.switch])) as DeviceStates
}

public async setDeviceState(deviceId: number, state: DeviceState) {
await this.axiosInstance.get(`/${deviceId}/${state}`)
}

public async getDeviceState(deviceId: number) {
const response = await this.axiosInstance.get<{ value: string }>(`/${deviceId}/attribute/switch`)
return response.data.value as DeviceState
}
}

0 comments on commit 5bc4cd6

Please sign in to comment.