Skip to content

Commit bb31e2e

Browse files
committed
#936 - Make it possible to shut on / off machine
1 parent 97db859 commit bb31e2e

4 files changed

Lines changed: 111 additions & 3 deletions

File tree

src/app/preparation/preparation-popover-actions/preparation-popover-actions.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PREPARATION_STYLE_TYPE } from '../../../enums/preparations/preparationS
88
import { PreparationDeviceType } from '../../../classes/preparationDevice';
99
import { PREPARATION_FUNCTION_PIPE_ENUM } from '../../../enums/preparations/preparationFunctionPipe';
1010
import { SanremoYOUDevice } from '../../../classes/preparationDevice/sanremo/sanremoYOUDevice';
11+
import { XeniaDevice } from '../../../classes/preparationDevice/xenia/xeniaDevice';
1112

1213
@Component({
1314
selector: 'preparation-popover-actions',
@@ -56,6 +57,24 @@ export class PreparationPopoverActionsComponent implements OnInit {
5657
} catch (ex) {
5758
this.isMachineConnected = false;
5859
}
60+
} else if (
61+
this.data.connectedPreparationDevice?.type ===
62+
PreparationDeviceType.XENIA
63+
) {
64+
const device: XeniaDevice =
65+
this.data.getConnectedDevice() as XeniaDevice;
66+
try {
67+
if (device.deviceConnected()) {
68+
this.isMachineConnected = true;
69+
device.isMachineTurnedOn().then((isTurnedOn) => {
70+
this.isMachineTurnedOn = isTurnedOn;
71+
});
72+
} else {
73+
this.isMachineConnected = false;
74+
}
75+
} catch (ex) {
76+
this.isMachineConnected = false;
77+
}
5978
}
6079
}
6180
}

src/classes/preparationDevice/meticulous/meticulousDevice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class MeticulousDevice extends PreparationDevice {
4646
realtimeWaterFlow.brew_time = '';
4747
realtimeWaterFlow.timestamp = timestamp;
4848
realtimeWaterFlow.smoothed_weight = 0;
49-
realtimeWaterFlow.flow_value = shotEntry.flow;
49+
realtimeWaterFlow.flow_value = shotEntry.gravimetric_flow;
5050
realtimeWaterFlow.timestampdelta = 0;
5151

5252
newBrewFlow.realtimeFlow.push(realtimeWaterFlow);

src/classes/preparationDevice/xenia/xeniaDevice.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Preparation } from '../../preparation/preparation';
55
import { IXeniaParams } from '../../../interfaces/preparationDevices/iXeniaParams';
66
import { UILog } from '../../../services/uiLog';
77
import { CapacitorHttp, HttpResponse } from '@capacitor/core';
8-
import moment from 'moment';
8+
import { sleep } from '../../devices';
99

1010
export class XeniaDevice extends PreparationDevice {
1111
public scriptList: Array<{ INDEX: number; TITLE: string }> = [];
@@ -162,6 +162,94 @@ export class XeniaDevice extends PreparationDevice {
162162
}
163163
}
164164

165+
public async isMachineTurnedOn(): Promise<boolean> {
166+
try {
167+
let url = this.getPreparation().connectedPreparationDevice.url;
168+
if (this.apiVersion === 1) {
169+
url += '/overview';
170+
} else {
171+
url += '/api/v2/overview';
172+
}
173+
174+
const options = {
175+
url: url,
176+
connectTimeout: 5000,
177+
};
178+
179+
const response: HttpResponse = await CapacitorHttp.get(options);
180+
const responseJSON = await response.data;
181+
182+
return responseJSON.MA_STATUS === 1;
183+
// TODO Capacitor migration: The code before the migration didn't do
184+
// anything else, but there was unreachable code below it.
185+
// Please double check.
186+
} catch (error) {
187+
this.logError('Error in isMachineTurnedOn():', error);
188+
return false;
189+
}
190+
}
191+
192+
public async turnOnMachine() {
193+
let url = this.getPreparation().connectedPreparationDevice.url;
194+
let options: any;
195+
196+
// TODO Capacitor migration: It's very likely that this will be broken by
197+
// the migration, please test again.
198+
if (this.apiVersion === 1) {
199+
return;
200+
} else {
201+
url += '/api/v2/machine/control/';
202+
options = {
203+
url: url,
204+
data: JSON.stringify({ action: '1' }),
205+
headers: {
206+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
207+
},
208+
};
209+
}
210+
211+
try {
212+
const response = await CapacitorHttp.post(options);
213+
const responseJSON = await response.data;
214+
await sleep(500);
215+
const isTurnedOn = await this.isMachineTurnedOn();
216+
return isTurnedOn;
217+
} catch (error) {
218+
this.logError('Error in turnOnMachine():', error);
219+
return false;
220+
}
221+
}
222+
public async turnOffMachine() {
223+
let url = this.getPreparation().connectedPreparationDevice.url;
224+
let options: any;
225+
226+
// TODO Capacitor migration: It's very likely that this will be broken by
227+
// the migration, please test again.
228+
if (this.apiVersion === 1) {
229+
return;
230+
} else {
231+
url += '/api/v2/machine/control/';
232+
options = {
233+
url: url,
234+
data: JSON.stringify({ action: '0' }),
235+
headers: {
236+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
237+
},
238+
};
239+
}
240+
241+
try {
242+
const response = await CapacitorHttp.post(options);
243+
const responseJSON = await response.data;
244+
await sleep(500);
245+
const isTurnedOn = await this.isMachineTurnedOn();
246+
return !isTurnedOn;
247+
} catch (error) {
248+
this.logError('Error in turnOffMachine():', error);
249+
return false;
250+
}
251+
}
252+
165253
public async getScripts(): Promise<any> {
166254
let url = this.getPreparation().connectedPreparationDevice.url;
167255
if (this.apiVersion === 1) {

src/components/brews/brew-brewing-preparation-device/brew-brewing-preparation-device.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ export class BrewBrewingPreparationDeviceComponent implements OnInit {
725725
realtimeWaterFlow.brew_time = '';
726726
realtimeWaterFlow.timestamp = timestamp;
727727
realtimeWaterFlow.smoothed_weight = 0;
728-
realtimeWaterFlow.flow_value = shotEntry.flow;
728+
729+
realtimeWaterFlow.flow_value = shotEntry.gravimetric_flow;
729730
realtimeWaterFlow.timestampdelta = 0;
730731

731732
newBrewFlow.realtimeFlow.push(realtimeWaterFlow);

0 commit comments

Comments
 (0)