Testing Smartthings sensors with some adjustments for 4th benchmark
[iot2.git] / iotjava / iotruntime / master / ZigbeeConfig.java
1 package iotruntime.master;
2
3 // Java packages
4 import java.io.IOException;
5 import java.net.DatagramPacket;
6 import java.net.DatagramSocket;
7 import java.net.InetAddress;
8 import java.net.SocketException;
9
10 /** Class ZigbeeConfig is a class that configures the zigbee
11  *  gateway in our network with the relevant policies
12  *  through the usage of static methods;
13  *
14  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
15  * @version     1.0
16  * @since       2016-05-04
17  */
18 public final class ZigbeeConfig {
19
20         /**
21          * Constants
22          */
23         public final int SOCKET_SEND_BUFFER_SIZE = 1024;
24         public final int SOCKET_RECEIVE_BUFFER_SIZE = 1024;
25
26         /**
27          * ZigbeeConfig properties
28          */
29         private String strZigbeeGatewayAddress;
30         private int iZigBeeGatewayPort;
31         private int iMasterPort;
32         private DatagramSocket socket;
33         private boolean bVerbose;
34
35         /**
36          * Class constructor
37          */
38         public ZigbeeConfig(String _strZigbeeGatewayAddress, int _iZigBeeGatewayPort, int _iMasterPort, boolean _bVerbose) 
39                 throws SocketException {
40
41                 strZigbeeGatewayAddress = _strZigbeeGatewayAddress;
42                 iZigBeeGatewayPort = _iZigBeeGatewayPort;
43                 iMasterPort = _iMasterPort;
44                 bVerbose = _bVerbose;
45
46                 socket = new DatagramSocket(iMasterPort);
47                 socket.setSendBufferSize(SOCKET_SEND_BUFFER_SIZE);
48                 socket.setReceiveBufferSize(SOCKET_RECEIVE_BUFFER_SIZE);
49
50                 RuntimeOutput.print("ZigbeeConfig: Zigbee gateway policy support for: " + 
51                         strZigbeeGatewayAddress + " with IoTMaster port: " + iMasterPort + 
52                         " and gateway port: " + iZigBeeGatewayPort, bVerbose);
53         }
54
55         /**
56          * clearAllPolicies() method to delete the zigbee policies
57          *
58          * @return  void
59          */
60         public void clearAllPolicies() throws IOException {
61
62                 // Clearing all policies on Zigbee gateway
63                 RuntimeOutput.print("ZigbeeConfig: Accessing Zigbee gateway and deleting policies...", bVerbose);
64
65                 String strMessage = "type: policy_clear\n";
66                 DatagramPacket dpSendPacket = new DatagramPacket(strMessage.getBytes(), 
67                         strMessage.getBytes().length, InetAddress.getByName(strZigbeeGatewayAddress), iZigBeeGatewayPort);
68                 socket.send(dpSendPacket);
69
70                 RuntimeOutput.print("ZigbeeConfig: Sending policy message to Zigbee gateway: " + strMessage, bVerbose);
71         }
72
73
74         /**
75          * setPolicy() method to set policies
76          *
77          * @param       String  Host address where the Zigbee driver is running as assigned by master
78          * @param       int     Host port number
79          * @param       String  String Zigbee device address
80          * @return  void
81          */
82         public void setPolicy(String strHostAddress, int iHostPort, String strZigbeeAddress) throws IOException {
83
84                 // Clearing all policies on Zigbee gateway
85                 RuntimeOutput.print("ZigbeeConfig: Accessing Zigbee gateway and sending a policy...", bVerbose);
86
87                 String strMessage = "type: policy_set\n";
88                 strMessage += "ip_address: " + strHostAddress + "\n";
89                 strMessage += "port: " + iHostPort + "\n";
90                 strMessage += "device_address_long: " + strZigbeeAddress + "\n";
91
92                 DatagramPacket dpSendPacket = new DatagramPacket(strMessage.getBytes(), 
93                         strMessage.getBytes().length, InetAddress.getByName(strZigbeeGatewayAddress), iZigBeeGatewayPort);
94                 socket.send(dpSendPacket);
95
96                 RuntimeOutput.print("ZigbeeConfig: Sending policy message to Zigbee gateway: " + strMessage, bVerbose);
97         }
98         
99         /**
100          * closeConnection()
101          *
102          * @return  void
103          */
104         public void closeConnection() {
105                 socket.close();
106         }
107 }