def delete_device_config(device, config_uuid): try: session = create_user_session() device_type = select_device_type(device) device_config = device_type.get_device_config_uuid( session, config_uuid) config_name = device_config.name if (device_config.job_state != enums.ConfigJobState.Running.value): configure_device_jobs(device, device_config, False) session.delete(device_config) session.commit() return messenger.raise_notification( True, 'Konfigūracija "' + config_name + '" panaikinta!') else: session.rollback() return messenger.raise_notification( False, 'Rutininis darbas vykdomas pagal šią konfigūraciją! Atšaukite rutininį darbą!' ) except Exception as ex: session.rollback() Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: session.close()
def save_device_aes_interval(device_id, interval): try: session = create_user_session() main_db_session = db.session.session_factory() interval = None if interval == "" else int(interval) device = session.query( models.UserDevices).filter_by(id=device_id).first() device.aes_key_interval = interval device_in_main = main_db_session.query( models.Devices).filter_by(mac=device.mac).first() device_in_main.aes_key_change_date = None if interval == None else datetime.now( ) + timedelta(seconds=interval) main_db_session.commit() session.commit() return messenger.raise_notification( True, 'AES rakto keitimo intervalas išsaugotas!') except Exception as ex: Logger.log_error(ex.args[0]) flash('Nenumatyta klaida', 'danger') session.rollback() main_db_session.rollback() finally: session.close() main_db_session.close()
def get_user_devices(): try: session = create_user_session() if not table_exists("user_devices"): models.UserDevices.__table__.create(session.bind) session.commit() devices = session.query(models.UserDevices).all() session.close() devicesArr = [] for device in devices: deviceObj = UserDevicesView( id=device.id, device_name=device.device_name, mac=device.mac, state=enums.DeviceState(device.status).name, date_added=str(device.date_added_local), device_type=device.device_type, device_type_name=enums.DeviceType(device.device_type).name, publish_interval=device.publish_interval, aes_key_interval=device.aes_key_interval) devicesArr.append(deviceObj) return jsonify({'data': [result.serialize for result in devicesArr]}) except Exception as ex: Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: session.close()
def stop_job(device, config_uuid): try: session = create_user_session() device_type = select_device_type(device) device_config = device_type.get_device_config_uuid( session, config_uuid) response = execute_device_action(device.id, "STOP JOB") main_db_session = db.session.session_factory() main_db_device = main_db_session.query(models.Devices).filter_by( mac=device.mac, user_id=current_user.id).first() if (main_db_device is None): # jei prietaisas nepriskirtas useriui return JsonParse.decode_jsonify( jsonify( success=False, message= "Naudotojas neturi priskirto prietaiso pagrindinėje duomenų bazėje!" )) job = main_db_session.query( models.DeviceJobs).filter_by(config_uuid=device_config.uuid, device_id=main_db_device.id).first() if (job is not None): job.running = False job.finish_time = None main_db_session.commit() else: return JsonParse.decode_jsonify( jsonify(success=False, message="Rutininis darbas nerastas!")) device_config.job_state = enums.ConfigJobState.Idle.value save_device_history(session=session, device=device, text="Rutininis darbas pagal konfigūraciją \"" + device_config.name + "\" sustabdytas naudotojo iniciatyva!") session.commit() return response except Exception as ex: session.rollback() main_db_session.rollback() Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: main_db_session.close() session.close()
def activate_device_configuration(device, config_uuid): try: session = create_user_session() device_type = select_device_type(device) device_config = device_type.get_device_config_uuid( session, config_uuid) if (device_config.job_state != enums.ConfigJobState.Running.value): if (device_config.is_active): device_config.is_active = enums.ConfigState.Disabled.value device_config.job_state = None response = configure_device_jobs(device, device_config, False) if (not response.success): session.rollback() return messenger.raise_notification( response.success, response.message) session.commit() return messenger.raise_notification( True, 'Konfigūracija "' + device_config.name + '" deaktyvuota!') else: device_config.is_active = enums.ConfigState.Active.value device_config.job_state = enums.ConfigJobState.Idle.value response = configure_device_jobs(device, device_config, True) if (not response.success): session.rollback() return messenger.raise_notification( response.success, response.message) session.commit() return messenger.raise_notification( True, 'Konfigūracija "' + device_config.name + '" aktyvuota!') else: session.rollback() return messenger.raise_notification( False, 'Rutininis darbas vykdomas pagal šią konfigūraciją! Atšaukite rutininį darbą!' ) except Exception as ex: session.rollback() Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: session.close()
def get_device_configurations(device_id): try: session = create_user_session() device = session.query( models.UserDevices).filter_by(id=device_id).first() config_objects_list = [] device_type = select_device_type(device) config_objects_list = device_type.get_configuration_view_list( session, device.id) session.close() return jsonify( {'data': [result.serialize for result in config_objects_list]}) except Exception as ex: Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: session.close()
def send_device_configuration(device_id, data_type, data): if current_user.is_authenticated: try: session = create_user_session() device = session.query( models.UserDevices).filter_by(id=device_id).first() if (device.status != enums.DeviceState.Active.value and device.status != enums.DeviceState.Registered.value): return messenger.raise_notification( False, 'Prietaisas nėra aktyvus! Negalima operacija!', ) # parenkamos temos configo siuntimui ir atsakymo gavimui systemName = "system" topic = current_user.uuid + "/" + systemName + "/" + device.mac + "/setconfig" response_topic = topic + "/response" payload = None # common commands if (data_type == 'interval'): payload = "delay=" + data else: return messenger.raise_notification(False, 'Komanda netinkamo tipo!') service = MqttService() # publishinam komanda response = service.publish_with_response(topic, response_topic, payload, 10, device.mac) if (response.success): if (data_type == 'interval'): device.publish_interval = data session.commit() return messenger.raise_notification( True, 'Komanda įvykdyta sėkmingai!') if (response.success is False and response.reason == "Time is up."): device.status = enums.DeviceState.Offline.value session.commit() Logger.log_error(response.reason) return messenger.raise_notification( False, 'Baigėsi laikas! Komunikacijos su prietaisu klaida.') else: return messenger.raise_notification( False, 'Komanda nebuvo įvykdyta.') except Exception as ex: session.rollback() Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: session.close() else: return messenger.raise_notification( False, 'Naudotojas nėra autentifikuotas!')
def execute_device_action(id, command): if current_user.is_authenticated: try: session = create_user_session() device = session.query(models.UserDevices).filter_by(id=id).first() if (device.status != enums.DeviceState.Active.value and device.status != enums.DeviceState.Registered.value and command != 'REBOOT'): return messenger.raise_notification( False, 'Prietaisas nėra aktyvus! Negalima operacija!', ) systemName = "system" topic = current_user.uuid + "/" + systemName + "/" + device.mac + "/control" response_topic = topic + "/response" payload = None # komandos parinkimas device_type = select_device_type(device) payload = device_type.form_mqtt_payload(command) # common commands if (command == 'REBOOT'): device.status = enums.DeviceState.Rebooting.value session.commit() payload = "reboot" service = MqttService() # publishinam komanda response = service.publish_with_response(topic, response_topic, payload, 10, device.mac) if (response.success): return messenger.raise_notification( True, 'Komanda įvykdyta sėkmingai!') if (response.success is False and response.reason == "Time is up."): device.status = enums.DeviceState.Offline.value session.commit() Logger.log_error(response.reason) return messenger.raise_notification( False, 'Baigėsi laikas! Komunikacijos su prietaisu klaida.') else: return messenger.raise_notification( False, 'Komanda nebuvo įvykdyta.') except Exception as ex: session.rollback() Logger.log_error(ex.args[0]) return messenger.raise_notification( False, 'Įvyko vidinė klaida:' + ex.args[0]) finally: session.close() else: return messenger.raise_notification( False, 'Naudotojas nėra autentifikuotas!')