Doorlock and outlet drivers made by Yuting and Jiawei; the only problem is that outle...
[iot2.git] / benchmarks / other / DoorlockAndOutlet / ZigbeeTest_outlet.java
1 import java.util.Map;
2 import java.net.*;
3 import java.io.*;
4 import java.util.*;
5
6 import iotruntime.*;
7 import iotruntime.zigbee.*;
8
9 import iotcode.interfaces.*;
10 import java.rmi.RemoteException;
11
12 //public class ZigbeeTest_outlet implements IoTZigbeeCallback {
13 public class ZigbeeTest_outlet implements SmartthingsSensorCallback {
14     public final int SOCKET_SEND_BUFFER_SIZE = 1024;
15     public final int SOCKET_RECEIVE_BUFFER_SIZE = 1024;
16     private static final String MY_IP_ADDRESS = "127.0.0.1";
17     public static final String DEVIDE_MAC_ADDRESS = "000d6f0005782e31"; //outlet sensor
18
19     public void newReadingAvailable(SmartthingsSensor _sensor) {
20         //public void newReadingAvailable(SmartthingsSensor _sensor) throws RemoteException {
21         System.out.println("New Message!!!!");
22         System.out.println(((OutletSensor)_sensor).getWatts());
23     }
24
25     public static void main(String[] args) throws UnknownHostException, SocketException, InterruptedException, IOException, IOException, RemoteException{
26
27         String message = "type: policy_set\n";
28         message += "ip_address: " + MY_IP_ADDRESS + "\n"; // local ip address
29         message += "port: " + "5959\n";  // port number
30         message += "device_address_long: " + DEVIDE_MAC_ADDRESS + "\n";
31         DatagramPacket sendPacket = new DatagramPacket(message.getBytes(), message.getBytes().length, InetAddress.getByName("127.0.0.1"), 5005); // address and port of the gateway which means Raspberry PI's IP address
32         DatagramSocket socket = new DatagramSocket(12345/*test number*/);
33         socket.setSendBufferSize(4096);
34         socket.setReceiveBufferSize(4096);
35         socket.send(sendPacket);
36         socket.setReuseAddress(true);
37         socket.close();
38
39         IoTDeviceAddress zigUdpAddr  = new IoTDeviceAddress("127.0.0.1", 5959, 5005,false,false);
40         IoTZigbeeAddress zigAddrLong = new IoTZigbeeAddress(DEVIDE_MAC_ADDRESS);
41
42         Set<IoTZigbeeAddress> zigSet = new HashSet<IoTZigbeeAddress>();
43         zigSet.add(zigAddrLong);
44         IoTSet<IoTZigbeeAddress> zigIotSet = new IoTSet<IoTZigbeeAddress>(zigSet);
45
46         Set<IoTDeviceAddress> devSet = new HashSet<IoTDeviceAddress>();
47         devSet.add(zigUdpAddr);
48         IoTSet<IoTDeviceAddress> devIotSet = new IoTSet<IoTDeviceAddress>(devSet);
49         OutletSensor sen = new OutletSensor(devIotSet, zigIotSet);
50         
51         System.out.println("About to init");
52         sen.init();
53
54         ZigbeeTest_outlet zTest = new ZigbeeTest_outlet();
55         sen.registerCallback(zTest);
56         sen.TurnOn();
57         sen.TurnOff();
58         sen.TurnOn();
59         System.out.println("Watt:");
60         System.out.println(sen.getWatts());
61         sen.TurnOff();
62         System.out.println("Loop Begin");
63         while (true) {
64
65         }
66     }
67
68 }