@@ -5,7 +5,7 @@ import { Preparation } from '../../preparation/preparation';
55import { IXeniaParams } from '../../../interfaces/preparationDevices/iXeniaParams' ;
66import { UILog } from '../../../services/uiLog' ;
77import { CapacitorHttp , HttpResponse } from '@capacitor/core' ;
8- import moment from 'moment ' ;
8+ import { sleep } from '../../devices ' ;
99
1010export 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 ) {
0 commit comments