import { DataPointValue , MindConnectAgent , MindsphereStandardEvent , retry , TimeStampedDataPoint } from ".." ; ( async function ( ) { const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; const configuration = require ( "../../agentconfig.json" ) ; const agent = new MindConnectAgent ( configuration ) ; const log = ( text ) => { console . log ( `[${ new Date ( ) . toISOString ( ) }] ${ text . toString ( ) }` ) ; } ; const RETRYTIMES = 5 ; if ( ! agent . IsOnBoarded ( ) ) { await retry ( RETRYTIMES , ( ) => agent . OnBoard ( ) ) ; log ( "Agent onboarded" ) ; } const targetAssetId = "1234567...." ; const targetAssetType = `${ agent . GetTenant ( ) }.Engine` ; const config = await agent . GenerateDataSourceConfiguration ( targetAssetType ) ; await agent . PutDataSourceConfiguration ( config ) ; const mappings = await agent . GenerateMappings ( targetAssetId ) ; await agent . PutDataMappings ( mappings ) ; await agent . ConfigureAgentForAssetId ( targetAssetId ) ; if ( ! agent . HasDataSourceConfiguration ( ) ) { await retry ( RETRYTIMES , ( ) => agent . GetDataSourceConfiguration ( ) ) ; log ( "Configuration aquired" ) ; } for ( let index = 0 ; index < 5 ; index ++ ) { try { log ( `Iteration : ${ index }` ) ; const values = [ { dataPointId : "DP-Temperature" , qualityCode : "0" , value : ( Math . sin ( index ) * ( 20 + ( index % 2 ) ) + 25 ) . toString ( ) , } , { dataPointId : "DP-Pressure" , qualityCode : "0" , value : ( Math . cos ( index ) * ( 20 + ( index % 25 ) ) + 25 ) . toString ( ) , } , { dataPointId : "DP-Humidity" , qualityCode : "0" , value : ( ( index + 30 ) % 100 ) . toString ( ) , } , { dataPointId : "DP-Acceleration" , qualityCode : "0" , value : ( 1000.0 + index ) . toString ( ) , } , { dataPointId : "DP-Frequency" , qualityCode : "0" , value : ( 60.0 + index * 0.1 ) . toString ( ) , } , { dataPointId : "DP-Displacement" , qualityCode : "0" , value : ( index % 10 ) . toString ( ) , } , { dataPointId : "DP-Velocity" , qualityCode : "0" , value : ( 50.0 + index ) . toString ( ) , } , ] ; await retry ( RETRYTIMES , ( ) => agent . PostData ( values ) ) ; log ( "Data posted" ) ; await sleep ( 1000 ) ; const event = { entityId : agent . ClientId ( ) , sourceType : "Event" , sourceId : "application" , source : "Meowz" , severity : 20 , timestamp : new Date ( ) . toISOString ( ) , description : "Test" , } ; await retry ( RETRYTIMES , ( ) => agent . PostEvent ( event ) ) ; log ( "event posted" ) ; await sleep ( 1000 ) ; await agent . UploadFile ( agent . ClientId ( ) , "custom/mindsphere/path/package.json" , "package.json" , { retry : RETRYTIMES , description : "File uploaded with MindConnect-NodeJS Library" , chunk : true , } ) ; log ( "file uploaded" ) ; await sleep ( 1000 ) ; const yesterday = new Date ( ) ; yesterday . setDate ( yesterday . getDate ( ) - 1 ) ; const bulk = [ { timestamp : yesterday . toISOString ( ) , values : [ { dataPointId : "DP-Temperature" , qualityCode : "0" , value : "10" } , { dataPointId : "DP-Pressure" , qualityCode : "0" , value : "10" } , ] , } , { timestamp : new Date ( ) . toISOString ( ) , values : [ { dataPointId : "DP-Temperature" , qualityCode : "0" , value : "10" } , { dataPointId : "DP-Pressure" , qualityCode : "0" , value : "10" } , ] , } , ] ; await retry ( RETRYTIMES , ( ) => agent . BulkPostData ( bulk ) ) ; log ( "bulk data uploaded" ) ; await sleep ( 1000 ) ; } catch ( err ) { console . error ( err ) ; } } } ) ( ) ;