Ejemplo n.º 1
0
    def post(self):
        #data coming as form data
        data = request.form
        ss_id = data['ss_id']
        if StationModel.find_by_id(ss_id):
            return {
                'message': "Station with id '{}' already exists.".format(ss_id)
            }, 400

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

        station = StationModel(**data)

        try:
            station.save_to_db()

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

        #return station.json(), 201
        return {'message': 'Station {} successfully added'.format(ss_id)}, 201
Ejemplo n.º 2
0
    def delete(self):

        data = request.form
        station_id = data['ss_id']
        station = StationModel.find_by_id(station_id)
        if station:
            station.delete_from_db()
        return {'message': "Station id {} deleted".format(station_id)}, 200
Ejemplo n.º 3
0
    def delete(self):

        data = request.form
        station_id = data['ss_id']
        station = StationModel.find_by_id(station_id)
        if station:
            station.delete_from_db()
        return {'message': 'Sensor Station deleted.'}
Ejemplo n.º 4
0
    def post(self):
        #form data
        data = request.form
        ss_id = data['ss_id']
        if StationModel.find_by_id(ss_id):
            return {
                'message':
                "Sensor Station with id '{}' already exists.".format(ss_id)
            }, 400

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

        station = StationModel(ss_id, **data)

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

        return station.json(), 201
Ejemplo n.º 5
0
    def patch(self):
        data = request.form
        ss_id = data['ss_id']
        station_update = []
        station = StationModel.find_by_id(ss_id)

        if station is None:
            return {
                'message':
                "Station id {} doesn't exist. Please add station first".format(
                    ss_id)
            }, 405

        if 'ss_model' in data:
            station.ss_model = data.get('ss_model')
            station_update.append('ss_model')

        if 'ss_sw' in data:
            station.ss_sw = data.get('ss_sw')
            station_update.append('ss_sw')

        if 'gw_id' in data:
            station.gw_id = data.get('gw_id')
            station_update.append('gw_id')

        if 'ss_site' in data:
            station.ss_site = data.get('ss_site')
            station_update.append('ss_site')

        if 'ss_num' in data:
            station.ss_num = data.get('ss_num')
            station_update.append('ss_num')

        if 'ss_locate' in data:
            station.ss_locate = data.get('ss_locate')
            station_update.append('ss_locate')

        station.update_db()
        station_update = " ".join(str(elem) for elem in station_update)
        return {
            'message':
            "Updated {} in Station id {}.".format(station_update, ss_id)
        }
Ejemplo n.º 6
0
 def get(self, ss_id):
     station = StationModel.find_by_id(ss_id)
     if station:
         return station.json()
     return {'message': 'Station not found'}, 404
Ejemplo n.º 7
0
	def put(self):
		data = request.form
		ss_id = data['ss_id']
		station = StationModel.find_by_id(ss_id)

		if station is None:
			station = StationModel(**data)
			station.save_to_db()
			return {'message': "A new station was added because a station with id '{}' didn't exist.".format(ss_id)}, 400

		else:

			station.ss_id = data['ss_id']
			station.ss_model = data['ss_model']
			station.ss_sw = data['ss_sw']
			station.gw_id = data['gw_id']
			station.ss_site = data['ss_site']
			station.ss_num = data['ss_num']
			station.ss_locate = data['ss_locate']
Ejemplo n.º 8
0
    def put(self):
        data = request.form
        ss_id = data['ss_id']

        station = StationModel.find_by_id(ss_id)

        if station is None:
            station = StationModel(**data)
        else:
            station.ss_id = data['ss_id']
            station.gw_id = data['gw_id']
            station.ss_site = data['ss_site']
            station.ss_num = data['ss_num']
            station.ss_locate = data['ss_locate']
            station.ss_vwcIdShlw = data['ss_vwcIdShlw']
            station.ss_vwcIdMid = data['ss_vwcIdMid']
            station.ss_vwcIdDeep = data['ss_vwcIdDeep']
            station.ss_phId = data['ss_phId']
            station.ss_co2Id = data['ss_co2Id']
            station.ss_tempId = data['ss_tempId']
            station.ss_bmeId = data['ss_bmeId']

        station.save_to_db()

        return station.json()