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 thermostatModel = new SmartDeviceModel() export class Thermostat implements Interfaces.ThermostatCommandsInterface { private sqsHelper:SqsHelper; constructor() { this.sqsHelper = new SqsHelper(); } /** * Function decides the operation which needs to be performed for the Access record and calls the appropriate function * @param event * @return Interfaces.ResponseObject * Steps * 1. Switch case based on typeOfOperation from the input event * 2. Call the model function from model. */ 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 core 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 { console.log('>>>>>>>>>>> unlinkThermostatFromProperty Function With Payload: ', JSON.stringify(payload)); // Step1: let currentTime = CommonHelper.getCurrentUTCTime(); let unlinkThermostatRes = await thermostatModel.unLinkThermostatMapping({ "conditions": { "id": payload.mappingId,"propertyId":payload.propertyId }, "data": { "isDeleted": 1, deletedDate: currentTime, modifiedDate:currentTime } }); if(unlinkThermostatRes[0]){ this.pushToSQS(payload); } return unlinkThermostatRes; } catch (error) { console.log(">>>>>>>>>>err occur in unlinkThermostatFromProperty ", 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:Thermostat <<< Function: pushToSQS started with payload : ' + JSON.stringify(payload)); try { let deviceSqsPayload: any = { propertyId: payload.propertyId, userId: payload.hostId, deviceType: "THERMOSTAT", deviceId: payload.mappingId, action: "removeDevice" } console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:Thermostat <<< Function: pushToSQS deviceSqsPayload :====', deviceSqsPayload); return await this.sqsHelper.sendDeviceMessageToSqs(deviceSqsPayload); } catch (err) { console.log('Lambda Package(smartdevices_unlink_from_property_lambda)>>>>>>>>>>> Class:Thermostat <<< Function: pushToSQS Error: ' + JSON.stringify(err)); CommonHelper.throwError(err); } } }