using System;
using System.Text;
using HDL_ON.DAL.Net;
using HDL_ON.Entity.Enumerative;
using Shared;
using Shared.Net;
namespace HDL_ON.DAL
{
public static class CommonPage
{
public static bool IsRemote;
//public static AirQuality AirQuality;
public static Action RefreshAir;
public static bool FindGateway = false;
public static bool FindGatewayChilren = false;
public static string FindGatewayChilrenIPAddress = new NetWiFi ().BroadcastIpAddress.ToString ();
public static bool LocalPhoneFindDevice = false;
private static string ip = "0.0.0.0";
public static System.Net.IPEndPoint EndPoint {
get {
try {
if (FindGateway) {
return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new NetWiFi ().BroadcastIpAddress.ToString ()), 6000);
} else if (FindGatewayChilren) {
try {
return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (FindGatewayChilrenIPAddress), 6000);
} catch {
return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("224.0.168.188"), 6000);
}
} else {
return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new NetWiFi ().BroadcastIpAddress.ToString ()), 6000);
}
} catch {
//防止异常导致程序退出
return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("127.0.0.1"), 6000);
}
}
}
///保存设备备注才用gb2312,其他情况用utf8
public static Encoding MyEncodingUTF8 = Encoding.UTF8;//Get
public static Encoding MyEncodingGB2312 {
get {
try {
if (System.Globalization.CultureInfo.InstalledUICulture.EnglishName.ToUpper().StartsWith("CZECH")) {
return System.Text.Encoding.GetEncoding (1250);
} else {
return Encoding.GetEncoding ("gb2312");
}
}catch (Exception ex) {
MainPage.Log ("MyEncodingGB2312 Erorr : "+ex.Message);
return Encoding.GetEncoding ("gb2312");
}
}
}
static bool isHttpListenerStart;
public static DateTime dt;
//public static byte currentSubnetID = 0;
public static void InitHttpListener ()
{
if (isHttpListenerStart) {
return;
}
HttpListener.Start (new NetWiFi ().IpAddress, 6001);
HttpListener.EventHandler -= httpListener_EventHandler;
HttpListener.EventHandler += httpListener_EventHandler;
isHttpListenerStart = true;
}
public static void CloseHttpListener ()
{
//isHttpListenerStart = false;
//return;
if (!isHttpListenerStart)
return;
HttpListener.Close ();
isHttpListenerStart = false;
}
///
/// 初始化处理socket接收的数据
///
public static void InitReceiveEvent ()
{
Packet.ReceiveEvent += Packet_ReceiveEvent;
}
public static float floatChange (byte b1, byte b2, byte b3, byte b4)
{
byte [] byteTemp = new byte [4] { b4, b3, b2, b1 };
return BitConverter.ToSingle (byteTemp, 0);
}
static void Packet_ReceiveEvent (byte subnetID, byte deviceID, Command command, byte [] usefullBytes, string revGatewayIP)
{
}
///
/// byte 变字符串
///
/// The to hex16.
/// The blue component.
public static string byteToHex16 (byte b)
{
string s = Convert.ToString (b, 16).ToUpper ();
if (s.Length <= 1) {
return "0" + s;
}
return s;//
}
static void httpListener_EventHandler (System.Collections.Specialized.NameValueCollection nameValueCollection, System.IO.Stream outputStream, System.IO.Stream inputStream)
{
try {
if (nameValueCollection ["Command"] != null && nameValueCollection ["Command"].StartsWith ("Get")) {
string tempFileName = nameValueCollection ["Command"].Replace ("Get", "");
if ("AllFiles" == tempFileName) {
byte [] bytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (MyIO.FileUtils.ReadFiles ()));
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Flush ();
} else {
byte [] bytes;
if (!MyIO.FileUtils.Exists (tempFileName)) {
bytes = System.Text.Encoding.UTF8.GetBytes ("文件名不存在!");
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Flush ();
return;
}
System.IO.FileStream fs = new System.IO.FileStream (Application.RootPath + tempFileName, System.IO.FileMode.Open);
bytes = new byte [fs.Length];
fs.Read (bytes, 0, bytes.Length);
fs.Close ();
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Flush ();
}
} else if (nameValueCollection ["Command"].StartsWith ("Upload")) {
string path = Application.RootPath + nameValueCollection ["Command"].Replace ("Upload", "");
string dePath = nameValueCollection ["Command"].Replace ("Upload", "");
} else {
byte [] bytes = System.Text.Encoding.UTF8.GetBytes ("请求命令无效!");
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Flush ();
}
} catch (Exception ex) {
MainPage.Log ("httpListener_EventHandler" + ex.ToString ());
}
}
///
/// 随机数高位
///
public static byte RandomHigh;
///
/// 随机数低位
///
public static byte RandomLow;
///
/// Updates Device's remake.
///
public static void UpdateRemark (byte subnetID, byte deviceID, string remark)
{
byte [] updateBytes = new byte [20];
byte [] remakeBytes = MyEncodingGB2312.GetBytes (remark);
Array.Copy (remakeBytes, 0, updateBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
Control.ControlBytesSend (Command.UpdataRemake, subnetID, deviceID, updateBytes);
}
///
/// 监听请求的Http端口
///
public static int Port = 5555;
static void httpListener_EventHandler (string rawUrl, System.IO.Stream outputStream, System.IO.Stream inputStream)
{
foreach (var musicInfo in MusicInfo.MusicInfoList) {
if ("audio-item-" + musicInfo.ID == rawUrl.TrimStart ('/')) {
var file = new System.IO.FileStream (musicInfo.Data, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte [] bytes = new byte [1024];
try {
while (file.CanRead) {
int len = file.Read (bytes, 0, bytes.Length);
if (len == 0) {
break;
}
outputStream.Write (bytes, 0, len);
}
} catch { }
file.Close ();
break;
}
}
}
}
}