import * as Interfaces from '../Interfaces/interfaces'; import { CommonHelper } from '../common/helper'; import { SqsHelper } from '../common/sqsHelper'; import * as CONFIG from '../common/config'; import { LOGGER_TAG } from '../common/config'; //import { SmartDeviceModel } from '../model/SmartDeviceModel' import { NoiceDeviceModel } from '../model/NoiseDeviceModel' let noiseSensorModel = new NoiceDeviceModel() export class NoiseSensor implements Interfaces.NoiseSensorCommandsInterface { private sqsHelper:SqsHelper; constructor() { this.sqsHelper = new SqsHelper(); } async processEvent(event: any) { console.log(LOGGER_TAG + "Start of processEvent with Payload" + JSON.stringify(event)); let response : Interfaces.ResponseObject ; try { // Step 1: switch (event.typeOfOperation) { case 'UNLINK': response = await this.unLinkSmartDevice(event); break; default: console.log(LOGGER_TAG + "Unsupported Type of operation : "+ event.typeOfOperation); response.status = false; response.errorMessage = "Unsupported Type of operation : "+ event.typeOfOperation; break; } console.log(LOGGER_TAG + "Response from smart device "+ JSON.stringify(response)); } catch ( error ) { console.log(LOGGER_TAG + "Error in core lock function call " + JSON.stringify(error)); CommonHelper.throwError(error); } return response; } async unLinkSmartDevice(payload: any) { // let res:Interfaces.ResponseObject; try { let res:any; console.log('>>>>>>>>>>> unlinknoiceDeviceFromProperty Function With Payload: ', JSON.stringify(payload)); // Step1:Get noiseDeviceList let LinkedNoiseDeviceList:any = await noiseSensorModel.getLinkedNoiseDevice({hostId:payload.hostId,propertyId:payload.propertyId,isDeleted: false}) console.log(">>>>>>> LinkedNoiseDeviceList response :", LinkedNoiseDeviceList); if(LinkedNoiseDeviceList.length > 0){ let currentTime = CommonHelper.getCurrentUTCTime(); for(let i =0 ; i < LinkedNoiseDeviceList.length ; i++){ //console.log("ooooooooooooooooooooooooooooo",LinkedNoiseDeviceList[i].hostId) let unlinkNoiseSensorRes = await noiseSensorModel.unlinkNoiseSensorMapping({ conditions: { deviceId: LinkedNoiseDeviceList[i].deviceId, propertyId:payload.propertyId, hostId: payload.hostId }, data: { isDeleted: true, deletedDate: currentTime, modifiedDate:currentTime } }); res = unlinkNoiseSensorRes; } } return res; } catch (error) { console.log(">>>>>>>>>>err occur in unlinkNoiceDeviceFromProperty ", JSON.stringify(error)); return CommonHelper.getResponseObj(true); } } /** * @desc Method to push a entry into SQS * @param payload * @returns {object} * @author Neeraj Negi */ async pushToSQS(payload: any): Promise { console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:NoiseSensor <<< Function: pushToSQS started with payload : ' + JSON.stringify(payload)); try { let deviceSqsPayload: any = { propertyId: payload.propertyId, userId: payload.hostId, deviceType: "CONTACT_SENSOR", deviceId: payload.mappingId, action: "removeDevice" } console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:NoiseSensor <<< Function: pushToSQS deviceSqsPayload :====', deviceSqsPayload); return await this.sqsHelper.sendDeviceMessageToSqs(deviceSqsPayload); } catch (err) { console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:NoiseSensor <<< Function: pushToSQS Error: ' + JSON.stringify(err)); CommonHelper.throwError(err); } } }