This commit is contained in:
Rémi Heredero 2023-01-10 08:51:22 +01:00
parent 913f1b7914
commit 5e002b3e7e
3 changed files with 166 additions and 26 deletions

View File

@ -8,12 +8,9 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="028e861c-982b-48af-abd9-036e0fbebabe" name="Default Changelist" comment=""> <list default="true" id="028e861c-982b-48af-abd9-036e0fbebabe" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/stream/UIClientTCP.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/doc/ch/hevs/synd/sin/utils/Utility.html" beforeDir="false" afterPath="$PROJECT_DIR$/doc/ch/hevs/synd/sin/utils/Utility.html" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/network/server/Transaction.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/network/server/Transaction.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/sensor/Measurement.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/sensor/Measurement.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/sensor/Sensor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/sensor/Sensor.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/utils/Utility.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/utils/Utility.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -30,31 +27,41 @@
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="HighlightingSettingsPerFile">
<setting file="file://$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/UICommands.java" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/network/server/Transaction.java" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/sensor/Measurement.java" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/sensor/MeasurementType.java" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/stream/UIClientTCP.java" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/src/main/java/ch/hevs/utils/Utility.java" root0="FORCE_HIGHLIGHTING" />
</component>
<component name="MarkdownSettingsMigration"> <component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" /> <option name="stateVersion" value="1" />
</component> </component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="useMavenConfig" value="true" />
</MavenGeneralSettings>
</option>
</component>
<component name="ProjectId" id="22EYrhtGQVdO4ZyPvaEpHP9sLPu" /> <component name="ProjectId" id="22EYrhtGQVdO4ZyPvaEpHP9sLPu" />
<component name="ProjectLevelVcsManager">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState"> <component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">{
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> &quot;keyToString&quot;: {
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
<property name="WebServerToolWindowFactoryState" value="false" /> &quot;last_opened_file_path&quot;: &quot;C:/Users/remi/Downloads/PlatformIOLion-0.2/PlatformIOLion/lib/PlatformIOLion-0.2.jar&quot;,
<property name="aspect.path.notification.shown" value="true" /> &quot;settings.editor.selected.configurable&quot;: &quot;discord-project&quot;
<property name="last_opened_file_path" value="$PROJECT_DIR$" /> }
<property name="node.js.detected.package.eslint" value="true" /> }</component>
<property name="node.js.detected.package.tslint" value="true" /> <component name="RunManager" selected="Application.Task1">
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="node.js.selected.package.tslint" value="(autodetect)" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.0" />
<property name="project.structure.side.proportion" value="0.0" />
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
</component>
<component name="RunManager">
<configuration default="true" type="ArquillianTestNG" factoryName="" nameIsGenerated="true"> <configuration default="true" type="ArquillianTestNG" factoryName="" nameIsGenerated="true">
<option name="arquillianRunConfiguration"> <option name="arquillianRunConfiguration">
<value> <value>
@ -68,6 +75,24 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="Task1" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="ch.hevs.synd.sin.stream.UIClientTCP" />
<module name="UIServerStudents" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Task2" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="ch.hevs.synd.sin.network.server.Transaction" />
<module name="UIServerStudents" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.Task2" />
<item itemvalue="Application.Task1" />
</list>
</component> </component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager"> <component name="TaskManager">

View File

@ -1,10 +1,17 @@
package ch.hevs.synd.sin.network.server; package ch.hevs.synd.sin.network.server;
import ch.hevs.synd.sin.UICommands;
import ch.hevs.synd.sin.UIConstants;
import ch.hevs.synd.sin.sensor.MeasurementType;
import ch.hevs.synd.sin.sensor.Sensor; import ch.hevs.synd.sin.sensor.Sensor;
import ch.hevs.utils.Utility;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;
public class Transaction { public class Transaction {
@ -41,7 +48,15 @@ public class Transaction {
/* */ /* */
/* **************************************************************************************************************** */ /* **************************************************************************************************************** */
public boolean processTransaction() { public boolean processTransaction() {
// TODO: Put your code here...
boolean r = true;
while (r) {
try {
r = transaction(new String(Utility.readLine(_in)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return false; return false;
} }
@ -54,7 +69,25 @@ public class Transaction {
// Private method which will interpret the given command and react in to it... // Private method which will interpret the given command and react in to it...
private boolean transaction(String dataIn) throws IOException { private boolean transaction(String dataIn) throws IOException {
// TODO: Analyze here the given action (dataIn) and react in to it by sending the correct reply to the output stream // TODO: Analyze here the given action (dataIn) and react in to it by sending the correct reply to the output stream
return false; String s;
switch (dataIn){
case "getu":
s = String.valueOf(_uSensor.getMeasurement().getValue());
s += _uSensor.getMeasurement().getType();
Utility.writeLine(_out, s.getBytes());
return true;
case "geti":
s = String.valueOf(_iSensor.getMeasurement().getValue());
s += _iSensor.getMeasurement().getType();
Utility.writeLine(_out, s.getBytes());
return true;
case "stop":
return false;
default:
s = UICommands.INVALID_COMMND;
Utility.writeLine(_out, s.getBytes());
return true;
}
} }
@ -63,7 +96,26 @@ public class Transaction {
/* MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- */ /* MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- MAIN -- */
/* */ /* */
/* **************************************************************************************************************** */ /* **************************************************************************************************************** */
public static void main(String[] args) { public static void main(String[] args) throws IOException {
// TODO: Put your code here ServerSocket ss = new ServerSocket(UIConstants.UI_SERVER_PORT);
Sensor uS = new Sensor(MeasurementType.Voltage,0);
Sensor iS = new Sensor(MeasurementType.Current, 90);
boolean process = true;
while (true){
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
Transaction t = new Transaction(is, os, uS, iS);
process = t.processTransaction();
if(socket.isClosed()) continue;
socket.close();
}
//uS.shutdown();
//iS.shutdown();
} }
} }

View File

@ -0,0 +1,63 @@
package ch.hevs.synd.sin.stream;
import ch.hevs.synd.sin.UICommands;
import ch.hevs.synd.sin.UIConstants;
import ch.hevs.utils.Utility;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Objects;
public class UIClientTCP {
public static void main(String[] args) {
// Open a TCP socket
try {
Socket socket = new Socket("sdi.hevs.ch", UIConstants.UI_SERVER_PORT);
getui(socket, 5, 5);
//System.out.println("Receiv: \n" + getui(socket, 5, 5));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Server can not be reached");
System.exit(-1);
}
}
public static String get(Socket s, String cmd) throws IOException {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
Utility.writeLine(os, cmd.getBytes());
return new String(Utility.readLine(is));
}
public static String getu(Socket s) throws IOException {
return get(s, UICommands.GET_U_SHORT_COMMAND);
}
public static String geti(Socket s) throws IOException {
return get(s, UICommands.GET_I_SHORT_COMMAND);
}
public static String getui(Socket s) throws IOException {
String received = "";
received += getu(s);
received += "\n";
received += geti(s);
return received;
}
public static void getui(Socket s, int z, int n) throws IOException {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
String cmdU = UICommands.GET_U_SHORT_COMMAND + ", " + z*1000 + ", " + n;
String cmdI = UICommands.GET_I_SHORT_COMMAND + ", " + z*1000 + ", " + n;
//getu(s);
Utility.writeLine(os, cmdU.getBytes());
Utility.writeLine(os, cmdI.getBytes());
while (true){
System.out.println(new String(Utility.readLine(is)));
}
}
}