<commit_before>package com.rehivetech.beeeon.network.demo;

import android.content.Context;

import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.exception.AppException;
import com.rehivetech.beeeon.gcm.notification.VisibleNotification;
import com.rehivetech.beeeon.household.device.Device;
import com.rehivetech.beeeon.household.device.DeviceType;
import com.rehivetech.beeeon.household.device.Module;
import com.rehivetech.beeeon.household.device.ModuleLog;
import com.rehivetech.beeeon.household.device.ModuleLog.DataInterval;
import com.rehivetech.beeeon.household.device.ModuleLog.DataType;
import com.rehivetech.beeeon.household.device.RefreshInterval;
import com.rehivetech.beeeon.household.device.values.EnumValue;
import com.rehivetech.beeeon.household.device.values.EnumValue.Item;
import com.rehivetech.beeeon.household.gate.Gate;
import com.rehivetech.beeeon.household.gate.GateInfo;
import com.rehivetech.beeeon.household.location.Location;
import com.rehivetech.beeeon.household.user.User;
import com.rehivetech.beeeon.household.user.User.Gender;
import com.rehivetech.beeeon.household.user.User.Role;
import com.rehivetech.beeeon.network.INetwork;
import com.rehivetech.beeeon.network.authentication.IAuthProvider;
import com.rehivetech.beeeon.util.DataHolder;
import com.rehivetech.beeeon.util.GpsData;
import com.rehivetech.beeeon.util.MultipleDataHolder;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Network service that handles communication in demo mode.
 *
 * @author Robyer
 */
public class DemoNetwork implements INetwork {
	private static final String TAG = DemoNetwork.class.getSimpleName();

	public static final String DEMO_USER_ID = "demo";
	private static final String DEMO_USER_BT = "12345";

	private static final int RAW_ENUM_VALUES_COUNT_IN_LOG = 100;

	private Context mContext;
	private User mUser;
	private String mBT;
	private boolean mInitialized;
	private Map<String, Random> mRandoms = new HashMap<>();

	public final DataHolder<GateInfo> mGates = new DataHolder<>();
	public final MultipleDataHolder<Location> mLocations = new MultipleDataHolder<>();
	public final MultipleDataHolder<Device> mDevices = new MultipleDataHolder<>();
	public final MultipleDataHolder<User> mUsers = new MultipleDataHolder<>();

	public DemoNetwork(Context context) {
		mContext = context;

		String demoModeName = mContext.getString(R.string.demo_network_demo_mode);
		String demoModeEmail = mContext.getString(R.string.demo_network_demo_mode_email);

		// Set user
		mUser = new User(DEMO_USER_ID, demoModeName, "", demoModeEmail, Gender.UNKNOWN, Role.Owner);

		// Set session token
		mBT = DEMO_USER_BT;
	}

	private boolean isGateAllowed(String gateId) {
		return getRandomForGate(gateId).nextBoolean();
	}

	private Random getRandomForGate(String gateId) {
		Random rand = mRandoms.get(gateId);

		if (rand == null) {
			try {
				int id = Integer.parseInt(gateId);
				rand = new Random(id);
			} catch (NumberFormatException e) {
				rand = new Random();
			}
			mRandoms.put(gateId, rand);
		}

		return rand;
	}

	private void setNewValue(Module module) {
		// Don't set new values for actors (unless it's the first value to be set during initialization)
		if (module.isActuator() && module.getValue().hasValue())
			return;

		Random rand = getRandomForGate(module.getDevice().getGateId());

		if (module.getValue() instanceof EnumValue) {
			EnumValue value = (EnumValue) module.getValue();
			List<Item> items = value.getEnumItems();
			Item item = items.get(rand.nextInt(items.size()));

			module.setValue(item.getValue());
		} else {
			double lastValue = module.getValue().getDoubleValue();
			double range = 5;

			RefreshInterval refresh = module.getDevice().getRefresh();
			if (refresh != null) {
				range = 2 + Math.log(refresh.getInterval());
			}

			if (Double.isNaN(lastValue)) {
				lastValue = rand.nextDouble() * 1000;
			}

			double addvalue = rand.nextInt((int) range * 1000) / 1000;
			boolean plus = rand.nextBoolean();
			lastValue = lastValue + addvalue * (plus ? 1 : -1);

			module.setValue(String.valueOf((int) lastValue));
		}
	}

	public void initDemoData() throws AppException {
		if (mInitialized)
			return;

		// Erase previous data if exists
		mGates.clear();
		mLocations.clear();
		mDevices.clear();
		mUsers.clear();

		DemoData demoData = new DemoData();
		mGates.setObjects(demoData.getGates(mContext));
		for (GateInfo gate : mGates.getObjects()) {
			String gateId = gate.getId();

			mLocations.setObjects(gateId, demoData.getLocation(mContext, gateId));
			mLocations.setLastUpdate(gateId, DateTime.now());

			mDevices.setObjects(gateId, demoData.getDevices(gateId));
			mDevices.setLastUpdate(gateId, DateTime.now());

			// Just one (self) user for now, anyone can create XML with more users and use it here like other items
			mUsers.setObjects(gateId, Arrays.asList(new User(mUser.getId(), "John", "Doe", "<EMAIL>", Gender.MALE, Role.Owner)));
			mUsers.setLastUpdate(gateId, DateTime.now());

			Random rand = getRandomForGate(gate.getId());

			// Set last update time to time between (-26 hours, now>
			for (Device device : mDevices.getObjects(gateId)) {
				// FIXME: is using getObjects() ok? It creates new list. But it should be ok, because inner objects are still only references. Needs test!
				device.setLastUpdate(DateTime.now(DateTimeZone.UTC).minusSeconds(rand.nextInt(60 * 60 * 26)));
			}
		}

		mInitialized = true;
	}

	@Override
	public boolean isAvailable() {
		return true;
	}

	@Override
	public String getSessionId() {
		return mBT;
	}

	@Override
	public void setSessionId(String token) {
		mBT = token;
	}<commit_msg>DemoNetwork: Apply some lint suggestions<commit_after>package com.rehivetech.beeeon.network.demo;

import android.content.Context;

import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.exception.AppException;
import com.rehivetech.beeeon.gcm.notification.VisibleNotification;
import com.rehivetech.beeeon.household.device.Device;
import com.rehivetech.beeeon.household.device.DeviceType;
import com.rehivetech.beeeon.household.device.Module;
import com.rehivetech.beeeon.household.device.ModuleLog;
import com.rehivetech.beeeon.household.device.ModuleLog.DataInterval;
import com.rehivetech.beeeon.household.device.ModuleLog.DataType;
import com.rehivetech.beeeon.household.device.RefreshInterval;
import com.rehivetech.beeeon.household.device.values.EnumValue;
import com.rehivetech.beeeon.household.device.values.EnumValue.Item;
import com.rehivetech.beeeon.household.gate.Gate;
import com.rehivetech.beeeon.household.gate.GateInfo;
import com.rehivetech.beeeon.household.location.Location;
import com.rehivetech.beeeon.household.user.User;
import com.rehivetech.beeeon.household.user.User.Gender;
import com.rehivetech.beeeon.household.user.User.Role;
import com.rehivetech.beeeon.network.INetwork;
import com.rehivetech.beeeon.network.authentication.IAuthProvider;
import com.rehivetech.beeeon.util.DataHolder;
import com.rehivetech.beeeon.util.GpsData;
import com.rehivetech.beeeon.util.MultipleDataHolder;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Network service that handles communication in demo mode.
 *
 * @author Robyer
 */
public class DemoNetwork implements INetwork {
	private static final String TAG = DemoNetwork.class.getSimpleName();

	public static final String DEMO_USER_ID = "demo";
	private static final String DEMO_USER_BT = "12345";

	private static final int RAW_ENUM_VALUES_COUNT_IN_LOG = 100;

	private final Context mContext;
	private final User mUser;
	private String mBT;
	private boolean mInitialized;
	private final Map<String, Random> mRandoms = new HashMap<>();

	private final DataHolder<GateInfo> mGates = new DataHolder<>();
	private final MultipleDataHolder<Location> mLocations = new MultipleDataHolder<>();
	private final MultipleDataHolder<Device> mDevices = new MultipleDataHolder<>();
	private final MultipleDataHolder<User> mUsers = new MultipleDataHolder<>();

	public DemoNetwork(Context context) {
		mContext = context;

		String demoModeName = mContext.getString(R.string.demo_network_demo_mode);
		String demoModeEmail = mContext.getString(R.string.demo_network_demo_mode_email);

		// Set user
		mUser = new User(DEMO_USER_ID, demoModeName, "", demoModeEmail, Gender.UNKNOWN, Role.Owner);

		// Set session token
		mBT = DEMO_USER_BT;
	}

	private boolean isGateAllowed(String gateId) {
		return getRandomForGate(gateId).nextBoolean();
	}

	private Random getRandomForGate(String gateId) {
		Random rand = mRandoms.get(gateId);

		if (rand == null) {
			try {
				int id = Integer.parseInt(gateId);
				rand = new Random(id);
			} catch (NumberFormatException e) {
				rand = new Random();
			}
			mRandoms.put(gateId, rand);
		}

		return rand;
	}

	private void setNewValue(Module module) {
		// Don't set new values for actors (unless it's the first value to be set during initialization)
		if (module.isActuator() && module.getValue().hasValue())
			return;

		Random rand = getRandomForGate(module.getDevice().getGateId());

		if (module.getValue() instanceof EnumValue) {
			EnumValue value = (EnumValue) module.getValue();
			List<Item> items = value.getEnumItems();
			Item item = items.get(rand.nextInt(items.size()));

			module.setValue(item.getValue());
		} else {
			double lastValue = module.getValue().getDoubleValue();
			double range = 5;

			RefreshInterval refresh = module.getDevice().getRefresh();
			if (refresh != null) {
				range = 2 + Math.log(refresh.getInterval());
			}

			if (Double.isNaN(lastValue)) {
				lastValue = rand.nextDouble() * 1000;
			}

			double addvalue = rand.nextInt((int) range * 1000) / 1000;
			boolean plus = rand.nextBoolean();
			lastValue = lastValue + addvalue * (plus ? 1 : -1);

			module.setValue(String.valueOf((int) lastValue));
		}
	}

	public void initDemoData() throws AppException {
		if (mInitialized)
			return;

		// Erase previous data if exists
		mGates.clear();
		mLocations.clear();
		mDevices.clear();
		mUsers.clear();

		DemoData demoData = new DemoData();
		mGates.setObjects(demoData.getGates(mContext));
		for (GateInfo gate : mGates.getObjects()) {
			String gateId = gate.getId();

			mLocations.setObjects(gateId, demoData.getLocation(mContext, gateId));
			mLocations.setLastUpdate(gateId, DateTime.now());

			mDevices.setObjects(gateId, demoData.getDevices(gateId));
			mDevices.setLastUpdate(gateId, DateTime.now());

			// Just one (self) user for now, anyone can create XML with more users and use it here like other items
			mUsers.setObjects(gateId, Collections.singletonList(new User(mUser.getId(), "John", "Doe", "<EMAIL>", Gender.MALE, Role.Owner)));
			mUsers.setLastUpdate(gateId, DateTime.now());

			Random rand = getRandomForGate(gate.getId());

			// Set last update time to time between (-26 hours, now>
			for (Device device : mDevices.getObjects(gateId)) {
				device.setLastUpdate(DateTime.now(DateTimeZone.UTC).minusSeconds(rand.nextInt(60 * 60 * 26)));
			}
		}

		mInitialized = true;
	}

	@Override
	public boolean isAvailable() {
		return true;
	}

	@Override
	public String getSessionId() {
		return mBT;
	}

	@Override
	public void setSessionId(String token) {
		mBT = token;
	}
{'fraction_non_alphanumeric': 0.11700008660258075, 'fraction_numerical': 0.005542565168442019, 'mean_word_length': 4.7971887550200805, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2407188', 'n_tokens_mistral': 4142, 'n_tokens_neox': 3774, 'n_words': 818}