using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using BLW_Log.Models; using Common; using CommonEntity; using IotManager.Common; using IotManager.job; using IotManager.private_key; using IotManager.services; using MathNet.Numerics.Interpolation; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using MySQLAccess.PGModels; using Quartz; using RestSharp; using ViewModels; namespace IotManager { public class DomainSettings { public string[] Ok { get; set; } } public class Program { static async Task TTT() { try { using PostgresContext dbcontext = new PostgresContext(); var userdata = await dbcontext.Deviceinfos.Select(A => new DeviceCacheInfo { ClientId = A.ClientId, SecretKey = A.SecretKey }).ToListAsync(); RestClient client1 = new RestClient("http://iot-manage.uts-data.com:5001"); string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var request1 = new RestRequest("/api/Values/SetDeviceInfoChache", Method.Post); request1.AddJsonBody(userdata); var re = await client1.ExecuteAsync(request1); } catch (Exception) { } await Task.CompletedTask; } public static void Main(string[] args) { //var iv = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff }; //byte[] key = new byte[] { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; //AFC1E534C82933EFA5805F21AFA494DE06534D055D432953299A46A4983614C60B5FA21C19843D var builder = WebApplication.CreateBuilder(args); var domainSettings = builder.Configuration.GetSection("AllowDomain").Get(); builder.Services.AddSingleton(); CSRedisCacheHelper.Configuration = builder.Configuration; // 注册MQTT客户端服务 // 启动时自动连接 builder.Services.AddMemoryCache(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); //builder.Services.AddHostedService(); //builder.Host.UseOrleansClient(ooo=> //{ // ooo.UseLocalhostClustering(); //}); builder.Services.AddControllers(); //builder.Services.AddDbContext(options => //{ // options.UseNpgsql(builder.Configuration.GetConnectionString("PGSqlStr")); //}); builder.Services.AddDbContextFactory(options => options.UseNpgsql(builder.Configuration.GetConnectionString("PGSqlStr"))); //builder.Services.AddScoped(); builder.Services.AddCors(options => { options.AddPolicy(name: "Vue3", policy => { //policy.WithOrigins(domainSettings.Ok) policy .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); builder.Services.AddAuthorization(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(option => { string DefaultKey = "~7#UZ~:-G6HDC7'!1uwq+f@9,zaJ4DH4JdM#PYyc9HxFT.^M7M}i40qM~?^Qt;mF98;21+Zn'67>>04=GOj2]P:-1p;G@ln03^h~19A]gB1308e57#&MtpM4n53UF1N}"; var sec = Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"] ?? DefaultKey); option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = builder.Configuration["JwT:Issuer"], ValidAudience = builder.Configuration["JwT:Audience"], IssuerSigningKey = new SymmetricSecurityKey(sec) }; option.Events = new JwtBearerEvents { OnMessageReceived = context => { var token = context.Request.Headers["token"].FirstOrDefault(); if (string.IsNullOrEmpty(token)) { // 如果没有找到 token 头部,则继续检查 Authorization 头部 token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); } // 如果找到了 token,则将其设置到 HttpContext 中 if (!string.IsNullOrEmpty(token)) { context.Token = token; } return Task.CompletedTask; } }; }); builder.Services.AddQuartz(q => { //var jobKey1 = new JobKey("ExcelGenerateJob"); //q.AddJob(opts => opts.WithIdentity(jobKey1)); //q.AddTrigger(opts => opts // .ForJob(jobKey1) // .WithIdentity("ExcelGenerateJob-trigger") // .StartAt(DateBuilder.TodayAt(h, m, s)) // .WithCalendarIntervalSchedule(x => x // .WithIntervalInDays(1) // .InTimeZone(TimeZoneInfo.Local)) //); //q.AddTrigger(opts => opts // .ForJob(jobKey1) // .WithIdentity("ExcelGenerateJob-trigger") // .WithCronSchedule("0 0 17 * * ?", (schedule) => // { // schedule.WithMisfireHandlingInstructionDoNothing().InTimeZone(TimeZoneInfo.Local); // }) // 每天17:00 //); ///真的还是假的 var jobKey2 = new JobKey("ShuJuTongBu"); q.AddJob(opts => opts.WithIdentity(jobKey2)); q.AddTrigger(opts => opts .ForJob(jobKey2) .WithIdentity("ShuJuTongBuScan-trigger") .WithCalendarIntervalSchedule(x => { x.WithIntervalInMinutes(30).Build(); //x.WithIntervalInSeconds(2).Build(); }) ); //真的还是假的 //var jobKey3 = new JobKey("DeleteFile"); //q.AddJob(opts => opts.WithIdentity(jobKey3)); //q.AddTrigger(opts => opts // .ForJob(jobKey3) // .WithIdentity("DeleteFile-trigger") // .WithCronSchedule("0 0 1 * * ?", (schedule) => // { // schedule.WithMisfireHandlingInstructionDoNothing().InTimeZone(TimeZoneInfo.Local); // }) // 每天17:00 //); }); builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true); var app = builder.Build(); app.UseHttpsRedirection(); app.UseCors("Vue3"); app.UseRouting(); app.UseAuthentication(); // 添加认证中间件 app.UseAuthorization(); // 使用授权中间件 app.MapControllers(); StaticData.GetWebAPIMethod(); StaticData.GetWebAPIMethod1(); //TTT().Wait(); //HHH(); //IronPython.Runtime.PythonList QQQ = StaticData.scope2.GetVariable("DeviceId"); //var QQQC= QQQ.Select(A=>(int)A).ToList(); //foreach (var item in QQQC) //{ // int a = (int)item; //} //string QQQ1 = Newtonsoft.Json.JsonConvert.SerializeObject(QQQ); //var QQQ2 = Newtonsoft.Json.JsonConvert.DeserializeObject>(QQQ1); app.Run(); } public static void HHH() { byte[] decryptedContent = new byte[] { 0x03, 0x00, 0x04, 0x00 }; string HexData = Tools.ByteToString(decryptedContent); //燃气报警数据为:03 00 04 00 Console.WriteLine("燃气报警数据为:" + HexData); byte 燃气报警状态 = decryptedContent[0]; byte 报警状态1 = decryptedContent[2]; //0x04是报警 //0x03是取消报警 if (decryptedContent.Length == 4) { if (报警状态1 == 0x04) { IronPython.Runtime.PythonList QQQ = StaticData.scope2.GetVariable("MobileNo"); IronPython.Runtime.PythonList III = StaticData.scope2.GetVariable("DeviceId"); } } } } }