将Python执行结果传输到.NET内核
我在做一个物联网项目。该项目的目标是建立一个物联网鸟巢,鸟巢由运行Raspbian操作系统(Linux)的Raspberry Pi 4照顾 尽管.NET Core支持物联网,但仍缺少许多设备绑定,对物联网的支持仍有待提高。 每个人都在使用Python进行iOT编程,这是一种语言,尽管流行的iOT没有任何好看的结构或冗长 我的目的是收集将Python执行结果传输到.NET内核,python,c#,linux,.net-core,raspbian,Python,C#,Linux,.net Core,Raspbian,我在做一个物联网项目。该项目的目标是建立一个物联网鸟巢,鸟巢由运行Raspbian操作系统(Linux)的Raspberry Pi 4照顾 尽管.NET Core支持物联网,但仍缺少许多设备绑定,对物联网的支持仍有待提高。 每个人都在使用Python进行iOT编程,这是一种语言,尽管流行的iOT没有任何好看的结构或冗长 我的目的是收集 巢外环境温度 巢内环境温度 巢内湿度 鸟巢中鸟的运动跟踪 控制用于加热巢的白炽灯泡 压电式湿巢喷雾器的控制 使用16x2 LCD屏幕在nest上显示消息 我不喜欢
using DigitalNest.DataModels;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi;
namespace DigitalNest
{
public class Program
{
public static IConfigurationRoot configuration;
private static Sensors sensors;
private static string connectionString;
private static void ConfigureServices(IServiceCollection serviceCollection)
{
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.Build();
serviceCollection.AddSingleton<IConfigurationRoot>(configuration);
var sensors = new Sensors();
configuration.Bind("Sensors", sensors);
serviceCollection.AddSingleton(sensors);
}
public Program(Sensors sensor)
{
sensors = sensor;
connectionString = configuration.GetConnectionString("DigitalNestDB");
}
private static void RecordTemprature(string temprature)
{
var bufferQuery = $"SELECT COUNT(*) FROM Tempratures";
var count = SqlHelper.Query<int>(bufferQuery, connectionString).FirstOrDefault();
if (count > sensors.DHT.TempBuffer)
{
var truncateQuery = $"DELETE FROM Tempratures WHERE `Event` IS NOT NULL ORDER BY `Event` ASC LIMIT 1";
SqlHelper.Query<int>(truncateQuery, connectionString);
}
else
{
var temp = new Temp
{
Temprature = temprature,
Event = DateTime.UtcNow
};
SqlHelper.Insert<Temp>(temp, connectionString);
}
}
private static void RecordHumidity(string humidity)
{
var bufferQuery = $"SELECT COUNT(*) FROM Humidities";
var count = SqlHelper.Query<int>(bufferQuery, connectionString).FirstOrDefault();
if (count > sensors.DHT.HumidityBuffer)
{
var truncateQuery = $"DELETE FROM Humidities WHERE `Event` IS NOT NULL ORDER BY `Event` ASC LIMIT 1";
SqlHelper.Query<int>(truncateQuery, connectionString);
}
else
{
var humid = new Humid
{
Humidity = humidity,
Event = DateTime.UtcNow
};
SqlHelper.Insert<Humid>(humid, connectionString);
}
}
private static void RecordMotion(bool isMotion)
{
var bufferQuery = $"SELECT COUNT(*) FROM Motion";
var count = SqlHelper.Query<int>(bufferQuery, connectionString).FirstOrDefault();
if (count > sensors.PIR.MotionBuffer)
{
var truncateQuery = $"DELETE FROM Motion WHERE `Event` IS NOT NULL ORDER BY `Event` ASC LIMIT 1";
SqlHelper.Query<int>(truncateQuery, connectionString);
}
else
{
var motion = new Motion
{
IsMotion = isMotion,
Event = DateTime.UtcNow
};
SqlHelper.Insert<Motion>(motion, connectionString);
}
}
private static void ReadDHTSensor()
{
while (true)
{
try
{
var stdio = sensors.DHT.Bash.BashExecute();
var output = JsonConvert.DeserializeObject<DHTData>(stdio);
RecordTemprature(output.Temprature);
RecordHumidity(output.Humidity);
Console.WriteLine(stdio);
Thread.Sleep(sensors.DHT.RefreshRate);
}
catch (Exception ex)
{
Console.WriteLine("DHT Error");
Thread.Sleep(sensors.DHT.RefreshRate);
}
}
}
private static void ReadPIRSensor()
{
while (true)
{
try
{
var stdio = sensors.PIR.Bash.BashExecute();
Console.WriteLine($"{stdio}");
var output = JsonConvert.DeserializeObject<PIRData>(stdio);
RecordMotion(output.Motion);
Console.WriteLine(stdio);
Thread.Sleep(sensors.PIR.RefreshRate);
}
catch (Exception ex)
{
Console.WriteLine("PIR Error");
Thread.Sleep(sensors.PIR.RefreshRate);
}
}
}
private static void GenerateUltrasonicSound()
{
while (true)
{
try
{
Console.WriteLine("Sounding..");
var passiveBuzzer = (GpioPin)Pi.Gpio[BcmPin.Gpio26];
passiveBuzzer.SoftToneFrequency = sensors.UltrasonicBuzzer.AudioFrequency;
Thread.Sleep(sensors.UltrasonicBuzzer.RefreshRate);
passiveBuzzer.SoftToneFrequency = 0;
}
catch (Exception ex)
{
Console.WriteLine("Ultrasonic PiezoBuzzer Error - " + ex.ToString());
Thread.Sleep(sensors.UltrasonicBuzzer.RefreshRate);
}
}
}
private static void Main(string[] args)
{
ServiceCollection serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
sensors = serviceProvider.GetService<Sensors>();
connectionString = configuration.GetConnectionString("DigitalNestDB");
Pi.Init<BootstrapWiringPi>();
Parallel.Invoke(
() => ReadPIRSensor(),
() => ReadDHTSensor(),
() => GenerateUltrasonicSound()
);
while (true)
{
}
}
}
}
使用Ironpython包调用python并从中检索结果
public static string BashExecute(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}