-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.service.ts
112 lines (100 loc) · 3.68 KB
/
command.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* The {@link CommandService} uses the `/Command.htm` endpoint of the ProCon.IP
* pool controller to enable manual dosage.
* @packageDocumentation
*/
import axios, { AxiosPromise, Method } from 'axios';
import { AbstractService } from './abstract-service';
/**
* This enum can be used with the {@link CommandService.setDosage} method. But
* there are also shorthand wrappers for all states ({@link CommandService.setChlorineDosage},
* {@link CommandService.setPhPlusDosage}, {@link CommandService.setPhMinusDosage}) that can be used.
*/
export enum DosageTarget {
CHLORINE = 0, // eslint-disable-line no-unused-vars
PH_MINUS = 1, // eslint-disable-line no-unused-vars
PH_PLUS = 2, // eslint-disable-line no-unused-vars
}
/**
* The {@link CommandService} uses the `/Command.htm` endpoint of the ProCon.IP
* pool controller to turn on manual dosage for a given amount of time/seconds.
*/
export class CommandService extends AbstractService {
/**
* Specific service endpoint.
*
* A path relative to the {@link IServiceConfig.controllerUrl}.
*/
public _endpoint = '/Command.htm';
/**
* HTTP request method for this specific service endpoint.
* See: `axios/Method`
*/
public _method: Method = 'get';
/**
* Set manuel chlorine dosage for given amount of time in seconds.
*
* @param dosageTime Dosage duration in seconds.
*/
public async setChlorineDosage(dosageTime: number): Promise<number> {
return this.setDosage(DosageTarget.CHLORINE, dosageTime);
}
/**
* Set manuel pH minus dosage for given amount of time in seconds.
*
* @param dosageTime Dosage duration in seconds.
*/
public async setPhMinusDosage(dosageTime: number): Promise<number> {
return this.setDosage(DosageTarget.PH_MINUS, dosageTime);
}
/**
* Set manuel pH plus dosage for given amount of time in seconds.
*
* @param dosageTime Dosage duration in seconds.
*/
public async setPhPlusDosage(dosageTime: number): Promise<number> {
return this.setDosage(DosageTarget.PH_PLUS, dosageTime);
}
/**
* Set the desired relay state.
*
* @param dosageTarget Dosage target (0 = chlorine, 1 = pH minus, 2 = pH plus).
* @param dosageDuration Desired duration in seconds.
*/
public async setDosage(dosageTarget: DosageTarget, dosageDuration: number): Promise<number> {
for (let errors = 0; errors < 3; errors++) {
try {
return await this._setDosage(dosageTarget, dosageDuration);
} catch (e) {
this.log.debug(`Error sending relay control command: ${e}`);
}
}
return -1;
}
private async _setDosage(dosageTarget: DosageTarget, dosageDuration: number): Promise<number> {
return new Promise<number>((resolve, reject) => {
this.sendManualDosage(dosageTarget, dosageDuration)
.then((response) => {
this.log.info(`Command.htm response: ${JSON.stringify(response.data)}`);
this.log.info(`Command.htm status: (${response.status}) ${response.statusText}`);
if (response.status === 200) {
resolve(dosageDuration);
} else {
reject(
new Error(
`(${response.status}: ${response.statusText}) Error sending dosage control command: ${response.data}`,
),
);
}
})
.catch((e) => {
reject(new Error(`Error sending dosage control command: ${e.response ? e.response : e}`));
});
});
}
private sendManualDosage(dosageTarget: DosageTarget, dosageDuration: number): AxiosPromise {
const requestConfig = this.axiosRequestConfig;
requestConfig.url += `?MAN_DOSAGE=${dosageTarget},${Math.trunc(dosageDuration)}`;
return axios.request(requestConfig);
}
}