-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRequest.cs
More file actions
150 lines (136 loc) · 7.08 KB
/
ServerRequest.cs
File metadata and controls
150 lines (136 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Collections;
using System.Timers;
using System.Windows;
using System.Diagnostics;
using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Tulpep.NotificationWindow;
using Timer = System.Timers.Timer;
namespace UDA_server_communication
{
class ServerRequest
{
private static System.Timers.Timer aTimer;
private Form1 main;
public int counter;
public string UDA_index1;
public string save_status;
ArrayList L = new ArrayList();
public ServerRequest(Form1 form, string f)
{
main = form;
UDA_index1 = f;
counter = 0;
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(Get_Status_UDA);
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
//Alberto: sta funzione ritorna l'url del put?? allora chiamala cosi
public string getPutUrl(string k)
{
int ik = Int32.Parse(k);
if (ik >= 0 && ik < 8)
return "https://www.sagosoft.it/_API_/cpim/luda/www/luda_20200901_0900//api/uda/put/?i="+ UDA_index1 + "&k=" + ik.ToString();
else
return "";
}
//Alberto: cosa vuol dire Recxmp1???? dai dei nomi sensati alle cose.
// Walter: Questo modulo serve per l'interrogazione con il server. Ho cambiato il suo nome in "Server_Request"
public async static Task<string> Server_Request(string url)
{
WebRequest server = HttpWebRequest.Create(url);
var response = server.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var result = await reader.ReadToEndAsync();
return result;
}
}
// Alberto: non ho capito cosa fa questa callback. qui ci devi mettere SOLO la parte di get, periodico.
// che riceve lo status e, solo se è nuovo, lo passa alla form che ha instanziato questa classe.
// si era detta, ma continui a non ascoltare, che ti saresti tenuto qui in memoria l'ultimo status
//Walter: La callback OneTimeEdvent (adesso Get_Status_UDA) serviva inizialmente per Get e Put. Ho cambiato adesso.
// Questa funzione viene invocata dal timer e serve per ottenere (get) lo stato della UDA selezionata.
// Dall' applicazione implementata da Sax, vado a cambiare lo stato della mia UDA tramite il comando PUT.
// L'UDA viene interrogata ogni secondo e solo se lo stato cambia viene mandato il messaggio di feedback
//al server tramite l'url di put. Esempio:
// L'UDA è al tempo 0 nello stato IDLE, quindi lo comunica al server, richiamando il modulo putstatus_Server.
// Per un pò resta così finchè, dalla applicazione di Sax, mando il messaggio START (k=1). Il modulo Get_Status_UDA
//fa un match con l'ultimo stato salvato. Quindi al tempo T(actual), siccome lo stato attuale e quello salvato non corrispondono,
// Get_Status_Uda chiama la funzione putstatus_Server (riga 117) che manda un messaggio al server (STARTED, tramite un put).
//Non so se mi sono spiegato bene, o se intendevi queste modifiche nel commento precedente,
//magari ho capito male io. Nel caso sentiamoci. Ho aggiunto una messagebox per segnalare il cambiamento di stato della UDA.
public async void Get_Status_UDA(object source, ElapsedEventArgs e)
{
string get_url,char_status,char_status1;
get_url = "https://www.sagosoft.it/_API_/cpim/luda/www/luda_20200901_0900//api/uda/get/?i="+ UDA_index1;
try
{
string json_string = await Server_Request(get_url);
main.Show_String(json_string, 1);
JObject json_parsed = JObject.Parse(json_string);
char_status = (string)json_parsed["status"]; //Alberto: t[88] cos'è!?!?!?!? se t cambia lunghezza non funziona piu.
// t è la versione string di una struttura dati di tipo JSON.
// tu devi fare il parsing di tale json e prendere il campo giusto.
// Walter: Ho fatto il parsing così da selezionare il campo giusto.
// Walter: La variabile main.contatore salva lo stato "attuale" per poi confrontarlo di volta in volta.
// Se il contatore è a 0, vuol dire che l'applicazione è appena partita quindi l'UDA si trova già in un suo
// status, dato dalla applicazione di Sax. Il contatore diventa quindi 1, e si passa all'else, all'interno
// del quale, si confronta lo stato attuale con quello precedente.
if (main.contatore == 0)
{
save_status = char_status;
putStatus_Server(char_status);
main.contatore++;
}
else
{
char_status1 = (string)json_parsed["status"];
if (!string.Equals(char_status1, save_status))
{
main.contatore = 0;
putStatus_Server(char_status1);
MessageBox.Show("Stato Cambiato!!", "Caption",MessageBoxButtons.OK);
}
}
}
catch (Exception ex)
{
// MessageBox.Show(Convert.ToString(ex));
throw new ApplicationException("Error", ex);
aTimer.Stop();
}
}
//Alberto: Questa classe deve esporre il metodo putStatus che setta lo status dell'uda
// e ritorna eventualmente un feedback se è riuscita a mandare l'informazione.
//Walter: Il metodo putStatus_Server, serve affinchè l'UDA possa mandare al server il messaggio
//del suo stato, quindi il feedback. Lo status dell'UDA, puoi soltato settarlo dall'applicazione di Sax se non vuoi che l'exe
// abbia alcun pulsante di interazione con l'utente.
//Quindi, se l'UDA ha per esempro ricevuto dal server, tramite la stringa PUT, il comando "START" (k=1), rimanderà al server la notifica "STARTED"
// tramite il suo comando di put.
//Il cambio nello stato del server lo si può vedere sia dalla textbox in basso a destra della Form sia dall'applicazione di Sax
public async void putStatus_Server(string status)
{
string url_put_server;
url_put_server = getPutUrl(status);
string server_status = await Server_Request(url_put_server);
main.Show_String(server_status, 2);
main.Status_Changed(status, 2);
}
}
}