def post(self): # Testing Cmd: curl -X POST -d "id=a03" -d "t=23.5" -d "h=98.5" http://%IP%:%Port%/logs args = parser.parse_args() while not lock(locker_key, locker_token, locker_expire): pass (Devices, Logs) = load_from_db('Devices', 'Logs') if args['id'] not in Devices.keys(): return "Device %s is not available!" % args['id'], 406 if args['id'] not in Logs.keys(): Logs[args['id']] = [] ts = int(time.time()) # ts = str(datetime.utcnow()) # datetime.strptime("2019-10-01 14:08:08.774648","%Y-%m-%d %H:%M:%S.%f") # if ts in Logs[args['id']].keys(): # return "Timestamp is duplicated!", 406 tmp = Logs[args['id']] tmp = tmp[-4:] if len(tmp) >= 4 else tmp try: tmp = tmp + \ [{'ts': ts, 't': float(args['t']), 'h': float(args['h'])}, ] Logs[args['id']] = tmp save_to_db(Logs=Logs) unlock(locker_key, locker_token) return Logs[args['id']], 201 except ValueError as err: unlock(locker_key, locker_token) return {'message': '%r' % err}, 500
def put(self, device_id): # Testing Cmd: curl -X PUT -d "lat=23.0" -d "lng=121.0" http://%IP%:%Port%/devices/a03 args = parser.parse_args() while not lock(locker_key, locker_token, locker_expire): pass (Devices, ) = load_from_db('Devices') Devices[device_id] = {'lat': args['lat'], 'lng': args['lng']} save_to_db(Devices=Devices) unlock(locker_key, locker_token) return Devices[device_id], 201
def delete(self, device_id): # Testing Cmd: curl -X DELETE http://%IP%:%Port%/devices/a03 while not lock(locker_key, locker_token, locker_expire): pass (Devices, ) = load_from_db('Devices') abort_if_device_doesnt_exist(device_id) del Devices[device_id] save_to_db(Devices=Devices) unlock(locker_key, locker_token) return '', 204
def post(self): # Testing Cmd: curl -X POST -d "id=a04" -d "lat=23.5" -d "lng=121.5" http://%IP%:%Port%/devices args = parser.parse_args() while not lock(locker_key, locker_token, locker_expire): pass (Devices, ) = load_from_db('Devices') if args['id'] in Devices.keys(): return "ID is duplicated!", 406 Devices[args['id']] = {'lat': args['lat'], 'lng': args['lng']} save_to_db(Devices=Devices) unlock(locker_key, locker_token) return Devices[args['id']], 201
def patch(self, device_id): # Testing Cmd: curl -X PATCH -d "lat=23.5" http://%IP%:%Port%/devices/a03 while not lock(locker_key, locker_token, locker_expire): pass (Devices, ) = load_from_db('Devices') abort_if_device_doesnt_exist(device_id) info = Devices[device_id] args = parser.parse_args() if args['lat'] != None: info['lat'] = float(args['lat']) if args['lng'] != None: info['lng'] = float(args['lng']) Devices[device_id] = info save_to_db(Devices=Devices) unlock(locker_key, locker_token) return Devices[device_id], 201
def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) data = msg.payload.decode('utf-8') args = json.loads(data) while not lock(locker_key, locker_token, locker_expire): pass (Devices, Logs) = load_from_db('Devices', 'Logs') if args['id'] not in Devices.keys(): print("Device %s is not available!" % args['id']) return if args['id'] not in Logs.keys(): Logs[args['id']] = [] ts = int(time.time()) tmp = Logs[args['id']] tmp = tmp[-4:] if len(tmp) >= 4 else tmp try: tmp = tmp+[{'ts': ts, 't': float(args['t']), 'h': float(args['h'])}, ] Logs[args['id']] = tmp save_to_db(Logs=Logs) unlock(locker_key, locker_token) print('Device %s added a log.' % args['id']) except ValueError as err: unlock(locker_key, locker_token) print('Error: %r \n when device %s adding a log.' % (err, args['id']))
def get(self): # Testing Cmd: curl http://%IP%:%Port%/logs (Logs, ) = load_from_db('Logs') return Logs
def get(self): # Testing Cmd: curl http://%IP%:%Port%/devices (Devices, ) = load_from_db('Devices') return Devices
def get(self, device_id): # Testing Cmd: curl http://%IP%:%Port%/devices/a03 (Devices, ) = load_from_db('Devices') abort_if_device_doesnt_exist(device_id) return Devices[device_id]
import telebot from db import load_from_db, save_state from handler_utils import * from state import State bot = telebot.TeleBot(TOKEN) states = load_from_db() def start_polling(): bot.polling(none_stop=True) @bot.callback_query_handler( func=lambda call: states[call.from_user.id].state == State.SETTING_LOCATION ) def setting_location_callback_handler(call): user_id = call.from_user.id message_id = call.message.message_id bot.delete_message(user_id, message_id) switch_to_state(user_id, State.SETTINGS) @bot.callback_query_handler( func=lambda call: states[call.from_user.id].state == State.SETTING_LANGUAGE ) def setting_language_callback_handler(call): user_id = call.from_user.id message_id = call.message.message_id if call.data != KeyboardButton.BACK.value: