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 waterleakSensorModel = new SmartDeviceModel() export class WaterleakageSensor implements Interfaces.WaterleakSensorCommandsInterface { private sqsHelper:SqsHelper; constructor() { this.sqsHelper = new SqsHelper(); } async processEvent(event: any) { console.log(LOGGER_TAG + "Start of [unlinkWaterleakSensor]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 unlinkWaterleakSensor device "+ JSON.stringify(response)); } catch ( error ) { console.log(LOGGER_TAG + "Error in core device function call " + JSON.stringify(error)); CommonHelper.throwError(error); } return response; } async unLinkSmartDevice(payload: any) { //let res:Interfaces.ResponseObject; try { console.log('>>>>>>>>>>> unlinkWaterleakSensorFromProperty Function With Payload: ', JSON.stringify(payload)); // Step1: let currentTime = CommonHelper.getCurrentUTCTime(); let unlinkWaterleakSensorRes = await waterleakSensorModel.unlinkWaterleakSensor({ "conditions": { "id": payload.mappingId, "propertyId":payload.propertyId }, "data": { "isDeleted": 1, deletedDate:currentTime, modifiedDate:currentTime } }); if(unlinkWaterleakSensorRes[0]){ this.pushToSQS(payload); } return unlinkWaterleakSensorRes; } catch (error) { console.log(">>>>>>>>>>err occur in unlinkWaterleakSensor", 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:WaterleakageSensor <<< Function: pushToSQS started with payload : ' + JSON.stringify(payload)); try { let deviceSqsPayload: any = { propertyId: payload.propertyId, userId: payload.hostId, deviceType: "WATER_LEAKAGE_SENSOR", deviceId: payload.mappingId, action: "removeDevice" } console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:WaterleakageSensor <<< Function: pushToSQS deviceSqsPayload :====', deviceSqsPayload); return await this.sqsHelper.sendDeviceMessageToSqs(deviceSqsPayload); } catch (err) { console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:WaterleakageSensor <<< Function: pushToSQS Error: ' + JSON.stringify(err)); CommonHelper.throwError(err); } } }