diff --git a/cdk.json b/cdk.json index 6a8760f..92eaf3b 100644 --- a/cdk.json +++ b/cdk.json @@ -79,7 +79,9 @@ "orionEndpoint": "https://orion.dev.hamamatsu.makeour.city", "fiwareService": "pifaa_test", "fiwareServicePath": "", - "pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud" + "pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud", + "peopleFlowSensorId": "INFO.LOUNGE_00114116", + "environmentDataSensorId": "017fbf7850b5da0adc59c80b00000000" } }, "stg": { @@ -101,7 +103,9 @@ "orionEndpoint": "https://orion.stg.hamamatsu.makeour.city", "fiwareService": "", "fiwareServicePath": "", - "pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud" + "pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud", + "peopleFlowSensorId": "INFO.LOUNGE_00114116", + "environmentDataSensorId": "017fbf7850b5da0adc59c80b00000000" } }, "prd": { @@ -123,7 +127,9 @@ "orionEndpoint": "https://orion.hamamatsu.makeour.city", "fiwareService": "hamamatsu_pifaa", "fiwareServicePath": "", - "pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud" + "pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud", + "peopleFlowSensorId": "HAMAMATSU-CITY_00324084", + "environmentDataSensorId": "017ed23ee24032205aa7cb7700000000" } } } diff --git a/lambda/src/helper/secretsManager.ts b/lambda/src/helper/secretsManager.ts index b3ad254..557f754 100644 --- a/lambda/src/helper/secretsManager.ts +++ b/lambda/src/helper/secretsManager.ts @@ -18,11 +18,27 @@ export const getSecretFromCache = async (secretId: string): Promise => { 'X-Aws-Parameters-Secrets-Token': process.env.AWS_SESSION_TOKEN, } } - const response = await axios.get(requestEndpoint, requestOptions) - if (response.data.SecretString === undefined) { - return undefined + + // not ready to serve traffic, please wait エラーに対応してリトライ処理を追加 + const maxRetries = 3 + const retryDelay = 1000 + + let attempt = 0 + while (attempt < maxRetries) { + try { + const response = await axios.get(requestEndpoint, requestOptions) + if (response.data.SecretString === undefined) { + return undefined + } + return JSON.parse(response.data.SecretString) + } catch (error) { + console.log(`Attempt ${attempt} failed: ${error}`) + await new Promise(resolve => setTimeout(resolve, retryDelay)) + attempt++ + } } - return JSON.parse(response.data.SecretString) + console.log('Max retries reached. Throwing error.') + throw new Error('Failed to retrieve secret after maximum retries.') } /** diff --git a/lambda/src/server.pifaa.ts b/lambda/src/server.pifaa.ts index 20bc1c1..367cd01 100644 --- a/lambda/src/server.pifaa.ts +++ b/lambda/src/server.pifaa.ts @@ -12,9 +12,6 @@ dayjs.extend(utc); dayjs.extend(timezone); dayjs.tz.setDefault("Asia/Tokyo"); -// 人流センサーID -const peopleFlowSensorId = 'INFO.LOUNGE_00114116' - /** * Username / Password でPifaaにログインし、Cookie一式をSecrets Managerに格納するLambdaハンドラ (30分毎に実行) */ @@ -47,14 +44,13 @@ export const setPifaaCookie: Handler = async () => { export const importPeopleFlowHandler: Handler = async () => { console.log('定期的なジョブを実行中: ' + new Date()) - try { - const endpoint = process.env.PIFAA_ENDPOINT ?? '' + const endpoint = process.env.PIFAA_ENDPOINT ?? '' + const peopleFlowSensorId = process.env.SENSOR_ID ?? '' - console.log(process.env.SECRET_PIFAA_COOKIES) - - const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '') - const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '') + const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '') + const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '') + try { const currentTimestamp = Math.floor(Date.now() / 1e3) const roundTimestamp10Min = Math.floor(currentTimestamp / 600) * 600 // 10分単位切り下げ const timestamp_start = roundTimestamp10Min - 600 * 2 // 20分前 @@ -110,9 +106,6 @@ export const importPeopleFlowHandler: Handler = async () => { } } -// 環境センサーID -const environmentDataSensorId = '017fbf7850b5da0adc59c80b00000000' - /** * 10分おきに環境データを取得し、Orionに格納するLambdaハンドラ * 気温・湿度・二酸化炭素濃度 @@ -120,14 +113,13 @@ const environmentDataSensorId = '017fbf7850b5da0adc59c80b00000000' export const importEnvironmentDataHandler: Handler = async () => { console.log('定期的な環境データ取得ジョブを実行:', new Date()) - try { + const endpoint = process.env.PIFAA_ENDPOINT ?? '' + const environmentDataSensorId = process.env.SENSOR_ID ?? '' - const endpoint = process.env.PIFAA_ENDPOINT ?? '' - console.log(process.env.SECRET_PIFAA_COOKIES) - - const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '') - const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '') + const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '') + const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '') + try { const currentTimestamp = Math.floor(Date.now() / 1e3) const roundTimestamp10Min = Math.floor(currentTimestamp / 600) * 600 // 10分単位切り下げ const timestamp_start = roundTimestamp10Min - 600 * 1 // 10分前 diff --git a/lib/hamamatsu-pifaa-stack.ts b/lib/hamamatsu-pifaa-stack.ts index 2f78477..3e5d5a8 100644 --- a/lib/hamamatsu-pifaa-stack.ts +++ b/lib/hamamatsu-pifaa-stack.ts @@ -4,7 +4,7 @@ import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets' import { Runtime, ParamsAndSecretsLayerVersion, - ParamsAndSecretsVersions + ParamsAndSecretsVersions, } from 'aws-cdk-lib/aws-lambda' import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs' import { Secret } from 'aws-cdk-lib/aws-secretsmanager' @@ -20,6 +20,8 @@ export interface Context { fiwareService: string fiwareServicePath: string pifaaEndpoint: string + peopleFlowSensorId: string + environmentDataSensorId: string } interface HamamatsuPifaaProps extends cdk.StackProps { @@ -63,6 +65,7 @@ export class HamamatsuPifaaStack extends cdk.Stack { FIWARE_SERVICE: props.context.fiwareService, FIWARE_SERVICE_PATH: props.context.fiwareServicePath, PIFAA_ENDPOINT: props.context.pifaaEndpoint, + SENSOR_ID: props.context.peopleFlowSensorId, }, paramsAndSecrets, // Use AWS Parameters and Secrets Lambda Extension }) @@ -80,6 +83,7 @@ export class HamamatsuPifaaStack extends cdk.Stack { FIWARE_SERVICE: props.context.fiwareService, FIWARE_SERVICE_PATH: props.context.fiwareServicePath, PIFAA_ENDPOINT: props.context.pifaaEndpoint, + SENSOR_ID: props.context.environmentDataSensorId, }, paramsAndSecrets, })