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'; let contactSensorModel = new SmartDeviceModel() export class ContactSensor implements Interfaces.ContactSensorCommandsInterface { private sqsHelper:SqsHelper; constructor() { this.sqsHelper = new SqsHelper(); } async processEvent(event: any) { console.log(LOGGER_TAG + "Start of [contactSensor]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 contactSensor device "+ JSON.stringify(response)); } catch ( error ) { console.log(LOGGER_TAG + "Error in contactSensor device function call " + JSON.stringify(error)); CommonHelper.throwError(error); } return response; } async unLinkSmartDevice(payload: any) { //let res:Interfaces.ResponseObject; try { console.log('>>>>>>>>>>> unlinkcontactSensorFromProperty Function With Payload: ', JSON.stringify(payload)); // Step1: let currentTime = CommonHelper.getCurrentUTCTime(); let unlinkContactSensorRes = await contactSensorModel.unlinkContactSensorMapping({ "conditions": { "id": payload.mappingId,"propertyId":payload.propertyId }, "data": { "isDeleted": 1, deletedDate: currentTime, modifiedDate:currentTime } }); // Step2: Lets push an entry to SQS if(unlinkContactSensorRes[0]){ // if unlinking done successfully await this.pushToSQS(payload); } return unlinkContactSensorRes; } catch (error) { console.log(">>>>>>>>>>err occur in contactSensor ", 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:ContactSensor <<< 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:ContactSensor <<< Function: pushToSQS deviceSqsPayload :====', deviceSqsPayload); return await this.sqsHelper.sendDeviceMessageToSqs(deviceSqsPayload); } catch (err) { console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:ContactSensor <<< Function: pushToSQS Error: ' + JSON.stringify(err)); CommonHelper.throwError(err); } } }