forked from cpe433-264/CPE433-64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PM25Plugin.cs
73 lines (66 loc) · 2.51 KB
/
PM25Plugin.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Text.Json;
using System.Runtime.Serialization.Json;
using System.Net;
using System.IO;
namespace DNWS
{
class PM25Plugin : IPlugin
{
private static readonly HttpClient client = new HttpClient();
protected static PM25Reading pm25reading = null;
public PM25Plugin()
{
if (pm25reading == null) {
RequestAqiCn();
}
}
public void RequestAqiCn()
{
HttpWebRequest http = (HttpWebRequest) WebRequest.Create("http://api.waqi.info/feed/shanghai/?token=demo");
HttpWebResponse response = (HttpWebResponse) http.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string content = sr.ReadToEnd();
pm25reading = JsonSerializer.Deserialize<PM25Reading>(content);
}
public void PreProcessing(HTTPRequest request)
{
throw new NotImplementedException();
}
public HTTPResponse GetResponse(HTTPRequest request)
{
if (pm25reading == null) {
RequestAqiCn();
} else {
DateTime now = DateTime.Now;
TimeSpan diff = now.Subtract(pm25reading.data.time.iso.DateTime);
if (diff >= new TimeSpan(1, 0, 0)) {
RequestAqiCn();
}
}
HTTPResponse response = null;
StringBuilder sb = new StringBuilder();
// sb.Append("<html><body>");
// sb.Append("Location : " + pm25reading.data.city.name + "<br />");
// sb.Append("Date/Time : " + pm25reading.data.time.iso + "<br />");
// sb.Append("PM25 : " + pm25reading.data.iaqi.pm25.v + "<br />");
// sb.Append("PM10 : " + pm25reading.data.iaqi.pm10.v + "<br />");
// sb.Append("</body></html>");
response = new HTTPResponse(200);
// response.body = Encoding.UTF8.GetBytes(sb.ToString());
String PM25_reader = JsonSerializer.Serialize(pm25reading); //convert to .json format
response.body = Encoding.UTF8.GetBytes(PM25_reader);
return response;
}
public HTTPResponse PostProcessing(HTTPResponse response)
{
throw new NotImplementedException();
}
}
}