Compare commits
11 Commits
v0.2.1-Bet
...
v0.3.0-Bet
Author | SHA1 | Date | |
---|---|---|---|
ce44ee5d17 | |||
23f75cae4f | |||
b9ba0dbcef | |||
92ef4cc0c6 | |||
ac02e4bd62 | |||
c3fb92d6eb | |||
05b15a1ea2 | |||
86dc38a8da | |||
7c8a0d3018 | |||
4b96ad576d | |||
f7383474ac |
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -4,7 +4,7 @@
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="true" project-jdk-name="13" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'com.wasteinformationserver'
|
||||
version '0.2.1-Beta'
|
||||
version '0.3.0-Beta'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
|
@ -1,3 +1,6 @@
|
||||
/**
|
||||
* dead code...
|
||||
*/
|
||||
package com.wasteinformationserver;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
|
@ -19,7 +19,7 @@ public class MySQLConnector extends Database {
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
DriverManager.setLoginTimeout(1); // TODO: 30.11.19 set higher maybe
|
||||
DriverManager.setLoginTimeout(1);
|
||||
return DriverManager.getConnection(
|
||||
"jdbc:mysql://" + host + ":" + port + "/" + dbName + "?useSSL=false",
|
||||
user,
|
||||
|
@ -3,19 +3,18 @@ package com.wasteinformationserver;
|
||||
import com.wasteinformationserver.basicutils.Info;
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import com.wasteinformationserver.mqtt.mqtt;
|
||||
import com.wasteinformationserver.mqtt.MqttService;
|
||||
import com.wasteinformationserver.website.Webserver;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class main {
|
||||
public static void main(String[] args) {
|
||||
Log.setLevel(Log.DEBUG);
|
||||
Info.init();
|
||||
|
||||
Log.setLevel(Log.INFO);
|
||||
Log.info("startup of WasteInformationServer");
|
||||
|
||||
Log.info("mem: "+Runtime.getRuntime().totalMemory());
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
@ -27,21 +26,18 @@ public class main {
|
||||
}
|
||||
}));
|
||||
|
||||
Info.init();
|
||||
Log.info("Server version: " + Info.getVersion());
|
||||
Log.debug("Build date: " + Info.getBuilddate());
|
||||
|
||||
//initial connect to db
|
||||
Log.message("initial login to db");
|
||||
new Thread(() -> {
|
||||
try {
|
||||
//JDCB.init("users", "admin0", "wasteinformation", "192.168.65.15", 1883);
|
||||
JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
|
||||
JDCB.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306);
|
||||
//JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
Log.error("no connection to db");
|
||||
}
|
||||
}).start();
|
||||
|
||||
|
||||
//startup web server
|
||||
@ -52,12 +48,10 @@ public class main {
|
||||
//startup mqtt service
|
||||
Log.message("starting mqtt service");
|
||||
try {
|
||||
mqtt m = new mqtt();
|
||||
m.notifymessage();
|
||||
MqttService m = new MqttService("mqtt.heili.eu", "1883");
|
||||
m.startupService();
|
||||
} catch (Exception e) {
|
||||
Log.error("An error occured in the class mqtt");
|
||||
}
|
||||
|
||||
Log.info("mem: "+Runtime.getRuntime().totalMemory());
|
||||
}
|
||||
}
|
174
src/java/com/wasteinformationserver/mqtt/MqttService.java
Normal file
174
src/java/com/wasteinformationserver/mqtt/MqttService.java
Normal file
@ -0,0 +1,174 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class MqttService {
|
||||
private MqttClient client = null;
|
||||
private String serveruri;
|
||||
JDCB db;
|
||||
|
||||
public MqttService(String serverurl, String port) {
|
||||
serveruri = "tcp://" + serverurl + ":" + port;
|
||||
try {
|
||||
db = JDCB.getInstance();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void startupService() {
|
||||
|
||||
try {
|
||||
client = new MqttClient(serveruri, "JavaSample42");
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setCleanSession(true);
|
||||
client.connect(connOpts);
|
||||
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
Log.error("connection lost");
|
||||
// TODO: 12.01.20 reconnect
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) {
|
||||
String message = new String(mqttMessage.getPayload());
|
||||
Log.message("received Request from PCB");
|
||||
|
||||
ResultSet res = db.executeQuery("SELECT * from devices WHERE DeviceID=" + message);
|
||||
try {
|
||||
res.last();
|
||||
if (res.getRow() != 0) {
|
||||
//existing device
|
||||
res.first();
|
||||
int cityid = res.getInt("CityID");
|
||||
if (cityid == -1) {
|
||||
//device not configured yet
|
||||
tramsmitMessage(message + ",-1");
|
||||
} else {
|
||||
checkDatabase(cityid, Integer.parseInt(message));
|
||||
}
|
||||
} else {
|
||||
//new device
|
||||
db.executeUpdate("INSERT INTO devices (DeviceID) VALUES (" + message + ")");
|
||||
Log.info("new device registered to server");
|
||||
tramsmitMessage(message + ",-1");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
client.subscribe("TopicIn");
|
||||
} catch (MqttException e) {
|
||||
Log.error("Connection to the Broker failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void checkDatabase(int citywastezoneid, int deviceid) {
|
||||
int wastetype = -1;
|
||||
ResultSet set2 = db.executeQuery("SELECT * FROM cities WHERE `id`='" + citywastezoneid + "'");
|
||||
try {
|
||||
set2.last();
|
||||
if (set2.getRow() != 1) {
|
||||
//error
|
||||
} else {
|
||||
String typ = set2.getString("wastetype");
|
||||
wastetype = getIntTyp(typ);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
ResultSet result = db.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=" + citywastezoneid);
|
||||
try {
|
||||
result.last();
|
||||
if (result.getRow() == 0) {
|
||||
//if not found in db --> send zero
|
||||
Log.debug("not found in db");
|
||||
|
||||
tramsmitMessage(deviceid + "," + wastetype + "," + 0);
|
||||
} else {
|
||||
Log.debug(result.getString("pickupdate"));
|
||||
|
||||
result.first();
|
||||
do {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
long timestamp = formatter.parse(result.getString("pickupdate")).getTime();
|
||||
long timestampnow = formatter.parse(formatter.format(new Date())).getTime();
|
||||
Log.debug("timestamp is :" + timestamp);
|
||||
|
||||
if (timestamp == timestampnow || timestamp == timestampnow + 86400000) { // 86400000 == one day
|
||||
// valid time
|
||||
tramsmitMessage(deviceid + "," + wastetype + "," + 1);
|
||||
Log.debug("valid time");
|
||||
return;
|
||||
}
|
||||
} while (result.next());
|
||||
tramsmitMessage(deviceid + "," + wastetype + "," + 0); //transmit zero if not returned before
|
||||
}
|
||||
} catch (SQLException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void tramsmitMessage(String temp) {
|
||||
Log.debug("sending message >>>"+temp);
|
||||
MqttMessage message = new MqttMessage(temp.getBytes());
|
||||
message.setQos(2);
|
||||
try {
|
||||
client.publish("TopicOut", message);
|
||||
} catch (MqttException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private String getTyp(int number) {
|
||||
if (number == 1) {
|
||||
return "Plastic";
|
||||
} else if (number == 2) {
|
||||
return "Metal";
|
||||
} else if (number == 3) {
|
||||
return "Residual waste";
|
||||
} else if (number == 4) {
|
||||
return "Biowaste";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private int getIntTyp(String temp) {
|
||||
int number = 0;
|
||||
switch (temp) {
|
||||
case "Plastic":
|
||||
number = 1;
|
||||
break;
|
||||
case "Metal":
|
||||
number = 2;
|
||||
break;
|
||||
case "Residual waste":
|
||||
number = 3;
|
||||
break;
|
||||
case "Biowaste":
|
||||
number = 4;
|
||||
break;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
}
|
@ -1,154 +0,0 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class mqtt {
|
||||
MqttClient client = null;
|
||||
|
||||
public mqtt() {
|
||||
|
||||
}
|
||||
|
||||
public void notifymessage() {
|
||||
|
||||
try {
|
||||
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample42");
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setCleanSession(true);
|
||||
client.connect(connOpts);
|
||||
} catch (MqttException e) {
|
||||
Log.error("Connection to the ESB was failed");
|
||||
}
|
||||
|
||||
mqttreceiver mr = new mqttreceiver(client);
|
||||
mr.addMessageReceivedListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Log.debug("received message");
|
||||
String[] split = e.getActionCommand().split(",");
|
||||
String wastetyp = getTyp(Integer.parseInt(split[2]));
|
||||
getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.zone FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + wastetyp + "' AND cities.zone=" + split[3] + ")", wastetyp, Integer.parseInt(split[0]));
|
||||
}
|
||||
});
|
||||
mr.getmessage();
|
||||
}
|
||||
|
||||
public void getDatabasedata(String message, String wastetyp, int clientidentify) {
|
||||
|
||||
Log.debug(message);
|
||||
Log.debug(wastetyp);
|
||||
Log.debug(clientidentify);
|
||||
|
||||
JDCB Database = null;
|
||||
try {
|
||||
Database = JDCB.getInstance();
|
||||
} catch (IOException e) {
|
||||
Log.error("No Connection to the databank");
|
||||
}
|
||||
int wastenumber = getIntTyp(wastetyp);
|
||||
|
||||
ResultSet result = Database.executeQuery(message);
|
||||
try {
|
||||
result.last();
|
||||
if (result.getFetchSize() == 0){
|
||||
//if not found in db --> send zero
|
||||
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 0);
|
||||
}
|
||||
result.first();
|
||||
while (result.next()) {
|
||||
String newDate = getDateDatabase(String.valueOf(result.getString("pickupdate")));
|
||||
String currentDate = getcurrentDate();
|
||||
String Datetomorrow = nexDayDate();
|
||||
|
||||
if (currentDate.equals(newDate) || currentDate.equals(Datetomorrow)) {
|
||||
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 1);
|
||||
} else {
|
||||
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 0);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Log.error("No data from database");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void transmitmessageAbfallart(String temp) {
|
||||
Log.debug("sending message >>>"+temp);
|
||||
mqtttransmitter mt = new mqtttransmitter(client);
|
||||
Log.debug(temp);
|
||||
mt.sendmessage(temp);
|
||||
}
|
||||
|
||||
private String nexDayDate() {
|
||||
|
||||
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||
Date currentDate = new Date();
|
||||
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(currentDate);
|
||||
c.add(Calendar.DATE, 1);
|
||||
Date currentDatePlusOne = c.getTime();
|
||||
|
||||
String temp = dateFormat.format(currentDatePlusOne);
|
||||
String split[] = temp.split("/");
|
||||
return split[2] + "." + split[1] + "." + split[0];
|
||||
}
|
||||
|
||||
private String getTyp(int number) {
|
||||
if (number == 1) {
|
||||
return "Plastic";
|
||||
} else if (number == 2) {
|
||||
return "Metal";
|
||||
} else if (number == 3) {
|
||||
return "Residual waste";
|
||||
} else if (number == 4) {
|
||||
return "Biowaste";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private int getIntTyp(String temp) {
|
||||
int number = 0;
|
||||
if (temp.equals("Plastic")) {
|
||||
number = 1;
|
||||
} else if (temp.equals("Metal")) {
|
||||
number = 2;
|
||||
} else if (temp.equals("Residual waste")) {
|
||||
number = 3;
|
||||
} else if (temp.equals("Biowaste")) {
|
||||
number = 4;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
private String getDateDatabase(String temptime) {
|
||||
String[] parts = temptime.split("-");
|
||||
String tempyear = parts[0];
|
||||
String[] yearsplit = tempyear.split("0");
|
||||
String tempyearnew = yearsplit[1];
|
||||
return parts[2] + "." + parts[1] + "." + tempyearnew;
|
||||
}
|
||||
|
||||
private String getcurrentDate() {
|
||||
GregorianCalendar now = new GregorianCalendar();
|
||||
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
|
||||
String date = df.format(now.getTime());
|
||||
String[] partstwo = date.split(",");
|
||||
return partstwo[0];
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class mqttreceiver {
|
||||
|
||||
private MqttClient client;
|
||||
public ArrayList<ActionListener> mylisteners = new ArrayList<>();
|
||||
public String message;
|
||||
|
||||
public mqttreceiver(MqttClient mqtt) {
|
||||
this.client = mqtt;
|
||||
}
|
||||
|
||||
public String getmessage() {
|
||||
|
||||
try {
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
Log.error("connection lost");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
message = new String(mqttMessage.getPayload());
|
||||
Log.info("received Request from PCB");
|
||||
notifylisteners(message);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
client.subscribe("TopicIn");
|
||||
Log.debug("subscribed topic");
|
||||
} catch (MqttException e) {
|
||||
Log.error("Connection to the ESB failed");
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
private void notifylisteners(String message) {
|
||||
for (ActionListener ac : mylisteners) {
|
||||
ac.actionPerformed(new ActionEvent(this, 0, message));
|
||||
}
|
||||
}
|
||||
|
||||
public void addMessageReceivedListener(ActionListener l) {
|
||||
mylisteners.add(l);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
|
||||
public class mqtttransmitter {
|
||||
MqttClient client;
|
||||
|
||||
public mqtttransmitter(MqttClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void sendmessage(String temp) {
|
||||
String topic = "TopicOut";
|
||||
String content = temp;
|
||||
int qos = 2;
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
|
||||
try {
|
||||
Log.debug("Connected");
|
||||
Log.debug("Publishing message: " + content);
|
||||
MqttMessage message = new MqttMessage(content.getBytes());
|
||||
message.setQos(qos);
|
||||
client.publish(topic, message);
|
||||
Log.debug("Message published");
|
||||
|
||||
|
||||
} catch (MqttException me) {
|
||||
Log.debug("reason " + me.getReasonCode());
|
||||
Log.debug("msg " + me.getMessage());
|
||||
Log.debug("loc " + me.getLocalizedMessage());
|
||||
Log.debug("cause " + me.getCause());
|
||||
Log.debug("excep " + me);
|
||||
}
|
||||
}
|
||||
}
|
@ -53,7 +53,7 @@ public class MainPage implements HttpHandler {
|
||||
Log.warning("wrong page sending 404");
|
||||
sendPage("/404Error.html", t);
|
||||
} else if (fs == null) {
|
||||
Log.warning("requested resource doesnt exist");
|
||||
Log.warning("requested resource doesnt exist --> "+path);
|
||||
} else {
|
||||
// Object exists and is a file: accept with response code 200.
|
||||
String mime = "text/html";
|
||||
|
@ -2,10 +2,7 @@ package com.wasteinformationserver.website;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.website.datarequests.AdminRequests;
|
||||
import com.wasteinformationserver.website.datarequests.DataRequest;
|
||||
import com.wasteinformationserver.website.datarequests.NewDateRequest;
|
||||
import com.wasteinformationserver.website.datarequests.RegisterRequest;
|
||||
import com.wasteinformationserver.website.datarequests.*;
|
||||
import com.wasteinformationserver.website.datarequests.login.CheckLoginState;
|
||||
import com.wasteinformationserver.website.datarequests.login.LoginRequest;
|
||||
|
||||
@ -29,6 +26,7 @@ public class Webserver {
|
||||
server.createContext("/senddata/wastedata", new DataRequest());
|
||||
server.createContext("/senddata/admindata", new AdminRequests());
|
||||
server.createContext("/senddata/newdate", new NewDateRequest());
|
||||
server.createContext("/senddata/Devicedata", new DeviceRequest());
|
||||
|
||||
server.setExecutor(null); // creates a default executor
|
||||
server.start();
|
||||
|
@ -0,0 +1,139 @@
|
||||
package com.wasteinformationserver.website.datarequests;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import com.wasteinformationserver.website.basicrequest.PostRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DeviceRequest extends PostRequest {
|
||||
@Override
|
||||
public String request(HashMap<String, String> params) {
|
||||
|
||||
JDCB jdcb = null;
|
||||
try {
|
||||
jdcb = JDCB.getInstance();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
switch (params.get("action")) {
|
||||
case "getdevices":
|
||||
ResultSet set = jdcb.executeQuery("SELECT * from devices");
|
||||
sb.append("{\"data\":[");
|
||||
try {
|
||||
while (set.next()) {
|
||||
int deviceid = set.getInt("DeviceID");
|
||||
int cityid = set.getInt("CityID");
|
||||
if (cityid == -1) {
|
||||
//not configured
|
||||
sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\"}");
|
||||
} else {
|
||||
String devicename = set.getString("DeviceName");
|
||||
String devicelocation = set.getString("DeviceLocation");
|
||||
|
||||
sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\",\"devicename\":\"" + devicename + "\",\"devicelocation\":\"" + devicelocation + "\"}");
|
||||
}
|
||||
|
||||
if (!set.isLast()) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
sb.append("]}");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
break;
|
||||
case "getCitynames":
|
||||
set = jdcb.executeQuery("select * from cities");
|
||||
Log.debug(set.toString());
|
||||
sb.append("{");
|
||||
try {
|
||||
String prev = "";
|
||||
while (set.next()) {
|
||||
if (prev.equals(set.getString("name"))) {
|
||||
|
||||
} else {
|
||||
if (!set.isFirst()) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append("\"" + set.getString("name") + "\":\"" + set.getString("name") + "\"");
|
||||
}
|
||||
prev = set.getString("name");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sb.append("}");
|
||||
Log.debug(sb.toString());
|
||||
break;
|
||||
case "getzones":
|
||||
set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC");
|
||||
Log.debug(set.toString());
|
||||
sb.append("{");
|
||||
try {
|
||||
int prev = 42;
|
||||
while (set.next()) {
|
||||
if (prev == set.getInt("zone")) {
|
||||
|
||||
} else {
|
||||
sb.append("\"" + set.getInt("zone") + "\":\"" + set.getInt("zone") + "\"");
|
||||
if (!set.isLast()) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
prev = set.getInt("zone");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sb.append("}");
|
||||
break;
|
||||
case "gettypes":
|
||||
set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC");
|
||||
Log.debug(set.toString());
|
||||
sb.append("{");
|
||||
try {
|
||||
String prev = "42";
|
||||
while (set.next()) {
|
||||
if (prev == set.getString("wastetype")) {
|
||||
|
||||
} else {
|
||||
sb.append("\"" + set.getString("wastetype") + "\":\"" + set.getString("wastetype") + "\"");
|
||||
if (!set.isLast()) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
prev = set.getString("wastetype");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sb.append("}");
|
||||
break;
|
||||
case "savetodb":
|
||||
set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'");
|
||||
try {
|
||||
set.last();
|
||||
if (set.getRow() != 1) {
|
||||
//error
|
||||
} else {
|
||||
int id = set.getInt("id");
|
||||
jdcb.executeUpdate("UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
//'action=savetodb&cityname=' + cityname + '&zonename=' + zone+'&wastetype='+wastetype+'&devicename='+devicename+'&devicelocation='+devicelocation
|
||||
break;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@
|
||||
<aside class="main-sidebar sidebar-dark-primary elevation-4">
|
||||
<!-- Brand Logo -->
|
||||
<a href="dashboard.html" class="brand-link">
|
||||
<img src="lib/AdminLTE/dist/img/AdminLTELogo.png"
|
||||
<img src="/favicon.png"
|
||||
alt="AdminLTE Logo"
|
||||
class="brand-image img-circle elevation-3"
|
||||
style="opacity: .8">
|
||||
|
@ -45,7 +45,7 @@
|
||||
<aside class="main-sidebar sidebar-dark-primary elevation-4">
|
||||
<!-- Brand Logo -->
|
||||
<a href="dashboard.html" class="brand-link">
|
||||
<img src="lib/AdminLTE/dist/img/AdminLTELogo.png" alt="AdminLTE Logo"
|
||||
<img src="/favicon.png" alt="AdminLTE Logo"
|
||||
class="brand-image img-circle elevation-3"
|
||||
style="opacity: .8">
|
||||
<span class="brand-text font-weight-light">Waste Control</span>
|
||||
@ -271,5 +271,7 @@
|
||||
|
||||
<script src="lib/AdminLTE/plugins/sweetalert2/sweetalert2.all.js"></script>
|
||||
|
||||
<script src="js/userManager.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
@ -47,7 +47,7 @@
|
||||
<aside class="main-sidebar sidebar-dark-primary elevation-4">
|
||||
<!-- Brand Logo -->
|
||||
<a href="dashboard.html" class="brand-link">
|
||||
<img src="lib/AdminLTE/dist/img/AdminLTELogo.png" alt="AdminLTE Logo"
|
||||
<img src="/favicon.png" alt="AdminLTE Logo"
|
||||
class="brand-image img-circle elevation-3"
|
||||
style="opacity: .8">
|
||||
<span class="brand-text font-weight-light">Waste Control</span>
|
||||
@ -440,12 +440,16 @@
|
||||
<script type="text/javascript"
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
|
||||
|
||||
<script type="text/javascript" src="js/dashboard.js"></script>
|
||||
<!-- DataTables -->
|
||||
<script src="lib/AdminLTE/plugins/datatables/jquery.dataTables.js"></script>
|
||||
<script src="lib/AdminLTE/plugins/datatables-bs4/js/dataTables.bootstrap4.js"></script>
|
||||
|
||||
<script src="lib/AdminLTE/plugins/sweetalert2/sweetalert2.all.js"></script>
|
||||
|
||||
<!-- OWN -->
|
||||
|
||||
<script type="text/javascript" src="js/dashboard.js"></script>
|
||||
<script src="js/userManager.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
@ -45,26 +45,12 @@
|
||||
<aside class="main-sidebar sidebar-dark-primary elevation-4">
|
||||
<!-- Brand Logo -->
|
||||
<a href="dashboard.html" class="brand-link">
|
||||
<img src="lib/AdminLTE/dist/img/AdminLTELogo.png" alt="AdminLTE Logo"
|
||||
<img src="/favicon.png" alt="AdminLTE Logo"
|
||||
class="brand-image img-circle elevation-3"
|
||||
style="opacity: .8">
|
||||
<span class="brand-text font-weight-light">Waste Control</span>
|
||||
</a>
|
||||
|
||||
<!-- Sidebar -->
|
||||
<div class="sidebar">
|
||||
<!-- Sidebar user panel (optional) -->
|
||||
<div class="user-panel mt-3 pb-3 mb-3 d-flex">
|
||||
<div class="image">
|
||||
<!-- <img src="lib/AdminLTE/dist/img/user2-160x160.jpg" class="img-circle elevation-2" alt="User Image"> -->
|
||||
<i class="nav-icon fas fa-user img-circle elevation-2" style="color:white"></i>
|
||||
</div>
|
||||
<!-- <i class="nav-icon fas fa-tachometer-alt"></i> -->
|
||||
<div class="info">
|
||||
<a href="user.html" class="d-block" id="userlabel">Username to set!</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Sidebar -->
|
||||
<div class="sidebar">
|
||||
<!-- Sidebar user panel (optional) -->
|
||||
@ -193,42 +179,32 @@
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">
|
||||
<i class="fas fa-chart-pie mr-1"></i>
|
||||
User Data
|
||||
All Devices
|
||||
</h3>
|
||||
<button id="btn-newdevice" type="button" class="btn btn-success"
|
||||
style="float:right;">Save
|
||||
</button>
|
||||
</div><!-- /.card-header -->
|
||||
<div class="card-body">
|
||||
<div class="form-group" style="margin-top: 15px; width: 10cm;">
|
||||
<!-- todo todo -->
|
||||
<label for="new_city_cityname">City/Village name</label>
|
||||
<input type="email" class="form-control" id="new_city_cityname"
|
||||
aria-describedby="emailHelp" placeholder="Enter city name here">
|
||||
<small class="form-text text-muted">Please try to use no special
|
||||
characters</small>
|
||||
</div>
|
||||
<div class="form-group" style="width: 10cm;">
|
||||
<label for="new_city_zonename">Zone Name</label>
|
||||
<input type="email" class="form-control" id="new_city_zonename"
|
||||
aria-describedby="emailHelp" placeholder="Enter Zone name here">
|
||||
<small class="form-text text-muted">Please try to use no special
|
||||
characters</small>
|
||||
</div>
|
||||
|
||||
<div class="input-group-prepend">
|
||||
<button id="dropdown-wastetype" type="button"
|
||||
class="btn btn-outline-dark dropdown-toggle"
|
||||
data-toggle="dropdown">
|
||||
Select waste type
|
||||
</button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item" href="#">Plastic</a>
|
||||
<a class="dropdown-item" href="#">Metal</a>
|
||||
<a class="dropdown-item" href="#">Residual waste</a>
|
||||
<a class="dropdown-item" href="#">Biowaste</a>
|
||||
</div>
|
||||
</div>
|
||||
<table id="table-devices" class="table table-bordered table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Device ID</th>
|
||||
<th>Devicename</th>
|
||||
<th>Devicelocation</th>
|
||||
<th>Zone</th>
|
||||
<th>X</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="devices-tablebody">
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th>Device ID</th>
|
||||
<th>Devicename</th>
|
||||
<th>Devicelocation</th>
|
||||
<th>Zone</th>
|
||||
<th>X</th>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div><!-- /.card-body -->
|
||||
</div>
|
||||
|
||||
@ -312,6 +288,7 @@
|
||||
|
||||
<script src="lib/AdminLTE/plugins/sweetalert2/sweetalert2.all.js"></script>
|
||||
|
||||
<script src="js/userManager.js"></script>
|
||||
<script src="js/device.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,6 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Login Page</title>
|
||||
<!--Made with love by Mutiullah Samim -->
|
||||
@ -18,7 +18,8 @@
|
||||
<script src="lib/bootstrap.min.js"></script>
|
||||
|
||||
<!--Fontawesome CDN-->
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css"
|
||||
integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
|
||||
|
||||
<!--Custom styles-->
|
||||
<link rel="stylesheet" type="text/css" href="css/index.css">
|
||||
@ -28,6 +29,8 @@
|
||||
<script src="lib/AdminLTE/plugins/sweetalert2/sweetalert2.all.js"></script>
|
||||
|
||||
<link rel="manifest" href="/manifest.json">
|
||||
|
||||
<link rel="icon" type="image/png" href="/favicon.png">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
|
@ -1,17 +1,4 @@
|
||||
$(document).ready(function () {
|
||||
console.log("page loaded");
|
||||
$.post('/senddata/checkloginstate', 'action=getloginstate', function (data) {
|
||||
console.log(data);
|
||||
if (data.loggedin == true) {
|
||||
$("#userlabel").html(" " + data.username);
|
||||
if (data.permission > 0) {
|
||||
$("#adminpanel").show();
|
||||
}
|
||||
} else {
|
||||
$("#userlabel").html(" not logged in!!");
|
||||
}
|
||||
}, 'json');
|
||||
|
||||
//load total collections
|
||||
$.post('/senddata/wastedata', 'action=getStartHeaderData', function (data) {
|
||||
console.log(data);
|
||||
@ -27,7 +14,7 @@ $(document).ready(function () {
|
||||
//load version footer
|
||||
//
|
||||
$.post('/senddata/wastedata', 'action=getversionandbuildtime', function (data) {
|
||||
$("#version-footer-label").html("<b>Version</b> "+data.version+" <b>Build</b> "+data.buildtime);
|
||||
$("#version-footer-label").html("<b>Version</b> " + data.version + " <b>Build</b> " + data.buildtime);
|
||||
}, 'json');
|
||||
|
||||
|
||||
@ -156,11 +143,11 @@ $(document).ready(function () {
|
||||
});
|
||||
}
|
||||
datetable = $("#table-pickupdates").DataTable({
|
||||
"order": [[ 3, "asc" ]]
|
||||
} );
|
||||
"order": [[3, "asc"]]
|
||||
});
|
||||
|
||||
//todo picupdates-tablebody
|
||||
},"json");
|
||||
}, "json");
|
||||
}
|
||||
|
||||
reloadtable();
|
||||
@ -274,7 +261,7 @@ $(document).ready(function () {
|
||||
dropdata.html("");
|
||||
console.log("clickeeeed");
|
||||
|
||||
$.post('/senddata/newdate', 'action=gettypes&cityname=' + $("#dropdown-city").html()+'&zonename='+$("#dropdown-zone").html(), function (data) {
|
||||
$.post('/senddata/newdate', 'action=gettypes&cityname=' + $("#dropdown-city").html() + '&zonename=' + $("#dropdown-zone").html(), function (data) {
|
||||
console.log(data);
|
||||
if (data.query == "ok") {
|
||||
for (var i = 0; i < data.data.length; i++) {
|
||||
@ -291,7 +278,6 @@ $(document).ready(function () {
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
@ -1,37 +1,127 @@
|
||||
$(document).ready(function () {
|
||||
$('#btn-newdevice').click(function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
Swal.showLoading({
|
||||
title: 'No connection to Database',
|
||||
html: 'Setup DB here --> <a href="index.html">click<a/>.',
|
||||
|
||||
$.post('/senddata/Devicedata', 'action=getdevices', function (data) {
|
||||
|
||||
console.log(data);
|
||||
for (var i = 0; i < data.data.length; i++) {
|
||||
var id = data.data[i].deviceid;
|
||||
var cityid = data.data[i].cityid;
|
||||
|
||||
if (cityid == -1) {
|
||||
$("#devices-tablebody").append("<tr><td>" + id + "</td><td>new Device</td><td><button deviceid=\"" + id + "\"type=\"button\" class=\"btn btn-primary configuredevicebutton\">Configure</button></td><td></td><td></td></tr>");
|
||||
} else {
|
||||
var devicename = data.data[i].devicename;
|
||||
var devicelocation = data.data[i].devicelocation;
|
||||
|
||||
$("#devices-tablebody").append("<tr><td>" + id + "</td><td>" + devicename + "</td><td>" + devicelocation + "</td><td>" + cityid + "</td><td>DEL</td></tr>");
|
||||
}
|
||||
console.log();
|
||||
//devices-tablebody
|
||||
|
||||
}
|
||||
$(".configuredevicebutton").click(function (event) {
|
||||
var id = event.target.getAttribute("deviceid");
|
||||
var cityname;
|
||||
var zone;
|
||||
var wastetype;
|
||||
var devicename;
|
||||
var devicelocation;
|
||||
|
||||
$.post('/senddata/Devicedata', 'action=getCitynames', function (data) {
|
||||
Swal.mixin({
|
||||
input: 'text',
|
||||
confirmButtonText: 'Next →',
|
||||
showCancelButton: true,
|
||||
progressSteps: ['1', '2', '3']
|
||||
}).queue([
|
||||
{
|
||||
title: 'Name of device',
|
||||
text: 'Please define a device name'
|
||||
}, {
|
||||
title: 'Location of device',
|
||||
text: 'Please define a device location'
|
||||
}, {
|
||||
title: 'City',
|
||||
text: 'Select your City',
|
||||
input: 'select',
|
||||
inputOptions: data
|
||||
}
|
||||
]).then((result) => {
|
||||
if (result.value) {
|
||||
console.log(result.value);
|
||||
const answers = JSON.stringify(result.value);
|
||||
cityname = result.value[2];
|
||||
devicename = result.value[0];
|
||||
devicelocation = result.value[1];
|
||||
|
||||
console.log("cityname=" + cityname);
|
||||
$.post('/senddata/Devicedata', 'action=getzones&cityname=' + cityname, function (data) {
|
||||
Swal.mixin({
|
||||
input: 'text',
|
||||
confirmButtonText: 'Next →',
|
||||
showCancelButton: true,
|
||||
progressSteps: ['1']
|
||||
}).queue([
|
||||
{
|
||||
title: 'City',
|
||||
text: 'Select your City',
|
||||
input: 'select',
|
||||
inputOptions: data
|
||||
}
|
||||
]).then((result) => {
|
||||
if (result.value) {
|
||||
console.log(result.value);
|
||||
zone = result.value[0];
|
||||
$.post('/senddata/Devicedata', 'action=gettypes&cityname=' + cityname + '&zonename=' + zone, function (data) {
|
||||
Swal.mixin({
|
||||
input: 'text',
|
||||
confirmButtonText: 'Next →',
|
||||
showCancelButton: true,
|
||||
progressSteps: ['1']
|
||||
}).queue([
|
||||
{
|
||||
title: 'City',
|
||||
text: 'Select your City',
|
||||
input: 'select',
|
||||
inputOptions: data
|
||||
}
|
||||
]).then((result) => {
|
||||
if (result.value) {
|
||||
console.log(result.value);
|
||||
wastetype = result.value[0];
|
||||
|
||||
$.post('/senddata/Devicedata', 'action=savetodb&deviceid=' + id + '&cityname=' + cityname + '&zonename=' + zone + '&wastetype=' + wastetype + '&devicename=' + devicename + '&devicelocation=' + devicelocation, function (data) {
|
||||
if (data.success) {
|
||||
Swal.fire({
|
||||
type: "success",
|
||||
title: 'Successfully configured!',
|
||||
html: 'This alert closes automatically.',
|
||||
timer: 1000,
|
||||
}).then((result) => {
|
||||
console.log('Popup closed. ')
|
||||
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Swal.fire({
|
||||
// type: "error",
|
||||
// title: 'No connection to Database',
|
||||
// html: 'Setup DB here --> <a href="index.html">click<a/>.',
|
||||
// onBeforeOpen: () => {
|
||||
// Swal.showLoading()
|
||||
// },
|
||||
// }).then((result) => {
|
||||
// console.log('Popup closed. ')
|
||||
//
|
||||
// });
|
||||
|
||||
|
||||
// $.post('/senddata/loginget', 'username=' + username + '&password=' + password, function (data) {
|
||||
//
|
||||
// console.log(data);
|
||||
// if (data.status == "nodbconn"){
|
||||
//
|
||||
// }
|
||||
// if (data.accept == true) {
|
||||
// console.log("successfully logged in!");
|
||||
// document.cookie = "username=" + username;
|
||||
// window.location = 'dashboard.html';
|
||||
// }
|
||||
// }, 'json');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
console.log("click..." + id);
|
||||
});
|
||||
var test = $('#table-devices').DataTable();
|
||||
}, 'json');
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
14
src/resources/wwwroot/js/userManager.js
Normal file
14
src/resources/wwwroot/js/userManager.js
Normal file
@ -0,0 +1,14 @@
|
||||
$(document).ready(function () {
|
||||
console.log("page loaded");
|
||||
$.post('/senddata/checkloginstate', 'action=getloginstate', function (data) {
|
||||
console.log(data);
|
||||
if (data.loggedin == true) {
|
||||
$("#userlabel").html(" " + data.username);
|
||||
if (data.permission > 0) {
|
||||
$("#adminpanel").show();
|
||||
}
|
||||
} else {
|
||||
$("#userlabel").html(" not logged in!!");
|
||||
}
|
||||
}, 'json');
|
||||
});
|
@ -45,7 +45,7 @@
|
||||
<aside class="main-sidebar sidebar-dark-primary elevation-4">
|
||||
<!-- Brand Logo -->
|
||||
<a href="dashboard.html" class="brand-link">
|
||||
<img src="lib/AdminLTE/dist/img/AdminLTELogo.png" alt="AdminLTE Logo"
|
||||
<img src="/favicon.png" alt="AdminLTE Logo"
|
||||
class="brand-image img-circle elevation-3"
|
||||
style="opacity: .8">
|
||||
<span class="brand-text font-weight-light">Waste Control</span>
|
||||
@ -289,5 +289,7 @@
|
||||
|
||||
<script src="lib/AdminLTE/plugins/sweetalert2/sweetalert2.all.js"></script>
|
||||
|
||||
<script src="js/userManager.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user