def handle_send_connect(data): app.logger.info('Send connect: {}'.format(data)) computer_id = data['id'] computer_name = data['name'] computer_location = data['location'] computer_instance = data['instance'] computer = Computer.query.filter( Computer.computer_id == computer_id).first() if computer is None: computer = Computer(computer_id=computer_id, computer_name=computer_name, computer_location=computer_location, computer_instance=computer_instance) if Computer.add(computer): socketIo.emit('receive_register', computer_info_w(computer)) computer.computer_power_status = 1 computer.computer_cmd = 1 if Computer.update(computer): # emit to user room dashboard socketIo.emit('receive_update', computer_info_w(computer), room='{}_{}'.format(computer.computer_instance, computer_location)) # emit to socket client socketIo.emit('receive_connect', computer_info_w(computer))
def handle_send_do_shutdown(data): app.logger.info('Do shutdown: {}'.format(data)) computer_id = data['data']['id'] # computer_power_status = data['data']['power_status'] computer = Computer.query.filter( Computer.computer_id == computer_id).first() if computer is None: return no_such_computer_w() computer.computer_power_status = 0 computer.computer_cmd = 0 if Computer.update(computer): # emit to user room dashboard socketIo.emit('receive_update', computer_info_w(computer), room='{}_{}'.format(computer.computer_instance, computer.computer_location))
def update_computer_data(): # inactive in seconds inactive = 60 computers = Computer.query.filter( (Computer.computer_power_status == 1) & (timestamp_jakarta() - Computer.computer_ping_timestamp > inactive)).all() for computer in computers: computer.computer_power_status = 0 if Computer.update(computer): # emit to dashboard socketIo.emit('receive_update', computer_info_w(computer), room='{}_{}'.format(computer.computer_instance, computer.computer_location)) return {'status': 'OK', 'data_updated': len(computers)}
def handle_send_shutdown_all(data): app.logger.info('Shutdown all: {}'.format(data)) location = data.get('location') computers = Computer.query.filter( (Computer.computer_instance == current_user.instance) & (Computer.computer_location == location)).all() for computer in computers: computer.computer_cmd = 0 # update computer cmd date computer.on_action() if Computer.update(computer): socketIo.emit('receive_do_shutdown', computer_info_w(computer)) data = {'status': 'OK', 'location': location} # emit to user room dashboard socketIo.emit('receive_shutdown_all', data, room='{}_{}'.format(current_user.instance, location))
def handle_send_shutdown(data): app.logger.info('Send shutdown: {}'.format(data)) # update computer cmd to shutdown computer_id = data.get('computer_id') computer = Computer.query.filter( Computer.computer_id == computer_id).first() computer.computer_cmd = 0 # update computer cmd date computer.on_action() if Computer.update(computer): # emit to user room dashboard socketIo.emit('receive_shutdown', computer_info_w(computer), room='{}_{}'.format(computer.computer_instance, computer.computer_location)) # emit to socket client socketIo.emit('receive_do_shutdown', computer_info_w(computer))
def computer_info(id_computer): computer = Computer.query.filter( Computer.computer_id == id_computer).first() if computer is None: return no_such_computer_w() if request.method == 'POST': jb = request.get_json() power_status = jb.get('power_status') cmd = jb.get('cmd') if cmd is None and power_status is None: return invalid_input_w() if power_status is not None: computer.computer_power_status = int(power_status) if cmd is not None: computer.computer_cmd = int(cmd) # update computer cmd date computer.on_action() if Computer.update(computer): # emit to dashboard socketIo.emit('receive_update', computer_info_w(computer), room='{}_{}'.format(computer.computer_instance, computer.computer_location)) return { 'status': 'OK', 'msg': 'Computer state updated: {}'.format(computer.computer_id) } else: return { 'status': 'ERROR', 'msg': 'Failed to update computer state' } # update cmd timestamp from http client computer.on_ping() return computer_info_w(computer)