342 lines
16 KiB
C#
342 lines
16 KiB
C#
using FluentScheduler;
|
||
using MQTTServerSideAPI.Controllers;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using System.Web.Http;
|
||
using System.Web.Mvc;
|
||
using System.Web.Optimization;
|
||
using System.Web.Routing;
|
||
using TencentCloud.Common.Profile;
|
||
using TencentCloud.Common;
|
||
using WebGrease;
|
||
using MySql.Data.MySqlClient;
|
||
using TencentCloud.Iotcloud.V20180614;
|
||
using TencentCloud.Iotcloud.V20180614.Models;
|
||
using System.Net.NetworkInformation;
|
||
using System.Threading;
|
||
|
||
namespace MQTTServerSideAPI
|
||
{
|
||
|
||
|
||
|
||
public class WebApiApplication : System.Web.HttpApplication
|
||
{
|
||
protected void Application_Start()
|
||
{ //注入定时任务
|
||
MyRegistry.GoWork();
|
||
AreaRegistration.RegisterAllAreas();
|
||
GlobalConfiguration.Configure(WebApiConfig.Register);
|
||
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
|
||
RouteConfig.RegisterRoutes(RouteTable.Routes);
|
||
BundleConfig.RegisterBundles(BundleTable.Bundles);
|
||
|
||
}
|
||
|
||
}
|
||
public class MyRegistry : Registry
|
||
{
|
||
public static string strSqliteConn = @"Server=blv-cloud-db.mysql.rds.aliyuncs.com;Database=mqqtmysql;Uid=blv_rcu;Pwd=fnadiaJDIJ7546;charset=utf8;port=3307;";
|
||
|
||
//无参构造函数
|
||
public MyRegistry()
|
||
{
|
||
//每30秒更新一次设备状态
|
||
Schedule(() =>
|
||
{
|
||
try
|
||
{
|
||
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
||
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
||
Credential cred = new Credential
|
||
{
|
||
SecretId = "AKIDKhz84sbEqFPRK1Uo0zUnwwjNp4884vc4",
|
||
SecretKey = "JQ8qZGxgQAtbCfvxb7VqzxujOtFVb1KT"
|
||
};
|
||
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||
ClientProfile clientProfile = new ClientProfile();
|
||
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||
HttpProfile httpProfile = new HttpProfile();
|
||
httpProfile.Endpoint = ("iotcloud.tencentcloudapi.com");
|
||
clientProfile.HttpProfile = httpProfile;
|
||
|
||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||
IotcloudClient client = new IotcloudClient(cred, "ap-guangzhou", clientProfile);
|
||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||
DescribeDevicesRequest req = new DescribeDevicesRequest();
|
||
req.ProductId = "HICL5RNXAU";
|
||
req.Offset = 0;
|
||
req.Limit = 250;
|
||
// 返回的resp是一个DescribeDevicesResponse的实例,与请求对象对应
|
||
DescribeDevicesResponse resp = client.DescribeDevicesSync(req);
|
||
// 输出json格式的字符串回包
|
||
var abs = AbstractModel.ToJsonString(resp);
|
||
apifanhinfo shujiu = JsonConvert.DeserializeObject<apifanhinfo>(abs);
|
||
DataSet ds = new DataSet();
|
||
|
||
string strSqlCmd = "SELECT DeviceName,Online FROM deviceinformation ";
|
||
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
||
{
|
||
|
||
using (MySqlCommand cmd = new MySqlCommand())
|
||
{
|
||
connection.Open();
|
||
cmd.Connection = connection;
|
||
MySqlDataAdapter command = new MySqlDataAdapter(strSqlCmd, connection);
|
||
command.Fill(ds);
|
||
}
|
||
}
|
||
List<string> devname = new List<string>();
|
||
List<int> devOnline = new List<int>();
|
||
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
|
||
{
|
||
string namessdd = ds.Tables[0].Rows[i]["DeviceName"].ToString();
|
||
int on = int.Parse(ds.Tables[0].Rows[i]["Online"].ToString());
|
||
devOnline.Add(on);
|
||
devname.Add(namessdd);
|
||
}
|
||
string sql = "";
|
||
List<deviceinformation> list = shujiu.Devices.Where(x => devname.Contains(x.DeviceName)).ToList();
|
||
|
||
List<deviceinformation> lists = shujiu.Devices.Where(x => !devname.Contains(x.DeviceName)).ToList();
|
||
|
||
string ip = "";
|
||
|
||
// 获取所有网络接口
|
||
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
||
|
||
// 遍历每个网络接口
|
||
foreach (NetworkInterface networkInterface in interfaces)
|
||
{
|
||
// 获取IP地址
|
||
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
|
||
UnicastIPAddressInformationCollection ipAddresses = ipProperties.UnicastAddresses;
|
||
|
||
// 遍历每个IP地址
|
||
foreach (UnicastIPAddressInformation ipAddress in ipAddresses)
|
||
{
|
||
// 判断是否为IPv4地址
|
||
if (ipAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||
{
|
||
//过滤本地ip
|
||
if (ipAddress.Address.ToString() != "127.0.0.1")
|
||
{
|
||
//局域网ip
|
||
ip = ipAddress.Address.ToString();
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
sql = "insert into programeventlog(eventname,modifycontent,ip,ctime) value(";
|
||
sql += "'访问腾讯云所以设备列表,修改本地全部设备状态,每30秒执行一次',";
|
||
sql += "'全部设备',";
|
||
//sql += list[i].Online + ",";
|
||
sql += "'" + ip + "',";
|
||
sql += "'" + DateTime.Now + "')";
|
||
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
||
{
|
||
using (MySqlCommand cmd = new MySqlCommand())
|
||
{
|
||
connection.Open();
|
||
cmd.Connection = connection;
|
||
cmd.CommandText = sql;
|
||
int retRecAffected = cmd.ExecuteNonQuery();
|
||
}
|
||
}
|
||
if (list.Count() > 0)
|
||
{ //sql案例
|
||
//UPDATE deviceinformation set Online = 1 where DeviceName = 'F305-00178'
|
||
for (int i = 0; i < list.Count(); i++)
|
||
{
|
||
sql = "UPDATE deviceinformation set Online = ";
|
||
sql += list[i].Online;
|
||
sql += " where DeviceName = ";
|
||
sql += "'" + list[i].DeviceName + "'";
|
||
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
||
{
|
||
using (MySqlCommand cmd = new MySqlCommand())
|
||
{
|
||
connection.Open();
|
||
cmd.Connection = connection;
|
||
var strSql = sql;
|
||
cmd.CommandText = strSql;
|
||
int retRecAffected = cmd.ExecuteNonQuery();
|
||
}
|
||
}
|
||
|
||
|
||
if (list[i].Online == 0 || list[i].Online == 3)
|
||
{
|
||
sql = "UPDATE deviceinformation set districtinfo = ";
|
||
sql += "' ',";
|
||
sql += "powerintakestatus=";
|
||
sql += "' ',";
|
||
sql += "Power=";
|
||
sql += "' ',";
|
||
sql += "pir1status=";
|
||
sql += "' ',";
|
||
sql += "pir2status=";
|
||
sql += "' ',";
|
||
sql += "pir3status=";
|
||
sql += "' ',";
|
||
sql += "pir4status=";
|
||
sql += "' ',";
|
||
sql += "gatemagnetstatus=";
|
||
sql += "' '";
|
||
sql += " where DeviceName = ";
|
||
sql += "'" + list[i].DeviceName + "'";
|
||
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
||
{
|
||
using (MySqlCommand cmd = new MySqlCommand())
|
||
{
|
||
connection.Open();
|
||
cmd.Connection = connection;
|
||
var strSql = sql;
|
||
cmd.CommandText = strSql;
|
||
int retRecAffected = cmd.ExecuteNonQuery();
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
if (lists.Count() > 0)
|
||
{
|
||
//sql案例
|
||
// insert into deviceinformation(DeviceName, Online, LoginTime, Version,
|
||
//DeviceCert, DevicePsk, DeviceType, Imei, Isp, ConnIP, LastUpdateTime, LoraDevEui,LoraMoteType, FirstOnlineTime, LastOfflineTime, CreateTime, LogLevel, CertState, EnableState,
|
||
//ClientIP, FirmwareUpdateTime, CreateUserId, NBIoTDeviceID)value(
|
||
//'1', 1, '1211321', '12331424', '123424242253', 'bdsnnskj', 2, 'daghj', 'safdag', 'vsghs','11', '1213', 1, 'wqewe', 'ahdauf', 'adawd', 12, 2, 3, 'dwda', 'dada', 12, '1221212')
|
||
for (int i = 0; i < lists.Count(); i++)
|
||
{
|
||
sql = "insert into deviceinformation(DeviceName, Online, LoginTime, Version,DeviceCert, DevicePsk, DeviceType, Imei, Isp, ConnIP, LastUpdateTime, LoraDevEui,LoraMoteType, FirstOnlineTime, LastOfflineTime, CreateTime, LogLevel, CertState, EnableState,ClientIP, FirmwareUpdateTime, CreateUserId, NBIoTDeviceID) value(";
|
||
sql += "'" + lists[i].DeviceName + "',";
|
||
sql += lists[i].Online + ",";
|
||
sql += "'" + lists[i].LoginTime + "',";
|
||
sql += "'" + lists[i].Version + "',";
|
||
sql += "'" + lists[i].DeviceCert + "',";
|
||
sql += "'" + lists[i].DevicePsk + "',";
|
||
sql += lists[i].DeviceType + ",";
|
||
sql += "'" + lists[i].Imei + "',";
|
||
sql += "'" + lists[i].Isp + "',";
|
||
sql += "'" + lists[i].ConnIP + "',";
|
||
sql += "'" + lists[i].LastUpdateTime + "',";
|
||
sql += "'" + lists[i].LoraDevEui + "',";
|
||
sql += lists[i].LoraMoteType + ",";
|
||
sql += "'" + lists[i].FirstOnlineTime + "',";
|
||
sql += "'" + lists[i].LastOfflineTime + "',";
|
||
sql += "'" + lists[i].CreateTime + "',";
|
||
sql += lists[i].LogLevel + ",";
|
||
sql += lists[i].CertState + ",";
|
||
sql += lists[i].EnableState + ",";
|
||
sql += "'" + lists[i].ClientIP + "',";
|
||
sql += "'" + lists[i].FirmwareUpdateTime + "',";
|
||
sql += lists[i].CreateUserId + ",";
|
||
sql += "'" + lists[i].NBIoTDeviceID + "')";
|
||
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
||
{
|
||
using (MySqlCommand cmd = new MySqlCommand())
|
||
{
|
||
connection.Open();
|
||
cmd.Connection = connection;
|
||
var strSql = sql;
|
||
cmd.CommandText = strSql;
|
||
int retRecAffected = cmd.ExecuteNonQuery();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
catch (Exception e)
|
||
{
|
||
Console.WriteLine(e.ToString());
|
||
}
|
||
//SQLiteDBHelper.ClearAllCallinRecord();
|
||
//if (!SyncHotelServer.SyncHotelGroups())
|
||
// Logs.WriteTimingUDPLog("2:00执行定时任务--清除酒店失败~");
|
||
}).ToRunNow().AndEvery(30).Seconds();
|
||
|
||
//立即执行,之后每月的第一个星期一3点再执行
|
||
//Schedule(() =>
|
||
//{
|
||
// string sql = "";
|
||
// string ip = "";
|
||
|
||
// // 获取所有网络接口
|
||
// NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
||
|
||
// // 遍历每个网络接口
|
||
// foreach (NetworkInterface networkInterface in interfaces)
|
||
// {
|
||
// // 获取IP地址
|
||
// IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
|
||
// UnicastIPAddressInformationCollection ipAddresses = ipProperties.UnicastAddresses;
|
||
|
||
// // 遍历每个IP地址
|
||
// foreach (UnicastIPAddressInformation ipAddress in ipAddresses)
|
||
// {
|
||
// // 判断是否为IPv4地址
|
||
// if (ipAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||
// {
|
||
// //过滤本地ip
|
||
// if (ipAddress.Address.ToString() != "127.0.0.1")
|
||
// {
|
||
// //局域网ip
|
||
// ip = ipAddress.Address.ToString();
|
||
// }
|
||
|
||
// }
|
||
// }
|
||
// }
|
||
// sql = "insert into programeventlog(eventname,modifycontent,ip,ctime) value(";
|
||
// sql += "'清除数据库uploginfo',";
|
||
// sql += "'30天的数据',";
|
||
// //sql += list[i].Online + ",";
|
||
// sql += "'" + ip + "',";
|
||
// sql += "'" + DateTime.Now + "')";
|
||
// using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
||
// {
|
||
// using (MySqlCommand cmd = new MySqlCommand())
|
||
// {
|
||
// connection.Open();
|
||
// cmd.Connection = connection;
|
||
// cmd.CommandText = sql;
|
||
// int retRecAffected = cmd.ExecuteNonQuery();
|
||
// }
|
||
// }
|
||
// //log.LogTrace("job started at " + DateTime.Now);
|
||
// //Thread.Sleep(10000);
|
||
// //log.LogTrace("job ended at" + DateTime.Now);
|
||
//}).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
|
||
|
||
|
||
//Schedule(() =>
|
||
//{
|
||
|
||
//}).ToRunNow().AndEvery(30).Seconds();
|
||
//.ToRunEvery(0).Days().At(21, 00);
|
||
//Schedule(() =>
|
||
//{
|
||
// SyncHotelServer.SyncAutho();
|
||
//}).ToRunEvery(0).Days().At(2, 05);
|
||
//每天2点
|
||
//Schedule<MyJob>().ToRunEvery(0).Days().At(2, 10);
|
||
}
|
||
/// <summary>
|
||
/// 同步与自动授权
|
||
/// </summary>
|
||
public static void GoWork()
|
||
{
|
||
JobManager.Initialize(new MyRegistry());
|
||
}
|
||
}
|
||
}
|