241 lines
12 KiB
C#
241 lines
12 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Net.Http;
|
|||
|
|
using System.Web.Helpers;
|
|||
|
|
using System.Web.Http;
|
|||
|
|
using Microsoft.Ajax.Utilities;
|
|||
|
|
using MQTTServerSideAPI.Models;
|
|||
|
|
using System.Web;
|
|||
|
|
using System.Text;
|
|||
|
|
using TencentCloud.Common;
|
|||
|
|
using TencentCloud.Common.Profile;
|
|||
|
|
using TencentCloud.Iotcloud.V20210408;
|
|||
|
|
using TencentCloud.Iotcloud.V20210408.Models;
|
|||
|
|
using Newtonsoft.Json.Linq;
|
|||
|
|
using System.Data.SQLite;
|
|||
|
|
using System.Data;
|
|||
|
|
using static MQTTServerSideAPI.SQLiteDBHelper;
|
|||
|
|
using MySql.Data.MySqlClient;
|
|||
|
|
using System.Timers;
|
|||
|
|
using System.Web.UI.WebControls.WebParts;
|
|||
|
|
using MySqlX.XDevAPI.Relational;
|
|||
|
|
using System.Xml.Linq;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Web.UI;
|
|||
|
|
|
|||
|
|
namespace MQTTServerSideAPI.Controllers
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public class ValuesController : ApiController
|
|||
|
|
{
|
|||
|
|
public static int count = 0;
|
|||
|
|
public List<string> Get(string devname, string startT, string endT, string productid, int cmdname = 3)
|
|||
|
|
{
|
|||
|
|
//List<string> t = new List<string>();
|
|||
|
|
//t = SQLiteDBHelper.GetLOGCallin(productid, devname, startT, endT, cmdname);
|
|||
|
|
//return t;
|
|||
|
|
return SQLiteDBHelper.GetAllpowerstatisticschart(devname, startT, endT, productid, cmdname);
|
|||
|
|
}
|
|||
|
|
public static Dictionary<string, DateTime> nameData = new Dictionary<string, DateTime>();
|
|||
|
|
public void Post([FromBody] JObject mqttmessage)//这个是腾讯那边发过来的包,设备发的包是包在这个包里面的。
|
|||
|
|
{
|
|||
|
|
//SQLiteDBHelper.InsertCallin(mqttmessage);
|
|||
|
|
//Logger.LogCalloutanaSide("准备开始线程:" + count);
|
|||
|
|
Thread revT = new Thread(xianc.InsertCallindd);
|
|||
|
|
revT.Start(mqttmessage);
|
|||
|
|
//byte[] btTmp = Convert.FromBase64String(mqttmessage["payload"].ToString());//取到设备发出来的原始包。
|
|||
|
|
//Logger.LogCalloutanaSide("设备" + mqttmessage["devicename"] + "上报字节:" + GetHexStringFromByteArray(btTmp));
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
// PUT api/values/5
|
|||
|
|
public void Put(int id, [FromBody] string value)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DELETE api/values/5
|
|||
|
|
public void Delete(int id)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static class xianc
|
|||
|
|
{
|
|||
|
|
public static void InsertCallindd(object mqqt)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
JObject mqttmessage = (mqqt as JObject);
|
|||
|
|
SQLiteDBHelper.InsertCallin(mqttmessage);
|
|||
|
|
byte[] btTmp = Convert.FromBase64String(mqttmessage["payload"].ToString());//取到设备发出来的原始包。
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
|
|||
|
|
for (int i = 0; i < btTmp.Length; i++)
|
|||
|
|
{
|
|||
|
|
sb.Append(btTmp[i].ToString("X2") + " ");
|
|||
|
|
}
|
|||
|
|
FixedHeaderPart headerPart = SQLiteDBHelper.AnalysisFixedHeader(btTmp);
|
|||
|
|
string yuanshidata = GetHexStringFromByteArray(btTmp);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
//10 当前取电状态
|
|||
|
|
string strResult = "";
|
|||
|
|
headerPart.PowerStatus = btTmp[10];
|
|||
|
|
if (btTmp[10] == 0x00)
|
|||
|
|
strResult = "无效";
|
|||
|
|
else if (btTmp[10] == 0x01)
|
|||
|
|
strResult = "取电";
|
|||
|
|
else if (btTmp[10] == 0x02)
|
|||
|
|
strResult = "断电";
|
|||
|
|
else
|
|||
|
|
strResult = "无效值";
|
|||
|
|
|
|||
|
|
switch (btTmp[11])
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
case 0x04: //设备上报定期动作
|
|||
|
|
IntervalMessagePara paraInt = AnalysisIntervalMessagePara(btTmp.Skip(12).ToArray());
|
|||
|
|
string sql = "INSERT INTO deviceelectricquantity(devicename,lockElectricQuantity,PIR1ElectricQuantity,PIR2ElectricQuantity,PIR3ElectricQuantity,PIR4ElectricQuantity,GMElectricQuantity,creationtime,Power,PIR5ElectricQuantity,PIR6ElectricQuantity,PIR7ElectricQuantity,PIR8ElectricQuantity)select ";
|
|||
|
|
sql += "'" + mqttmessage["devicename"] + "',";
|
|||
|
|
sql += paraInt.LockCapacity + ",";
|
|||
|
|
sql += paraInt.PIR1Capacity + ",";
|
|||
|
|
sql += paraInt.PIR2Capacity + ",";
|
|||
|
|
sql += paraInt.PIR3Capacity + ",";
|
|||
|
|
sql += paraInt.PIR4Capacity + ",";
|
|||
|
|
sql += paraInt.GatemagnetCapacity + ",";
|
|||
|
|
sql += "'" + DateTime.Now + "', ";
|
|||
|
|
sql += "'" + paraInt.CheckP + "', ";
|
|||
|
|
sql += paraInt.PIR5Capacity + ",";
|
|||
|
|
sql += paraInt.PIR6Capacity + ",";
|
|||
|
|
sql += paraInt.PIR7Capacity + ",";
|
|||
|
|
sql += paraInt.PIR8Capacity;
|
|||
|
|
sql += " where not exists(select 1 from deviceelectricquantity where devicename = '" + mqttmessage["devicename"] + "' and creationtime > date_add('" + DateTime.Now + "', interval - 1 hour));";
|
|||
|
|
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
|||
|
|
{
|
|||
|
|
using (MySqlCommand cmd = new MySqlCommand())
|
|||
|
|
{
|
|||
|
|
connection.Open();
|
|||
|
|
cmd.Connection = connection;
|
|||
|
|
//var strSqlname = sql;
|
|||
|
|
cmd.CommandText = sql;
|
|||
|
|
int retRecAffected = cmd.ExecuteNonQuery();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string strSqlCmd = "select DeviceName from deviceinformation where `Online`= 1 and DeviceName LIKE " + "'%" + mqttmessage["devicename"] + "%'";
|
|||
|
|
DataSet ds = new DataSet();
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (DataRow row in ds.Tables[0].Rows)
|
|||
|
|
{
|
|||
|
|
string pw = "";
|
|||
|
|
if (paraInt.CheckP.ToString().Length >= 8)
|
|||
|
|
{
|
|||
|
|
pw = paraInt.CheckP.ToString().Substring(0, 8);
|
|||
|
|
}
|
|||
|
|
string updatesql = "update deviceinformation set ";
|
|||
|
|
updatesql += " powerintakestatus='" + strResult + "',";
|
|||
|
|
updatesql += "Power='" + pw + "',";
|
|||
|
|
updatesql += "pir1status='" + paraInt.PIR1OnlineStatus + "',";
|
|||
|
|
updatesql += "pir2status='" + paraInt.PIR2OnlineStatus + "',";
|
|||
|
|
updatesql += "pir3status='" + paraInt.PIR3OnlineStatus + "',";
|
|||
|
|
updatesql += "pir4status='" + paraInt.PIR4OnlineStatus + "',";
|
|||
|
|
updatesql += "gatemagnetstatus='" + paraInt.GatemagnetStatus + "',";
|
|||
|
|
updatesql += "pir5status='" + paraInt.PIR5OnlineStatus + "',";
|
|||
|
|
updatesql += "pir6status='" + paraInt.PIR6OnlineStatus + "',";
|
|||
|
|
updatesql += "pir7status='" + paraInt.PIR7OnlineStatus + "',";
|
|||
|
|
updatesql += "pir8status='" + paraInt.PIR8OnlineStatus + "' ";
|
|||
|
|
updatesql += " where DeviceName='" + row["DeviceName"] + "'";
|
|||
|
|
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
|||
|
|
{
|
|||
|
|
using (MySqlCommand cmd = new MySqlCommand())
|
|||
|
|
{
|
|||
|
|
connection.Open();
|
|||
|
|
cmd.Connection = connection;
|
|||
|
|
cmd.CommandText = updatesql;
|
|||
|
|
int retRecAffected = cmd.ExecuteNonQuery();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case 0x0C: //设备启动信息
|
|||
|
|
DeviceStartPara paradvst = AnalysisDeviceStartPara(btTmp.Skip(12).ToArray());
|
|||
|
|
|
|||
|
|
|
|||
|
|
string strSqlCmds = "select DeviceName from deviceinformation where `Online`= 1 and DeviceName LIKE " + "'%" + mqttmessage["devicename"] + "%'";
|
|||
|
|
DataSet dss = new DataSet();
|
|||
|
|
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
using (MySqlCommand cmd = new MySqlCommand())
|
|||
|
|
{
|
|||
|
|
connection.Open();
|
|||
|
|
cmd.Connection = connection;
|
|||
|
|
MySqlDataAdapter command = new MySqlDataAdapter(strSqlCmds, connection);
|
|||
|
|
command.Fill(dss);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (dss.Tables.Count > 0 && dss.Tables[0].Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (DataRow row in dss.Tables[0].Rows)
|
|||
|
|
{
|
|||
|
|
string pw = "";
|
|||
|
|
|
|||
|
|
string updatesql = "update deviceinformation set ";
|
|||
|
|
updatesql += "powerOnEquipment='" + paradvst.reason + "',";
|
|||
|
|
updatesql += "equipmenttime='" + DateTime.Now + "',";
|
|||
|
|
updatesql += "powerOnEquipmentinfo='" + paradvst.strHex + "',";
|
|||
|
|
updatesql += "yingjianbanbinfo='" + paradvst.verHardware + "',";
|
|||
|
|
updatesql += "ruanjianbanbinfo='" + paradvst.verHigh + "."+ paradvst.verLow + "' ";
|
|||
|
|
updatesql += " where DeviceName='" + row["DeviceName"] + "'";
|
|||
|
|
using (MySqlConnection connection = new MySqlConnection(strSqliteConn))
|
|||
|
|
{
|
|||
|
|
using (MySqlCommand cmd = new MySqlCommand())
|
|||
|
|
{
|
|||
|
|
connection.Open();
|
|||
|
|
cmd.Connection = connection;
|
|||
|
|
cmd.CommandText = updatesql;
|
|||
|
|
int retRecAffected = cmd.ExecuteNonQuery();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Logger.LogCalloutanaSide("直接解析错误:" + ex);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
//Thread.Sleep(20000);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
Logger.LogCalloutanaSide("多线程错误:" + ex);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|