def create_action(actionData): print("Creating Action") if not 'actionName' in actionData or not 'actionCommands' in actionData: raise ParameterException( 400, "Invalid Parameters: missing actionName or actionCommands") #Validate all commands before create for command in actionData['actionCommands']: if not 'paramID' in command or not 'paramValue' in command: raise ParameterException( 400, "Invalid actionCommand: missing paramID or paramValue") uid = uuid().hex nowtime = datetime.now().isoformat() action = { 'uuid': uid, 'actionName': actionData['actionName'], 'actionCommands': actionData['actionCommands'], 'created_at': nowtime, 'updated_at': nowtime } actions_table().put_item(Item=action) response = { "isBase64Encoded": "false", "statusCode": 200, "body": json.dumps(action) } return response
def create_device(deviceData, roomID): print("Creating Device") #ValidateInput if not 'deviceName' in deviceData: raise ParameterException(400, "Invalid Parameter: Missing deviceName") if not 'deviceType' in deviceData: raise ParameterException(400, "Invalid Parameter: Missing deviceType") if not 'parameters' in deviceData: raise ParameterException(400, "Invalid Parameter: Missing parameters") uid = uuid().hex nowtime = datetime.now().isoformat() device = { 'uuid': uid, 'roomID': roomID, 'deviceName': deviceData['deviceName'], 'deviceType': deviceData['deviceType'], 'created_at': nowtime, 'updated_at': nowtime } params = create_parameters(deviceData['parameters'], uid) devices_table().put_item(Item=device) device['path'] = "/devices/" + uid device['parameters'] = params response = { "isBase64Encoded": "false", "statusCode": 200, "body": json.dumps(device) } return response
def load_action(actionID): if not actionID: raise ParameterException(400, "Invalid parameters: missing actionID") response = actions_table().query( KeyConditionExpression=Key('uuid').eq(actionID)) if len(response['Items']) != 1: raise ParameterException(404, "Action not found") return response['Items'][0]
def load_parameter(paramID, deviceID): if not paramID: raise ParameterException(404, "Missing ParameterID") response = params_table().query(KeyConditionExpression=Key('uuid').eq(paramID)) if len(response['Items'])!=1: raise ParameterException(404, "Parameter Not Found") parameter = response['Items'][0] if deviceID and parameter['deviceID'] != deviceID: #wrong device ID raise ParameterException(404, "Device parameter not found") return parameter
def get_room_devices(roomID): print("Getting Room Devices") table = devices_table() if not roomID: raise ParameterException(400, "Invalid Parameter: Missing roomID") #This is inefficient because we are scaning the full table, and then removing results. #A query would be better, but then we would need a local secondary index. #Since there will be a limited number of devices (<50), I am not worrying about it. result = table.scan(FilterExpression=Key('roomID').eq(roomID)) devices = result['Items'] while 'LastEvaluateKey' in result: result = table.scan(ExclusiveStartKey=result['LastEvaluateKey']) devices += result['Items'] for device in devices: params = get_parameters(device['uuid']) device['parameters'] = params response = { "isBase64Encoded": "false", "statusCode": 200, "body": json.dumps(devices) } return response
def create_parameters(parameters, deviceID): print("Creating device parameters") #Validate Parameters for parameter in parameters: if not ('paramName' in parameter and 'paramType' in parameter and 'paramActions' in parameter): raise ParameterException( 400, "Invalid Parameter: Device Parameters do not include all required fields. Need paramName, paramType, paramActions" ) parameters_table = get_table_ref('PARAMETERS') nowtime = datetime.now().isoformat() newParams = [] with parameters_table.batch_writer() as batch: for parameter in parameters: uid = uuid().hex paramItem = { 'uuid': uid, 'deviceID': deviceID, 'paramName': parameter['paramName'], 'paramType': parameter['paramType'], 'paramActions': parameter['paramActions'], 'created_at': nowtime, 'updated_at': nowtime } batch.put_item(Item=paramItem) newParams.append(paramItem) return newParams
def update_device(deviceID, deviceData): print("Updating Device") if not deviceID: raise ParameterException(404, "Missing Device ID") nowtime = datetime.now().isoformat() updateExpressions = [] attributeValues = {} if 'deviceName' in deviceData: updateExpressions.append("deviceName = :n") attributeValues[':n'] = deviceData['deviceName'] if 'deviceType' in deviceData: updateExpressions.append("deviceType = :t") attributeValues[':t'] = deviceData['deviceType'] if 'roomID' in deviceData: updateExpressions.append("roomID = :r") attributeValues[':r'] = deviceData['roomID'] print if len(updateExpressions) < 1: #error if not updating anything raise ParameterException(400, "Not updating any properties.") #update time updateExpressions.append("updated_at = :u") attributeValues[':u'] = datetime.now().isoformat() updateExpressionStr = "set " + (",".join(updateExpressions)) print(updateExpressionStr) print(attributeValues) result = devices_table().update_item( Key={'uuid': deviceID}, UpdateExpression=updateExpressionStr, ExpressionAttributeValues=attributeValues) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"message\": \"Device updated\"}" } return response
def update_condition(conditionID, conditionData): print("Updating Condition") condition = load_condition(conditionID) #verifies condition exists comparisonType = conditionData[ 'comparisonType'] if 'comparisonType' in conditionData else condition[ 'comparisonType'] updateExpressions = [] attributeValues = {} if 'conditionName' in conditionData: updateExpressions.append("conditionName = :n") attributeValues[':n'] = conditionData['conditionName'] if 'actionID' in conditionData: updateExpressions.append("actionID = :a") attributeValues[':a'] = conditionData['actionID'] if 'paramID' in conditionData: updateExpressions.append("paramID = :p") attributeValues[':p'] = conditionData['paramID'] if 'comparison' in conditionData: updateExpressions.append("comparison = :c") attributeValues[':c'] = conditionData['comparison'] if 'comparisonValue' in conditionData and comparisonType == 'static': updateExpressions.append("comparisonValue = :v") attributeValues[':v'] = conditionData['comparisonValue'] if 'comparisonType' in conditionData: updateExpressions.append("comparisonType = :t") attributeValues[':t'] = conditionData['comparisonType'] if 'comparisonParameter' in conditionData and comparisonType == 'dynamic': updateExpressions.append("comparisonParameter = :t") attributeValues[':t'] = conditionData['comparisonParameter'] if 'tolerance' in conditionData: updateExpressions.append("tolerance = :o") attributeValues[':o'] = conditionData["tolerance"] if len(updateExpressions) < 1: #error if not updating anything raise ParameterException(400, "Nothing to update") #update time updateExpressions.append("updated_at = :u") attributeValues[':u'] = datetime.now().isoformat() updateExpressionStr = "set " + (",".join(updateExpressions)) print(updateExpressionStr) result = conditions_table().update_item( Key={'uuid': conditionID}, UpdateExpression=updateExpressionStr, ExpressionAttributeValues=attributeValues) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"message\": \"Condition updated\"}" } return response
def update_room(roomID, roomData): print(" Starting Update Room Lambda Function") if not roomID: raise ParameterException(400, "Invalid Parameter: Missing roomID in path") if not 'roomName' in roomData: raise ParameterException(400, "Invalid Parameters: Missing roomName") nowtime = datetime.now().isoformat() result = rooms_table().update_item( Key={'uuid': roomID}, UpdateExpression="set roomName = :n", ExpressionAttributeValues={':n': roomData['roomName']}) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"message\": \"Room updated\"}" } return response
def update_action(actionID, actionData): print("Updating Action") if not actionID: raise ParameterException(400, "Invalid Parameters: Missing actionID") old_action = load_action(actionID) #Makes sure action exists. updateExpressions = [] attributeValues = {} if 'actionName' in actionData: updateExpressions.append("actionName = :n") attributeValues[':n'] = actionData['actionName'] if 'actionCommands' in actionData: if not validateParamters(actionData['actionCommands']): #new parameters do not validate raise ParameterException( 400, "Invalid Action Parameters, missing paramID or paramValue") updateExpressions.append("actionCommands = :c") attributeValues[':c'] = actionData['actionCommands'] if len(updateExpressions) < 1: #error if not updating anything raise ParameterException(400, "Invalid Parameters, nothing to update") #update time updateExpressions.append("updated_at = :u") attributeValues[':u'] = datetime.now().isoformat() updateExpressionStr = "set " + (",".join(updateExpressions)) result = actions_table().update_item( Key={'uuid': actionID}, UpdateExpression=updateExpressionStr, ExpressionAttributeValues=attributeValues) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"message\": \"Action updated\"}" } return response
def delete_room(roomID): print("Deleting Room") if not roomID: raise ParameterException(400, "Invalid Paramters: Missing roomID in path") response = rooms_table().delete_item(Key={'uuid': roomID}) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"message\": \"Room Deleted.\"}" } return response
def create_condition(conditionData): print("Creating Condition ") if not 'conditionName' in conditionData: raise ParameterException(400, "Invalid Parameters: Missing conditionName") if not 'actionID' in conditionData: raise ParameterException(400, "Invalid Parameters: Missing actoinID") if not 'paramID' in conditionData: raise ParameterException(400, "Invalid Parameters: Missing paramID") if not 'comparison' in conditionData: raise ParameterException(400, "Invalid Parameters: Missing comparison") if not 'comparisonType' in conditionData: conditionData['comparisonType'] = 'static' #Make sure condition makes sense if conditionData['comparisonType'] == 'dynamic': if not 'comparisonParameter' in conditionData: raise ParameterException( 400, "Invalid Parameters: Missing comparisonParameter for dynamic comparison" ) else: conditionData[ 'comparisonType'] = 'static' #make sure it isn't another value if not 'comparisonValue' in conditionData: raise ParameterException( 400, "Invalid Parameters: Missing comparisonValue for static comparison" ) uid = uuid().hex nowtime = datetime.now().isoformat() condition = { 'uuid': uid, 'actionID': conditionData['actionID'], 'conditionName': conditionData['conditionName'], 'paramID': conditionData['paramID'], 'comparison': conditionData['comparison'], 'comparisonType': conditionData['comparisonType'], 'created_at': nowtime, 'updated_at': nowtime } if conditionData['comparisonType'] == 'dynamic': condition['comparisonParameter'] = conditionData['comparisonParameter'] else: condition['comparisonValue'] = conditionData['comparisonValue'] if 'tolerance' in conditionData: condition['tolerance'] = conditionData['tolerance'] conditions_table().put_item(Item=condition) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"condition\": " + json.dumps(condition) + " }" } return response
def get_room(roomID): print("Getting Room") if not roomID: raise ParameterException(400, "Invalid Parameter: Missing roomID in path") room = load_room(roomID) if room == None: response = { "isBase64Encoded": "false", "statusCode": 404, "body": "{\"errorMessage\": \"Room not found.\"}" } else: response = { "isBase64Encoded": "false", "statusCode": 200, "body": json.dumps(room) } return response
def update_parameter(paramID, deviceID, paramData): print("Updating Parameter") parameter = load_parameter(paramID,deviceID) if not 'paramValue' in paramData: raise ParameterException(400, "Invalid parameters: Missing paramValue") nowtime = datetime.now().strftime('%x-%X') params_table().update_item( Key={'uuid': paramID}, UpdateExpression="set paramValue = :v, updated_at = :t", ExpressionAttributeValues={':v': paramData['paramValue'], ':t': nowtime} ) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"Messsage\": \"Parameter Updated\"}" } return response
def create_room(roomData): print("Creating Room") uid = uuid().hex nowtime = datetime.now().isoformat() print("UID is " + uid) if 'roomName' in roomData: room = { 'uuid': uid, 'roomName': roomData['roomName'], 'created_at': nowtime, 'updated_at': nowtime } rooms_table().put_item(Item=room) response = { "isBase64Encoded": "false", "statusCode": 200, "body": json.dumps(room) } else: raise ParameterException(400, "Invalid Parameters: Missing roomName") return response
def update_device_params(deviceID, params): print("Updating Device Parameters") nowtime = datetime.now().strftime('%x-%X') #verify params for param in params: if not 'uuid' in param or not 'paramValue' in param: raise ParameterException(400, "Invalid Paramters: missing uuid or paramValue") for param in params: params_table().update_item( Key={'uuid': param['uuid']}, UpdateExpression="set paramValue = :v, updated_at = :t", ExpressionAttributeValues={':v': param['paramValue'], ':t': nowtime} ) response = { "isBase64Encoded": "false", "statusCode": 200, "body": "{\"Messsage\": \"Parameters Updated\"}" } return response