initIReadyService method

Future initIReadyService()

Implementation

Future initIReadyService() async {
  try {
    deviceId.value =
        localStg.read<int>(StorageKey.deviceId) ?? generateDeviceId();
    localStg.write(StorageKey.deviceId, deviceId.value);

    var message =
        localStg.read<Map<String, dynamic>>(StorageKey.stateMessage);
    if (message == null) {
      localStg.write(StorageKey.stateMessage, stateMessage.value.toJson());
    } else {
      stateMessage.value = StateMessage.fromJson(message);
      stateMessage.refresh();
    }

    message = localStg.read<Map<String, dynamic>>(StorageKey.renameMessage);
    if (message == null) {
      renameMessage.value = RenameMessage(
        event: MessageEvent.rename,
        alias: getRandomAlias(),
      );
      localStg.write(StorageKey.renameMessage, renameMessage.value!.toJson());
    } else {
      renameMessage.value = RenameMessage.fromJson(message);
      renameMessage.refresh();
    }
  } catch (e, st) {
    log("[initGlobalServer] 读取持久化数据失败", error: e, stackTrace: st);
  }

  eventBus.on<MqttServerInfoCollected>().listen((event) {
    connectMqttServer(event.wrapper);
  });
  // 异步执行
  initMulticast();
}