Skip to content

Commit

Permalink
✨ Support new commands
Browse files Browse the repository at this point in the history
- getPower
- getEnvironmentTemperature
- getTargetTemperature
  • Loading branch information
AndreMiras committed Dec 5, 2024
1 parent adcc978 commit b45735f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ const createProgram = (): Command => {
mac: string
) => api.deviceInfo(jwtToken, mac),
},
{
commandName: "getPower",
description: "Retrieve device power status",
getter: (
api: ReturnType<typeof configure>,
jwtToken: string,
mac: string
) => api.getPower(jwtToken, mac),
},
{
commandName: "getEnvironmentTemperature",
description: "Retrieve environment temperature",
getter: (
api: ReturnType<typeof configure>,
jwtToken: string,
mac: string
) => api.getEnvironmentTemperature(jwtToken, mac),
},
{
commandName: "getTargetTemperature",
description: "Retrieve target temperature",
getter: (
api: ReturnType<typeof configure>,
jwtToken: string,
mac: string
) => api.getTargetTemperature(jwtToken, mac),
},
].forEach(({ commandName, description, getter }) => {
addMacOption(
addAuthOptions(program.command(commandName).description(description))
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ export {
UserParametersType,
} from "./types";

export const { deviceInfo, setPower, setPowerOff, setPowerOn } = configure();
export const {
deviceInfo,
setPower,
setPowerOff,
setPowerOn,
getPower,
getEnvironmentTemperature,
getTargetTemperature,
} = configure();
50 changes: 49 additions & 1 deletion src/library.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ describe("library", () => {
});

describe("configure", () => {
const expectedApi = ["deviceInfo", "setPower", "setPowerOff", "setPowerOn"];
const expectedApi = [
"deviceInfo",
"setPower",
"setPowerOff",
"setPowerOn",
"getPower",
"getEnvironmentTemperature",
"getTargetTemperature",
];
it("should create API methods with the correct baseURL", () => {
const baseURL = "https://example.com/api";
const api = configure(baseURL);
Expand Down Expand Up @@ -177,5 +185,45 @@ describe("library", () => {
assert.equal(result.status, 200);
});
});

const getterTests = [
{
method: "getPower",
call: (api: ReturnType<typeof configure>, token: string, mac: string) =>
api.getPower(token, mac),
expectedResult: true,
},
{
method: "getEnvironmentTemperature",
call: (api: ReturnType<typeof configure>, token: string, mac: string) =>
api.getEnvironmentTemperature(token, mac),
expectedResult: 19,
},
{
method: "getTargetTemperature",
call: (api: ReturnType<typeof configure>, token: string, mac: string) =>
api.getTargetTemperature(token, mac),
expectedResult: 22,
},
];
getterTests.forEach(({ method, call, expectedResult }) => {
it(`should call axios and return the correct value for ${method}`, async () => {
const mockAxios = {
get: sinon.stub().resolves({ data: mockDeviceInfo }),
};
axiosStub.returns(mockAxios);
const api = configure("https://example.com/api");

const result = await call(api, expectedToken, "mockMacAddress");

assert.deepEqual(mockAxios.get.args, [
[
"device/mockMacAddress/info",
{ headers: { Authorization: `Bearer ${expectedToken}` } },
],
]);
assert.equal(result, expectedResult);
});
});
});
});
37 changes: 37 additions & 0 deletions src/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,54 @@ const setPowerOff =
(axiosInstance: AxiosInstance) => (jwtToken: string, macAddress: string) =>
setPower(axiosInstance)(jwtToken, macAddress, 0);

/**
* Get device current power value.
*/
const getPower =
(axiosInstance: AxiosInstance) =>
async (jwtToken: string, macAddress: string): Promise<boolean> => {
const info = await deviceInfo(axiosInstance)(jwtToken, macAddress);
return info.status.commands.power;
};

/**
* Get the environment temperature coming from sensors.
*/
const getEnvironmentTemperature =
(axiosInstance: AxiosInstance) =>
async (jwtToken: string, macAddress: string): Promise<number> => {
const info = await deviceInfo(axiosInstance)(jwtToken, macAddress);
return info.status.temperatures.enviroment;
};

/**
* Get target temperature value.
*/
const getTargetTemperature =
(axiosInstance: AxiosInstance) =>
async (jwtToken: string, macAddress: string): Promise<number> => {
const info = await deviceInfo(axiosInstance)(jwtToken, macAddress);
return info.nvm.user_parameters.enviroment_1_temperature;
};

const configure = (baseURL: string = API_URL) => {
const axiosInstance = axios.create({ baseURL });
const deviceInfoInstance = deviceInfo(axiosInstance);
const setPowerInstance = setPower(axiosInstance);
const setPowerOffInstance = setPowerOff(axiosInstance);
const setPowerOnInstance = setPowerOn(axiosInstance);
const getPowerInstance = getPower(axiosInstance);
const getEnvironmentTemperatureInstance =
getEnvironmentTemperature(axiosInstance);
const getTargetTemperatureInstance = getTargetTemperature(axiosInstance);
return {
deviceInfo: deviceInfoInstance,
setPower: setPowerInstance,
setPowerOff: setPowerOffInstance,
setPowerOn: setPowerOnInstance,
getPower: getPowerInstance,
getEnvironmentTemperature: getEnvironmentTemperatureInstance,
getTargetTemperature: getTargetTemperatureInstance,
};
};

Expand Down

0 comments on commit b45735f

Please sign in to comment.