355 lines
16 KiB
C#
355 lines
16 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Http;
|
||
using System.Web.Http;
|
||
using System.Web.Mvc;
|
||
using TencentCloud.Common.Profile;
|
||
using TencentCloud.Common;
|
||
using TencentCloud.Iotcloud.V20180614;
|
||
using TencentCloud.Iotcloud.V20180614.Models;
|
||
using Newtonsoft.Json;
|
||
using static MQTTServerSideAPI.SQLiteDBHelper;
|
||
using K4os.Compression.LZ4;
|
||
using MySqlX.XDevAPI;
|
||
using static Org.BouncyCastle.Math.EC.ECCurve;
|
||
using System.Web.UI.WebControls;
|
||
using MySql.Data.MySqlClient;
|
||
using System.Data;
|
||
using static System.Data.Entity.Infrastructure.Design.Executor;
|
||
using System.Net.NetworkInformation;
|
||
|
||
namespace MQTTServerSideAPI.Controllers
|
||
{
|
||
public class apifanhinfo
|
||
{
|
||
public int TotalCount { get; set; }
|
||
public List<deviceinformation> Devices { get; set; }
|
||
public string RequestId { get; set; }
|
||
|
||
|
||
}
|
||
public class deviceinformation
|
||
{
|
||
//public int id { get; set; }
|
||
public string DeviceName { get; set; }
|
||
public int Online { get; set; }
|
||
public string LoginTime { get; set; }
|
||
|
||
public string Version { get; set; }
|
||
public string DeviceCert { get; set; }
|
||
|
||
public string DevicePsk { get; set; }
|
||
public int DeviceType { get; set; }
|
||
public string Imei { get; set; }
|
||
|
||
public string Isp { get; set; }
|
||
public string ConnIP { get; set; }
|
||
public string LastUpdateTime { get; set; }
|
||
public string LoraDevEui { get; set; }
|
||
public int LoraMoteType { get; set; }
|
||
|
||
public string FirstOnlineTime { get; set; }
|
||
public string LastOfflineTime { get; set; }
|
||
public string CreateTime { get; set; }
|
||
public int LogLevel { get; set; }
|
||
public int CertState { get; set; }
|
||
public int EnableState { get; set; }
|
||
public string ClientIP { get; set; }
|
||
public string FirmwareUpdateTime { get; set; }
|
||
public int CreateUserId { get; set; }
|
||
public string NBIoTDeviceID { get; set; }
|
||
}
|
||
public class LogsController : ApiController
|
||
{
|
||
// GET api/<controller>
|
||
public List<string> Get(string Name, int zhant)
|
||
{
|
||
return SQLiteDBHelper.GetAllCallinBYName(Name, zhant);
|
||
}
|
||
public List<string> Get(string productid, string devicename, string shijian, int cmdname)
|
||
{
|
||
return SQLiteDBHelper.jianzaogduo(productid, devicename, shijian, cmdname);
|
||
}
|
||
public void Get()
|
||
{
|
||
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();
|
||
// }
|
||
|
||
// }
|
||
// }
|
||
//}
|
||
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();
|
||
}
|
||
}
|
||
//sql = "insert into programeventlog(eventname,modifycontent,Online,ip,ctime) value(";
|
||
//sql += "'访问腾讯云所以设备列表,修改本地设备状态',";
|
||
//sql += "'" + list[i].DeviceName + "',";
|
||
//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[i].Online == 0 || list[i].Online == 3)
|
||
{
|
||
sql = "UPDATE deviceinformation set districtinfo = ";
|
||
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();
|
||
}
|
||
}
|
||
////日志记录
|
||
//sql = "insert into programeventlog(eventname,modifycontent,ip,ctime) value(";
|
||
//sql += "'访问腾讯云所以设备列表,修改本地设备地址消息为空',";
|
||
//sql += "'设备名称:" + list[i].DeviceName + "',";
|
||
//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();
|
||
// }
|
||
//}
|
||
}
|
||
}
|
||
}
|
||
//日志记录
|
||
//List<deviceinformation> listdsd = new List<deviceinformation>();
|
||
//for (int j = 0; j < list.Count(); j++)
|
||
//{
|
||
// deviceinformation listdsds = shujiu.Devices.Where(x => x.Online != devOnline[j]).FirstOrDefault();
|
||
// if (listdsds != null)
|
||
// {
|
||
// listdsd.Add(listdsds);
|
||
// }
|
||
|
||
//}
|
||
|
||
//for (int p = 0; p < list.Count(); p++)
|
||
//{
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
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();
|
||
}
|
||
}
|
||
////日志记录
|
||
//sql = "insert into programeventlog(eventname,modifycontent,Online,ip,ctime) value(";
|
||
//sql += "'访问腾讯云所以设备列表,添加设备消息',";
|
||
//sql += "'" + list[i].DeviceName + "',";
|
||
//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();
|
||
// }
|
||
//}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
catch (Exception e)
|
||
{
|
||
Console.WriteLine(e.ToString());
|
||
}
|
||
}
|
||
|
||
public List<string> Get(string productid, string devicename, string stime, string etime,int cmdname)
|
||
{
|
||
List<string> t = new List<string>();
|
||
t = SQLiteDBHelper.GetLOGCallin(productid, devicename, stime, etime, cmdname);
|
||
return t;
|
||
}
|
||
|
||
// POST api/<controller>
|
||
public void Post([FromBody] string value)
|
||
{
|
||
}
|
||
|
||
// PUT api/<controller>/5
|
||
public void Put(int id, [FromBody] string value)
|
||
{
|
||
}
|
||
|
||
// DELETE api/<controller>/5
|
||
public void Delete(int id)
|
||
{
|
||
}
|
||
}
|
||
} |