-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWebSocketsCirculatorManager.ts
108 lines (92 loc) · 3.96 KB
/
WebSocketsCirculatorManager.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
const { CirculatorSDK, CSApiClient } = require("exports-loader?window!./bundle.js")
import * as _ from "underscore"
import authenticationService from "./authenticationService"
import CirculatorProgram from "./CirculatorProgram"
import { appConfig, circulatorConnectionStates, csConfig, routingConfig } from "./constants"
import rootLogger from "./rootLogger"
import WebSocketConnectionProvider from "./WebSocketConnectionProvider"
class WebSocketsCirculatorManager {
public currentCirculatorClient: any
public sdkCirculatorManager: any
private userToken: string
private scanTimer = null
private circulatorConnectionState: any
private cachedFindAllCirculators
private cookHistoryApi: any
public initiateCirculatorManager(userInfo) {
this.userToken = userInfo.token
const applicationAddressHex = authenticationService.getCallerAddress(this.userToken)
const connectionProviders = [new WebSocketConnectionProvider(applicationAddressHex)]
this.sdkCirculatorManager = new CirculatorSDK.CirculatorManager({
routingConfig,
connectionProviders,
logger: rootLogger,
callerAddress: applicationAddressHex,
localCirculatorStorage: null,
apiConfig: csConfig,
userAuthenticationToken: this.userToken,
})
this.cookHistoryApi = new CSApiClient.CookHistoryApi(csConfig.chefstepsEndpoint, (() => this.userToken), rootLogger)
}
public resetScanTimer() {
this.scanTimer && window.clearTimeout(this.scanTimer)
this.scanTimer = null
}
public setCirculatorConnectionState(e) {
this.circulatorConnectionState = e
}
public updateCurrentCirculatorClient(newClient) {
this.currentCirculatorClient = newClient
this.currentCirculatorClient.setActive(!0)
}
public async circulatorScan() {
this.resetScanTimer()
return this.sdkCirculatorManager.discoverCirculators().then((e) => {
if (_.isEmpty(e) && !this.currentCirculatorClient) {
const scanInterval = appConfig.scanIntervalMilliseconds
this.scanTimer = window.setTimeout(() => {
return null == this.currentCirculatorClient ? this.circulatorScan() : undefined
}, scanInterval)
} else if (!this.currentCirculatorClient) {
this.setCirculatorConnectionState(circulatorConnectionStates.jouleFound)
}
}).progress(function(newClient) {
return newClient.once("paired", function(e) {
return this.updateCurrentCirculatorClient(newClient)
})
})
}
public async findAllCirculators(shouldRefresh) {
if (shouldRefresh || !this.cachedFindAllCirculators) {
return this.sdkCirculatorManager.findAllCirculators().then((e) => {
this.cachedFindAllCirculators = e
return this.cachedFindAllCirculators
})
} else {
this.cachedFindAllCirculators = this.sdkCirculatorManager.knownCirculators
return this.cachedFindAllCirculators
}
}
public async findLastAccessedCirculator() {
this.setCirculatorConnectionState(circulatorConnectionStates.connecting)
return this.findAllCirculators(true).then((n) => {
const lastAccessedCirculator = this.sdkCirculatorManager.findLastAccessedCirculator(n)
if (lastAccessedCirculator) {
lastAccessedCirculator.dataObserver.start()
this.updateCurrentCirculatorClient(lastAccessedCirculator)
} else {
const jouleFound = this.circulatorConnectionState === circulatorConnectionStates.jouleFound
this.setCirculatorConnectionState(jouleFound ? jouleFound : circulatorConnectionStates.unpaired)
}
})
}
public async startProgram(program: {setPoint: number, cookTime?: number, programType?: string, programMetadata: any}) {
const circulatorProgram = new CirculatorProgram(program)
this.currentCirculatorClient.startProgram(circulatorProgram)
this.cookHistoryApi.create(circulatorProgram.asApiPersistable())
}
public async stopProgram() {
this.currentCirculatorClient.stopProgram()
}
}
export default WebSocketsCirculatorManager