<commit_before>package dea.monitor.broadcast;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.commons.io.IOExceptionWithCause;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dea.monitor.checker.CheckUrl;
import twitter4j.JSONArray;
import twitter4j.JSONException;
import twitter4j.JSONObject;

/**
 * Methods for talking to Homeseer
 * 
 * @author dea
 * 
 */
public class Homeseer extends CheckUrl implements BroadcastInterface {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	private String location1;
	private String location2;
	private String eventGroup;
	private String createEvent;

	public Homeseer() {
		loadBundle();
	}

	/********************************************
	 * Interface methods
	 ********************************************/

	public void speak(String text) throws IOException, UnsupportedOperationException {
		try {
			speak(text, null);
		} catch (Exception e) {
			throw new IOExceptionWithCause(e);
		}
	}

	public void sendVal(Integer refID, Float val) throws IOException, UnsupportedOperationException {
		try {
			controldevicebyvalue(refID, val);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendStatusString(Integer refID, String status) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "NewDevString", status);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendError(Integer refID, String errMsg) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Attention", errMsg);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	/**
	 * UserNote is used in my Homeseer system for cat storage when a device is
	 * marked offline
	 */
	public void sendDetails(Integer refID, String details) throws IOException {
//		try {
//			String tmp = details;
//			if (tmp != null && tmp.length() > 140) {
//				tmp = details.substring(0, 140);
//			}
//			setdeviceproperty(refID, "UserNote", tmp);
//		} catch (MalformedURLException | JSONException e) {
//			throw new IOException(e);
//		}
	}

	public void sendNameChg(Integer refID, String name) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Name", name);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendRegionChg(Integer refID, String name) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Location", name);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendAddressChg(Integer refID, String name) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Address", name);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendTypeChg(Integer refID, String type) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Device_Type_String", type);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public Set<String> getRegions() throws JSONException, IOException {
		HashSet<String> set = new HashSet<String>();
		JSONObject obj = getlocations();
		JSONArray regions = obj.getJSONArray("location1");
		for (int i = 0; i < regions.length(); i++) {
			String region = regions.getString(i);
			set.add(region.toString());
		}
		return set;
	}

	public Map<String, JSONObject> getDevices() throws JSONException, IOException {
		return getstatus(null, null, location2);
	}

	public Map<String, JSONObject> getDevicesByRegion(String region) throws JSONException, IOException {
		return getstatus(null, region, location2);
	}

	public Integer getDeviceId(String deviceName) throws JSONException, IOException {
		Integer id = null;
		Map<String, JSONObject> map = getstatus(null, null, location2);
		if (map != null) {
			JSONObject obj = map.get(deviceName);
			id = obj.getInt("ref");
		}
		return id;
	}

	public Integer updateDevice(Integer refID, String deviceName, String region, String type, String address)
			throws JSONException, IOException {
		if (deviceName == null) {
			throw new IOException("deviceName is required");
		}
		if (region == null) {
			throw new IOException("region is required");
		}

		JSONObject obj = null;
		if (refID == 0) {
			Map<String, JSONObject> map = getstatus(null, null, location2);
			if (map != null) {
				obj = map.get(deviceName);
				if (obj != null) {
					refID = obj.getInt("ref");
					log.info("Found remote device by name");
				}
			}
		} else {
			Map<String, JSONObject> map = getstatus(refID, null, null);
			if (map != null && !map.isEmpty()) {
				obj = map.values().iterator().next();
				if (obj != null) {
					refID = obj.getInt("ref");
					log.info("Found remote device by refID");
				}
			}
		}

		// could not find by name or refID so add one.
		if (obj == null) {
			// create a new object
			runevent(eventGroup, createEvent + type);
			// get new device<commit_msg>Add check for item marked offline<commit_after>package dea.monitor.broadcast;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.commons.io.IOExceptionWithCause;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dea.monitor.checker.CheckUrl;
import twitter4j.JSONArray;
import twitter4j.JSONException;
import twitter4j.JSONObject;

/**
 * Methods for talking to Homeseer
 * 
 * @author dea
 * 
 */
public class Homeseer extends CheckUrl implements BroadcastInterface {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	private String location1;
	private String location2;
	private String eventGroup;
	private String createEvent;

	public Homeseer() {
		loadBundle();
	}

	/********************************************
	 * Interface methods
	 ********************************************/

	public void speak(String text) throws IOException, UnsupportedOperationException {
		try {
			speak(text, null);
		} catch (Exception e) {
			throw new IOExceptionWithCause(e);
		}
	}

	public void sendVal(Integer refID, Float val) throws IOException, UnsupportedOperationException {
		try {
			controldevicebyvalue(refID, val);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendStatusString(Integer refID, String status) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "NewDevString", status);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendError(Integer refID, String errMsg) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Attention", errMsg);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	/**
	 * UserNote is used in my Homeseer system for cat storage when a device is
	 * marked offline
	 */
	public void sendDetails(Integer refID, String details) throws IOException {
//		try {
//			String tmp = details;
//			if (tmp != null && tmp.length() > 140) {
//				tmp = details.substring(0, 140);
//			}
//			setdeviceproperty(refID, "UserNote", tmp);
//		} catch (MalformedURLException | JSONException e) {
//			throw new IOException(e);
//		}
	}

	public void sendNameChg(Integer refID, String name) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Name", name);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendRegionChg(Integer refID, String name) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Location", name);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendAddressChg(Integer refID, String name) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Address", name);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public void sendTypeChg(Integer refID, String type) throws IOException, UnsupportedOperationException {
		try {
			setdeviceproperty(refID, "Device_Type_String", type);
		} catch (MalformedURLException | JSONException e) {
			throw new IOException(e);
		}
	}

	public Set<String> getRegions() throws JSONException, IOException {
		HashSet<String> set = new HashSet<String>();
		JSONObject obj = getlocations();
		JSONArray regions = obj.getJSONArray("location1");
		for (int i = 0; i < regions.length(); i++) {
			String region = regions.getString(i);
			set.add(region.toString());
		}
		return set;
	}

	public Map<String, JSONObject> getDevices() throws JSONException, IOException {
		return getstatus(null, null, location2);
	}

	public Map<String, JSONObject> getDevicesByRegion(String region) throws JSONException, IOException {
		return getstatus(null, region, location2);
	}

	public Integer getDeviceId(String deviceName) throws JSONException, IOException {
		Integer id = null;
		Map<String, JSONObject> map = getstatus(null, null, location2);
		if (map != null) {
			JSONObject obj = map.get(deviceName);
			id = obj.getInt("ref");
		}
		return id;
	}

	public Integer updateDevice(Integer refID, String deviceName, String region, String type, String address)
			throws JSONException, IOException {
		if (deviceName == null) {
			throw new IOException("deviceName is required");
		}
		if (region == null) {
			throw new IOException("region is required");
		}

		JSONObject obj = null;
		if (refID == 0) {
			Map<String, JSONObject> map = getstatus(null, null, location2);
			if (map != null) {
				obj = map.get(deviceName);
				if (obj != null) {
					refID = obj.getInt("ref");
					log.info("Found remote device by name");
				}
			} else { // if running ChkSensors it might be in the offline group.
				map = getstatus(null, null, "offline");
				if (map != null) {
					obj = map.get(deviceName);
					if (obj != null) {
						refID = obj.getInt("ref");
						log.info("Found remote device by name");
					}
				}
			}
		} else {
			Map<String, JSONObject> map = getstatus(refID, null, null);
			if (map != null && !map.isEmpty()) {
				obj = map.values().iterator().next();
				if (obj != null) {
					refID = obj.getInt("ref");
					log.info("Found remote device by refID");
				}
			}
		}

		// could not find by name or refID so add one.
		if (obj == null) {
			// create a new object
			runevent(eventGroup, createEvent + type);
			// get new device
{'fraction_non_alphanumeric': 0.12694229895502043, 'fraction_numerical': 0.0038164470695138575, 'mean_word_length': 4.152621722846442, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '7385074', 'n_tokens_mistral': 3700, 'n_tokens_neox': 3281, 'n_words': 940}