<commit_before>/*
 * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.device.mgt.iot.arduino.api;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.iot.arduino.api.util.DeviceJSON;
import org.wso2.carbon.device.mgt.iot.arduino.api.util.MqttArduinoSubscriber;
import org.wso2.carbon.device.mgt.iot.arduino.constants.ArduinoConstants;
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
import org.wso2.carbon.device.mgt.iot.common.datastore.impl.DataStreamDefinitions;
import org.wso2.carbon.device.mgt.iot.common.exception.UnauthorizedException;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.*;

public class ArduinoControllerService {

	private static Log log = LogFactory.getLog(ArduinoControllerService.class);

	private static Map<String, LinkedList<String>> replyMsgQueue = new HashMap<>();
	private static Map<String, LinkedList<String>> internalControlsQueue = new HashMap<>();
	private static MqttArduinoSubscriber mqttArduinoSubscriber;


	public void setMqttArduinoSubscriber(MqttArduinoSubscriber mqttArduinoSubscriber) {
		ArduinoControllerService.mqttArduinoSubscriber = mqttArduinoSubscriber;
		try {
			mqttArduinoSubscriber.subscribe();
		} catch (DeviceManagementException e) {
			log.error(e.getErrorMessage());
		}
	}

	public MqttArduinoSubscriber getMqttArduinoSubscriber() {
		return mqttArduinoSubscriber;
	}

	public static Map<String, LinkedList<String>> getReplyMsgQueue() {
		return replyMsgQueue;
	}

	public static Map<String, LinkedList<String>> getInternalControlsQueue() {
		return internalControlsQueue;
	}

	/*    Service to switch arduino bulb (pin 13) between "ON" and "OFF"
			   Called by an external client intended to control the Arduino */
	@Path("/bulb/{deviceId}/{state}")
	@POST
	public void switchBulb(@QueryParam("owner") String owner,
						   @PathParam("deviceId") String deviceId,
						   @PathParam("state") String state,
						   @Context HttpServletResponse response) {

		String switchToState = state.toUpperCase();

		if (!switchToState.equals(ArduinoConstants.STATE_ON) && !switchToState.equals(
				ArduinoConstants.STATE_OFF)) {
			log.error("The requested state change shoud be either - 'ON' or 'OFF'");
			response.setStatus(HttpStatus.SC_BAD_REQUEST);
			return;
		}

		try {
			DeviceController deviceController = new DeviceController();
			boolean result = deviceController.publishMqttControl(owner,
																 ArduinoConstants.DEVICE_TYPE,
																 deviceId, "BULB", switchToState);
			if (result) {
				response.setStatus(HttpStatus.SC_ACCEPTED);

			} else {
				response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);

			}

		} catch (UnauthorizedException e) {
			response.setStatus(HttpStatus.SC_UNAUTHORIZED);

		}

	}

	/*    Service to poll the control-queue for the controls sent to the Arduino
			   Called by the Arduino device  */
	@Path("/readcontrols/{deviceId}")
	@GET
	public String readControls(@QueryParam("owner") String owner,
							   @PathParam("deviceId") String deviceId,<commit_msg>changed to correct exception in Arduino API<commit_after>/*
 * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.device.mgt.iot.arduino.api;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.iot.arduino.api.util.DeviceJSON;
import org.wso2.carbon.device.mgt.iot.arduino.api.util.MqttArduinoSubscriber;
import org.wso2.carbon.device.mgt.iot.arduino.constants.ArduinoConstants;
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
import org.wso2.carbon.device.mgt.iot.common.datastore.impl.DataStreamDefinitions;
import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException;
import org.wso2.carbon.device.mgt.iot.common.exception.UnauthorizedException;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.*;

public class ArduinoControllerService {

	private static Log log = LogFactory.getLog(ArduinoControllerService.class);

	private static Map<String, LinkedList<String>> replyMsgQueue = new HashMap<>();
	private static Map<String, LinkedList<String>> internalControlsQueue = new HashMap<>();
	private static MqttArduinoSubscriber mqttArduinoSubscriber;


	public void setMqttArduinoSubscriber(MqttArduinoSubscriber mqttArduinoSubscriber) {
		ArduinoControllerService.mqttArduinoSubscriber = mqttArduinoSubscriber;
		try {
			mqttArduinoSubscriber.subscribe();
		} catch (DeviceManagementException e) {
			log.error(e.getErrorMessage());
		}
	}

	public MqttArduinoSubscriber getMqttArduinoSubscriber() {
		return mqttArduinoSubscriber;
	}

	public static Map<String, LinkedList<String>> getReplyMsgQueue() {
		return replyMsgQueue;
	}

	public static Map<String, LinkedList<String>> getInternalControlsQueue() {
		return internalControlsQueue;
	}

	/*    Service to switch arduino bulb (pin 13) between "ON" and "OFF"
			   Called by an external client intended to control the Arduino */
	@Path("/bulb/{deviceId}/{state}")
	@POST
	public void switchBulb(@QueryParam("owner") String owner,
						   @PathParam("deviceId") String deviceId,
						   @PathParam("state") String state,
						   @Context HttpServletResponse response) {

		String switchToState = state.toUpperCase();

		if (!switchToState.equals(ArduinoConstants.STATE_ON) && !switchToState.equals(
				ArduinoConstants.STATE_OFF)) {
			log.error("The requested state change shoud be either - 'ON' or 'OFF'");
			response.setStatus(HttpStatus.SC_BAD_REQUEST);
			return;
		}

		try {
			DeviceController deviceController = new DeviceController();
			boolean result = deviceController.publishMqttControl(owner,
																 ArduinoConstants.DEVICE_TYPE,
																 deviceId, "BULB", switchToState);
			if (result) {
				response.setStatus(HttpStatus.SC_ACCEPTED);

			} else {
				response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);

			}

		} catch (DeviceControllerException e) {
			response.setStatus(HttpStatus.SC_UNAUTHORIZED);

		}

	}

	/*    Service to poll the control-queue for the controls sent to the Arduino
			   Called by the Arduino device  */
	@Path("/readcontrols/{deviceId}")
	@GET
	public String readControls(@QueryParam("owner") String owner,
							   @PathParam("deviceId") String deviceId,
{'fraction_non_alphanumeric': 0.10229254921505108, 'fraction_numerical': 0.005357587839521555, 'mean_word_length': 4.6329824561403505, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '266282', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2396, 'n_words': 573}