[JsonObject("sensorModel")]
class Sensor
{
[JsonProperty("temperature")]
public int Temperature { get; set; }
[JsonProperty("humidity")]
public int Humidity { get; set; }
[JsonProperty("pressure")]
public int Pressure { get; set; }
}
クラス名は何でもいいですが、パラメータの名前は送信側と合わせる必要があります。
最後にViewModelの処理です。
public class MainWindowViewModel : BindableBase
{
private string _title = "ラズパイモニター";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
private int _temperature;
private int _humidity;
private int _pressure;
public int Temperature
{
get { return _temperature; }
set { SetProperty(ref _temperature, value); }
}
public int Humidity
{
get { return _humidity; }
set { SetProperty(ref _humidity, value); }
}
public int Pressure
{
get { return _pressure; }
set { SetProperty(ref _pressure, value); }
}
public DelegateCommand ButtonClickCommand { get; }
public MainWindowViewModel()
{
ButtonClickCommand = new DelegateCommand(() =>
{
Thread thread = new Thread(new ThreadStart(() => {
try
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://192.168.1.3:8000/");
listener.Start();
while (true)
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest req = context.Request;
using (StreamReader reader = new StreamReader(req.InputStream, req.ContentEncoding))
{
string s = reader.ReadToEnd();
Sensor sensor = JsonConvert.DeserializeObject<Sensor>(s);
Temperature = sensor.Temperature;
Humidity = sensor.Humidity;
Pressure = sensor.Pressure;
}
HttpListenerResponse res = context.Response;
res.StatusCode = 200;
byte[] content = Encoding.UTF8.GetBytes("HELLO");
res.OutputStream.Write(content, 0, content.Length);
res.Close();
}
}
catch (Exception ex)
{
}
}));
thread.Start();
});
}
}
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
x, y = 0, 0
clear = [0, 0, 0]
colours = [[255,0,0], [0,255,0], [0,0,255], [255,255,0], [255,0,255], [0,255,255]]
colour = 0
sense.set_pixel(x, y, colours[colour])
while True:
for event in sense.stick.get_events():
#print(event.direction, event.action)
sense.set_pixel(x, y, colours[colour])
if event.action == 'pressed' and event.direction == 'up':
if y > 0:
sense.set_pixel(x, y, clear)
y -= 1
sense.set_pixel(x, y, colours[colour])
if event.action == 'pressed' and event.direction == 'down':
if y < 7:
sense.set_pixel(x, y, clear)
y += 1
sense.set_pixel(x, y, colours[colour])
if event.action == 'pressed' and event.direction == 'right':
if x < 7:
sense.set_pixel(x, y, clear)
x += 1
sense.set_pixel(x, y, colours[colour])
if event.action == 'pressed' and event.direction == 'left':
if x > 0:
sense.set_pixel(x, y, clear)
x -= 1
sense.set_pixel(x, y, colours[colour])
if event.action == 'pressed' and event.direction == 'middle':
colour += 1
if colour == len(colours):
colour = 0
sense.set_pixel(x, y, colours[colour])