Exemplo n.º 1
0
    def post(self):
        #csv in post body of post request
        data = request.data.decode('utf-8')

        #save reading to CSV file
        with open(
                "metadata.csv",
                "a",
        ) as f:
            writer = csv.writer(f, delimiter=",")
            writer.writerow([time.ctime(), data])

    #prep data for db
        lines = data.strip().split('\n')
        for line in lines:
            #print(line)
            data = line.strip().split(',')
            gw_id = data[0]
            gw_site = data[1]
            gw_locate = data[2]
            gw_url = data[3]
            gw_dir = data[4]
            gw_port = data[5]

            gateway = GatewayModel(gw_id, gw_site, gw_locate, gw_url, gw_dir,
                                   gw_port)

        try:
            gateway.save_to_db()
        except:
            return {
                'message': 'An error occurred while creating the gateway'
            }, 500

        return gateway.json(), 201
Exemplo n.º 2
0
    def post(self):
        #data coming as form
        data = request.form
        gw_id = data['gw_id']

        if GatewayModel.find_by_id(gw_id):
            return {
                'message': "Gateway with id '{}' already exists.".format(gw_id)
            }, 400

        with open(
                "metadata.csv",
                "a",
        ) as f:
            writer = csv.writer(f, delimiter=",")
            writer.writerow([time.ctime(), gw_id, *data.values()])

        data = Gateway.request
        gateway = GatewayModel(**data)

        try:
            gateway.save_to_db()

        except:
            return {
                'message': 'An error occurred while creating the gateway'
            }, 500

        #return gateway.json(), 201
        return {'message': 'Gateway successfully added'}, 201
Exemplo n.º 3
0
	def patch(self):
		data = request.form
		gw_id = data['gw_id']
		gw_update = []
		gateway = GatewayModel.find_by_id(gw_id)
		if gateway is None:
			return {'message': "Gateway id {} doesn't exist. Please add gateway first".format(gw_id)}, 405

		if 'gw_model' in data:
			gateway.gw_model = data.get('gw_model')
			gw_update.append('gw_model')

		if 'gw_sw' in data:
			gateway.gw_sw = data.get('gw_sw')
			gw_update.append('gw_sw')

		if 'gw_lora_addr' in data:
			gateway.gw_lora_addr = data.get('gw_lora_addr')
			gw_update.append('gw_lora_addr')

		if 'gw_site' in data:
			gateway.gw_site = data.get('gw_site')
			gw_update.append('gw_site')

		if 'gw_locate' in data:
			gateway.gw_locate = data.get('gw_locate')
			gw_update.append('gw_locate')

		gateway.update_db()
		gw_update = " ".join(str(elem) for elem in gw_update)
		return {'message' : "Updated {} in Gateway id {}.".format(gw_update,gw_id)}
Exemplo n.º 4
0
    def delete(self):
        #gw_id sent via form
        data = request.form
        gw_id = data['gw_id']
        gateway = GatewayModel.find_by_id(gw_id)
        if gateway:
            gateway.delete_from_db()

        return {'message': 'Gateway deleted'}
Exemplo n.º 5
0
    def put(self):
        data = request.form
        gw_id = data['gw_id']

        gateway = GatewayModel.find_by_id(gw_id)

        if gateway is None:
            gateway = GatewayModel(gw_id, data['gw_site'], data['gw_locate'])
        else:
            gateway.gw_site = data['gw_site']
            gateway.gw_locate = data['gw_locate']

        gateway.save_to_db()

        return gateway.json()
Exemplo n.º 6
0
 def get(self, gw_id):
     gateway = GatewayModel.find_by_id(gw_id)
     if gateway:
         return gateway.json()
     return {'message': 'Gateway not found'}, 404
Exemplo n.º 7
0
    def put(self):
        data = request.form
        gw_id = data['gw_id']
        gateway = GatewayModel.find_by_id(gw_id)
        if gateway is None:
            gateway = GatewayModel(gw_id, **data)
        else:
            gateway.gw_id = data['gw_id']
            gateway.gw_model = data['gw_model']
            gateway.gw_sw = data['gw_sw']
            gateway.gw_lora_addr = data['gw_lora_addr']
            gateway.gw_site = data['gw_site']
            gateway.gw_locate = data['gw_locate']

        gateway.save_to_db()

        return gateway.json()