事件受信API(Event reception API)
coding: utf-8\n"""イベント受集API(Event reception API)."""
from basecommon.websocket_service.event_data_util import EventDataUtil from basecommon.util.api_common import ApiCommon from gpfframework.util.log_util import GpfLogging
import basecommon.constants.equipment_const as const import json import boto3 import os
def lambda_handler(event, context): # ワームスタンバイ用のPayloadの場合、returnする。 # In case of Payload for warm standby, return. if event.get('warm_standby_polling') is True: return """イベント受集API(Event reception API).""" logger = GpfLogging().get_logger('EmergencyOpeEventsReceive') logger.info('START', event, context) logger.debug('REQUEST', event, context)
if event.get('Records') is None:
# 引数チェック
# Argument check
logger.error('ERRORINFO', 'Records does not exist.')
return
client = boto3.client('sqs')
queue_url = os.environ.get('SQS_URL')
event_data_util = EventDataUtil()
# 1件ごとに処理させる
# Process each case
for record in event.get('Records'):
receipt_handle = record.get('receiptHandle')
body = json.loads(record.get('body'))
events = json.loads(body['body'])
# TopEntityを取得(Get TopEntity)
equipment_top_entity_map = dict()
try:
equipment_ids = set()
for status in events['events']:
if status.get('code') == "GPF_COV_NORMAL_6093":
equipment_ids.add(status.get('sId'))
if equipment_ids:
response = event_data_util.equipment_util.get_spec_data(list(equipment_ids))
if not response['is_success']:
error = ApiCommon.get_error_state(response['body'])
logger.error('ERRORINFO', error['err_msg'])
elif response['status'] == 200:
for item in response['body']['items']:
equipment_top_entity_map[item['equipmentId']] = item['topEntity']
except Exception as e:
logger.error('ERRORINFO', str(e.args))
for status in events['events']:
payload = json.dumps(status)
if status.get('code') == "GPF_COV_NORMAL_6093":
top_entity = equipment_top_entity_map.get(status.get('sId'))
if not top_entity:
logger.error('ERRORINFO', '{} could not find any equipment data'.format(status.get('sId')))
continue
if top_entity != const.TOP_ENTITY_OUTDOOR:
continue
# Asynchronous Lambda function call
try:
logger.debug('emergencyOperationInformationNotification call code:', status.get('code'))
boto3.client('lambda').invoke(FunctionName=os.environ.get('GPF_ENV') + '-emergencyOperationInformationNotification', InvocationType='Event', Payload=payload)
except Exception as e:
logger.error('ERRORINFO', str(e.args))
else:
# Asynchronous Lambda function call
try:
logger.debug('code:', status.get('code'))
boto3.client('lambda').invoke(FunctionName=os.environ.get('GPF_ENV') + '-emergencyOperationStateNotification', InvocationType='Event', Payload=payload)
except Exception as e:
logger.error('ERRORINFO', str(e.args))
try:
# SQSメッシーキューの削除(Delete SQS message queue)
client.delete_message(QueueUrl=queue_url, ReceiptHandle=receipt_handle)
except Exception as e:
logger.error('ERRORINFO', e)
return
logger.info('END')
return
原文地址: https://www.cveoy.top/t/topic/p2jm 著作权归作者所有。请勿转载和采集!