package com.ericsson.fiberbox.coap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.network.config.NetworkConfig;

public class Simulator
{
	private static final Logger LOG = LoggerFactory.getLogger(Simulator.class);

	private static final int COAP_PORT = NetworkConfig.getStandard().getInt(NetworkConfig.Keys.COAP_PORT);

	public static void main(String[] args) throws Exception
	{
		//String hostName = args[0]; ;
		//int port = Integer.parseInt(args[1]) ;

		Simulator simulator = new Simulator();
		simulator.sendMessage("hello world!");

		while(true)
		{
			try{
				Thread.sleep(1000);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public void sendMessage(String msg){
		CoapClient client = createClientFor("devices");
		/*DeviceRegistrationRequest registration = new DeviceRegistrationRequest();
		registration.setHardwareId("111-COAP-TEST-444");
		registration.setSpecificationToken("da05f689-2056-4786-ac9f-4f25b406369a");
		registration.setSiteToken("bb105f8d-3150-41f5-b9d1-db04965668d3");
		Map<String, String> metadata = new HashMap<String, String>();
		metadata.put("ipaddress", "localhost");
		registration.setMetadata(metadata);*/
		try {
			CoapResponse response = client.post(msg, MediaTypeRegistry.TEXT_PLAIN);
			System.out.println(response.getCode());
			System.out.println(response.getOptions());
			System.out.println(response.getResponseText());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected CoapClient createClientFor(String relativeUrl) {
		return new CoapClient("coap://localhost:" + COAP_PORT + "/" + relativeUrl);
	}
}

