import { ActivityInfoModel , ActivitySourceDataModel , ActivityStreamsModel , AnalysisDataModel , AthleteSnapshotModel , Gender , UserSettingsModel } from "@elevate/shared/models" ; import { AppResourcesModel } from "../models/app-resources.model" ; import { ComputeActivityThreadMessageModel } from "../models/compute-activity-thread-message.model" ; import { VacuumProcessor } from "./vacuum-processor" ; import { AthleteSnapshotResolver } from "@elevate/shared/resolvers" ; const ComputeAnalysisWorker = require ( "worker-loader?inline!./workers/compute-analysis.worker" ) ; export class ActivityProcessor { public static cachePrefix = "elevate_activity_" ; protected appResources : AppResourcesModel ; protected vacuumProcessor : VacuumProcessor ; protected athleteModelResolver : AthleteSnapshotResolver ; protected zones : any ; protected activityInfo : ActivityInfoModel ; protected computeAnalysisThread : Worker ; protected userSettings : UserSettingsModel ; constructor ( vacuumProcessor , athleteModelResolver , appResources , userSettings , activityInfo ) { this . vacuumProcessor = vacuumProcessor ; this . athleteModelResolver = athleteModelResolver ; this . appResources = appResources ; this . userSettings = userSettings ; this . activityInfo = activityInfo ; this . zones = this . userSettings . zones ; } public getAnalysisData ( activityInfo , bounds , callback ) { if ( ! this . activityInfo . type ) { console . error ( "No activity type set for ActivityProcessor" ) ; } setTimeout ( ( ) => { this . vacuumProcessor . getActivityStream ( this . activityInfo , ( activitySourceData , activityStream , athleteWeight , athleteGender , hasPowerMeter ) => { const onDate = ( this . activityInfo . startTime ) ? this . activityInfo . startTime : new Date ( ) ; const athleteSnapshot = this . athleteModelResolver . resolve ( onDate ) ; if ( ! this . activityInfo . isOwner ) { athleteSnapshot . athleteSettings . weight = athleteWeight ; athleteSnapshot . gender = athleteGender ; } console . log ( "Compute with AthleteSnapshotModel" , JSON . stringify ( athleteSnapshot ) ) ; this . computeAnalysisThroughDedicatedThread ( hasPowerMeter , athleteSnapshot , activitySourceData , activityStream , bounds , ( resultFromThread ) => { callback ( athleteSnapshot , resultFromThread ) ; } ) ; } ) ; } ) ; } private computeAnalysisThroughDedicatedThread ( hasPowerMeter , athleteSnapshot , activitySourceData , activityStream , bounds , callback ) { this . computeAnalysisThread = new ComputeAnalysisWorker ( ) ; const threadMessage = { activityType : this . activityInfo . type , supportsGap : this . activityInfo . supportsGap , isTrainer : this . activityInfo . isTrainer , appResources : this . appResources , userSettings : this . userSettings , isOwner : this . activityInfo . isOwner , athleteSnapshot : athleteSnapshot , hasPowerMeter : hasPowerMeter , activitySourceData : activitySourceData , activityStream : activityStream , bounds : bounds , returnZones : true } ; this . computeAnalysisThread . postMessage ( threadMessage ) ; this . computeAnalysisThread . onmessage = ( messageFromThread ) => { callback ( messageFromThread . data ) ; this . computeAnalysisThread . terminate ( ) ; } ; } }