📄 serverthread_sensor.java
字号:
package Utopia;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import javax.comm.*;
/**
* This class is used to converting sensor data into ontology
* and save the file as ontology format
* @author Administrator
*/
class ServerThread_Sensor extends Thread {
File file = new File("envModified.owl");
BufferedReader inFromClient;
DataOutputStream outToClient;
String requestMessageLine;
String fileName;
Socket socket;
Document document;
LampControl lampControl = new LampControl();
// TurnOffLamp sendcom1 = new TurnOffLamp();
//there are only two types of sensors: rfid and Oscilloscope sensor.
protected static String sensorvalue[] = new String[3];
protected static String rfidvalue[] = new String[4];
public ServerThread_Sensor(Socket cs) {
socket = cs;
try {
inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outToClient = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println("ERROR: " + e);
}
createDocument();
}
@Override
public void run() {
try {
System.out.println("ServerThread_Sensor 53: Started .. ");
requestMessageLine = inFromClient.readLine();
System.out.println(requestMessageLine);
StringTokenizer tokenStr = new StringTokenizer(requestMessageLine, ",");
String current_moteID = tokenStr.nextToken();
if (!current_moteID.equals("EVENT")) {
String current_illumination = tokenStr.nextToken();
String current_date = tokenStr.nextToken();
String current_time = tokenStr.nextToken();
CreateIlluminationEnvironmentElement(current_moteID, current_illumination, current_date, current_time);
// CreateRFIDEnvironmentElement(current_moteID, current_illumination, current_date, current_time);
printToFile();
System.out.println("Sensor data generated successfully in the file");
System.out.println("");
} else {
String enterence = tokenStr.nextToken();
String door = tokenStr.nextToken();
String id = tokenStr.nextToken();
String name = tokenStr.nextToken();
String position = tokenStr.nextToken();
String department = tokenStr.nextToken();
String enter_time = tokenStr.nextToken();
CreateRFIDEnvironmentElement(enterence, door, name, enter_time);
printToFile();
System.out.println("RFID data generated successfully in the file");
System.out.println("");
}
System.out.println("Generated file successfully.");
System.out.println("");
socket.close();
} catch (IOException e) {
}
}
private void CreateIlluminationEnvironmentElement(String current_moteID,
String current_illumination, String current_date,
String current_time) {
/*
* if the ontology database is not used well, then use these command to
* control the light directly.
* */
if (Integer.valueOf(current_illumination) > 15000) {
//turn off the lamp
lampControl.TurnOff();
System.out.println("turn off the lamp~~~");
}
else{
//turn on the lamp
lampControl.TurnOn();
System.out.println("turn on the lamp~~~");
}
int classcount = 0;
String eventvalue[] = new String[3];
String illumination = "illumination";
String date = "date";
String time = "time";
String valueArttribute[] = new String[3];
valueArttribute[0] = "illumination";
valueArttribute[1] = "date";
valueArttribute[2] = "time";
/*
String previous_moteID= "0";
String previous_temperature = "20";
String previous_humidity = "30";
String previous_date = "20070411";
String previous_time= "115500";
*/
eventvalue[0] = current_illumination;
eventvalue[1] = current_date;
eventvalue[2] = current_time;
if (sensorvalue[0] == null || sensorvalue[1] == null || sensorvalue[2] == null) {
System.out.println("this is the first time to run the program. Save the value");
for (int i = 0; i < 3; i++) {
sensorvalue[i] = eventvalue[i];
System.out.println(sensorvalue[i] + "||" + eventvalue[i]);
}
}
boolean compare_illumination;
boolean compare_date;
boolean compare_time;
boolean ch[] = new boolean[3];
compare_illumination = !sensorvalue[0].equals(eventvalue[0]);
ch[0] = compare_illumination;
compare_date = !sensorvalue[1].equals(eventvalue[1]);
ch[1] = compare_date;
compare_time = !sensorvalue[2].equals(eventvalue[2]);
ch[2] = compare_time;
for (int i = 0; i < 3; i++) {
if (ch[i]) {
System.out.println("The value of '" + valueArttribute[i] + "' is " + sensorvalue[i] +
". This attribute changed to the value: " + eventvalue[i]);
sensorvalue[i] = eventvalue[i];
classcount++;
}
}
if (compare_illumination || compare_date || compare_time) {
System.out.println("current_moteID : " + current_moteID);
System.out.println("current_illumination : " + current_illumination);
System.out.println("current_date : " + current_date);
System.out.println("current_time : " + current_time);
System.out.println("");
Element rdf = document.getDocumentElement();
Element environmentInfo = document.createElement("EnvironmentInfo");
environmentInfo.setAttribute("rdf:ID", current_illumination + current_time);
rdf.appendChild(environmentInfo);
Element Illumination = document.createElement("Illumination");
Illumination.setAttribute("rdf:resource", "#_" + current_illumination);
environmentInfo.appendChild(Illumination);
Element Date = document.createElement("Date");
Date.setAttribute("rdf:resource", "#_" + current_date);
environmentInfo.appendChild(Date);
Element Time = document.createElement("Time");
Time.setAttribute("rdf:resource", "#_" + current_time);
environmentInfo.appendChild(Time);
}
//this part below, I don't konw what mean
if (compare_illumination) {
String classname = "Illumination";
OntologyEventProvider event_illumination = new OntologyEventProvider();
event_illumination.SaveEvent(classname, classcount); // 锟教猴拷飘 锟剿革拷
System.out.println("event test = illumination");
// event_illumination.socket();
}
if (compare_date) {
String classname = "Date";
OntologyEventProvider event_date = new OntologyEventProvider();
event_date.SaveEvent(classname, classcount); // 锟教猴拷飘 锟剿革拷
System.out.println("event test = date");
// event_date.socket();
}
if (compare_time) {
String classname = "Time";
OntologyEventProvider event_time = new OntologyEventProvider();
event_time.SaveEvent(classname, classcount); // 锟教猴拷飘 锟剿革拷
System.out.println("event test = time");
// event_time.socket();
}
// return environmentInfo;
}
private void createDocument() {
//get an instance of factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocumentBuilder();
//What's this file's format?
try {
// document = db.parse(new File("\\\\168.131.150.213\\WebRoot\\owl\\env.owl"));
document = db.parse(file);
} catch (Exception ex) {
}
} catch (ParserConfigurationException pce) {
//dump it
System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
System.exit(1);
//RUNNING = 0; HOOKS = 1; FINALIZERS = 2;
}
}
/**
*
* @param enterence1
* @param door1
* @param name1
* @param enter_time1
*/
private void CreateRFIDEnvironmentElement(String enterence1, String door1, String name1, String enter_time1) {
System.out.println(name1 + "雼橃澊 鞛呾灔頃橃叏鞀惦媹雼
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -