初始化CRICS
This commit is contained in:
294
RCUHost/Implement/AbnormalAlarmHandler.cs
Normal file
294
RCUHost/Implement/AbnormalAlarmHandler.cs
Normal file
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class AbnormalAlarmHandler
|
||||
{
|
||||
public IAlarmSettingRepository AlarmSettingRepository { get; set; }
|
||||
public IRoomServiceRepository RoomServiceRepository { get; set; }
|
||||
public IRoomServiceRecordRepository RoomServiceRecordRepository { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 处理“非客人在房门关”异常
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
public void FKRZFMG(Host host)
|
||||
{
|
||||
// "非客人在房门关报警" 设置
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A05");
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
bool isUpdated = false;
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C06");
|
||||
if (abnormity != null && host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 && !host.DoorLockStatus)
|
||||
{
|
||||
isUpdated = true;
|
||||
var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A06");
|
||||
if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
|
||||
{
|
||||
if (!abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “非客人在房门关”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C06");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//启用线程执行延时报警
|
||||
Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
|
||||
{
|
||||
if (!abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “非客人在房门关”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C06");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!isUpdated)
|
||||
{
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “非客人在房门关 记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理“客人在房门开”异常
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
public void KRZFMK(Host host)
|
||||
{
|
||||
// "客人在房门开报警" 设置
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A01");
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID == 0x20 && host.DoorLockStatus)
|
||||
{
|
||||
//报警延时设置
|
||||
var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A02");
|
||||
if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C01");
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “客人在房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C01");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//启用线程执行延时报警
|
||||
Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C01");
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “客人在房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C01");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C01");
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “客人在房门开”异常 记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
abnormityRecord.Status = false;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理“无人房门开”异常
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
public void WRFMK(Host host)
|
||||
{
|
||||
// "无人在房门开报警" 设置
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A03");
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
if (host.RoomCard == null && host.DoorLockStatus)
|
||||
{
|
||||
//报警延时设置
|
||||
var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A04");
|
||||
if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C02");
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “无人房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C02");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//启用线程执行延时报警
|
||||
Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C02");
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “无人房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C02");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C02");
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “无人房门开”异常 记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
abnormityRecord.Status = false;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理“非客人在保险箱开”异常
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
public void FKRZBXXK(Host host)
|
||||
{
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C03");
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, abnormitySetting.Code);
|
||||
if (abnormitySetting != null && abnormity != null)
|
||||
{
|
||||
bool isUpdated = false;
|
||||
//如果非客人,且已出租,且保险箱开,则报警
|
||||
if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 &&
|
||||
host.RoomStatus != null && host.RoomStatus.ID == 0x02 && host.SafeStatus == 1)
|
||||
{
|
||||
isUpdated = true;
|
||||
if (!abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C03", true);
|
||||
if (abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormitySetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isUpdated)//关掉报警
|
||||
{
|
||||
if (abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C03", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
308
RCUHost/Implement/AirConditionStatusReceiver.cs
Normal file
308
RCUHost/Implement/AirConditionStatusReceiver.cs
Normal file
@@ -0,0 +1,308 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class AirConditionStatusReceiver : GenericReceiverBase, IAirConditionStatusReceiver
|
||||
{
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
public IHostAirRepository HostAirRepository { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 应用空调设置
|
||||
/// </summary>
|
||||
/// <param name="hostAir"></param>
|
||||
public void SendAirConditionSetting(HostAir hostAir, Host host)
|
||||
{
|
||||
byte[] data = CreateAirConditionSettingDataPacket(hostAir);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 空调单个属性下发
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="airNo"></param>
|
||||
/// <param name="property"></param>
|
||||
/// <param name="status"></param>
|
||||
/// <param name="seasonData">季节数据</param>
|
||||
public void SetAirProperty(Host host, int airNo, AirProperty property, int status, byte[] seasonData = null)
|
||||
{
|
||||
if (property == AirProperty.CompensatoryTemp ||
|
||||
property == AirProperty.LockTemp ||
|
||||
property == AirProperty.SleepMode ||
|
||||
property == AirProperty.Time)
|
||||
{
|
||||
throw new ApplicationException("无效的属性值,请使用对应方法设置。");
|
||||
}
|
||||
byte[] statusData = new byte[1];
|
||||
if (property == AirProperty.Season)
|
||||
{
|
||||
statusData = new byte[12];
|
||||
statusData = seasonData;
|
||||
}
|
||||
else
|
||||
{
|
||||
statusData[0] = (byte)status;
|
||||
}
|
||||
var data = CreateAirPropertyDataPacket(airNo, property, statusData);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置空调补偿温度
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="airNo"></param>
|
||||
/// <param name="compensatoryTemp"></param>
|
||||
public void SetCompensatoryTemp(Host host, int airNo, float compensatoryTemp)
|
||||
{
|
||||
byte[] statusData = new byte[1];
|
||||
|
||||
statusData[0] = ConvertCompensatoryTempToHost(compensatoryTemp);
|
||||
|
||||
var data = CreateAirPropertyDataPacket(airNo, AirProperty.CompensatoryTemp, statusData);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置空调锁定温度
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="airNo"></param>
|
||||
/// <param name="isLock"></param>
|
||||
/// <param name="lockTemp"></param>
|
||||
public void SetLockTemp(Host host, int airNo, bool isLock, int lockTemp)
|
||||
{
|
||||
byte[] statusData = new byte[2];
|
||||
statusData[0] = (byte)(isLock ? 1 : 0);
|
||||
statusData[1] = (byte)lockTemp;
|
||||
|
||||
var data = CreateAirPropertyDataPacket(airNo, AirProperty.LockTemp, statusData);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置空调睡眠模式
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="airNo"></param>
|
||||
/// <param name="sleepFlag"></param>
|
||||
/// <param name="SleepDevition"></param>
|
||||
/// <param name="sleepStartTime"></param>
|
||||
/// <param name="sleepEndTime"></param>
|
||||
public void SetSleepMode(Host host, int airNo, bool sleepFlag, int sleepDevition, string sleepStartTime, string sleepEndTime)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(sleepFlag);
|
||||
writer.Write((byte)sleepDevition);
|
||||
writer.Write(ConvertTimeToBytes(sleepStartTime));
|
||||
writer.Write(ConvertTimeToBytes(sleepEndTime));
|
||||
|
||||
var data = CreateAirPropertyDataPacket(airNo, AirProperty.SleepMode, stream.ToArray());
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置空调定时设置
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="airNo"></param>
|
||||
/// <param name="timeFlag"></param>
|
||||
/// <param name="startTime1"></param>
|
||||
/// <param name="endTime1"></param>
|
||||
/// <param name="startTime2"></param>
|
||||
/// <param name="endTime2"></param>
|
||||
/// <param name="startTime3"></param>
|
||||
/// <param name="endTime3"></param>
|
||||
public void SetTimeSetting(Host host, int airNo, bool timeFlag, string startTime1, string endTime1, string startTime2, string endTime2, string startTime3, string endTime3)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(timeFlag);
|
||||
writer.Write(ConvertTimeToBytes(startTime1));
|
||||
writer.Write(ConvertTimeToBytes(endTime1));
|
||||
writer.Write(ConvertTimeToBytes(startTime2));
|
||||
writer.Write(ConvertTimeToBytes(endTime2));
|
||||
writer.Write(ConvertTimeToBytes(startTime3));
|
||||
writer.Write(ConvertTimeToBytes(endTime3));
|
||||
|
||||
var data = CreateAirPropertyDataPacket(airNo, AirProperty.Time, stream.ToArray());
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.AirConditionStatus; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private byte[] CreateAirConditionSettingDataPacket(HostAir hostAir)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
AirConditionSettingPacket airConditionSettingPacket = CreateAirConditionSettingPacket(hostAir);
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(airConditionSettingPacket);
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] src1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(src1, 0, buffer, 0, src1.Length);
|
||||
|
||||
byte[] src3 = StructConverter.StructToBytes(airConditionSettingPacket);
|
||||
Array.Copy(src3, 0, buffer, src1.Length, src3.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 AirConditionSettingPacket
|
||||
/// </summary>
|
||||
/// <param name="hostAir"></param>
|
||||
/// <returns></returns>
|
||||
private AirConditionSettingPacket CreateAirConditionSettingPacket(HostAir hostAir)
|
||||
{
|
||||
AirConditionSettingPacket packet = new AirConditionSettingPacket();
|
||||
|
||||
packet.AirNo = Convert.ToByte(hostAir.Modal.ModalType.ID);
|
||||
|
||||
packet.OnOff = Convert.ToByte(hostAir.OnOff);
|
||||
packet.SettingTemp = Convert.ToByte(hostAir.SettingTemp);
|
||||
packet.CompensatoryTemp = ConvertCompensatoryTempToHost(hostAir.CompensatoryTemp);
|
||||
packet.Speed = Convert.ToByte(hostAir.Speed);
|
||||
packet.Mode = Convert.ToByte(hostAir.Mode);
|
||||
packet.IsLockTemp = Convert.ToByte(hostAir.IsLockTemp);
|
||||
packet.LockTemp = Convert.ToByte(hostAir.LockTemp);
|
||||
|
||||
packet.KeepTemp = Convert.ToByte(hostAir.KeepTemp);
|
||||
packet.InitTemp = Convert.ToByte(hostAir.InitTemp);
|
||||
packet.HighTemp = Convert.ToByte(hostAir.HightTemp);
|
||||
packet.LowerTemp = Convert.ToByte(hostAir.LowerTemp);
|
||||
packet.ColdHotSwitchDelayTime = Convert.ToByte(hostAir.ColdHotSwitchDelayTime);
|
||||
packet.ColdHotMode = Convert.ToByte(hostAir.ColdHotMode);
|
||||
packet.DeadTemp = Convert.ToByte(hostAir.DeadTemp);
|
||||
packet.HotDevition = Convert.ToByte(hostAir.HotDevition);
|
||||
packet.ColdDevition = Convert.ToByte(hostAir.ColdDevition);
|
||||
packet.WelcomeTime = Convert.ToByte(hostAir.WelcomeTime);
|
||||
packet.RelateRoomStatus = Convert.ToByte(hostAir.RelateRoomStatus);
|
||||
packet.RelateDoorContact = Convert.ToByte(hostAir.RelateDoorContact);
|
||||
packet.FanStop = Convert.ToByte(hostAir.FanStop);
|
||||
packet.DisableFanHighSpeed = Convert.ToByte(hostAir.DisableFanHighSpeed);
|
||||
|
||||
packet.SleepFlag = Convert.ToByte(hostAir.SleepFlag);
|
||||
packet.SleepStartTime = ConvertTimeToBytes(hostAir.SleepStartTime);
|
||||
packet.SleepEndTime = ConvertTimeToBytes(hostAir.SleepEndTime);
|
||||
packet.SleepDevition = Convert.ToByte(hostAir.SleepDevition);
|
||||
|
||||
packet.TimeFlag = Convert.ToByte(hostAir.TimeFlag);
|
||||
packet.TimeStartTime1 = ConvertTimeToBytes(hostAir.TimeStartTime1);
|
||||
packet.TimeEndTime1 = ConvertTimeToBytes(hostAir.TimeEndTime1);
|
||||
packet.TimeStartTime2 = ConvertTimeToBytes(hostAir.TimeStartTime2);
|
||||
packet.TimeEndTime2 = ConvertTimeToBytes(hostAir.TimeEndTime2);
|
||||
packet.TimeStartTime3 = ConvertTimeToBytes(hostAir.TimeStartTime3);
|
||||
packet.TimeEndTime3 = ConvertTimeToBytes(hostAir.TimeEndTime3);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private byte[] CreateAirPropertyDataPacket(int airNo, AirProperty perperty, byte[] data)
|
||||
{
|
||||
if(data == null || data.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException("data");
|
||||
}
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
int headerSize = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerSize, SeekOrigin.Begin);
|
||||
writer.Write((byte)airNo); //空调号
|
||||
writer.Write((byte)perperty); //空调属性
|
||||
writer.Write(data); //属性值
|
||||
writer.Write(new byte[] { 0, 0 }); //CRC16校验
|
||||
|
||||
var systemHeader = CreateSystemHeader(CommandType.AirProperty, (int)stream.Length - headerSize);
|
||||
var headerData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
writer.Write(headerData);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换时间到字节数组
|
||||
/// 例如:
|
||||
/// 12:05 => byte0=12, byte1=05
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private byte[] ConvertTimeToBytes(string time)
|
||||
{
|
||||
byte[] bytes = new byte[] { 0, 0 };
|
||||
|
||||
if (!String.IsNullOrEmpty(time))
|
||||
{
|
||||
string[] temp = time.Split(':');
|
||||
|
||||
bytes[0] = Convert.ToByte(temp[0]);
|
||||
bytes[1] = Convert.ToByte(temp[1]);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将补偿温度转换为主机存储格式
|
||||
/// 具体格式参见协议
|
||||
/// </summary>
|
||||
/// <param name="temp"></param>
|
||||
/// <returns></returns>
|
||||
private byte ConvertCompensatoryTempToHost(float temp)
|
||||
{
|
||||
byte val = Convert.ToByte(Math.Floor(Math.Abs(temp * 10)));
|
||||
|
||||
if (temp < 0)
|
||||
{
|
||||
return (val |= 0x80);
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
114
RCUHost/Implement/AskRoomStatusChangedReceiver.cs
Normal file
114
RCUHost/Implement/AskRoomStatusChangedReceiver.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class AskRoomStatusChangedReceiver : GenericReceiverBase, IAskRoomStatusChangedReceiver
|
||||
{
|
||||
public void SendRoomStatusSelf(string HostNumber, string Mac, RoomStatus status, byte[] FrameNo)
|
||||
{
|
||||
byte[] data = CreateRoomStatusChangedRequestPacket(status);
|
||||
|
||||
//AA 55 11 00 54 33 53 41 32
|
||||
//03 80 帧号
|
||||
//E9 03 6B 24 33 13
|
||||
data[9] = FrameNo[0];
|
||||
data[10] = FrameNo[1];
|
||||
SendAndPushCommandQueue(data, HostNumber, Mac);// host.IP, host.Port);
|
||||
}
|
||||
public void SendRoomStatusSelfNew(string HostNumber, string Mac, RoomStatus status, byte[] FrameNo, byte PMSState)
|
||||
{
|
||||
byte[] data = CreateRoomStatusChangedRequestPacketNew(status, PMSState);
|
||||
|
||||
//AA 55 11 00 54 33 53 41 32
|
||||
//03 80 帧号
|
||||
//E9 03 6B 24 33 13
|
||||
data[9] = FrameNo[0];
|
||||
data[10] = FrameNo[1];
|
||||
SendAndPushCommandQueue(data, HostNumber, Mac);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理房态相关逻辑
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="roomStatus"></param>
|
||||
private void ProcessRoomStatus(Host host, RoomStatus roomStatus)
|
||||
{
|
||||
#region 处理“退房保险箱关”异常
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.AskRoomStatus; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private byte[] CreateRoomStatusChangedRequestPacket(RoomStatus status)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
RoomStatusChangedPacket roomStatusChangedPacket = CreateRoomStatusChangedPacket(status);
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(roomStatusChangedPacket);
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] src1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(src1, 0, buffer, 0, src1.Length);
|
||||
|
||||
byte[] src3 = StructConverter.StructToBytes(roomStatusChangedPacket);
|
||||
Array.Copy(src3, 0, buffer, src1.Length, src3.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
private byte[] CreateRoomStatusChangedRequestPacketNew(RoomStatus status, byte PMS_ChangeState)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
RoomStatusChangedPacket roomStatusChangedPacket = CreateRoomStatusChangedPacket(status);
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(roomStatusChangedPacket);
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] src1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(src1, 0, buffer, 0, src1.Length);
|
||||
|
||||
byte[] src3 = StructConverter.StructToBytes(roomStatusChangedPacket);
|
||||
Array.Copy(src3, 0, buffer, src1.Length, src3.Length);
|
||||
|
||||
var UUA1 = buffer.Take(buffer.Length - 2).ToList();
|
||||
UUA1.Add(PMS_ChangeState);
|
||||
UUA1.AddRange(new byte[] { 0x00, 0x00 });
|
||||
var buffer1 = UUA1.ToArray();
|
||||
|
||||
return buffer1;
|
||||
}
|
||||
|
||||
private RoomStatusChangedPacket CreateRoomStatusChangedPacket(RoomStatus status)
|
||||
{
|
||||
return new RoomStatusChangedPacket { Status = (byte)status.ID };
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
109
RCUHost/Implement/AuthorizationReceiver.cs
Normal file
109
RCUHost/Implement/AuthorizationReceiver.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using Dao;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class AuthorizationReceiver : GenericReceiverBase, IAuthorizationReceiver
|
||||
{
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
public void AccreditForHost(IList<Host> hostList, DateTime expires)
|
||||
{
|
||||
ushort days = Convert.ToUInt16((DateTime.Parse(expires.ToString("yyyy-MM-dd " + DateTime.Now.ToLongTimeString())) - DateTime.Now).TotalDays);
|
||||
|
||||
foreach (var host in hostList)
|
||||
{
|
||||
//Send(CreateDataPacket(days), host.IP, host.Port);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(ushort days)
|
||||
{
|
||||
//SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
//using (MemoryStream stream = new MemoryStream())
|
||||
//{
|
||||
// var headerLen = StructConverter.SizeOf(systemHeader);
|
||||
|
||||
// stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
// stream.Write(BitConverter.GetBytes(days), 0, 2);
|
||||
// stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
// var systemHeaderData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
|
||||
// stream.Seek(0, SeekOrigin.Begin);
|
||||
// stream.Write(systemHeaderData, 0, systemHeaderData.Length);
|
||||
|
||||
// return stream.ToArray();
|
||||
//}
|
||||
|
||||
byte[] day = BitConverter.GetBytes(days);
|
||||
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + day.Length + 2;
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(day);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解码
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private AuthorizationReply? DecodeAuthorizationPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(AuthorizationReply)) as AuthorizationReply?;
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//base.Process(context);
|
||||
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
|
||||
AuthorizationReply? reply = DecodeAuthorizationPacketReply(context.Data, startIndex);
|
||||
|
||||
if (reply.HasValue)
|
||||
{
|
||||
//var authorizationHost = HostRepository.GetByIP(context.RemoteEndPoint.Address.ToString());
|
||||
|
||||
//if (authorizationHost != null)
|
||||
//{
|
||||
// Int32 authorizationTime = System.BitConverter.ToInt32(reply.Value.Time, 0);
|
||||
|
||||
// authorizationHost.AuthorizedHours = authorizationTime;
|
||||
|
||||
// HostRepository.Update(authorizationHost);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.HostAuthorization; }
|
||||
}
|
||||
}
|
||||
}
|
||||
44
RCUHost/Implement/CarbonVIP.cs
Normal file
44
RCUHost/Implement/CarbonVIP.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using RCUHost.Protocols;
|
||||
using Common;
|
||||
using System.IO;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class CarbonVIP : GenericReceiverBase
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(CarbonVIP));
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
byte[] data = context.Data;
|
||||
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(byte[] send_msg)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
int size = StructConverter.SizeOf(systemHeader) + send_msg.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(send_msg);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.SetRCULog; }
|
||||
}
|
||||
}
|
||||
}
|
||||
62
RCUHost/Implement/ConnectingRoomReceiver.cs
Normal file
62
RCUHost/Implement/ConnectingRoomReceiver.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class ConnectingRoomReceiver : GenericReceiverBase, IConnectingRoomReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置/取消连通房
|
||||
/// </summary>
|
||||
/// <param name="hostList"></param>
|
||||
/// <param name="cancel">false/设置连通房,true/取消连通房,默认false</param>
|
||||
public void ConnectRoom(IList<Host> hostList, bool cancel = false)
|
||||
{
|
||||
byte[] data = CreateDataPacket(hostList, cancel);
|
||||
|
||||
foreach (var host in hostList)
|
||||
{
|
||||
Send(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.ConnectingRoom; }
|
||||
}
|
||||
|
||||
public byte[] CreateDataPacket(IList<Host> hostList, bool cancel)
|
||||
{
|
||||
int headerSize = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
stream.Seek(headerSize, SeekOrigin.Begin);
|
||||
|
||||
stream.WriteByte((byte)(cancel ? 0 : 1));
|
||||
stream.WriteByte((byte)hostList.Count);
|
||||
foreach (var host in hostList)
|
||||
{
|
||||
stream.Write(IPAddress.Parse(host.IP).GetAddressBytes(), 0, 4);
|
||||
}
|
||||
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
var header = CreateSystemHeader((int)stream.Length - headerSize);
|
||||
var headerData = StructConverter.StructToBytes(header);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
58
RCUHost/Implement/CurtainControlReceiver.cs
Normal file
58
RCUHost/Implement/CurtainControlReceiver.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 窗帘控制
|
||||
/// </summary>
|
||||
public class CurtainControlReceiver : GenericReceiverBase, ICurtainControlReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送窗帘控制
|
||||
/// </summary>
|
||||
/// <param name="host">主机</param>
|
||||
/// <param name="CurtainId">窗帘id</param>
|
||||
/// <param name="ctrl">窗帘控制</param>
|
||||
public void SendCtrl(Host host, int CurtainId, CurtainCtrl ctrl)
|
||||
{
|
||||
var data = CreateDataPacket(CurtainId, ctrl);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(int CurtainId, CurtainCtrl key)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
int headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
stream.WriteByte((byte)CurtainId);
|
||||
stream.WriteByte((byte)key);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
SystemHeader systemHeader = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
|
||||
var systemHeaderData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(systemHeaderData, 0, systemHeaderData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.CurtainControl; }
|
||||
}
|
||||
}
|
||||
}
|
||||
227
RCUHost/Implement/DeviceControlReceiver.cs
Normal file
227
RCUHost/Implement/DeviceControlReceiver.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class DeviceControlReceiver : GenericReceiverBase, IDeviceControlReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(DeviceControlReceiver));
|
||||
|
||||
public void Send(Host host, Device device)
|
||||
{
|
||||
Send(host, new List<Device> { device });
|
||||
}
|
||||
|
||||
public void Send(Host host, IList<Device> devices)
|
||||
{
|
||||
var data = CreateDeviceControlPacket(devices);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
|
||||
//需要在这里添加代码
|
||||
//StringBuilder builder = new StringBuilder();
|
||||
//for (int i = 0; i < data.Length; i++)
|
||||
//{
|
||||
// builder.Append(string.Format("{0:X2} ", data[i]));
|
||||
//}
|
||||
//string nnn = builder.ToString().Trim();
|
||||
//logger.Error(string.Format("给酒店({0})客房({1})发送控制命令{2}", host.SysHotel.Code, host.RoomNumber,builder.ToString()));
|
||||
}
|
||||
public void Send_Repeat(string Key,Host host, Device device)
|
||||
{
|
||||
Send_Repeat(Key,host, new List<Device> { device });
|
||||
}
|
||||
public void Send_Repeat(string PrefixKey, Host host, IList<Device> devices)
|
||||
{
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.Append(CacheKey.SendDataRepeatKey);
|
||||
sb.Append("_");
|
||||
sb.Append(PrefixKey);
|
||||
string SingleKey = sb.ToString();
|
||||
|
||||
var data = CreateDeviceControlPacket(devices);
|
||||
|
||||
//AA 55 11 00 54 33 53 41 0F
|
||||
//80 2E 2C 08 0E BD
|
||||
//8F CF
|
||||
byte[] store_xuhao = null;
|
||||
var obj = MemoryCacheHelper.Get(SingleKey);
|
||||
if (obj != null)
|
||||
{
|
||||
store_xuhao = (byte[])obj;
|
||||
data[9] = store_xuhao[0];
|
||||
data[10] = store_xuhao[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
var xuhao = data.Skip(9).Take(2);
|
||||
MemoryCacheHelper.Set(SingleKey, xuhao.ToArray());
|
||||
}
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
//需要在这里添加代码
|
||||
//StringBuilder builder = new StringBuilder();
|
||||
//for (int i = 0; i < data.Length; i++)
|
||||
//{
|
||||
// builder.Append(string.Format("{0:X2} ", data[i]));
|
||||
//}
|
||||
//string nnn = builder.ToString().Trim();
|
||||
//logger.Error(string.Format("给酒店({0})客房({1})发送控制命令{2}", host.SysHotel.Code, host.RoomNumber,builder.ToString()));
|
||||
}
|
||||
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public byte[] CreateDeviceControlPacket(IList<Device> devices)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
byte[] deviceControlDataPacket = CreateDeviceControlDataPacket(devices);
|
||||
int size = StructConverter.SizeOf(systemHeader) + deviceControlDataPacket.Length + 2;
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(deviceControlDataPacket);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] CreateDeviceControlDataPacket(IList<Device> devices)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(buffer))
|
||||
{
|
||||
writer.Write((byte)devices.Count);//数量
|
||||
foreach (var device in devices)
|
||||
{
|
||||
switch (device.AddressType)
|
||||
{
|
||||
case AddressType.DeviceAddress:
|
||||
//根据新的A6主机调整 Modified By Jophy Wu on 20180704
|
||||
//先把继电器、扩展板、调光、可控硅和灯带加上去,其他几个设备可以走之前的协议
|
||||
writer.Write(DeviceAddress.Parse(device.Address).GetBytes());//地址
|
||||
//判断设备类型,对应输出类型(方式1字节+内容1字节)
|
||||
switch (device.Type)
|
||||
{
|
||||
case DeviceType.Relay://主机继电器
|
||||
case DeviceType.Expand://扩展继电器
|
||||
case DeviceType.ServiceInfo://服务信息
|
||||
case DeviceType.Curtain://窗帘
|
||||
case DeviceType.WXLock://微信锁
|
||||
case DeviceType.SwitchExpand://开关扩展
|
||||
case DeviceType.A9IORelay://A9IO继电器
|
||||
case DeviceType.CardPower://插卡取电
|
||||
case DeviceType.InFrared://红外感应
|
||||
case DeviceType.PB20_RELAY:
|
||||
writer.Write(device.Status);//1开,2关
|
||||
writer.Write((byte)0);
|
||||
break;
|
||||
case DeviceType.Dimmer://LED调光
|
||||
case DeviceType.Traic://可控硅调光
|
||||
case DeviceType.Strip://灯带调光
|
||||
case DeviceType.PWMDimmer://PWM调光
|
||||
case DeviceType.PWMExpand://PWM扩展
|
||||
case DeviceType.PBLED://PB LED
|
||||
case DeviceType.LVout://弱电输出
|
||||
case DeviceType.PB20:
|
||||
case DeviceType.PB20_LD:
|
||||
case DeviceType.PB20_LS:
|
||||
writer.Write(device.Status);
|
||||
writer.Write(device.Brightness);
|
||||
break;
|
||||
case DeviceType.AirConditioner://空调
|
||||
byte[] airExecMode = Tools.Int32ToByte2(device.AirExecMode);
|
||||
foreach (byte b in airExecMode)
|
||||
{
|
||||
writer.Write(b);
|
||||
}
|
||||
break;
|
||||
case DeviceType.FloorHot://地暖
|
||||
byte[] floorHotExecMode = Tools.Int32ToByte2(device.FloorHotExecMode);
|
||||
foreach (byte b in floorHotExecMode)
|
||||
{
|
||||
writer.Write(b);
|
||||
}
|
||||
break;
|
||||
case DeviceType.Music://音乐
|
||||
byte[] musicExecMode = Tools.Int32ToByte2(device.MusicExecMode);
|
||||
foreach (byte b in musicExecMode)
|
||||
{
|
||||
writer.Write(b);
|
||||
}
|
||||
break;
|
||||
case DeviceType.TV://电视
|
||||
writer.Write(device.Status);//1开,2关,3匹配,4频道。
|
||||
switch (device.Status)
|
||||
{
|
||||
case 0x03://status==3时:1空调,2电视
|
||||
break;
|
||||
case 0x04://status==4时:1~255频道
|
||||
writer.Write(device.Valve);
|
||||
break;
|
||||
default://功能:1~15,只是开关时,内容是0
|
||||
writer.Write(device.Mode);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case DeviceType.ColorTemp://色溫
|
||||
writer.Write(device.Status);
|
||||
switch (device.Status)
|
||||
{
|
||||
case 3:
|
||||
writer.Write(device.Temperature);//0~100
|
||||
break;
|
||||
default:
|
||||
writer.Write(device.Brightness);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
//其他走以前的协议
|
||||
writer.Write((byte)device.AddressType);//地址类型
|
||||
writer.Write(device.Status);//状态
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case AddressType.GroupAddress://指令场景
|
||||
writer.Write((byte)0);//设备类型1字节
|
||||
writer.Write(GroupAddress.Parse(device.Address).GetBytes());//地址
|
||||
writer.Write(device.Status);//1开,2关
|
||||
writer.Write((byte)0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.DeviceControl; }
|
||||
}
|
||||
}
|
||||
}
|
||||
70
RCUHost/Implement/DeviceSecretReceiver.cs
Normal file
70
RCUHost/Implement/DeviceSecretReceiver.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送命令给rcu,设置密钥
|
||||
/// </summary>
|
||||
public class DeviceSecretReceiver : GenericReceiverBase, IDeviceSecretReceiver
|
||||
{
|
||||
public void Send(Host host)
|
||||
{
|
||||
var data = CreateDeviceSecretPacket(host);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
private byte[] CreateDeviceSecretPacket(Host host)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
byte[] deviceSecretDataPacket = CreateDeviceSecretDataPacket(host);
|
||||
int size = StructConverter.SizeOf(systemHeader) + deviceSecretDataPacket.Length + 2;
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(deviceSecretDataPacket);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 下发内容
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateDeviceSecretDataPacket(Host host)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(buffer))
|
||||
{
|
||||
writer.Write(System.Text.Encoding.Default.GetBytes("TSW1"));//小主机TSW1
|
||||
byte[] mac = new byte[host.DeviceName.Length / 2];
|
||||
for (int i = 0; i < mac.Length; i++)
|
||||
{
|
||||
writer.Write(Convert.ToByte(host.DeviceName.Substring(i * 2, 2), 16));//MAC 6字节
|
||||
}
|
||||
writer.Write(System.Text.Encoding.Default.GetBytes(host.DeviceSecret));
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.SetDeviceSecret; }
|
||||
}
|
||||
}
|
||||
}
|
||||
97
RCUHost/Implement/EnergySavingModeReceiver.cs
Normal file
97
RCUHost/Implement/EnergySavingModeReceiver.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class EnergySavingModeReceiver : GenericReceiverBase, IEnergySavingModeReceiver
|
||||
{
|
||||
public void ApplyEnergySavingMode(Model model, Host host)
|
||||
{
|
||||
byte[] data = CreateEnergySavingModeRequestPacket(model);
|
||||
|
||||
Send(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.EnergySavingMode; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private byte[] CreateEnergySavingModeRequestPacket(Model model)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
EnergySavingModePacket energySavingModePacket = CreateEnergySavingModePacket(model);
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(energySavingModePacket);
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] src1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(src1, 0, buffer, 0, src1.Length);
|
||||
|
||||
byte[] src3 = StructConverter.StructToBytes(energySavingModePacket);
|
||||
Array.Copy(src3, 0, buffer, src1.Length, src3.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private EnergySavingModePacket CreateEnergySavingModePacket(Model model)
|
||||
{
|
||||
EnergySavingModePacket packet = new EnergySavingModePacket();
|
||||
|
||||
packet.CorrectedTemp = Convert.ToByte(model.CorrectedTemp);
|
||||
packet.FanRunStatus = Convert.ToByte(model.FanRunStatus ? 1 : 0);
|
||||
packet.ExhaustFanStatus = Convert.ToByte(model.ExhaustFanStatus);
|
||||
packet.ExhaustFanTime = Convert.ToByte(model.ExhausFanTime);
|
||||
|
||||
packet.InfraredDelayPO = Convert.ToByte(model.InfraredDelayPO);
|
||||
packet.DoorDelayPO = Convert.ToByte(model.DoorDelayPO);
|
||||
packet.PullCardDelayPO = Convert.ToByte(model.PullCardDelayPO);
|
||||
|
||||
ModelDetail detail1 = model.Details.FirstOrDefault(r => r.ModelStatus == "插卡时");
|
||||
ModelDetail detail2 = model.Details.FirstOrDefault(r => r.ModelStatus == "拔卡时");
|
||||
ModelDetail detail3 = model.Details.FirstOrDefault(r => r.ModelStatus == "待租时");
|
||||
ModelDetail detail4 = model.Details.FirstOrDefault(r => r.ModelStatus == "停租时");
|
||||
|
||||
packet.FengJiStatus = new FengJiStatus[4]
|
||||
{
|
||||
CreateFengJiStatus(detail1),
|
||||
CreateFengJiStatus(detail2),
|
||||
CreateFengJiStatus(detail3),
|
||||
CreateFengJiStatus(detail4)
|
||||
};
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private FengJiStatus CreateFengJiStatus(ModelDetail detail)
|
||||
{
|
||||
return new FengJiStatus
|
||||
{
|
||||
OnOff = detail.OnOff,
|
||||
Type = Convert.ToByte(detail.ModelType),
|
||||
Speed = Convert.ToByte(detail.Speed),
|
||||
SummerTemp = Convert.ToByte(detail.SummerTemp),
|
||||
WinterTemp = Convert.ToByte(detail.WinterTemp),
|
||||
TimingControl = Convert.ToByte(detail.TimingControl ? 1 : 0),
|
||||
Timer = Convert.ToByte(detail.Timer),
|
||||
AllowElectric = Convert.ToByte(detail.AllowElectric ? 1 : 0),
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
61
RCUHost/Implement/ExplainDomainIPReceiver.cs
Normal file
61
RCUHost/Implement/ExplainDomainIPReceiver.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CacheManager.Core;
|
||||
using Common;
|
||||
using Dao;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 解释域名的IP Receiver
|
||||
/// </summary>
|
||||
public class ExplainDomainIPReceiver : GenericReceiverBase, IExplainDomainIPReceiver
|
||||
{
|
||||
//private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HeartReceiver));
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//logger.Error("收到获取IP命令:" + Tools.ByteToString(context.Data));
|
||||
byte[] data = CreateDataPacket(context);
|
||||
Send(data, context.RemoteEndPoint);//解释域名IP,返回给rcu
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(ReceiverContext context)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
var HostNumberOnly = context.SystemHeader.Value.HostNumber;
|
||||
System.Net.IPAddress ipAddr = System.Net.IPAddress.Parse("106.53.160.26");
|
||||
long code = HostNumberOnly.ToHotelCode();
|
||||
if (code != 1564)
|
||||
{
|
||||
ipAddr = Tools.GetIPByDomain("BoonliveNAS.synology.me");
|
||||
}
|
||||
byte[] ip = ipAddr.GetAddressBytes();
|
||||
//logger.Error("解释域名IP为:" + ipAddr.ToString());
|
||||
int size = StructConverter.SizeOf(systemHeader) + ip.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(ip);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.ExplainDomainIP; }
|
||||
}
|
||||
}
|
||||
}
|
||||
364
RCUHost/Implement/GenericReceiverBase.cs
Normal file
364
RCUHost/Implement/GenericReceiverBase.cs
Normal file
@@ -0,0 +1,364 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using RCUHost.Protocols;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public abstract class GenericReceiverBase : IReceiver
|
||||
{
|
||||
|
||||
#region Static Memberss
|
||||
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(GenericReceiverBase));
|
||||
private static readonly Object _locker = new object();
|
||||
protected static ushort frameNo = 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates Properties
|
||||
|
||||
private IHostServer hostServer;
|
||||
private ISysSettingRepository sysSettingRepository;
|
||||
public ISysSystemLogsRepository SysSystemLogsRepository { get; set; }
|
||||
/// <summary>
|
||||
/// 服务器通信IP
|
||||
/// </summary>
|
||||
private string messageIP = "0.0.0.0";
|
||||
/// <summary>
|
||||
/// 服务器通信端口
|
||||
/// </summary>
|
||||
private int messagePort = 3389;
|
||||
/// <summary>
|
||||
/// 组播地址
|
||||
/// </summary>
|
||||
private string multicastIP = "239.7.8.9";
|
||||
/// <summary>
|
||||
/// RCU主机通信端口,组播端口
|
||||
/// </summary>
|
||||
private int mulitcastPort = 3341;
|
||||
private string ftpServer = "106.75.37.225";
|
||||
private ushort ftpPort = 21;
|
||||
private string ftpName = "blw";
|
||||
private string ftpPassword = "blw@123";
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public IHostServer HostServer
|
||||
{
|
||||
get { return this.hostServer; }
|
||||
set
|
||||
{
|
||||
this.hostServer = value;
|
||||
//this.hostServer.BeforeStart += new EventHandler(hostServer_BeforeStart);
|
||||
//this.hostServer.AfterStart += new EventHandler(hostServer_AfterStart);
|
||||
}
|
||||
}
|
||||
public ISysSettingRepository SysSettingRepository
|
||||
{
|
||||
get { return this.sysSettingRepository; }
|
||||
set
|
||||
{
|
||||
this.sysSettingRepository = value;
|
||||
this.multicastIP = SysSettingRepository.GetValue("MulticastIP").ToString();
|
||||
this.mulitcastPort = Convert.ToInt32(SysSettingRepository.GetValue("MulticastPort"));
|
||||
this.messageIP = SysSettingRepository.GetValue("MessageIP").ToString();
|
||||
this.messagePort = Convert.ToInt32(SysSettingRepository.GetValue("MessagePort"));
|
||||
|
||||
this.ftpServer = SysSettingRepository.GetValue("FTPServer").ToString();
|
||||
this.ftpPort = Convert.ToUInt16(SysSettingRepository.GetValue("FTPPort"));
|
||||
this.ftpName = SysSettingRepository.GetValue("FTPName").ToString();
|
||||
this.ftpPassword = SysSettingRepository.GetValue("FTPPassword").ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 服务器通信IP,默认 0.0.0.0
|
||||
/// </summary>
|
||||
public string MessageIP
|
||||
{
|
||||
get { return this.messageIP; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 服务器通信端口,默认3389
|
||||
/// </summary>
|
||||
public int MessagePort
|
||||
{
|
||||
get { return this.messagePort; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 组播地址,默认 239.7.8.9
|
||||
/// </summary>
|
||||
public string MulticastIP
|
||||
{
|
||||
get { return this.multicastIP; }
|
||||
}
|
||||
/// <summary>
|
||||
/// RCU主机通信端口,组播端口,默认 3341
|
||||
/// </summary>
|
||||
public int MulticastPort
|
||||
{
|
||||
get { return this.mulitcastPort; }
|
||||
}
|
||||
public string FTPServer
|
||||
{
|
||||
get { return this.ftpServer; }
|
||||
}
|
||||
public ushort FTPPort
|
||||
{
|
||||
get { return this.ftpPort; }
|
||||
}
|
||||
public string FTPName
|
||||
{
|
||||
get { return this.ftpName; }
|
||||
}
|
||||
public string FTPPassword
|
||||
{
|
||||
get { return this.ftpPassword; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 命令类型
|
||||
/// </summary>
|
||||
public abstract CommandType CommandType { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 处理数据
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public virtual void Process(ReceiverContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static bool DealWwith(ReceiverContext context)
|
||||
{
|
||||
string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
|
||||
var UUU = context.SystemHeader.Value;
|
||||
string IP_PORTStr = context.RemoteEndPoint.ToString();
|
||||
if (UUU.CmdType == 0x02)
|
||||
{
|
||||
//如果公告的帧号和要处理的不一致就直接返回
|
||||
//ushort CurrentFrameNO = context.SystemHeader.Value.FrameNo;
|
||||
//string PublicPop = CacheKey.PublicKeyboard + "_" + IP_PORTStr;
|
||||
//object ooo = MemoryCacheHelper.Get(PublicPop);
|
||||
//if (ooo != null)
|
||||
//{
|
||||
// ushort Newlast = Convert.ToUInt16(ooo);
|
||||
// if (Newlast != CurrentFrameNO)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
//如果超过2秒就不再处理了
|
||||
//string ShiJianLanJie = CacheKey.SyncTimeIntercept + "_" + HostNumberOnly;
|
||||
string ShiJianLanJie = CacheKey.TimeIntercept + "_" + HostNumberOnly;
|
||||
object VVV = MemoryCacheHelper.Get(ShiJianLanJie);
|
||||
if (VVV == null)
|
||||
{
|
||||
string hotelCode = context.SystemHeader.Value.HostNumber.ToHotelCode().ToString();//获取酒店编码
|
||||
string RegisterKey1 = "RoomStatusTimeOutDrop";
|
||||
RCUHost.RCUHostCommon.tools.LanJieData(RegisterKey1, hotelCode);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取下一个帧号:小于32767
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected ushort GetNextFrameNo()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (frameNo >= 0x7EFF)//32767
|
||||
{
|
||||
frameNo = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
frameNo++;
|
||||
}
|
||||
}
|
||||
return frameNo;
|
||||
}
|
||||
/// <summary>
|
||||
/// HostServer 启动之前调用
|
||||
/// </summary>
|
||||
/// <param name="hostServer"></param>
|
||||
protected virtual void HostServer_BeforeStart(HostServer hostServer)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// HostServer 启动之后调用
|
||||
/// </summary>
|
||||
/// <param name="hostServer"></param>
|
||||
protected virtual void HostServer_AfterStart(HostServer hostServer)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送数据
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="hostNumber"></param>
|
||||
protected void Send(byte[] data, string hostNumber, string mac)
|
||||
{
|
||||
string ipAndPort = CSRedisCacheHelper.Get<string>(hostNumber, mac);
|
||||
if (!string.IsNullOrEmpty(ipAndPort))
|
||||
{
|
||||
//if (HostServer!=null)
|
||||
//{
|
||||
// logger.Error("1111111111");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// logger.Error("222222222222222222222");
|
||||
//}
|
||||
HostServer.Send(data, ipAndPort.ToString().Split(':')[0], int.Parse(ipAndPort.ToString().Split(':')[1]));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送数据
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="endPoint"></param>
|
||||
protected void Send(byte[] data, IPEndPoint endPoint)
|
||||
{
|
||||
HostServer.Send(data, endPoint);
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送数据并添加到队列
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="hostNumber"></param>
|
||||
protected void SendAndPushCommandQueue(byte[] data, string hostNumber, string mac)
|
||||
{
|
||||
string ipAndPort = CSRedisCacheHelper.Get<string>(hostNumber, mac);
|
||||
if (!string.IsNullOrEmpty(ipAndPort))
|
||||
{
|
||||
HostServer.SendAndPushCommandQueue(data, ipAndPort.ToString().Split(':')[0], Convert.ToInt32(ipAndPort.ToString().Split(':')[1]));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 回复下位机命令
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
protected void Reply(ReceiverContext context)
|
||||
{
|
||||
var headerLen = StructConverter.SizeOf(context.SystemHeader);
|
||||
var headerData = new byte[headerLen + 2];
|
||||
Array.Copy(context.Data, headerData, headerLen);
|
||||
headerData[2] = 0x11;
|
||||
headerData[3] = 0x00;
|
||||
Send(headerData, context.RemoteEndPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 SystemHeader
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected SystemHeader CreateSystemHeader()
|
||||
{
|
||||
return CreateSystemHeader(0);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建 SystemHeader
|
||||
/// </summary>
|
||||
/// <param name="dataLength">数据包长度,不包含头部长度</param>
|
||||
/// <returns></returns>
|
||||
protected SystemHeader CreateSystemHeader(int dataLength)
|
||||
{
|
||||
return CreateSystemHeader(CommandType, dataLength);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建 SystemHeader
|
||||
/// </summary>
|
||||
/// <param name="commandType">命令类型</param>
|
||||
/// <param name="dataLength">数据包长度,不包含头部长度</param>
|
||||
/// <returns></returns>
|
||||
protected SystemHeader CreateSystemHeader(CommandType commandType, int dataLength)
|
||||
{
|
||||
var systemHeader = new SystemHeader();
|
||||
systemHeader.Signature = SystemHeader.SIGNATURE;
|
||||
systemHeader.FrameLength = (ushort)(StructConverter.SizeOf(systemHeader) + dataLength);
|
||||
systemHeader.SystemID = SystemHeader.SYSTEM_ID.ToCharArray();
|
||||
systemHeader.CmdType = (byte)commandType;
|
||||
systemHeader.FrameNo = GetNextFrameNo();
|
||||
systemHeader.HostNumber = new HostNumber { NBuild = 0xFF, NFloor = 0xFF, NRoom = 0xFF, NUnit = 0xFF };
|
||||
return systemHeader;
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建 DeviceHeader
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected DeviceHeader CreateDeviceHeader()
|
||||
{
|
||||
return new DeviceHeader
|
||||
{
|
||||
NodeAddr = 0x00,
|
||||
CmdType = (ushort)IPAddress.HostToNetworkOrder((short)this.CommandType),
|
||||
AskAck = 0x01,
|
||||
RoomNumber = new HostNumber
|
||||
{
|
||||
NBuild = 0xff,
|
||||
NFloor = 0xff,
|
||||
NRoom = 0xff,
|
||||
NUnit = 0xff
|
||||
}
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// 记录操作日志
|
||||
/// </summary>
|
||||
/// <param name="authorityID">权限ID</param>
|
||||
/// <param name="action">动作</param>
|
||||
/// <param name="detail">详细</param>
|
||||
/// <param name="result">操作结果</param>
|
||||
/// <param name="account">帐号</param>
|
||||
/// <param name="hotelID">所属酒店ID</param>
|
||||
protected void SaveSystemLog(int authorityID, string action, string detail, string result, string account, string ip, int hotelID)
|
||||
{
|
||||
try
|
||||
{
|
||||
Domain.SysSystemLogs entity = new Domain.SysSystemLogs();
|
||||
entity.ID = 0;
|
||||
entity.AuthorityID = authorityID;
|
||||
entity.Action = action;
|
||||
entity.Detail = detail;
|
||||
entity.Result = result;
|
||||
entity.Account = account;
|
||||
entity.IP = ip;
|
||||
entity.Time = DateTime.Now;
|
||||
entity.HotelID = hotelID;
|
||||
|
||||
SysSystemLogsRepository.Save(entity);
|
||||
}
|
||||
catch (Exception ex) { logger.Error("保存操作日志失败:" + ex.ToString()); }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void hostServer_BeforeStart(object sender, EventArgs e)
|
||||
{
|
||||
HostServer_BeforeStart(sender as HostServer);
|
||||
}
|
||||
|
||||
private void hostServer_AfterStart(object sender, EventArgs e)
|
||||
{
|
||||
HostServer_AfterStart(sender as HostServer);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
219
RCUHost/Implement/HeartReceiver.cs
Normal file
219
RCUHost/Implement/HeartReceiver.cs
Normal file
@@ -0,0 +1,219 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CacheManager.Core;
|
||||
using Common;
|
||||
using Dao;
|
||||
using RCUHost.Protocols;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 心跳包 Receiver
|
||||
/// </summary>
|
||||
public class HeartReceiver : GenericReceiverBase, IHeartReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HeartReceiver));
|
||||
public void SendHeart(string hostNumber, string mac)
|
||||
{
|
||||
byte[] data = CreateDataPacket();
|
||||
Send(data, hostNumber, mac);
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
bool bkb = GenericReceiverBase.DealWwith(context);
|
||||
if (bkb == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//心跳包维持连接
|
||||
//新的 使用MAC
|
||||
//logger.Error("下位机来连接了");
|
||||
//AA 55 17 00 54 33 53 41 02 78 90 1F 08 75 D9
|
||||
//34 D0 B8 11 75 D9 MAC地址
|
||||
//D5 B6
|
||||
if (context.Data.Length > 17)
|
||||
{
|
||||
//logger.Error("下位机来连接了");
|
||||
//17: 34-D0-B8-11-5C-77 172.16.5.58:3341
|
||||
//logger.Error("17: " + BitConverter.ToString(context.Data, 15, 6)+context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString());
|
||||
string Key = BitConverter.ToString(context.Data, 15, 6);
|
||||
|
||||
CSRedisCacheHelper.Set(Key, context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString());//mac地址
|
||||
|
||||
|
||||
string EndPoint = context.RemoteEndPoint.ToString();
|
||||
|
||||
string hostnum = context.SystemHeader.Value.HostNumber.ToString();
|
||||
long hotelcode = context.SystemHeader.Value.HostNumber.ToHotelCode();
|
||||
//PO~P5:RCU 主机MAC地址←
|
||||
//P6:解析版本←
|
||||
//P6:取电状态<
|
||||
//P7:设备类型<
|
||||
//P8:设备地址<
|
||||
//P8~P9:设备回路<
|
||||
//P10:设备数据长度<
|
||||
//P11~P12:通道电压,单位:10mV
|
||||
//P13~P16:通道电流,单位:10mV
|
||||
//P17~P20:通道功率,单位:mW
|
||||
//P21~P24:通道能耗,单位:Wh(1度电=1KWh)
|
||||
//P25~P28:通道总能耗,单位:Wh(1度电=1KWh)
|
||||
var Data = context.Data;
|
||||
if (Data.Length > 17 + 6)
|
||||
{
|
||||
//AA 55 2F 00 54 33 53 41 02 34 80 EB 03 6B 24
|
||||
//34 D0 B8 11 6B 24
|
||||
//01 解析版本
|
||||
//01 取电
|
||||
//01 设备数量
|
||||
//39
|
||||
//01
|
||||
//01
|
||||
//00
|
||||
//10 ///长度
|
||||
//F0 55 //电压
|
||||
//E8 03 //电流
|
||||
//E8 03 00 00 功率
|
||||
//E8 03 00 00 能耗
|
||||
//E8 03 00 00 总能耗
|
||||
//9E 00
|
||||
|
||||
|
||||
//byte[] MAC = Data.Skip(15).Take(6).ToArray();
|
||||
|
||||
//string MACStr = Tools.ByteToString(MAC).Trim();
|
||||
//MACStr = MACStr.Replace(" ", "-");
|
||||
//byte Version = Data.Skip(21).Take(1).FirstOrDefault();
|
||||
|
||||
////从第21个数据开始
|
||||
//byte TakeCard = Data.Skip(22).Take(1).FirstOrDefault();
|
||||
|
||||
////设备数量
|
||||
//byte DeviceCount = Data.Skip(23).Take(1).FirstOrDefault();
|
||||
//byte LeiXing = Data.Skip(24).Take(1).FirstOrDefault();
|
||||
//byte Address = Data.Skip(25).Take(1).FirstOrDefault();
|
||||
//byte[] Num = Data.Skip(26).Take(2).ToArray();
|
||||
|
||||
//List<byte> lll = new List<byte>();
|
||||
//lll.Add(LeiXing);
|
||||
//lll.Add(Address);
|
||||
//lll.AddRange(Num);
|
||||
|
||||
//string address = new DeviceAddress(lll.ToArray()).ToString();
|
||||
|
||||
//byte Len = Data.Skip(28).Take(1).FirstOrDefault();
|
||||
//byte[] DianYa = Data.Skip(29).Take(2).ToArray();
|
||||
//byte[] DianLiu = Data.Skip(31).Take(2).ToArray();
|
||||
//byte[] Power = Data.Skip(33).Take(4).ToArray();
|
||||
//byte[] PowerUsed = Data.Skip(37).Take(4).ToArray();
|
||||
//byte[] TotalPowerUsed = Data.Skip(41).Take(4).ToArray();
|
||||
//uint dianya = BitConverter.ToUInt16(DianYa, 0);
|
||||
//uint dianliu = BitConverter.ToUInt16(DianLiu, 0);
|
||||
//uint gonglv = BitConverter.ToUInt32(Power, 0);
|
||||
|
||||
//uint nenghao = BitConverter.ToUInt32(PowerUsed, 0);
|
||||
//uint zongnenghao = BitConverter.ToUInt32(TotalPowerUsed, 0);
|
||||
|
||||
//double V = (double)dianya * 10 / 1000;
|
||||
//double A = (double)dianliu * 10 / 1000;
|
||||
//double P = (double)gonglv / 1000;
|
||||
|
||||
//double KW_H = (double)nenghao / 1000;
|
||||
//double Sum_KW_H = (double)zongnenghao / 1000;
|
||||
|
||||
//NengHao n = new NengHao();
|
||||
//n.HotelCode = hotelcode;
|
||||
//n.HostNumber = hostnum;
|
||||
//n.Mac = MACStr;
|
||||
//n.EndPoint = EndPoint;
|
||||
//n.Version = ((int)Version).ToString();
|
||||
//if (TakeCard == 0x01)
|
||||
//{
|
||||
// n.IsTakeCard = true;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// n.IsTakeCard = false;
|
||||
//}
|
||||
//n.V = Math.Round(V, 2);
|
||||
//n.A = Math.Round(A, 2);
|
||||
//n.P = Math.Round(P, 2);
|
||||
//n.KW_H = KW_H;
|
||||
//n.Sum_KW_H = Sum_KW_H;
|
||||
|
||||
//long ti = Tools.GetUnixTime();
|
||||
//n.CreateTime = ti;
|
||||
|
||||
//string KKey = CacheKey.NengHao + "_" + MACStr;
|
||||
////CSRedisCacheHelper.Forever<NengHao>(KKey, n);
|
||||
//CSRedisCacheHelper.Set_PartitionWithTime<NengHao>(KKey, n, 30, 1);
|
||||
|
||||
//string mns = Newtonsoft.Json.JsonConvert.SerializeObject(n);
|
||||
//CSRedisCacheHelper.Publish("redis-power", mns);
|
||||
|
||||
//string KeyFilter = "能耗";
|
||||
//RCUHost.RCUHostCommon.tools.LanJieData(KeyFilter, hotelcode.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//旧版本数据
|
||||
//使用hostnumber
|
||||
//logger.Error("MAC: "+context.SystemHeader.Value.HostNumber.ToString()+" "+ context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString());
|
||||
string Key = context.SystemHeader.Value.HostNumber.ToString();
|
||||
string hotelCode = context.SystemHeader.Value.HostNumber.ToHotelCode().ToString();
|
||||
|
||||
|
||||
//string EEE = CSRedisCacheHelper.Get<string>(Key);
|
||||
//if (string.IsNullOrEmpty(EEE))
|
||||
//{
|
||||
// OnOffLineData o = new OnOffLineData();
|
||||
// o.HostNumber = Key;
|
||||
// o.HotelCode = hotelCode;
|
||||
// o.CurrentStatus = "on";
|
||||
// o.CurrentTime = DateTime.Now;
|
||||
// //新来的数据
|
||||
// var n = Newtonsoft.Json.JsonConvert.SerializeObject(o);
|
||||
// CSRedisCacheHelper.Publish("redis-on_off_line", n);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
CSRedisCacheHelper.Set(Key, context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString());//主机编码
|
||||
//CSRedisCacheHelper.HMSet(CacheKey.RCUOnLine,Key);
|
||||
|
||||
}
|
||||
//判断是否需要给下位机回复命令(下位机判断累计三次未回复则重启主机)
|
||||
//string frameNo = Tools.ByteToString(context.Data).Substring(27, 5);//(frameNo == "FF FF")
|
||||
if (context.SystemHeader.Value.FrameNo == 65535)
|
||||
{
|
||||
Send(CreateDataPacket(), context.RemoteEndPoint);
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.Heart; }
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket()
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
var headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
var header = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
byte[] headerData = StructConverter.StructToBytes(header);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
361
RCUHost/Implement/HostRegisterReceiver.cs
Normal file
361
RCUHost/Implement/HostRegisterReceiver.cs
Normal file
@@ -0,0 +1,361 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using Dao;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
using System.Configuration;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送命令给rcu,获取寄存器内容:改为读取主机信息命令
|
||||
/// </summary>
|
||||
public class HostRegisterReceiver : GenericReceiverBase, IHostRegisterReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostRegisterReceiver));
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IHostRCURepository HostRCURepository { get; set; }
|
||||
public IRoomStatusRepository RoomStatusRepository { get; set; }
|
||||
|
||||
public void Send(Host host)
|
||||
{
|
||||
var data = CreateHostSecretPacket(host);
|
||||
Send(data, host.HostNumber, host.MAC);
|
||||
}
|
||||
|
||||
public void Send(string hostnumber, string mac)
|
||||
{
|
||||
var data = CreateHostSecretPacket();
|
||||
Send(data, hostnumber, mac);
|
||||
}
|
||||
|
||||
public byte[] CreateHostSecretPacket()
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
byte[] registerAddr = new byte[] { 0, 0, 0, 0 };
|
||||
int size = StructConverter.SizeOf(systemHeader) + 2 + registerAddr.Length;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(registerAddr);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
public byte[] CreateHostSecretPacket(Host host)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
byte[] registerAddr = new byte[] { 0, 0, 0, 0 };
|
||||
//byte[] registerAddr = new byte[] {
|
||||
// 13, //需要读取寄存器的个数
|
||||
// 0, 0, 0, 0, //局域網IP
|
||||
// 4, 0, 0, 0, //局域网端口
|
||||
// 8, 0, 0, 0, //子网掩码
|
||||
// 12, 0, 0, 0, //网关
|
||||
// 16, 0, 0, 0, //DNS
|
||||
// 24, 0, 0, 0, //服务器IP
|
||||
// 28, 0, 0, 0, //服务器端口
|
||||
// 32, 0, 0, 0, //授权到期时间
|
||||
// 40, 0, 0, 0, //设置到期时间
|
||||
// 48, 0, 0, 0, //IP类型
|
||||
// 60, 0, 0, 0, //固件版本号
|
||||
// 64, 0, 0, 0, //配置版本号
|
||||
// 80, 0, 0, 0 //季节
|
||||
//};
|
||||
int size = StructConverter.SizeOf(systemHeader) + 2 + registerAddr.Length;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(registerAddr);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//public static string Debug_RegeditInfo = ConfigurationManager.AppSettings["debug_registerinfo"];
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
string msg = context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString() + ":" + Tools.ByteToString(context.Data);
|
||||
//logger.Error("收到主机信息数据:" + msg);
|
||||
|
||||
|
||||
string dataHex = Tools.ByteToString(context.Data);
|
||||
string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
|
||||
Host host = HostRepository.GetByHostNumber(HostNumberOnly);
|
||||
|
||||
if (host != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
int offset = StructConverter.SizeOf(context.SystemHeader);
|
||||
int length = context.Data.Length - offset - 2;
|
||||
using (MemoryStream stream = new MemoryStream(context.Data, offset, length))
|
||||
{
|
||||
using (BinaryReader reader = new BinaryReader(stream))
|
||||
{
|
||||
int ipType = reader.ReadByte();//IP类型
|
||||
string type_number = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(16)).Replace(@"\u0000", "").Replace("", "").Trim();//机型编码
|
||||
string lan_ip = String.Join(".", reader.ReadBytes(4));//局域网IP
|
||||
string server_ip = String.Join(".", reader.ReadBytes(4));//服务器IP
|
||||
string subnet_mask = String.Join(".", reader.ReadBytes(4));//子网掩码
|
||||
string gateway = String.Join(".", reader.ReadBytes(4));//网关
|
||||
int lan_port = BitConverter.ToInt32(reader.ReadBytes(4), 0);//局域网端口
|
||||
string dns = String.Join(".", reader.ReadBytes(4));//DNS
|
||||
string software_version = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(20)).Replace(@"\u0000", "").Replace("", "").Trim();//软件版本号
|
||||
int year = reader.ReadByte();
|
||||
int month = reader.ReadByte();
|
||||
int day = reader.ReadByte();
|
||||
int hour = reader.ReadByte();
|
||||
int minute = reader.ReadByte();
|
||||
int second = reader.ReadByte();
|
||||
string rcuTime = string.Format("20{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second);
|
||||
//logger.Error("rcu time:" + rcuTime);
|
||||
DateTime rcu_time = DateTime.Now;
|
||||
DateTime.TryParse(rcuTime, out rcu_time);
|
||||
string launcher_version = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(20)).Replace(@"\u0000", "").Replace("", "").Trim();//Launcher版本
|
||||
string mac = BitConverter.ToString(reader.ReadBytes(6));//MAC
|
||||
long hotel_code = Tools.Byte4ToLong(reader.ReadBytes(4));//项目编码
|
||||
int host_id = BitConverter.ToInt32(reader.ReadBytes(4), 0);//房号ID
|
||||
int roomtype_id = BitConverter.ToInt32(reader.ReadBytes(4), 0);//房型ID
|
||||
string setting_version = String.Join(".", reader.ReadBytes(4));//配置版本号
|
||||
int room_status_id = BitConverter.ToInt32(reader.ReadBytes(4), 0);//房态
|
||||
byte[] season = reader.ReadBytes(4);//季节
|
||||
StringBuilder sbSeason = new StringBuilder();
|
||||
sbSeason.Append((season[0] >> 0) & 3);
|
||||
sbSeason.Append((season[0] >> 2) & 3);
|
||||
sbSeason.Append((season[0] >> 4) & 3);
|
||||
sbSeason.Append((season[0] >> 6) & 3);
|
||||
|
||||
sbSeason.Append((season[1] >> 0) & 3);
|
||||
sbSeason.Append((season[1] >> 2) & 3);
|
||||
sbSeason.Append((season[1] >> 4) & 3);
|
||||
sbSeason.Append((season[1] >> 6) & 3);
|
||||
|
||||
sbSeason.Append((season[2] >> 0) & 3);
|
||||
sbSeason.Append((season[2] >> 2) & 3);
|
||||
sbSeason.Append((season[2] >> 4) & 3);
|
||||
sbSeason.Append((season[2] >> 6) & 3);
|
||||
|
||||
int lock_status = BitConverter.ToInt32(reader.ReadBytes(4), 0);//锁定状态
|
||||
long set_expiration_time = BitConverter.ToUInt32(reader.ReadBytes(4), 0);//授权时间戳
|
||||
long expiration_time = BitConverter.ToUInt32(reader.ReadBytes(4), 0);//授权到期时间戳
|
||||
string roomnumber = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(16)).Replace(@"\u0000", "").Replace("", "").Trim();//房号备注
|
||||
string roomtype = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(16)).Replace(@"\u0000", "").Replace("", "").Trim();//房型备注
|
||||
string room_remark = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(96)).Replace(@"\u0000", "").Replace("", "").Trim();//房间备注
|
||||
string core = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(64)).Replace(@"\u0000", "").Replace("", "").Trim();//MCU机型名称
|
||||
string model = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(64)).Replace(@"\u0000", "").Replace("", "").Trim();//中控机型名称
|
||||
string hotel_name = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(32)).Replace(@"\u0000", "").Replace("", "").Trim();//配置数据酒店名称
|
||||
string roomtype_remark = Encoding.GetEncoding("GBK").GetString(reader.ReadBytes(32)).Replace(@"\u0000", "").Replace("", "").Trim();//配置数据房型别名
|
||||
var hostRCU = HostRCURepository.GetByHostID(host_id);
|
||||
if (hostRCU == null)
|
||||
{
|
||||
hostRCU = new HostRCU();
|
||||
}
|
||||
hostRCU.HotelID = host.SysHotel.ID;
|
||||
hostRCU.IPType = ipType;
|
||||
hostRCU.TypeNumber = type_number;
|
||||
hostRCU.LanIP = lan_ip;
|
||||
hostRCU.ServerIP = server_ip;
|
||||
hostRCU.SubnetMask = subnet_mask;
|
||||
hostRCU.Gateway = gateway;
|
||||
hostRCU.LanPort = lan_port;
|
||||
hostRCU.DNS = dns;
|
||||
hostRCU.Version = software_version;
|
||||
hostRCU.RunTime = (rcu_time.Year < 1800 ? DateTime.Now : rcu_time);
|
||||
hostRCU.LauncherVersion = launcher_version;
|
||||
hostRCU.MAC = mac;
|
||||
hostRCU.HotelCode = hotel_code.ToString();
|
||||
hostRCU.HostID = host_id;
|
||||
hostRCU.RoomTypeID = roomtype_id;
|
||||
hostRCU.ConfigVersion = setting_version.Substring(0, 5);
|
||||
|
||||
var TTT = new Tuple<Host, string>(host, hostRCU.ConfigVersion);
|
||||
Task.Factory.StartNew((State) =>
|
||||
{
|
||||
var NNN = (Tuple<Host, string>)State;
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = NNN.Item1.ID;
|
||||
//这里才是真正的版本数据
|
||||
string output = HostSearchReceiver.NormalizeVersion(NNN.Item2);
|
||||
bbb.ConfiguraVersion = output;
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(NNN.Item1, bbb);
|
||||
}, TTT);
|
||||
|
||||
hostRCU.RoomStatusID = room_status_id;
|
||||
|
||||
#region 更新缓存
|
||||
////host_take.HotelID = host.SysHotel.ID;
|
||||
//host_take.IPType = ipType;
|
||||
////host_take.TypeNumber = type_number;
|
||||
//host_take.LanIP = lan_ip;
|
||||
//host_take.ServerIP = server_ip;
|
||||
//host_take.SubnetMask = subnet_mask;
|
||||
//host_take.Gateway = gateway;
|
||||
//host_take.LanPort = lan_port;
|
||||
//host_take.DNS = dns;
|
||||
//host_take.Version = software_version;
|
||||
//host_take.RunTime = (rcu_time.Year < 1800 ? DateTime.Now : rcu_time);
|
||||
//host_take.LauncherVersion = launcher_version;
|
||||
//host_take.MAC = mac;
|
||||
//host_take.SysHotel.Code = hotel_code.ToString();
|
||||
//host_take.ID = host_id;
|
||||
//host_take.RoomType.ID = roomtype_id;
|
||||
//host_take.ConfigVersion = setting_version.Substring(0, 5);
|
||||
//host_take.RoomStatus.ID = room_status_id;
|
||||
//host_take.Season = sbSeason.ToString();
|
||||
//host_take.LockStatus = lock_status;
|
||||
|
||||
|
||||
//host_take.RoomNumber = roomnumber;
|
||||
//host_take.Model = model;
|
||||
//host_take.UpgradeTime = DateTime.Now;
|
||||
#endregion
|
||||
|
||||
|
||||
var roomStatus = RoomStatusRepository.Get(room_status_id);
|
||||
if (roomStatus != null)
|
||||
{
|
||||
hostRCU.RoomStatus = roomStatus.Name;
|
||||
}
|
||||
hostRCU.Season = sbSeason.ToString();
|
||||
hostRCU.LockStatus = lock_status;
|
||||
switch (expiration_time)
|
||||
{
|
||||
case 0://永久
|
||||
hostRCU.ExpireTime = Convert.ToDateTime("2100-12-31");
|
||||
hostRCU.SetExpireTime = TimeHelper.ToDateTime(set_expiration_time);
|
||||
|
||||
break;
|
||||
case 65535://未设置当永久
|
||||
hostRCU.ExpireTime = Convert.ToDateTime("2100-12-31");
|
||||
|
||||
break;
|
||||
default:
|
||||
hostRCU.ExpireTime = TimeHelper.ToDateTime(expiration_time);
|
||||
hostRCU.SetExpireTime = TimeHelper.ToDateTime(set_expiration_time);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
hostRCU.RoomNumber = roomnumber;
|
||||
hostRCU.RoomType = roomtype;
|
||||
hostRCU.RoomRemark = room_remark;
|
||||
hostRCU.Core = core;
|
||||
hostRCU.Model = model;
|
||||
hostRCU.HotelName = hotel_name;
|
||||
hostRCU.RoomTypeRemark = roomtype_remark;
|
||||
hostRCU.UpdateTime = DateTime.Now;
|
||||
|
||||
HostRepository.SetModelAndLauncher(host, lan_ip, lan_port, model, launcher_version, hostRCU.ExpireTime, hostRCU.SetExpireTime);
|
||||
|
||||
HostRCURepository.SaveOrUpdate(hostRCU);
|
||||
|
||||
//logger.Error(string.Format("酒店({0})客房({1})主机已同步信息,Model:{2},Launcher:{3}", host.SysHotel.Name + host.SysHotel.Code, host.RoomNumber, model, launcher_version));
|
||||
|
||||
#region 寄存器读取(弃用)
|
||||
/*int count = reader.ReadByte();//个数
|
||||
reader.ReadBytes(4);
|
||||
string lan_ip = String.Join(".", reader.ReadBytes(4));//局域网IP
|
||||
reader.ReadBytes(4);
|
||||
int lan_port = BitConverter.ToInt32(reader.ReadBytes(4), 0);//局域网端口
|
||||
reader.ReadBytes(4);
|
||||
string subnet_mask = String.Join(".", reader.ReadBytes(4));//子网掩码
|
||||
reader.ReadBytes(4);
|
||||
string gateway = String.Join(".", reader.ReadBytes(4));//网关
|
||||
reader.ReadBytes(4);
|
||||
string dns = String.Join(".", reader.ReadBytes(4));//DNS
|
||||
reader.ReadBytes(4);
|
||||
string server_ip = String.Join(".", reader.ReadBytes(4));//服务器IP
|
||||
reader.ReadBytes(4);
|
||||
int server_port = BitConverter.ToInt32(reader.ReadBytes(4), 0);//服务器端口
|
||||
reader.ReadBytes(4);
|
||||
long expiration_time = BitConverter.ToInt32(reader.ReadBytes(4), 0);//授权到期时间戳
|
||||
reader.ReadBytes(4);
|
||||
long set_expiration_time = BitConverter.ToInt32(reader.ReadBytes(4), 0);//设置到期时间戳
|
||||
reader.ReadBytes(4);
|
||||
int ip_way = reader.ReadByte();//IP类型:1自动,2手动
|
||||
reader.ReadBytes(3);
|
||||
reader.ReadBytes(4);
|
||||
string firmware_version = String.Join(".", reader.ReadBytes(4));//固件版本号
|
||||
reader.ReadBytes(4);
|
||||
string setting_version = String.Join(".", reader.ReadBytes(4));//配置版本号
|
||||
reader.ReadBytes(4);
|
||||
byte[] season = reader.ReadBytes(4);//季节
|
||||
StringBuilder sbSeason = new StringBuilder();
|
||||
sbSeason.Append((season[0] >> 0) & 3);
|
||||
sbSeason.Append((season[0] >> 2) & 3);
|
||||
sbSeason.Append((season[0] >> 4) & 3);
|
||||
sbSeason.Append((season[0] >> 6) & 3);
|
||||
|
||||
sbSeason.Append((season[1] >> 0) & 3);
|
||||
sbSeason.Append((season[1] >> 2) & 3);
|
||||
sbSeason.Append((season[1] >> 4) & 3);
|
||||
sbSeason.Append((season[1] >> 6) & 3);
|
||||
|
||||
sbSeason.Append((season[2] >> 0) & 3);
|
||||
sbSeason.Append((season[2] >> 2) & 3);
|
||||
sbSeason.Append((season[2] >> 4) & 3);
|
||||
sbSeason.Append((season[2] >> 6) & 3);
|
||||
|
||||
logger.Error(string.Format("count:{0},server_ip:{1},expiration_time:{2},ip_way:{3},firmware_version:{4},setting_version:{5},season:{6}",
|
||||
count, server_ip, expiration_time, ip_way, firmware_version, setting_version, sbSeason.ToString()));
|
||||
|
||||
switch (expiration_time)
|
||||
{
|
||||
case 0://永久
|
||||
host.ExpireTime = Convert.ToDateTime("2999-12-31");
|
||||
host.SetExpireTime = TimeHelper.ToDateTime(set_expiration_time.ToString());
|
||||
break;
|
||||
case 65535://未设置
|
||||
host.ExpireTime = host.SetExpireTime = null;
|
||||
break;
|
||||
default:
|
||||
host.ExpireTime = TimeHelper.ToDateTime(expiration_time.ToString());
|
||||
host.SetExpireTime = TimeHelper.ToDateTime(set_expiration_time.ToString());
|
||||
break;
|
||||
}
|
||||
host.LanIP = lan_ip;
|
||||
host.LanPort = lan_port;
|
||||
host.SubnetMask = subnet_mask;
|
||||
host.Gateway = gateway;
|
||||
host.DNS = dns;
|
||||
host.ServerIP = server_ip;
|
||||
host.ServerPort = server_port;
|
||||
host.IPType = ip_way;
|
||||
//host.Version = firmware_version;
|
||||
//host.ConfigVersion = setting_version;
|
||||
host.Season = sbSeason.ToString();
|
||||
HostRepository.Update(host);*/
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(string.Format("解释酒店({0})客房({1})主机信息失败,原因:{2},数据:{3}", host.SysHotel.Name + host.SysHotel.Code, host.RoomNumber, ex.ToString(), msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.RCUInfo; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
110
RCUHost/Implement/HostRegisterSetReceiver.cs
Normal file
110
RCUHost/Implement/HostRegisterSetReceiver.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送命令给rcu,设置寄存器内容(改为下发房型房号)
|
||||
/// </summary>
|
||||
public class HostRegisterSetReceiver : GenericReceiverBase, IHostRegisterSetReceiver
|
||||
{
|
||||
//private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostRegisterSetReceiver));
|
||||
/// <summary>
|
||||
/// 下发所有寄存器内容
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
public void Send(Host host)
|
||||
{
|
||||
var data = CreateDevicePacket(host, new byte[] { });
|
||||
//logger.Error(Tools.ByteToString(data));
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);
|
||||
}
|
||||
/// <summary>
|
||||
/// 指定下发内容
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="sendData"></param>
|
||||
public void Send(Host host, byte[] sendData)
|
||||
{
|
||||
var data = CreateDevicePacket(host, sendData);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);
|
||||
}
|
||||
|
||||
private byte[] CreateDevicePacket(Host host, byte[] sendData)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
byte[] deviceSecretDataPacket = sendData.Length > 0 ? sendData : CreateDeviceDataPacket(host);
|
||||
int size = StructConverter.SizeOf(systemHeader) + deviceSecretDataPacket.Length + 2;
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(deviceSecretDataPacket);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 下发内容
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateDeviceDataPacket(Host host)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(buffer))
|
||||
{
|
||||
writer.Write(new byte[] { 0x0B });//存储寄存器内容的个数
|
||||
|
||||
writer.Write(new byte[] { 0x38, 0, 0, 0 });//项目编号
|
||||
writer.Write(Tools.LongToByte4(Convert.ToInt64(host.SysHotel.Code)));// Encoding.Default.GetBytes(host.SysHotel.Code), 0, 4);
|
||||
|
||||
writer.Write(new byte[] { 0x44, 0, 0, 0 });//房号ID
|
||||
writer.Write(BitConverter.GetBytes(host.ID), 0, 4);
|
||||
|
||||
writer.Write(new byte[] { 0x48, 0, 0, 0 });//房型ID
|
||||
writer.Write(Tools.LongToByte4(Convert.ToInt64(host.RoomType.ID)));//BitConverter.GetBytes(host.RoomType.ID), 0, 4);
|
||||
|
||||
byte[] roomNumber = Tools.GetBytes(host.RoomNumber, 16);
|
||||
writer.Write(new byte[] { 0, 0x01, 0, 0 });//房号名称(第1个寄存器)
|
||||
writer.Write(roomNumber, 0, 4);
|
||||
writer.Write(new byte[] { 0x04, 0x01, 0, 0 });//房号名称(第2个寄存器)
|
||||
writer.Write(roomNumber, 4, 4);
|
||||
writer.Write(new byte[] { 0x08, 0x01, 0, 0 });//房号名称(第3个寄存器)
|
||||
writer.Write(roomNumber, 8, 4);
|
||||
writer.Write(new byte[] { 0x0C, 0x01, 0, 0 });//房号名称(第4个寄存器)
|
||||
writer.Write(roomNumber, 12, 4);
|
||||
|
||||
byte[] roomType = Tools.GetBytes(host.RoomType.Name, 16);
|
||||
writer.Write(new byte[] { 0x10, 0x01, 0, 0 });//房型名称(第1个寄存器)
|
||||
writer.Write(roomType, 0, 4);
|
||||
writer.Write(new byte[] { 0x14, 0x01, 0, 0 });//房型名称(第2个寄存器)
|
||||
writer.Write(roomType, 4, 4);
|
||||
writer.Write(new byte[] { 0x18, 0x01, 0, 0 });//房型名称(第3个寄存器)
|
||||
writer.Write(roomType, 8, 4);
|
||||
writer.Write(new byte[] { 0x1C, 0x01, 0, 0 });//房型名称(第4个寄存器)
|
||||
writer.Write(roomType, 12, 4);
|
||||
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.SetRegister; }
|
||||
}
|
||||
}
|
||||
}
|
||||
509
RCUHost/Implement/HostSearchReceiver.cs
Normal file
509
RCUHost/Implement/HostSearchReceiver.cs
Normal file
@@ -0,0 +1,509 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class HostSearchReceiver : GenericReceiverBase, IHostSearchReceiver
|
||||
{
|
||||
public static object objlock = new object();
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostSearchReceiver));
|
||||
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IHostModalRepository HostModalRepository { get; set; }
|
||||
public IRoomTypeModalRepository RoomTypeModalRepository { get; set; }
|
||||
public ISysHotelRepository SysHotelRepository { get; set; }
|
||||
public IGroupRepository GroupRepository { get; set; }
|
||||
public IRoomTypeRepository RoomTypeRepository { get; set; }
|
||||
public IDeviceSecretReceiver DeviceSecretReceiver { get; set; }
|
||||
public IHostRegisterReceiver HostRegisterReceiver { get; set; }//注册时通知同步主机信息
|
||||
|
||||
private static bool searching = false;
|
||||
|
||||
/// <summary>
|
||||
/// 当前搜索主机用户
|
||||
/// </summary>
|
||||
private static String user = "";
|
||||
|
||||
private SearchHostResultHandler searchHostResultHandler;
|
||||
|
||||
public void Start(SearchHostResultHandler handler)
|
||||
{
|
||||
searchHostResultHandler = handler;
|
||||
searching = true;
|
||||
HostServer.AddReceiver(this);
|
||||
Start();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
//byte[] data = CreateSearchHostRequestPacket();
|
||||
//Send(data, MulticastIP, MulticastPort);
|
||||
}
|
||||
|
||||
public bool Searching
|
||||
{
|
||||
get { return searching; }
|
||||
}
|
||||
|
||||
public string User
|
||||
{
|
||||
get { return user; }
|
||||
set { user = value ?? String.Empty; }
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
HostServer.RemoveReceiver(this);
|
||||
searchHostResultHandler = null;
|
||||
searching = false;
|
||||
user = "";
|
||||
}
|
||||
/// <summary>
|
||||
/// 修改协议:原来的搜索改成注册,当mac地址已存在时,更新数据,否则插入新主机
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
StepTongJi.SendInfo(4, "注册命令Task内部开始执行", context.MessageID, context.IsMonitor);
|
||||
//Reply(context);
|
||||
|
||||
var OriginalByte = context.Data;
|
||||
|
||||
int lll = OriginalByte.Length;
|
||||
var A1 = OriginalByte.Skip(15).Take(lll - 15 - 2).ToArray();
|
||||
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
SearchHostPacketReply? reply = DecodeSearchHostPacketReply(context.Data, startIndex);
|
||||
SearchHostPacketReplyV2? reply2 = null;
|
||||
if (context.Data.Length > 100)//V2版本新补充的内容
|
||||
{
|
||||
reply2 = DecodeSearchHostPacketReplyV2(context.Data, 58);
|
||||
}
|
||||
if (reply.HasValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
string hhostnumber = context.SystemHeader.Value.HostNumber.ToString();
|
||||
string hotelCode = context.SystemHeader.Value.HostNumber.ToHotelCode().ToString();//获取酒店编码
|
||||
string mac = BitConverter.ToString(reply.Value.MAC);
|
||||
string version = reply.Value.Version;
|
||||
string ip = context.RemoteEndPoint.Address.ToString();
|
||||
int port = context.RemoteEndPoint.Port;
|
||||
Host exitEntity = null;
|
||||
StringBuilder sbSQL = new StringBuilder();
|
||||
sbSQL.Append("UPDATE tb_Hosts SET ");
|
||||
if (reply2.HasValue)//C系列主机处理:只能靠后台手工添加
|
||||
{
|
||||
#region C系列主要
|
||||
if (string.IsNullOrEmpty(context.SystemHeader.Value.HostNumber.ToString()))
|
||||
{
|
||||
//logger.Error(string.Format("C主机注册主机编号不能为空。HotelCode:{0},MAC:{1}", hotelCode, mac));
|
||||
return;
|
||||
}
|
||||
|
||||
Host EEE = null;
|
||||
lock (objlock)
|
||||
{
|
||||
EEE = HostRepository.LoadByMac(mac).OrderByDescending(r => r.ID).FirstOrDefault();
|
||||
}
|
||||
if (EEE == null)
|
||||
{
|
||||
//logger.Error(string.Format("C主机注册通过MAC和版本号C开头找不到记录。HotelCode:{0},MAC:{1}", hotelCode, mac));
|
||||
return;//通过mac地址找不到C系列主机,则不新增
|
||||
}
|
||||
if (string.IsNullOrEmpty(EEE.RoomNumber))
|
||||
{
|
||||
//logger.Error(string.Format("C主机注册通过MAC和版本号C开头找到记录的房号为空。HotelCode:{0},MAC:{1}", hotelCode, mac));
|
||||
return;//如果房号是空,则不更新
|
||||
}
|
||||
string KKK1 = CacheKey.HostInfo_Key_MAC + "_" + hotelCode + "_" + mac;
|
||||
string KKK2 = CacheKey.HostInfo_Key_HostNumber + "_" + hhostnumber;
|
||||
object ooo = MemoryCacheHelper.Get(KKK1);
|
||||
if (ooo != null)
|
||||
{
|
||||
exitEntity = (Host)ooo;
|
||||
}
|
||||
else
|
||||
{
|
||||
exitEntity = EEE;
|
||||
MemoryCacheHelper.Set(KKK1, EEE);
|
||||
MemoryCacheHelper.SlideSet(KKK2, EEE);
|
||||
}
|
||||
exitEntity.DNS = string.Join(".", reply2.Value.DNS);
|
||||
sbSQL.Append("DNS='" + exitEntity.DNS + "'");
|
||||
if (!version.ToUpper().StartsWith("C"))
|
||||
{
|
||||
version = "C" + version;//如果命令里的版本号不是C开头,则补上C
|
||||
}
|
||||
//if (exitEntity.RoomNumber != reply2.Value.RoomNumber)
|
||||
//{
|
||||
// //如果房号不匹配,则给rcu发命令,同步房号
|
||||
//}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
#region 老主机
|
||||
string Key = CacheKey.HotelInfo_Key_Code + "_" + hotelCode; ;
|
||||
object ooo = MemoryCacheHelper.Get(Key);
|
||||
SysHotel sysHotel = null;
|
||||
if (ooo != null)
|
||||
{
|
||||
sysHotel = (SysHotel)ooo;
|
||||
}
|
||||
else
|
||||
{
|
||||
sysHotel = SysHotelRepository.GetByCode(hotelCode);//获取酒店记录
|
||||
MemoryCacheHelper.SlideSet(hotelCode, sysHotel);
|
||||
}
|
||||
if (sysHotel == null)//如果无酒店记录,则设置默认酒店
|
||||
{
|
||||
sysHotel = new SysHotel { ID = 1 };
|
||||
}
|
||||
exitEntity = HostRepository.GetByMAC(mac, sysHotel.ID);//根据mac地址获取指定酒店下主机
|
||||
if (exitEntity == null)//老主机新增
|
||||
{
|
||||
var entity = new Host();
|
||||
entity.HostNumber = context.SystemHeader.Value.HostNumber.ToString();
|
||||
entity.RoomNumber = context.SystemHeader.Value.HostNumber.ToRoomNumber();
|
||||
entity.MAC = mac;
|
||||
entity.SysHotel = sysHotel;
|
||||
Group group = GroupRepository.GetGroupList(sysHotel.ID).OrderBy(r => r.ID).FirstOrDefault();
|
||||
entity.Group = group != null ? group : new Group { ID = 1 };
|
||||
RoomType roomType = RoomTypeRepository.LoadAll().Where(r => r.HotelID == sysHotel.ID && r.Default == true).OrderBy(r => r.ID).FirstOrDefault();
|
||||
entity.RoomType = roomType != null ? roomType : new RoomType { ID = 1 };
|
||||
entity.IP = ip;
|
||||
entity.Port = port;
|
||||
entity.Version = reply.Value.Version;
|
||||
entity.ConfigVersion = String.Join(".", reply.Value.ConfigVersion);
|
||||
entity.SubnetMask = String.Join(".", reply.Value.SubnetMask);
|
||||
entity.Gateway = String.Join(".", reply.Value.Gateway);
|
||||
entity.Remark = hotelCode;
|
||||
entity.RoomStatus = new RoomStatus { ID = 16 };//默认房态“空房”
|
||||
entity.SafeStatus = 2;//默认未接保险箱
|
||||
entity.RegisterDate = DateTime.Now;
|
||||
entity.IsSyncRoomNumber = false;
|
||||
entity.IsAutoUpdate = false;
|
||||
entity.Last_Modified_Time = DateTime.Now;//标识有更新
|
||||
entity = GetDeviceSecret(entity, ref sbSQL);//获取密钥
|
||||
HostRepository.Save(entity);//保存主机
|
||||
PublishDeviceSecret(entity);//下发密钥
|
||||
Task.Factory.StartNew<bool>(() => UpdateHostModals(entity));//创建新回路
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
sbSQL.Append("MAC='" + mac + "'");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
bool isChanged = false;
|
||||
string hostNumber = context.SystemHeader.Value.HostNumber.ToString();
|
||||
string configVersion = String.Join(".", reply.Value.ConfigVersion);
|
||||
string subnetMask = String.Join(".", reply.Value.SubnetMask);
|
||||
string gateway = String.Join(".", reply.Value.Gateway);
|
||||
if (true)
|
||||
//if (exitEntity.HostNumber != hostNumber)
|
||||
{
|
||||
exitEntity.HostNumber = hostNumber;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",HostNumber='" + exitEntity.HostNumber + "'");
|
||||
}
|
||||
//if (exitEntity.Version != version)
|
||||
if (true)
|
||||
{
|
||||
exitEntity.Version = version;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",Version='" + exitEntity.Version + "'");
|
||||
}
|
||||
if (true)
|
||||
//if (exitEntity.ConfigVersion != configVersion)
|
||||
{
|
||||
exitEntity.ConfigVersion = configVersion;
|
||||
isChanged = true;
|
||||
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = exitEntity.ID;
|
||||
|
||||
string output = NormalizeVersion(exitEntity.ConfigVersion); // 输出 "15.0.0"
|
||||
bbb.ConfiguraVersion = output;
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(exitEntity, bbb);
|
||||
|
||||
sbSQL.Append(",ConfigVersion='" + exitEntity.ConfigVersion + "'");
|
||||
}
|
||||
//if (exitEntity.SubnetMask != subnetMask)
|
||||
if (true)
|
||||
{
|
||||
exitEntity.SubnetMask = subnetMask;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",SubnetMask='" + exitEntity.SubnetMask + "'");
|
||||
}
|
||||
//if (exitEntity.Gateway != gateway)
|
||||
if (true)
|
||||
{
|
||||
exitEntity.Gateway = gateway;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",Gateway='" + exitEntity.Gateway + "'");
|
||||
}
|
||||
//if (exitEntity.Remark != hotelCode)
|
||||
if (true)
|
||||
{
|
||||
exitEntity.Remark = hotelCode;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",Remark='" + exitEntity.Remark + "'");
|
||||
}
|
||||
if (isChanged)
|
||||
{
|
||||
exitEntity.Last_Modified_Time = DateTime.Now;//标识有更新
|
||||
sbSQL.Append(",Last_Modified_Time=GETDATE()");
|
||||
}
|
||||
//if (exitEntity.IP != ip)
|
||||
if (true)
|
||||
{
|
||||
exitEntity.IP = ip;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",IP='" + exitEntity.IP + "'");
|
||||
}
|
||||
if (true)
|
||||
//if (exitEntity.Port != port)
|
||||
{
|
||||
exitEntity.Port = port;
|
||||
isChanged = true;
|
||||
sbSQL.Append(",Port=" + exitEntity.Port);
|
||||
}
|
||||
//Host hostTemp = HostRepository.Get(exitEntity.ID);//重新获取主机最新升级更新状态数据
|
||||
//exitEntity.UpgradeStatus = hostTemp.UpgradeStatus;
|
||||
//exitEntity.UpgradeTime = hostTemp.UpgradeTime;
|
||||
if (isChanged)
|
||||
{
|
||||
exitEntity = GetDeviceSecret(exitEntity, ref sbSQL);//获取密钥
|
||||
sbSQL.Append(" WHERE ID=" + exitEntity.ID);
|
||||
|
||||
//string RegisterKey1 = "SearchHostPass";
|
||||
//string RegisterKey2 = "SearchHostFilter";
|
||||
//暂时的取消
|
||||
if (hotelCode.Equals("1197"))
|
||||
{
|
||||
HostRepository.Update(sbSQL.ToString());//更新主机,用sql语句更新更高效
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
//string KKK = "RegisterKey_" + hhostnumber;
|
||||
//object OOO = MemoryCacheHelper.Get(KKK);
|
||||
//if (OOO != null)
|
||||
//{
|
||||
// RCUHost.RCUHostCommon.tools.LanJieData(RegisterKey2, hotelCode);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
// MemoryCacheHelper.Set(KKK, ti, DateTimeOffset.Now.AddMinutes(5));
|
||||
// HostRepository.Update(sbSQL.ToString());//更新主机,用sql语句更新更高效
|
||||
// RCUHost.RCUHostCommon.tools.LanJieData(RegisterKey1, hotelCode);
|
||||
//}
|
||||
HostRepository.Update(sbSQL.ToString());//更新主机,用sql语句更新更高效
|
||||
|
||||
//有升级的时候,不能被跳过
|
||||
string Key = "Upgrade_UpdateSQL_" + exitEntity.HostNumber;
|
||||
object OOO1 = MemoryCacheHelper.Get(Key);
|
||||
if (OOO1 != null)
|
||||
{
|
||||
HostRepository.Update(sbSQL.ToString());//更新主机,用sql语句更新更高效
|
||||
MemoryCacheHelper.Delete(Key);
|
||||
}
|
||||
}
|
||||
|
||||
PublishDeviceSecret(exitEntity);//下发密钥
|
||||
}
|
||||
if (exitEntity.Version.StartsWith("C"))
|
||||
{
|
||||
CSRedisCacheHelper.Set(mac, exitEntity.IP + ":" + exitEntity.Port);//心跳包更新
|
||||
|
||||
string RegisterKey1 = "HostSearchReceiveDrop";
|
||||
RCUHost.RCUHostCommon.tools.LanJieData(RegisterKey1, hotelCode);
|
||||
|
||||
//这里的功能暂时去掉
|
||||
//这里会发送 B1 数据 一定 要注意
|
||||
//B1不能被去掉
|
||||
HostRegisterReceiver.Send(exitEntity);//通知同步工具
|
||||
|
||||
//logger.Error(string.Format("通知酒店({0})客房({1})同步主机信息。", exitEntity.SysHotel.Code, exitEntity.RoomNumber));
|
||||
}
|
||||
|
||||
string HKey1 = CacheKey.HostInfo_Key_MAC + hotelCode + "_" + mac;
|
||||
string HKey = CacheKey.HostInfo_Key_HostNumber + "_" + hostNumber;
|
||||
object takeout_obj = MemoryCacheHelper.Get(HKey);
|
||||
if (takeout_obj == null)
|
||||
{
|
||||
MemoryCacheHelper.SlideSet(HKey, exitEntity);
|
||||
MemoryCacheHelper.SlideSet(HKey1, exitEntity);
|
||||
}
|
||||
|
||||
|
||||
string YiJingChuLiGuo = CacheKey.AllReadyDealWith01_Prefix + "_" + hostNumber;
|
||||
MemoryCacheHelper.Set(YiJingChuLiGuo, A1, DateTimeOffset.Now.AddMinutes(5));
|
||||
StepTongJi.SendInfo(5, "注册命令Task内部执行完毕", context.MessageID, context.IsMonitor);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(string.Format("终端注册失败,来自:{0},原因:{1},数据:{2}", context.RemoteEndPoint.Address.ToString(), ex.ToString(), Tools.ByteToString(context.Data)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string NormalizeVersion(string version, int desiredParts = 3)
|
||||
{
|
||||
// 移除末尾的冗余点并分割
|
||||
var parts = version.TrimEnd('.').Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
// 补零到目标位数
|
||||
while (parts.Length < desiredParts)
|
||||
{
|
||||
parts = parts.Concat(new[] { "0" }).ToArray();
|
||||
}
|
||||
|
||||
return string.Join(".", parts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取主机设备密钥
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <returns></returns>
|
||||
private Host GetDeviceSecret(Host host, ref StringBuilder sbSQL)
|
||||
{
|
||||
//如果所属酒店需要自动获取密钥,且目前密钥是空,则自动获取
|
||||
if (host.SysHotel.IsAutoGetKey && string.IsNullOrEmpty(host.DeviceSecret))
|
||||
{
|
||||
DeviceRegisterResult resultData = FreeGoOperation.DeviceRegister(host.MAC);
|
||||
if (resultData.errcode == "0")
|
||||
{
|
||||
host.DeviceName = resultData.device_name;
|
||||
sbSQL.Append(",DeviceName='" + host.DeviceName + "'");
|
||||
host.DeviceSecret = resultData.device_secret;
|
||||
sbSQL.Append(",DeviceSecret='" + host.DeviceSecret + "'");
|
||||
host.IotId = resultData.iot_id;
|
||||
sbSQL.Append(",IotId='" + host.IotId + "'");
|
||||
host.ProductKey = resultData.product_key;
|
||||
sbSQL.Append(",ProductKey='" + host.ProductKey + "'");
|
||||
host.IsPublish = false;
|
||||
sbSQL.Append(",IsPublish=0");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error(string.Format("调用FreeGo接口返回错误:RoomNumber:{0},ErrorCode:{1},ErrorMessage:{2}", host.RoomNumber, resultData.errcode, resultData.msg));
|
||||
}
|
||||
}
|
||||
return host;
|
||||
}
|
||||
/// <summary>
|
||||
/// 下发设备密钥
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
private void PublishDeviceSecret(Host host)
|
||||
{
|
||||
if (!host.IsPublish && !string.IsNullOrEmpty(host.DeviceSecret))
|
||||
{
|
||||
DeviceSecretReceiver.Send(host);
|
||||
HostRepository.SetPublish(host, true);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建新回路
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <returns></returns>
|
||||
private bool UpdateHostModals(Host host)
|
||||
{
|
||||
if (host == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
HostModalRepository.DeteleByHostID(host.ID);
|
||||
var list = RoomTypeModalRepository.LoadAll().Where(r => r.RoomType == host.RoomType);
|
||||
DateTime now = DateTime.Now;
|
||||
foreach (var modal in list)
|
||||
{
|
||||
HostModalRepository.Save(new HostModal { HostID = host.ID, Modal = modal, Status = 2, Time = 0, UpdateTime = now });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.SearchHost; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private byte[] CreateSearchHostRequestPacket()
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
SearchHostPacket searchHostPacket = CreateSearchHostPacket();
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(searchHostPacket);
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] src1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(src1, 0, buffer, 0, src1.Length);
|
||||
|
||||
byte[] src3 = StructConverter.StructToBytes(searchHostPacket);
|
||||
Array.Copy(src3, 0, buffer, src1.Length, src3.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private SearchHostPacket CreateSearchHostPacket()
|
||||
{
|
||||
#if DEBUG
|
||||
var ip = Tools.GetLocalIP();
|
||||
#else
|
||||
var ip = MessageIP;
|
||||
#endif
|
||||
|
||||
SearchHostPacket packet = new SearchHostPacket
|
||||
{
|
||||
IsSave = 1,
|
||||
IP = IPAddress.Parse(ip).GetAddressBytes(),
|
||||
Port = Convert.ToUInt16(MessagePort)
|
||||
};
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码 SearchHostPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private SearchHostPacketReply? DecodeSearchHostPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(SearchHostPacketReply)) as SearchHostPacketReply?;
|
||||
}
|
||||
|
||||
private SearchHostPacketReplyV2? DecodeSearchHostPacketReplyV2(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(SearchHostPacketReplyV2)) as SearchHostPacketReplyV2?;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
94
RCUHost/Implement/HostSecretReceiver.cs
Normal file
94
RCUHost/Implement/HostSecretReceiver.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using Dao;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送命令给rcu,获取密钥
|
||||
/// </summary>
|
||||
public class HostSecretReceiver : GenericReceiverBase, IHostSecretReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostSecretReceiver));
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
public void Send(Host host)
|
||||
{
|
||||
var data = CreateHostSecretPacket(host);
|
||||
Send(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
public byte[] CreateHostSecretPacket(Host host)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
int size = StructConverter.SizeOf(systemHeader) + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//base.Process(context);
|
||||
|
||||
string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
|
||||
Host host = null;
|
||||
string Key = CacheKey.HostInfo_Key_HostNumber + "_" + HostNumberOnly;
|
||||
object obj = MemoryCacheHelper.Get(Key);
|
||||
if (obj != null)
|
||||
{
|
||||
host = obj as Host;
|
||||
}
|
||||
else
|
||||
{
|
||||
host = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());
|
||||
MemoryCacheHelper.SlideSet(Key,host);
|
||||
}
|
||||
|
||||
if (host != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
int offset = StructConverter.SizeOf(context.SystemHeader);
|
||||
int length = context.Data.Length - offset - 2;
|
||||
using (MemoryStream stream = new MemoryStream(context.Data, offset, length))
|
||||
{
|
||||
using (BinaryReader reader = new BinaryReader(stream))
|
||||
{
|
||||
reader.ReadBytes(4);//string hostVersion = String.Join(".", reader.ReadBytes(4));
|
||||
reader.ReadBytes(6);//string mac = BitConverter.ToString(reader.ReadBytes(6));
|
||||
string hostSecret = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(32));
|
||||
host.HostSecret = hostSecret;
|
||||
HostRepository.Update(host);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString() + ":" + Tools.ByteToString(context.Data));
|
||||
logger.Error("解析【" + host.HostNumber + "】客房主机密钥出错。", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.GetHostSecret; }
|
||||
}
|
||||
}
|
||||
}
|
||||
2336
RCUHost/Implement/HostServer.cs
Normal file
2336
RCUHost/Implement/HostServer.cs
Normal file
File diff suppressed because it is too large
Load Diff
77
RCUHost/Implement/HotelTimeReceiver.cs
Normal file
77
RCUHost/Implement/HotelTimeReceiver.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using Dao;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 酒店上下午时间:白天黑夜处理
|
||||
/// </summary>
|
||||
public class HotelTimeReceiver : GenericReceiverBase, IHotelTimeReceiver
|
||||
{
|
||||
//private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HotelTimeReceiver));
|
||||
|
||||
/// <summary>
|
||||
/// 下发白天黑夜
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
public void Send(Host host)
|
||||
{
|
||||
var data = CreateDevicePacket(host);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//logger.Error(string.Format("收到白天起始时间命令回复,编码:{0},IP:{1}", context.SystemHeader.Value.HostNumber.ToString(), context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString()));
|
||||
}
|
||||
|
||||
private byte[] CreateDevicePacket(Host host)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
byte[] deviceSecretDataPacket = CreateDeviceDataPacket(host);
|
||||
int size = StructConverter.SizeOf(systemHeader) + deviceSecretDataPacket.Length + 2;
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(deviceSecretDataPacket);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 下发内容
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateDeviceDataPacket(Host host)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(buffer))
|
||||
{
|
||||
writer.Write(new byte[] { (byte)host.SysHotel.StartDayTime, (byte)host.SysHotel.EndDayTime });
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.HotelTime; }
|
||||
}
|
||||
}
|
||||
}
|
||||
57
RCUHost/Implement/MusicControlReceiver.cs
Normal file
57
RCUHost/Implement/MusicControlReceiver.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 音乐控制
|
||||
/// </summary>
|
||||
public class MusicControlReceiver : GenericReceiverBase, IMusicControlReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送音乐按键
|
||||
/// </summary>
|
||||
/// <param name="host">主机</param>
|
||||
/// <param name="key">按键</param>
|
||||
public void SendKey(Host host, MusicKey key)
|
||||
{
|
||||
var data = CreateDataPacket( key);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(MusicKey key)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
int headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
stream.WriteByte((byte)0);
|
||||
stream.WriteByte((byte)key);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
SystemHeader systemHeader = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
|
||||
var systemHeaderData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(systemHeaderData, 0, systemHeaderData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.MusicControl; }
|
||||
}
|
||||
}
|
||||
}
|
||||
53
RCUHost/Implement/NetworkSettingReceiver.cs
Normal file
53
RCUHost/Implement/NetworkSettingReceiver.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using RCUHost.Protocols;
|
||||
using Common;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class NetworkSettingReceiver : GenericReceiverBase, INetworkSettingReceiver
|
||||
{
|
||||
public bool SetNetwork(Domain.Host host, string ip, string subnetmask, string gateway, int port)
|
||||
{
|
||||
byte[] data = CreateDataPacket(ip, subnetmask, gateway, port);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(string ip, string subnetmask, string gateway, int port)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
var headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
stream.Write(IPAddress.Parse(ip).GetAddressBytes(), 0, 4);
|
||||
stream.Write(IPAddress.Parse(subnetmask).GetAddressBytes(), 0, 4);
|
||||
stream.Write(IPAddress.Parse(gateway).GetAddressBytes(), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes((ushort)port), 0, 2);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
var header = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
|
||||
byte[] headerData = StructConverter.StructToBytes(header);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.NetworkSetting; }
|
||||
}
|
||||
}
|
||||
}
|
||||
1790
RCUHost/Implement/New_RoomStatusReceiver.cs
Normal file
1790
RCUHost/Implement/New_RoomStatusReceiver.cs
Normal file
File diff suppressed because it is too large
Load Diff
97
RCUHost/Implement/PowerSupplyControlReceiver.cs
Normal file
97
RCUHost/Implement/PowerSupplyControlReceiver.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using Dao;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 主机电源控制
|
||||
/// </summary>
|
||||
public class PowerSupplyControlReceiver : GenericReceiverBase, IPowerSupplyControlReceiver
|
||||
{
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发送主机电源控制
|
||||
/// </summary>
|
||||
/// <param name="host">主机</param>
|
||||
/// <param name="ctrl">电源控制</param>
|
||||
public void SendCtrl(Host host, PowerSupplyCtrl ctrl)
|
||||
{
|
||||
var data = CreateDataPacket(ctrl);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private PowerSupplyPacketReply? DecodePowerSupplyPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(PowerSupplyPacketReply)) as PowerSupplyPacketReply?;
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//base.Process(context);
|
||||
|
||||
//int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
|
||||
//PowerSupplyPacketReply? reply = DecodePowerSupplyPacketReply(context.Data, startIndex);
|
||||
|
||||
//if (reply.HasValue)
|
||||
//{
|
||||
// var supplyPacketHost = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());
|
||||
|
||||
// if (supplyPacketHost != null)
|
||||
// {
|
||||
// if (reply.Value.Status == PowerSupplyPacketReply.Open)
|
||||
// {
|
||||
// supplyPacketHost.PowerSupply = true;
|
||||
// }
|
||||
// else if (reply.Value.Status == PowerSupplyPacketReply.Close)
|
||||
// {
|
||||
// supplyPacketHost.PowerSupply = false;
|
||||
// }
|
||||
// HostRepository.Update(supplyPacketHost);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
private byte[] CreateDataPacket(PowerSupplyCtrl key)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
int headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
stream.WriteByte((byte)key);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
SystemHeader systemHeader = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
|
||||
var systemHeaderData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(systemHeaderData, 0, systemHeaderData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.PowerSupplyControl; }
|
||||
}
|
||||
}
|
||||
}
|
||||
59
RCUHost/Implement/RCULogReceiver.cs
Normal file
59
RCUHost/Implement/RCULogReceiver.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CacheManager.Core;
|
||||
using Common;
|
||||
using Dao;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志服务器域名设置
|
||||
/// </summary>
|
||||
public class RCULogReceiver : GenericReceiverBase, IRCULogReceiver
|
||||
{
|
||||
//private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HeartReceiver));
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//logger.Error("收到获取IP命令:" + Tools.ByteToString(context.Data));
|
||||
byte[] data = CreateDataPacket(context);
|
||||
Send(data, context.RemoteEndPoint);//解释域名IP,返回给rcu
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(ReceiverContext context)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
var HostNumberOnly = context.SystemHeader.Value.HostNumber;
|
||||
System.Net.IPAddress ipAddr = System.Net.IPAddress.Parse("106.53.160.26");
|
||||
long code = HostNumberOnly.ToHotelCode();
|
||||
if (code != 1564)
|
||||
{
|
||||
ipAddr = Tools.GetIPByDomain("BoonliveNAS.synology.me");
|
||||
}
|
||||
byte[] ip = ipAddr.GetAddressBytes();
|
||||
int size = StructConverter.SizeOf(systemHeader) + ip.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(ip);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.SetRCULog; }
|
||||
}
|
||||
}
|
||||
}
|
||||
46
RCUHost/Implement/ReceiverContext.cs
Normal file
46
RCUHost/Implement/ReceiverContext.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class ReceiverContext
|
||||
{
|
||||
private byte[] orginData;
|
||||
|
||||
private IPEndPoint remoteEndPoint;
|
||||
|
||||
private long customer;
|
||||
|
||||
/// <summary>
|
||||
/// 监控ID
|
||||
/// </summary>
|
||||
public string MessageID="";
|
||||
public bool IsMonitor = false;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="orginData"></param>
|
||||
/// <param name="remoteEndPoint"></param>
|
||||
/// <param name="customer">自定义序号:用于推送通讯命令</param>
|
||||
public ReceiverContext(byte[] orginData, IPEndPoint remoteEndPoint, long customer)
|
||||
{
|
||||
this.orginData = orginData;
|
||||
this.remoteEndPoint = remoteEndPoint;
|
||||
this.customer = customer;
|
||||
}
|
||||
|
||||
public byte[] Data { get { return this.orginData; } }
|
||||
|
||||
public IPEndPoint RemoteEndPoint { get { return this.remoteEndPoint; } }
|
||||
|
||||
public SystemHeader? SystemHeader { get; set; }
|
||||
/// <summary>
|
||||
/// 自定义序号:用于推送通讯命令
|
||||
/// </summary>
|
||||
public long Customer { get { return this.customer; } }
|
||||
}
|
||||
}
|
||||
441
RCUHost/Implement/RoomCardReceiver.cs
Normal file
441
RCUHost/Implement/RoomCardReceiver.cs
Normal file
@@ -0,0 +1,441 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using System.Xml;
|
||||
using System.Threading.Tasks;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 插卡取电 Receiver
|
||||
/// </summary>
|
||||
public class RoomCardReceiver : GenericReceiverBase
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(RoomCardReceiver));
|
||||
//private static string debug_hotel_code = System.Configuration.ConfigurationManager.AppSettings["debug_hotel_code"];
|
||||
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IRoomCardRepository RoomCardRepository { get; set; }
|
||||
public IHostRoomCardRepository HostRoomCardRepository { get; set; }
|
||||
public IRoomCardTypeRepository RoomCardTypeRepository { get; set; }
|
||||
public IAlarmSettingRepository AlarmSettingRepository { get; set; }
|
||||
public IRoomServiceRepository RoomServiceRepository { get; set; }
|
||||
public IRoomServiceRecordRepository RoomServiceRecordRepository { get; set; }
|
||||
public IHostModalRepository HostModalRepository { get; set; }
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
Reply(context);
|
||||
string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
InsertCardPacket? packet = DecodeInsertCardPacket(context.Data, startIndex);
|
||||
if (packet.HasValue)
|
||||
{
|
||||
//SomeDeviceExistsData host = null;
|
||||
//RoomStatusReceiver R = new RoomStatusReceiver();
|
||||
//host = R.HostDataTake(context);
|
||||
|
||||
Host host = null;
|
||||
string Key = CacheKey.RoomStatus_Prefix + "_" + HostNumberOnly;
|
||||
object obj = MemoryCacheHelper.Get(Key);
|
||||
if (obj != null)
|
||||
{
|
||||
host = obj as Host;
|
||||
}
|
||||
else
|
||||
{
|
||||
Host host_O = HostRepository.GetByHostNumber(HostNumberOnly);
|
||||
MemoryCacheHelper.SlideSet(Key,host_O);
|
||||
host = host_O;
|
||||
}
|
||||
|
||||
|
||||
if (host != null)
|
||||
{
|
||||
//Host hhh = new Host();
|
||||
//hhh.ID = host.HostID;
|
||||
//hhh.FrameNo = host.FrameNo;
|
||||
//hhh.PowerSupply = host.PowerSupply;
|
||||
//hhh.LockVoltage = host.LockVoltage;
|
||||
//hhh.HostTemp = host.HostTemp;
|
||||
|
||||
//var tuple_a = state as Tuple<Host, InsertCardPacket?>;
|
||||
//var host = tuple_a.Item1;
|
||||
//var packet = tuple_a.Item2;
|
||||
switch (packet.Value.InsertCard)
|
||||
{
|
||||
case 1://插卡
|
||||
var cardType = RoomCardTypeRepository.Get((int)packet.Value.CardType);//获取房卡类型
|
||||
if (cardType == null)
|
||||
{
|
||||
cardType = RoomCardTypeRepository.Get(1);//如果没有记录,获取有人房卡类型
|
||||
}
|
||||
var roomCard = RoomCardRepository.GetByCartType(cardType, packet.Value.CardNumber.ToString(), host.SysHotel.ID); //GetRoomCard(packet.Value.CardNumber.ToString(), cardType);
|
||||
if (roomCard == null)//如果该房卡未创建记录,自动创建
|
||||
{
|
||||
roomCard = new RoomCard();
|
||||
roomCard.CardNumber = packet.Value.CardNumber.ToString();
|
||||
roomCard.RoomCardType = cardType;
|
||||
roomCard.HotelID = host.SysHotel.ID;
|
||||
RoomCardRepository.Save(roomCard);
|
||||
}
|
||||
HostRepository.SetRoomCard(host, roomCard);
|
||||
break;
|
||||
default://拔卡
|
||||
HostRepository.SetRoomCard(host, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理连通房插卡取电
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
//private void ProcessConnectingRooms(Host host, byte insertCard, RoomCardType cardType, RoomCard roomCard)
|
||||
//{
|
||||
// byte[] data = CreateInsertCardDataPacket(insertCard, cardType, roomCard);
|
||||
// var connectingRoomHosts = HostRepository.GetConnectRoomHosts(host);
|
||||
// foreach (var host1 in connectingRoomHosts)
|
||||
// {
|
||||
// Send(data, host.HostNumber);// host1.IP, host1.Port);
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 处理房卡相关逻辑
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
private void ProcessRoomCard(Host host)
|
||||
{
|
||||
#region 处理“非客人在保险箱开”异常
|
||||
{
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C03");
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, abnormitySetting.Code);
|
||||
if (abnormitySetting != null && abnormity != null)
|
||||
{
|
||||
bool isUpdated = false;
|
||||
//如果非客人,且已出租,且保险箱开,则报警
|
||||
if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 &&
|
||||
host.RoomStatus != null && host.RoomStatus.ID == 0x02 && host.SafeStatus == 1)
|
||||
{
|
||||
isUpdated = true;
|
||||
if (!abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C03", true);
|
||||
if (abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormitySetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isUpdated)//关掉报警
|
||||
{
|
||||
if (abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C03", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 处理“非客人在房门关”异常
|
||||
{
|
||||
// "非客人在房门关报警" 设置
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A05");
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
bool isUpdated = false;
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C06");
|
||||
if (abnormity != null && host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 && !host.DoorLockStatus)
|
||||
{
|
||||
isUpdated = true;
|
||||
var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A06");
|
||||
if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
|
||||
{
|
||||
if (!abnormity.Status && abnormity.StartTime < DateTime.Now.AddSeconds(-Convert.ToInt16(delaySetting.Value)))
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “非客人在房门关”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C06");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//启用线程执行延时报警
|
||||
Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
|
||||
{
|
||||
if (!abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “非客人在房门关”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C06");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!isUpdated)
|
||||
{
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “非客人在房门关 记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 处理“无人房门开”异常
|
||||
{
|
||||
// "无人在房门开报警" 设置
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A03");
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
if (host.RoomCard == null && host.DoorLockStatus)
|
||||
{
|
||||
//报警延时设置
|
||||
var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A04");
|
||||
if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C02");
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “无人房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C02");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//启用线程执行延时报警
|
||||
Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C02");
|
||||
if (abnormity != null && !abnormity.Status && abnormity.StartTime < DateTime.Now.AddSeconds(-Convert.ToInt16(delaySetting.Value)))
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “无人房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C02");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C02");
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “无人房门开”异常 记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 处理“客人在房门开”异常
|
||||
{
|
||||
// "客人在房门开报警" 设置
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A01");
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID == 0x20 && host.DoorLockStatus)
|
||||
{
|
||||
//报警延时设置
|
||||
var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A02");
|
||||
if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C01");
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “客人在房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C01");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//启用线程执行延时报警
|
||||
Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C01");
|
||||
if (abnormity != null && !abnormity.Status && abnormity.StartTime < DateTime.Now.AddSeconds(-Convert.ToInt16(delaySetting.Value)))
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “客人在房门开”异常 记录
|
||||
var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C01");
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
|
||||
if (abnormityRecordSetting != null && abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, "C01");
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
// “客人在房门开”异常 记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
//private RoomCard GetRoomCard(string cardNumber, RoomCardType cardType)
|
||||
//{
|
||||
// return RoomCardRepository.Get(cardType, cardNumber);
|
||||
//}
|
||||
/// <summary>
|
||||
/// 获取最后一次插卡未拔卡的记录
|
||||
/// </summary>
|
||||
/// <param name="hostID"></param>
|
||||
/// <returns></returns>
|
||||
private HostRoomCard GetLastHostRoomCard(int hostID)
|
||||
{
|
||||
return HostRoomCardRepository.GetLastHostRoomCard(hostID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建插卡数据包
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateInsertCardDataPacket(byte insertCard, RoomCardType cardType, RoomCard roomCard)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
var headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
stream.WriteByte(insertCard);
|
||||
stream.WriteByte((byte)(cardType != null ? cardType.ID : 0));
|
||||
int cardNumber = 0;
|
||||
if (roomCard != null && String.IsNullOrEmpty(roomCard.CardNumber))
|
||||
{
|
||||
cardNumber = Convert.ToInt32(roomCard.CardNumber);
|
||||
}
|
||||
stream.Write(BitConverter.GetBytes(cardNumber), 0, 4);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
var systemHeader = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
var headerData = StructConverter.StructToBytes(systemHeader);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 解码 InsertCardPacket
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private InsertCardPacket? DecodeInsertCardPacket(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(InsertCardPacket)) as InsertCardPacket?;
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.InsertCard; }
|
||||
}
|
||||
}
|
||||
}
|
||||
108
RCUHost/Implement/RoomStatusChangedReceiver.cs
Normal file
108
RCUHost/Implement/RoomStatusChangedReceiver.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class RoomStatusChangedReceiver : GenericReceiverBase, IRoomStatusChangedReceiver
|
||||
{
|
||||
public void SendRoomStatus(Host host, RoomStatus status)
|
||||
{
|
||||
byte[] data = CreateRoomStatusChangedRequestPacket(status);
|
||||
|
||||
var U = data.Take(16).ToList();
|
||||
//U.Add(0x00);
|
||||
U.Add(0x01);
|
||||
U.AddRange(new byte[] { 0x00, 0x00 });
|
||||
SendAndPushCommandQueue(U.ToArray(), host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
|
||||
//ProcessRoomStatus(host, status);
|
||||
}
|
||||
public void SendRoomStatusSelf(string HostNumber, string Mac, RoomStatus status)
|
||||
{
|
||||
byte[] data = CreateRoomStatusChangedRequestPacket(status);
|
||||
|
||||
SendAndPushCommandQueue(data, HostNumber, Mac);// host.IP, host.Port);
|
||||
|
||||
//ProcessRoomStatus(host, status);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新版本要 状态位要变成2个字节
|
||||
/// </summary>
|
||||
/// <param name="HostNumber"></param>
|
||||
/// <param name="Mac"></param>
|
||||
/// <param name="status"></param>
|
||||
/// <param name="FrameNo"></param>
|
||||
public void SendRoomStatusSelfWithFrameNo(string HostNumber, string Mac, RoomStatus status, byte[] FrameNo)
|
||||
{
|
||||
//AA 55 12 00 54 33 53 41 0C 22 80 FF FF FF FF 04 00 80 CO
|
||||
byte[] data = CreateRoomStatusChangedRequestPacket(status);
|
||||
|
||||
data[9] = FrameNo[0];
|
||||
data[10] = FrameNo[1];
|
||||
var U = data.Take(16).ToList();
|
||||
U.Add(0x00);
|
||||
U.AddRange(new byte[] { 0x00, 0x00 });
|
||||
SendAndPushCommandQueue(U.ToArray(), HostNumber, Mac);// host.IP, host.Port);
|
||||
|
||||
//ProcessRoomStatus(host, status);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理房态相关逻辑
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="roomStatus"></param>
|
||||
private void ProcessRoomStatus(Host host, RoomStatus roomStatus)
|
||||
{
|
||||
#region 处理“退房保险箱关”异常
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.RoomStatusChanged; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private byte[] CreateRoomStatusChangedRequestPacket(RoomStatus status)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
RoomStatusChangedPacket roomStatusChangedPacket = CreateRoomStatusChangedPacket(status);
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(roomStatusChangedPacket);
|
||||
size = size + 1;
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] src1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(src1, 0, buffer, 0, src1.Length);
|
||||
|
||||
byte[] src3 = StructConverter.StructToBytes(roomStatusChangedPacket);
|
||||
Array.Copy(src3, 0, buffer, src1.Length, src3.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private RoomStatusChangedPacket CreateRoomStatusChangedPacket(RoomStatus status)
|
||||
{
|
||||
return new RoomStatusChangedPacket { Status = (byte)status.ID };
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2564
RCUHost/Implement/RoomStatusReceiver.cs
Normal file
2564
RCUHost/Implement/RoomStatusReceiver.cs
Normal file
File diff suppressed because it is too large
Load Diff
398
RCUHost/Implement/ServiceReceiver.cs
Normal file
398
RCUHost/Implement/ServiceReceiver.cs
Normal file
@@ -0,0 +1,398 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class ServiceReceiver : GenericReceiverBase, IServiceReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ServiceReceiver));
|
||||
private ConcurrentDictionary<string, ConcurrentDictionary<int, ServiceStatus>> roomsServices = new ConcurrentDictionary<string, ConcurrentDictionary<int, ServiceStatus>>();
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IAlarmSettingRepository AlarmSettingRepository { get; set; }
|
||||
public IRoomServiceRepository RoomServiceRepository { get; set; }
|
||||
public IRoomServiceRecordRepository RoomServiceRecordRepository { get; set; }
|
||||
|
||||
public void SetService(Host host, int serviceCode, int status)
|
||||
{
|
||||
byte[] data = CreateServiceControlDataPacket(serviceCode, status);
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
bool bkb = GenericReceiverBase.DealWwith(context);
|
||||
if (bkb == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Reply(context);
|
||||
Host host = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());//.RemoteEndPoint.Address.ToString());
|
||||
if (host != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
int offset = StructConverter.SizeOf(context.SystemHeader);
|
||||
int length = context.Data.Length - offset - 2;
|
||||
using (MemoryStream stream = new MemoryStream(context.Data, offset, length))
|
||||
{
|
||||
var services = DecodeServices(host.IP, stream);
|
||||
foreach (var service in services)
|
||||
{
|
||||
if (service.Value.Status != 2)
|
||||
{
|
||||
//处理服务
|
||||
ProcessServices(host, service.Value);
|
||||
//设置Host表的保险箱状态
|
||||
if (service.Value.Code == 12)
|
||||
{
|
||||
HostRepository.SetSafeBoxStatus(host, service.Value.Status);
|
||||
}
|
||||
//处理异常
|
||||
ProcessAbnormities(host, service.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (logger.IsErrorEnabled)
|
||||
{
|
||||
logger.Error(context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString() + ":" + Tools.ByteToString(context.Data));
|
||||
logger.Error("解析【" + host.HostNumber + "】客房服务数据出错。", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理服务
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="status"></param>
|
||||
private void ProcessServices(Host host, ServiceStatus status)
|
||||
{
|
||||
var serviceSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'B', status.Code);
|
||||
if (serviceSetting != null && status.Status != 2)
|
||||
{
|
||||
//更新客房服务状态
|
||||
var roomService = RoomServiceRepository.Get(host.ID, serviceSetting.Code);
|
||||
if (roomService != null && roomService.Status != Convert.ToBoolean(status.Status))
|
||||
{
|
||||
roomService.Status = Convert.ToBoolean(status.Status);
|
||||
roomService.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(roomService);
|
||||
}
|
||||
|
||||
if (status.Status == 0)
|
||||
{
|
||||
//更新清除服务记录状态和时间
|
||||
var serviceRecord = RoomServiceRecordRepository.Get(host.ID, serviceSetting.Code, true);
|
||||
if (serviceRecord != null)
|
||||
{
|
||||
serviceRecord.Status = false;
|
||||
serviceRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(serviceRecord);
|
||||
}
|
||||
}
|
||||
else if (status.Status == 1)
|
||||
{
|
||||
//判断数据库中是否存在服务开启记录,不存在才去记录服务
|
||||
var existServiceRecord = RoomServiceRecordRepository.Get(host.ID, serviceSetting.Code, true);
|
||||
if (existServiceRecord == null)
|
||||
{
|
||||
var serviceRecord = new RoomServiceRecord();
|
||||
serviceRecord.HostID = host.ID;
|
||||
serviceRecord.RoomNumber = host.RoomNumber;
|
||||
serviceRecord.AlarmCode = serviceSetting.Code;
|
||||
serviceRecord.Name = serviceSetting.Name;
|
||||
serviceRecord.Status = true;
|
||||
serviceRecord.StartTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Save(serviceRecord);
|
||||
}
|
||||
}
|
||||
//给FreeGo(福瑞狗)上报服务状态
|
||||
//FreeGoOperation.UploadService(host.SysHotel.Code, host.RoomNumber, serviceSetting.Name, status.Status.ToString());
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理异常
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="status"></param>
|
||||
private void ProcessAbnormities(Host host, ServiceStatus status)
|
||||
{
|
||||
#region 处理“非客人在保险箱开”异常
|
||||
|
||||
var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C03", status.Code);
|
||||
if (abnormitySetting != null)
|
||||
{
|
||||
var abnormity = RoomServiceRepository.Get(host.ID, abnormitySetting.Code);
|
||||
bool isUpdated = false;
|
||||
if (status.Status == 1)
|
||||
{
|
||||
//保险箱开 如果非客人卡且出租房态,则报警
|
||||
if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 &&
|
||||
host.RoomStatus != null && host.RoomStatus.ID == 0x02)
|
||||
{
|
||||
isUpdated = true;
|
||||
if (abnormity != null && !abnormity.Status)
|
||||
{
|
||||
abnormity.Status = true;
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
//插入保险箱开的记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, abnormitySetting.Code, true);
|
||||
if (abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormitySetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//保险箱关或未接保险箱(关掉报警)
|
||||
if (!isUpdated)
|
||||
{
|
||||
if (abnormity != null && abnormity.Status)
|
||||
{
|
||||
abnormity.Status = false;//取消报警
|
||||
abnormity.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity);
|
||||
//更新保险箱关的记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, abnormitySetting.Code, true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 处理“退房保险箱关”异常
|
||||
|
||||
abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C04", status.Code);
|
||||
var abnormity1 = RoomServiceRepository.Get(host.ID, "C04");
|
||||
if (abnormitySetting != null && abnormity1 != null)
|
||||
{
|
||||
bool isUpdated = false;
|
||||
if (status.Status == 0)
|
||||
{
|
||||
//保险箱关 如果是“退房”状态,则报警
|
||||
if (host.RoomStatus != null && host.RoomStatus.ID == 0x08)
|
||||
{
|
||||
isUpdated = true;
|
||||
if (!abnormity1.Status)
|
||||
{
|
||||
abnormity1.Status = true;
|
||||
abnormity1.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity1);
|
||||
//异常记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, abnormitySetting.Code, true);
|
||||
if (abnormityRecord == null)
|
||||
{
|
||||
abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormitySetting);
|
||||
RoomServiceRecordRepository.Save(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isUpdated)
|
||||
{
|
||||
//保险箱开或未接保险箱(关掉报警)
|
||||
if (abnormity1.Status)
|
||||
{
|
||||
abnormity1.Status = false;
|
||||
abnormity1.StartTime = DateTime.Now;
|
||||
RoomServiceRepository.Update(abnormity1);
|
||||
//异常记录
|
||||
var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, abnormitySetting.Code, true);
|
||||
if (abnormityRecord != null)
|
||||
{
|
||||
abnormityRecord.Status = false;
|
||||
abnormityRecord.EndTime = DateTime.Now;
|
||||
RoomServiceRecordRepository.Update(abnormityRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建服务控制数据包
|
||||
/// </summary>
|
||||
/// <param name="devices"></param>
|
||||
/// <returns></returns>
|
||||
public byte[] CreateServiceControlDataPacket(int serviceCode, int status)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream())
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(buffer))
|
||||
{
|
||||
var headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
buffer.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
writer.Write((byte)1); //写入服务数量
|
||||
writer.Write((byte)serviceCode); //写入服务代码
|
||||
writer.Write((byte)status); //写入服务状态
|
||||
writer.Write(new byte[] { 0, 0 }, 0, 2);//预留两字节CRC16校验码
|
||||
|
||||
buffer.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
var header = CreateSystemHeader((int)buffer.Length - headerLen);
|
||||
|
||||
var headerData = StructConverter.StructToBytes(header);
|
||||
|
||||
writer.Write(headerData, 0, headerData.Length);
|
||||
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.Service; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析服务状态
|
||||
/// </summary>
|
||||
/// <param name="stream"></param>
|
||||
/// <returns></returns>
|
||||
private ConcurrentDictionary<int, ServiceStatus> DecodeServices(string hostIP, Stream stream)
|
||||
{
|
||||
ConcurrentDictionary<int, ServiceStatus> services = null;
|
||||
if (roomsServices.ContainsKey(hostIP))
|
||||
{
|
||||
services = roomsServices[hostIP];
|
||||
}
|
||||
else
|
||||
{
|
||||
services = new ConcurrentDictionary<int, ServiceStatus>();
|
||||
roomsServices[hostIP] = services;
|
||||
}
|
||||
using (BinaryReader reader = new BinaryReader(stream))
|
||||
{
|
||||
try
|
||||
{
|
||||
int count = reader.ReadByte();//读取服务数量
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
ServiceStatus serviceStatus = null;
|
||||
int serviceCode = reader.ReadByte();//读取服务代码
|
||||
if (services.ContainsKey(serviceCode))
|
||||
{
|
||||
serviceStatus = services[serviceCode];
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceStatus = new ServiceStatus();
|
||||
services[serviceCode] = serviceStatus;
|
||||
}
|
||||
|
||||
serviceStatus.Code = serviceCode;
|
||||
serviceStatus.Status = reader.ReadByte();//读取服务状态
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class ServiceStatus
|
||||
{
|
||||
private int code;
|
||||
private int status = 2;
|
||||
public int Code
|
||||
{
|
||||
get { return this.code; }
|
||||
set
|
||||
{
|
||||
this.code = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Status
|
||||
{
|
||||
get { return this.status; }
|
||||
set
|
||||
{
|
||||
this.status = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal delegate void ServiceChangedDelegate(string hostIP, ServiceStatus serviceStatus);
|
||||
|
||||
internal class RoomServiceCache
|
||||
{
|
||||
private Hashtable services = new Hashtable();
|
||||
|
||||
public string HostIP { get; set; }
|
||||
|
||||
public event ServiceChangedDelegate ServiceChanged;
|
||||
|
||||
public void AddOrUpdate(int code, int status)
|
||||
{
|
||||
ServiceStatus service = services[code] as ServiceStatus;
|
||||
if (service != null)
|
||||
{
|
||||
if (service.Status != status)
|
||||
{
|
||||
service.Status = status;
|
||||
|
||||
RasieServiceChanged(service);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
service = new ServiceStatus();
|
||||
service.Code = code;
|
||||
service.Status = status;
|
||||
|
||||
services[code] = service;
|
||||
|
||||
RasieServiceChanged(service);
|
||||
}
|
||||
}
|
||||
|
||||
public IList<ServiceStatus> Services
|
||||
{
|
||||
get
|
||||
{
|
||||
IList<ServiceStatus> values = new List<ServiceStatus>();
|
||||
|
||||
foreach (var service in services.Values)
|
||||
{
|
||||
values.Add((ServiceStatus)service);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
private void RasieServiceChanged(ServiceStatus serviceStatus)
|
||||
{
|
||||
if (ServiceChanged != null)
|
||||
{
|
||||
ServiceChanged(HostIP, serviceStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
175
RCUHost/Implement/SyncTimeReceiver.cs
Normal file
175
RCUHost/Implement/SyncTimeReceiver.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using RCUHost.Protocols;
|
||||
using Domain;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class SyncTimeReceiver : GenericReceiverBase, ISyncTimeReceiver
|
||||
{
|
||||
public ISysHotelRepository SysHotelRepository { get; set; }
|
||||
|
||||
private System.Timers.Timer timer;
|
||||
|
||||
//protected override void HostServer_AfterStart(HostServer hostServer)
|
||||
//{
|
||||
// Start();
|
||||
//}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
bool bkb = GenericReceiverBase.DealWwith(context);
|
||||
if (bkb == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int startDayTime = 6;//白天开始时间
|
||||
int endDayTime = 18;//白天截止时间
|
||||
|
||||
string hotelCode = context.SystemHeader.Value.HostNumber.ToHotelCode().ToString();
|
||||
|
||||
string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
|
||||
//如果超过2秒就不再处理了
|
||||
string ShiJianLanJie = CacheKey.SyncTimeIntercept + "_" + HostNumberOnly;
|
||||
object VVV = MemoryCacheHelper.Get(ShiJianLanJie);
|
||||
if (VVV == null)
|
||||
{
|
||||
string RegisterKey1 = "SyncTimeTimeOutDrop";
|
||||
RCUHost.RCUHostCommon.tools.LanJieData(RegisterKey1, hotelCode);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string Key = CacheKey.HotelInfo_Key_Code + "_" + hotelCode; ;
|
||||
object ooo = MemoryCacheHelper.Get(Key);
|
||||
SysHotel sysHotel = null;
|
||||
if (ooo != null)
|
||||
{
|
||||
sysHotel = (SysHotel)ooo;
|
||||
}
|
||||
else
|
||||
{
|
||||
sysHotel = SysHotelRepository.GetByCode(hotelCode);//获取酒店记录
|
||||
MemoryCacheHelper.SlideSet(hotelCode, sysHotel);
|
||||
}
|
||||
|
||||
//SysHotel sysHotel = SysHotelRepository.GetByCode(context.SystemHeader.Value.HostNumber.ToHotelCode().ToString());
|
||||
if (sysHotel != null)
|
||||
{
|
||||
startDayTime = sysHotel.StartDayTime;
|
||||
endDayTime = sysHotel.EndDayTime;
|
||||
}
|
||||
byte[] data = CreateSysTimePacket(DateTime.Now, startDayTime, endDayTime);
|
||||
|
||||
ushort frame_no = context.SystemHeader.Value.FrameNo;
|
||||
byte[] framenolist = BitConverter.GetBytes(frame_no);
|
||||
if (framenolist[0] == 0xFF && framenolist[1] == 0xFF)
|
||||
{
|
||||
Send(data, context.RemoteEndPoint);
|
||||
}
|
||||
else
|
||||
{
|
||||
data[9] = framenolist[0];
|
||||
data[10] = framenolist[1];
|
||||
Send(data, context.RemoteEndPoint);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="now">当前时间</param>
|
||||
/// <param name="startDayTime">白天开始时间</param>
|
||||
/// <param name="endDayTime">白天截止时间</param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateSysTimePacket(DateTime now, int startDayTime, int endDayTime)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
SyncTimePacket syncTimePacket = new SyncTimePacket
|
||||
{
|
||||
Year = (ushort)now.Year,
|
||||
Month = (byte)now.Month,
|
||||
Day = (byte)now.Day,
|
||||
Week = (byte)(now.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)now.DayOfWeek),
|
||||
Hour = (byte)now.Hour,
|
||||
Minute = (byte)now.Minute,
|
||||
Second = (byte)now.Second,
|
||||
StartDayTime = (byte)startDayTime,
|
||||
EndDayTime = (byte)endDayTime
|
||||
};
|
||||
|
||||
int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(syncTimePacket);
|
||||
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
byte[] data1 = StructConverter.StructToBytes(systemHeader);
|
||||
Array.Copy(data1, buffer, data1.Length);
|
||||
|
||||
byte[] data2 = StructConverter.StructToBytes(syncTimePacket);
|
||||
Array.Copy(data2, 0, buffer, data1.Length, data2.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (timer == null)
|
||||
{
|
||||
timer = new System.Timers.Timer();
|
||||
timer.AutoReset = true;
|
||||
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
||||
}
|
||||
|
||||
timer.Interval = 60000; //每隔60s
|
||||
|
||||
if (!timer.Enabled)
|
||||
{
|
||||
timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (timer != null)
|
||||
{
|
||||
timer.Stop();
|
||||
timer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
//整点同步时间
|
||||
if (now.Minute == 0)
|
||||
{
|
||||
//Sync(now, MulticastIP, MulticastPort);
|
||||
}
|
||||
}
|
||||
|
||||
//private void Sync(DateTime now, string host, int port)
|
||||
//{
|
||||
// Sync(now, new IPEndPoint(IPAddress.Parse(host), port));
|
||||
//}
|
||||
|
||||
//private void Sync(DateTime now, string hostNumber, string mac)
|
||||
//{
|
||||
// byte[] data = CreateSysTimePacket(now);
|
||||
// SendAndPushCommandQueue(data, hostNumber, mac);
|
||||
//}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.SyncTime; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
150
RCUHost/Implement/TFTPReceiver.cs
Normal file
150
RCUHost/Implement/TFTPReceiver.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using RCUHost.Protocols;
|
||||
using System.IO;
|
||||
using Common;
|
||||
using Domain;
|
||||
using Dao;
|
||||
using RestSharp;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class TFTPReceiver : GenericReceiverBase, IT_FTPReceiver
|
||||
{
|
||||
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(TFTPReceiver));
|
||||
|
||||
public ITFTP_SetRepository TFTPLogRepository { get; set; }
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.TFTPLog; }
|
||||
|
||||
}
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//logger.Error(string.Format("收到白天起始时间命令回复,编码:{0},IP:{1}", context.SystemHeader.Value.HostNumber.ToString(), context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString()));
|
||||
var data = context.Data;
|
||||
|
||||
|
||||
string msg = context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString() + ":" + Tools.ByteToString(context.Data);
|
||||
string hostnum = context.SystemHeader.Value.HostNumber.ToString();
|
||||
logger.Error("收到主机信息数据:" + msg);
|
||||
logger.Error("收到主机信息数据:" + hostnum);
|
||||
Host host = HostRepository.GetByHostNumber(hostnum);//.RemoteEndPoint.Address.ToString());
|
||||
|
||||
if (host != null)
|
||||
{
|
||||
string dataHex = Tools.ByteToString(context.Data);
|
||||
|
||||
int offset = StructConverter.SizeOf(context.SystemHeader);
|
||||
int length = context.Data.Length - offset - 2;
|
||||
using (MemoryStream stream = new MemoryStream(context.Data, offset, length))
|
||||
{
|
||||
using (BinaryReader reader = new BinaryReader(stream))
|
||||
{
|
||||
byte isEnable = reader.ReadByte();//是否启用
|
||||
int report_port = BitConverter.ToUInt16(reader.ReadBytes(2), 0);//局域网端口
|
||||
int report_lasttime = BitConverter.ToUInt16(reader.ReadBytes(2), 0);//上传多久
|
||||
int domain_len = reader.ReadByte();
|
||||
byte[] domain_fact = reader.ReadBytes(domain_len);
|
||||
string domainstring = Encoding.UTF8.GetString(domain_fact);
|
||||
|
||||
int hostid = host.ID;
|
||||
int hotelid = host.SysHotel.ID;
|
||||
|
||||
bool isenable = false;
|
||||
//日志开启状态
|
||||
//FF:全开 00:全关
|
||||
if (isEnable == 0xFF)
|
||||
{
|
||||
isenable = true;
|
||||
}
|
||||
string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
|
||||
TFTP_Set t = new TFTP_Set();
|
||||
|
||||
|
||||
t.HotelCode = host.SysHotel.Code;
|
||||
t.HotelID = host.SysHotel.ID;
|
||||
|
||||
t.HostID = hostid;
|
||||
t.RoomNumber = host.RoomNumber;
|
||||
|
||||
t.IsTrigger = isenable;
|
||||
t.LastTime = report_lasttime;
|
||||
t.TargetPort = report_port;
|
||||
t.TargetDomain = domainstring;
|
||||
t.CreateTime = ti;
|
||||
|
||||
TFTP_Set ttt = TFTPLogRepository.GetDataBy(hotelid, hostid);
|
||||
if (ttt != null)
|
||||
{
|
||||
|
||||
ttt.IsTrigger = isenable;
|
||||
ttt.LastTime = report_lasttime;
|
||||
ttt.TargetPort = report_port;
|
||||
ttt.TargetDomain = domainstring;
|
||||
ttt.CreateTime = ti;
|
||||
|
||||
TFTPLogRepository.Update(ttt);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TFTPLogRepository.Save(t);
|
||||
}
|
||||
|
||||
string debug_log_report_url = RCUHost.TimingHelper.Common.debug_log_report_url;
|
||||
string debug_log_report_mqtt_topic = "blw/tftp/report/"+hotelid;
|
||||
|
||||
string str = Newtonsoft.Json.JsonConvert.SerializeObject(t);
|
||||
var client1 = new RestClient(debug_log_report_url);
|
||||
var request1 = new RestRequest("/", Method.POST);
|
||||
|
||||
//注意方法是POST
|
||||
//两个参数名字必须是 topic 和payload ,区分大小写的
|
||||
request1.AddParameter("topic", debug_log_report_mqtt_topic);
|
||||
request1.AddParameter("payload", str);
|
||||
|
||||
|
||||
client1.ExecuteAsync(request1, (response) => { });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] CreateDataPacket()
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
//加2是 最后是CRC
|
||||
byte[] ip = new byte[] { 0x00 };
|
||||
int size = StructConverter.SizeOf(systemHeader) + ip.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(ip);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send_QueryData(byte[] originaldata, string hostnumber, string mac)
|
||||
{
|
||||
var data = CreateDataPacket();
|
||||
string vvv = Tools.ByteToString(data);
|
||||
logger.Error("cha xun:" + vvv + " hostnumber:" + hostnumber + " mac:" + mac);
|
||||
Send(data, hostnumber, mac);
|
||||
}
|
||||
}
|
||||
}
|
||||
57
RCUHost/Implement/TFTPSettingReceiver.cs
Normal file
57
RCUHost/Implement/TFTPSettingReceiver.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using RCUHost.Protocols;
|
||||
using System.IO;
|
||||
using Common;
|
||||
using Domain;
|
||||
using Dao;
|
||||
using RestSharp;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class TFTPSettingReceiver : GenericReceiverBase, IT_FTPSettingReceiver
|
||||
{
|
||||
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(TFTPReceiver));
|
||||
|
||||
//public ITFTP_SetRepository TFTPLogRepository { get; set; }
|
||||
//public IHostRepository HostRepository { get; set; }
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.TFTPLog_Setting; }
|
||||
|
||||
}
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public byte[] CreateDataPacket_Setting(byte[] data)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
|
||||
//加2是 最后是CRC
|
||||
int size = StructConverter.SizeOf(systemHeader) + data.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(data);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send_Setting_Data(byte[] data, string hostnumber, string mac)
|
||||
{
|
||||
byte[] result= CreateDataPacket_Setting(data);
|
||||
Send(result, hostnumber, mac);
|
||||
}
|
||||
}
|
||||
}
|
||||
57
RCUHost/Implement/TVControlReceiver.cs
Normal file
57
RCUHost/Implement/TVControlReceiver.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 电视控制
|
||||
/// </summary>
|
||||
public class TVControlReceiver : GenericReceiverBase, ITVControlReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送电视按键
|
||||
/// </summary>
|
||||
/// <param name="host">主机</param>
|
||||
/// <param name="key">按键</param>
|
||||
public void SendKey(Host host, TvKey key)
|
||||
{
|
||||
var data = CreateDataPacket( key);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(TvKey key)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
int headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
stream.WriteByte((byte)0);
|
||||
stream.WriteByte((byte)key);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
SystemHeader systemHeader = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
|
||||
var systemHeaderData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(systemHeaderData, 0, systemHeaderData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.TvControl; }
|
||||
}
|
||||
}
|
||||
}
|
||||
76
RCUHost/Implement/TouChuang.cs
Normal file
76
RCUHost/Implement/TouChuang.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using RCUHost.Protocols;
|
||||
using Common;
|
||||
using System.IO;
|
||||
using Domain;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 透传
|
||||
/// </summary>
|
||||
public class TouChuang : GenericReceiverBase, IHostTouChuan
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(TouChuang));
|
||||
public void Send(string HostNumber, string MAC, byte[] sendData, byte SelfCommandType, bool isoriginal = false)
|
||||
{
|
||||
if (isoriginal)
|
||||
{
|
||||
Send(sendData, HostNumber, MAC);
|
||||
}
|
||||
else
|
||||
{
|
||||
CommandType TTT = (CommandType)SelfCommandType;
|
||||
var data = CreateHostSecretPacket(sendData, TTT);
|
||||
Send(data, HostNumber, MAC);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] CreateHostSecretPacket(byte[] data, CommandType TTT)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeaderOverWrite(TTT, 0);
|
||||
//加2是 最后是CRC
|
||||
int size = StructConverter.SizeOf(systemHeader) + data.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(data);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected SystemHeader CreateSystemHeaderOverWrite(CommandType commandType, int dataLength)
|
||||
{
|
||||
var systemHeader = new SystemHeader();
|
||||
systemHeader.Signature = SystemHeader.SIGNATURE;
|
||||
systemHeader.FrameLength = (ushort)(StructConverter.SizeOf(systemHeader) + dataLength);
|
||||
systemHeader.SystemID = SystemHeader.SYSTEM_ID.ToCharArray();
|
||||
systemHeader.CmdType = (byte)commandType;
|
||||
systemHeader.FrameNo = GetNextFrameNo();
|
||||
systemHeader.HostNumber = new HostNumber { NBuild = 0xFF, NFloor = 0xFF, NRoom = 0xFF, NUnit = 0xFF };
|
||||
return systemHeader;
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get
|
||||
{
|
||||
return CommandType.RCUInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
RCUHost/Implement/UnlockControlReceiver.cs
Normal file
51
RCUHost/Implement/UnlockControlReceiver.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 开锁
|
||||
/// </summary>
|
||||
public class UnlockControlReceiver : GenericReceiverBase, IUnlockControlReceiver
|
||||
{
|
||||
public void Send(Host host)
|
||||
{
|
||||
var data = CreateDataPacket(1);
|
||||
|
||||
SendAndPushCommandQueue(data, host.HostNumber, host.MAC);
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(int status)
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
int headerLen = StructConverter.SizeOf(typeof(SystemHeader));
|
||||
|
||||
stream.Seek(headerLen, SeekOrigin.Begin);
|
||||
|
||||
stream.WriteByte((byte)status);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
SystemHeader systemHeader = CreateSystemHeader((int)stream.Length - headerLen);
|
||||
|
||||
var systemHeaderData = StructConverter.StructToBytes(systemHeader);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Write(systemHeaderData, 0, systemHeaderData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.UnlockControl; }
|
||||
}
|
||||
}
|
||||
}
|
||||
142
RCUHost/Implement/UpdateConfigReceiver.cs
Normal file
142
RCUHost/Implement/UpdateConfigReceiver.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class UpdateConfigReceiver : GenericReceiverBase, IUpdateConfigReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpdateConfigReceiver));
|
||||
private const int BLOCK_SIZE = 512; //TFTP块大小512字节
|
||||
|
||||
public void Update(HostUpdate hostUpdate, IList<Host> hosts)
|
||||
{
|
||||
if (hosts == null || hosts.Count == 0)
|
||||
{
|
||||
throw new ApplicationException("升级失败,没有找到需要升级的主机。");
|
||||
}
|
||||
if (String.IsNullOrEmpty(hostUpdate.Href))
|
||||
{
|
||||
throw new ApplicationException("升级失败,无效的配置文件。");
|
||||
}
|
||||
string updateFile = Tools.GetApplicationPath() + hostUpdate.Href;
|
||||
if (!File.Exists(updateFile))
|
||||
{
|
||||
throw new ApplicationException("升级失败,【" + updateFile + "】不是有效的配置文件。");
|
||||
}
|
||||
FileInfo fileInfo = new FileInfo(updateFile);
|
||||
ushort blockNum = (ushort)Math.Ceiling((double)fileInfo.Length / BLOCK_SIZE);
|
||||
//升级配置文件
|
||||
foreach (var host in hosts)
|
||||
{
|
||||
SendUpdateRequest(host, hostUpdate.Md5, blockNum);
|
||||
//this.updateHostList.Add(new UpdateHostWorker(host, hostUpdate));
|
||||
}
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//base.Process(context);
|
||||
//int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
//UpdateConfigPacketReply? reply = DecodeUpdateConfigPacketReply(context.Data, startIndex);
|
||||
//if (reply.HasValue)
|
||||
//{
|
||||
// logger.Error(string.Format("收到配置文件升级回复命令({0}:{1}):{2}", context.RemoteEndPoint.Address.ToString(), context.RemoteEndPoint.Port, Tools.ByteToString(context.Data)));
|
||||
// var updateHostWorker = updateHostList.FirstOrDefault(r => r.Host.HostNumber == context.SystemHeader.Value.HostNumber.ToString());
|
||||
// if (updateHostWorker != null)
|
||||
// {
|
||||
// //主机升级后重启会发送回复搜索命令,这时更新版本号
|
||||
// if (reply.Value.Status == UpdateHostPacketReply.Ready)
|
||||
// {
|
||||
// updateHostWorker.Update();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// updateHostList.Remove(updateHostWorker);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.UpdateConfig; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// 发送升级指令到RCU主机
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="updateFileMd5">升级文件MD5值</param>
|
||||
public void SendUpdateRequest(Host host, string updateFileMd5, ushort blockNum)
|
||||
{
|
||||
byte[] data = CreateUpdateConfigRequestPacket(updateFileMd5, blockNum);
|
||||
Send(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建升级配置数据请求数据包
|
||||
/// </summary>
|
||||
/// <param name="updateFileMd5">升级文件MD5值</param>
|
||||
/// <param name="blockNum">块数量</param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateUpdateConfigRequestPacket(string updateFileMd5, ushort blockNum)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
int headerSize = StructConverter.SizeOf(systemHeader);
|
||||
|
||||
#if DEBUG
|
||||
byte[] ipArr = IPAddress.Parse(Tools.GetLocalIP()).GetAddressBytes();
|
||||
#else
|
||||
byte[] ipArr = IPAddress.Parse(MessageIP).GetAddressBytes();
|
||||
#endif
|
||||
byte[] portArr = BitConverter.GetBytes((ushort)MessagePort);
|
||||
uint[] md5Arr = Tools.MD5StringToUIntArray(updateFileMd5);
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
stream.Seek(headerSize, SeekOrigin.Begin);
|
||||
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[0]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[1]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[2]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[3]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(blockNum), 0, 2);
|
||||
stream.Write(ipArr, 0, ipArr.Length);
|
||||
stream.Write(portArr, 0, portArr.Length);
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);
|
||||
|
||||
//填充 SystemHeader
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
systemHeader.FrameLength = (ushort)stream.Length;
|
||||
systemHeader.FrameNo = 0xFFFF;
|
||||
byte[] headerData = StructConverter.StructToBytes(systemHeader);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码 UpdateHostPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private UpdateConfigPacketReply? DecodeUpdateConfigPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(UpdateConfigPacketReply)) as UpdateConfigPacketReply?;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
131
RCUHost/Implement/UpdateHostCompletedReceiver.cs
Normal file
131
RCUHost/Implement/UpdateHostCompletedReceiver.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 专门处理升级完成时更新使用
|
||||
/// </summary>
|
||||
public class UpdateHostCompletedReceiver : GenericReceiverBase, IUpdateHostReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpdateHostCompletedReceiver));
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IHostUpdateStatusRepository HostUpdateStatusRepository { get; set; }
|
||||
private IList<UpdateHostWorker> updateHostList = new List<UpdateHostWorker>();
|
||||
|
||||
public void Update(HostUpdate hostUpdate, FileType fileType, string fileHref, string fileMd5, IList<Host> hosts)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
UpdateHostPacketReply? reply = DecodeUpdateHostPacketReply(context.Data, startIndex);
|
||||
if (reply.HasValue)
|
||||
{
|
||||
//logger.Error(string.Format("收到tftp升级回复命令({0}:{1}):{2}", context.RemoteEndPoint.Address.ToString(), context.RemoteEndPoint.Port, Tools.ByteToString(context.Data)));
|
||||
var updateHostWorker = this.updateHostList.FirstOrDefault(r => r.Host.SysHotel.Code == context.SystemHeader.Value.HostNumber.ToHotelCode().ToString() && r.Host.HostNumber == context.SystemHeader.Value.HostNumber.ToString());
|
||||
if (updateHostWorker != null)
|
||||
{
|
||||
|
||||
string Upgrade_Status = "";
|
||||
HostUpdateStatus hostUpdateStatus = HostUpdateStatusRepository.Get(updateHostWorker.Host, updateHostWorker.HostUpdate);
|
||||
if (hostUpdateStatus == null)
|
||||
{
|
||||
hostUpdateStatus = new HostUpdateStatus();
|
||||
hostUpdateStatus.Host = updateHostWorker.Host;
|
||||
hostUpdateStatus.HostUpdate = updateHostWorker.HostUpdate;
|
||||
hostUpdateStatus.PublishTime = DateTime.Now;
|
||||
hostUpdateStatus.Status = 0;
|
||||
}
|
||||
switch (reply.Value.Status)
|
||||
{
|
||||
case UpdateHostPacketReply.Ready:
|
||||
hostUpdateStatus.Status = 0;//升级就绪
|
||||
Upgrade_Status = "升级就绪";
|
||||
break;
|
||||
case UpdateHostPacketReply.Completed:
|
||||
Upgrade_Status = "升级完成";
|
||||
Reply(context);//升级完成回复主机
|
||||
hostUpdateStatus.Status = 1;//升级完成
|
||||
updateHostList.Remove(updateHostWorker);
|
||||
break;
|
||||
default:
|
||||
Upgrade_Status = "升级失败";
|
||||
hostUpdateStatus.Status = 2;//升级失败
|
||||
updateHostList.Remove(updateHostWorker);
|
||||
break;
|
||||
}
|
||||
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = updateHostWorker.Host.ID;
|
||||
bbb.Upgrade_status = Upgrade_Status;
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(updateHostWorker.Host, bbb);
|
||||
|
||||
hostUpdateStatus.UpdatedTime = DateTime.Now;
|
||||
HostUpdateStatusRepository.SaveOrUpdate(hostUpdateStatus);
|
||||
}
|
||||
else
|
||||
{
|
||||
Host host = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());
|
||||
if (host != null)
|
||||
{
|
||||
string Upgrade_Status = "";
|
||||
switch (reply.Value.Status)
|
||||
{
|
||||
case UpdateHostPacketReply.Ready:
|
||||
host.UpgradeStatus = 0;//升级就绪
|
||||
Upgrade_Status = "升级就绪";
|
||||
break;
|
||||
case UpdateHostPacketReply.Completed:
|
||||
Reply(context);//升级完成回复主机
|
||||
host.UpgradeStatus = 1;//升级完成
|
||||
Upgrade_Status = "升级完成";
|
||||
break;
|
||||
default:
|
||||
host.UpgradeStatus = 2;//升级失败
|
||||
Upgrade_Status = "升级失败";
|
||||
break;
|
||||
}
|
||||
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = host.ID;
|
||||
bbb.Upgrade_status = Upgrade_Status;
|
||||
bbb.Upgrade_DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(host, bbb);
|
||||
|
||||
host.UpgradeTime = DateTime.Now;
|
||||
HostRepository.Update(host);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.UpdateHost; }
|
||||
}
|
||||
#region Private Methods
|
||||
/// <summary>
|
||||
/// 解码 UpdateHostPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private UpdateHostPacketReply? DecodeUpdateHostPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(UpdateHostPacketReply)) as UpdateHostPacketReply?;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
180
RCUHost/Implement/UpdateHostFTPReceiver.cs
Normal file
180
RCUHost/Implement/UpdateHostFTPReceiver.cs
Normal file
@@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class UpdateHostFTPReceiver : GenericReceiverBase, IUpdateHostFTPReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpdateHostFTPReceiver));
|
||||
public IHostUpdateStatusRepository HostUpdateStatusRepository { get; set; }
|
||||
private IList<UpdateHostWorker> updateHostList = new List<UpdateHostWorker>();
|
||||
|
||||
public void Update(HostUpdate hostUpdate, FileType fileType, string fileHref, string fileMd5, IList<Host> hosts)
|
||||
{
|
||||
if (hosts == null || hosts.Count == 0)
|
||||
{
|
||||
throw new ApplicationException("升级失败,没有找到需要升级的主机。");
|
||||
}
|
||||
if (String.IsNullOrEmpty(fileHref))
|
||||
{
|
||||
throw new ApplicationException("升级失败,无效的升级文件。");
|
||||
}
|
||||
string updateFile = Tools.GetApplicationPath() + fileHref;
|
||||
if (!File.Exists(updateFile))
|
||||
{
|
||||
throw new ApplicationException("升级失败,【" + updateFile + "】文件不存在。");
|
||||
}
|
||||
this.updateHostList.Clear();
|
||||
foreach (var host in hosts)
|
||||
{
|
||||
SendUpdateRequest(host, fileMd5, (byte)fileType, fileHref.Substring(fileHref.IndexOf("/")));
|
||||
|
||||
this.updateHostList.Add(new UpdateHostWorker(host, hostUpdate, MessageIP, fileHref, updateFile)); ;
|
||||
HostUpdateStatus hostUpdateStatus = HostUpdateStatusRepository.Get(host, hostUpdate);
|
||||
if (hostUpdateStatus == null)
|
||||
{
|
||||
hostUpdateStatus = new HostUpdateStatus();
|
||||
hostUpdateStatus.Host = host;
|
||||
hostUpdateStatus.HostUpdate = hostUpdate;
|
||||
hostUpdateStatus.PublishTime = DateTime.Now;
|
||||
}
|
||||
hostUpdateStatus.Status = 0;
|
||||
hostUpdateStatus.UpdatedTime = DateTime.Now;
|
||||
HostUpdateStatusRepository.SaveOrUpdate(hostUpdateStatus);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
UpdateHostPacketReply? reply = DecodeUpdateHostPacketReply(context.Data, startIndex);
|
||||
if (reply.HasValue)
|
||||
{
|
||||
//logger.Error(string.Format("收到ftp升级回复命令({0}:{1}):{2}", context.RemoteEndPoint.Address.ToString(), context.RemoteEndPoint.Port, Tools.ByteToString(context.Data)));
|
||||
var updateHostWorker = updateHostList.FirstOrDefault(r => r.Host.SysHotel.Code == context.SystemHeader.Value.HostNumber.ToHotelCode().ToString() && r.Host.HostNumber == context.SystemHeader.Value.HostNumber.ToString());
|
||||
if (updateHostWorker != null)
|
||||
{
|
||||
SaveSystemLog(30, "升级主机", string.Format("收到主机({0})升级回复命令,状态:{1}", updateHostWorker.Host.RoomNumber, reply.Value.Status), "收到命令", "RCU", context.RemoteEndPoint.Address.ToString(), updateHostWorker.Host.SysHotel.ID);
|
||||
HostUpdateStatus hostUpdateStatus = HostUpdateStatusRepository.Get(updateHostWorker.Host, updateHostWorker.HostUpdate);
|
||||
if (hostUpdateStatus == null)
|
||||
{
|
||||
hostUpdateStatus = new HostUpdateStatus();
|
||||
hostUpdateStatus.Host = updateHostWorker.Host;
|
||||
hostUpdateStatus.HostUpdate = updateHostWorker.HostUpdate;
|
||||
hostUpdateStatus.PublishTime = DateTime.Now;
|
||||
hostUpdateStatus.Status = 0;
|
||||
}
|
||||
switch (reply.Value.Status)
|
||||
{
|
||||
case UpdateHostPacketReply.Ready:
|
||||
hostUpdateStatus.Status = 0;
|
||||
break;
|
||||
case UpdateHostPacketReply.Completed:
|
||||
Reply(context);//升级完成回复主机
|
||||
hostUpdateStatus.Status = 1;
|
||||
updateHostList.Remove(updateHostWorker);
|
||||
break;
|
||||
default:
|
||||
hostUpdateStatus.Status = 2;
|
||||
updateHostList.Remove(updateHostWorker);
|
||||
break;
|
||||
}
|
||||
hostUpdateStatus.UpdatedTime = DateTime.Now;
|
||||
HostUpdateStatusRepository.SaveOrUpdate(hostUpdateStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.FTPUpdate; }
|
||||
}
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// 发送升级指令到RCU主机
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="updateFileMd5">升级文件MD5值</param>
|
||||
private void SendUpdateRequest(Host host, string updateFileMd5, byte fileType, string fileName)
|
||||
{
|
||||
byte[] data = CreateUpdateRequestPacket(updateFileMd5, fileType, fileName);
|
||||
Send(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建主机升级请求数据包
|
||||
/// </summary>
|
||||
/// <param name="updateFileMd5">升级文件MD5值</param>
|
||||
/// <param name="blockNum">块数量</param>
|
||||
/// <param name="fileType">文件类型</param>
|
||||
/// <param name="fileName">文件名(含酒店编码)</param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateUpdateRequestPacket(string updateFileMd5, byte fileType, string fileName)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
int headerSize = StructConverter.SizeOf(systemHeader);
|
||||
|
||||
byte[] ipArr = IPAddress.Parse(this.FTPServer).GetAddressBytes();
|
||||
byte[] portArr = BitConverter.GetBytes(this.FTPPort);//ftp通讯端口
|
||||
|
||||
//string ftpName = SysSettingRepository.GetValue("FTPName").ToString();
|
||||
byte[] bFTPName = System.Text.Encoding.Default.GetBytes(this.FTPName);
|
||||
|
||||
//string ftpPassword = SysSettingRepository.GetValue("FTPPassword").ToString();
|
||||
byte[] bFTPPassword = System.Text.Encoding.Default.GetBytes(this.FTPPassword);
|
||||
|
||||
uint[] md5Arr = Tools.MD5StringToUIntArray(updateFileMd5);
|
||||
|
||||
byte[] bFileName = System.Text.Encoding.Default.GetBytes(fileName);
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
stream.Seek(headerSize, SeekOrigin.Begin);
|
||||
stream.Write(ipArr, 0, ipArr.Length);
|
||||
stream.Write(portArr, 0, portArr.Length);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[0]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[1]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[2]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[3]), 0, 4);
|
||||
stream.Write(new byte[] { fileType }, 0, 1);//文件类型:0是bin,1是cfg
|
||||
stream.Write(BitConverter.GetBytes(this.FTPName.Length), 0, 1);//用户名长度
|
||||
stream.Write(BitConverter.GetBytes(this.FTPPassword.Length), 0, 1);//密码长度
|
||||
stream.Write(BitConverter.GetBytes(Tools.GetLength(fileName)), 0, 1);//文件名长度
|
||||
stream.Write(bFTPName, 0, bFTPName.Length);//用户名
|
||||
stream.Write(bFTPPassword, 0, bFTPPassword.Length);//密码
|
||||
stream.Write(bFileName, 0, bFileName.Length);//文件名
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);//补两个长度
|
||||
|
||||
//填充 SystemHeader
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
systemHeader.FrameLength = (ushort)stream.Length;
|
||||
systemHeader.FrameNo = 0xFFFF;
|
||||
byte[] headerData = StructConverter.StructToBytes(systemHeader);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 解码 UpdateHostPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private UpdateHostPacketReply? DecodeUpdateHostPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(UpdateHostPacketReply)) as UpdateHostPacketReply?;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
283
RCUHost/Implement/UpdateHostReceiver.cs
Normal file
283
RCUHost/Implement/UpdateHostReceiver.cs
Normal file
@@ -0,0 +1,283 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class UpdateHostReceiver : GenericReceiverBase, IUpdateHostReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpdateHostReceiver));
|
||||
private readonly int TFTP_PORT = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["TFTPPort"]);//TFTP通信端口
|
||||
private const int BLOCK_SIZE = 512;//TFTP块大小512字节
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IHostUpdateStatusRepository HostUpdateStatusRepository { get; set; }
|
||||
private IList<UpdateHostWorker> updateHostList = new List<UpdateHostWorker>();
|
||||
/// <summary>
|
||||
/// 升级
|
||||
/// </summary>
|
||||
/// <param name="hostUpdate"></param>
|
||||
/// <param name="fileType"></param>
|
||||
/// <param name="fileHref"></param>
|
||||
/// <param name="fileMd5"></param>
|
||||
/// <param name="hosts"></param>
|
||||
public void Update(HostUpdate hostUpdate, FileType fileType, string fileHref, string fileMd5, IList<Host> hosts)
|
||||
{
|
||||
if (hosts == null || hosts.Count == 0)
|
||||
{
|
||||
throw new ApplicationException("升级失败,没有找到需要升级的主机。");
|
||||
}
|
||||
if (String.IsNullOrEmpty(fileHref))
|
||||
{
|
||||
throw new ApplicationException("升级失败,无效的升级文件。");
|
||||
}
|
||||
string updateFile = Tools.GetApplicationPath() + fileHref;
|
||||
if (!File.Exists(updateFile))
|
||||
{
|
||||
throw new ApplicationException("升级失败,【" + updateFile + "】文件不存在。");
|
||||
}
|
||||
|
||||
//添加升级功能标志,告诉缓存,不能被拦截
|
||||
foreach (var item in hosts)
|
||||
{
|
||||
string Key="Upgrade_UpdateSQL_"+ item.HostNumber;
|
||||
MemoryCacheHelper.Set(Key,item.ID,DateTimeOffset.Now.AddMinutes(3));
|
||||
}
|
||||
|
||||
FileInfo fileInfo = new FileInfo(updateFile);
|
||||
ushort blockNum = (ushort)Math.Ceiling((double)fileInfo.Length / BLOCK_SIZE);
|
||||
this.updateHostList.Clear();
|
||||
foreach (var host in hosts)
|
||||
{
|
||||
SendUpdateRequest(host, fileMd5, blockNum, (byte)fileType, fileHref.Substring(fileHref.IndexOf("/")));//发送升级通知
|
||||
this.updateHostList.Add(new UpdateHostWorker(host, hostUpdate, MessageIP, fileHref, updateFile));
|
||||
if (hostUpdate == null)//从api获取升级文件方式为null,保存主机表升级状态信息
|
||||
{
|
||||
HostRepository.SetUpgradeStatus(host, 0, true);//重置升级状态
|
||||
}
|
||||
else
|
||||
{
|
||||
HostUpdateStatus hostUpdateStatus = HostUpdateStatusRepository.Get(host, hostUpdate);
|
||||
if (hostUpdateStatus == null)
|
||||
{
|
||||
hostUpdateStatus = new HostUpdateStatus();
|
||||
hostUpdateStatus.Host = host;
|
||||
hostUpdateStatus.HostUpdate = hostUpdate;
|
||||
hostUpdateStatus.PublishTime = DateTime.Now;
|
||||
}
|
||||
hostUpdateStatus.Status = 0;
|
||||
hostUpdateStatus.UpdatedTime = DateTime.Now;
|
||||
HostUpdateStatusRepository.SaveOrUpdate(hostUpdateStatus);
|
||||
}
|
||||
//logger.Error(string.Format("酒店{0}客房{1}开始升级:{2}", host.SysHotel.Code, host.RoomNumber, fileHref));
|
||||
}
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context1)
|
||||
{
|
||||
int startIndex = StructConverter.SizeOf(context1.SystemHeader);
|
||||
UpdateHostPacketReply? reply1 = DecodeUpdateHostPacketReply(context1.Data, startIndex);
|
||||
var TTT = new Tuple<ReceiverContext, UpdateHostPacketReply?>(context1,reply1);
|
||||
//logger.Error(string.Format("收到tftp升级回复命令({0}:{1}):{2},解析结果:{3}", context.RemoteEndPoint.Address.ToString(), context.RemoteEndPoint.Port, Tools.ByteToString(context.Data), reply.HasValue));
|
||||
if (reply1.HasValue)
|
||||
{
|
||||
Task.Factory.StartNew((State) =>
|
||||
{
|
||||
var NNN = State as Tuple<ReceiverContext, UpdateHostPacketReply?>;
|
||||
var context = NNN.Item1;
|
||||
var reply = NNN.Item2;
|
||||
var updateHostWorker = this.updateHostList.FirstOrDefault(r => r.Host.HostNumber == context.SystemHeader.Value.HostNumber.ToString());
|
||||
//logger.Error(string.Format("酒店{0}客房{1}升级({2}),状态{3}", updateHostWorker.Host.SysHotel.Code, updateHostWorker.Host.RoomNumber, updateHostWorker.RemoteFile, reply.Value.Status));
|
||||
SaveSystemLog(30, "升级主机", string.Format("收到主机({0})升级回复命令,状态:{1}", updateHostWorker.Host.RoomNumber, reply.Value.Status), "收到命令", "RCU", context.RemoteEndPoint.Address.ToString(), updateHostWorker.Host.SysHotel.ID);
|
||||
if (updateHostWorker.HostUpdate == null)
|
||||
{
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = updateHostWorker.Host.ID;
|
||||
|
||||
switch (reply.Value.Status)
|
||||
{
|
||||
case UpdateHostPacketReply.Ready:
|
||||
HostRepository.SetUpgradeStatus(updateHostWorker.Host, 0);//升级就绪
|
||||
//updateHostWorker.Update();
|
||||
bbb.Upgrade_status = "升级就绪";
|
||||
break;
|
||||
case UpdateHostPacketReply.Completed:
|
||||
Reply(context);//升级完成回复主机
|
||||
HostRepository.SetUpgradeStatus(updateHostWorker.Host, 1);//升级完成
|
||||
bbb.Upgrade_status = "升级完成";
|
||||
break;
|
||||
default:
|
||||
HostRepository.SetUpgradeStatus(updateHostWorker.Host, 2);//升级失败
|
||||
bbb.Upgrade_status = "升级失败";
|
||||
break;
|
||||
}
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(updateHostWorker.Host, bbb);
|
||||
}
|
||||
else
|
||||
{
|
||||
HostUpdateStatus hostUpdateStatus = HostUpdateStatusRepository.Get(updateHostWorker.Host, updateHostWorker.HostUpdate);
|
||||
if (hostUpdateStatus == null)
|
||||
{
|
||||
hostUpdateStatus = new HostUpdateStatus();
|
||||
hostUpdateStatus.Host = updateHostWorker.Host;
|
||||
hostUpdateStatus.HostUpdate = updateHostWorker.HostUpdate;
|
||||
hostUpdateStatus.PublishTime = DateTime.Now;
|
||||
hostUpdateStatus.Status = 0;
|
||||
}
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = updateHostWorker.Host.ID;
|
||||
switch (reply.Value.Status)
|
||||
{
|
||||
case UpdateHostPacketReply.Ready:
|
||||
//updateHostWorker.Update();
|
||||
hostUpdateStatus.Status = 0;//升级就绪
|
||||
bbb.Upgrade_status = "升级就绪";
|
||||
break;
|
||||
case UpdateHostPacketReply.Completed:
|
||||
bbb.Upgrade_status = "升级完成";
|
||||
Reply(context);//升级完成回复主机
|
||||
hostUpdateStatus.Status = 1;//升级完成
|
||||
updateHostList.Remove(updateHostWorker);
|
||||
break;
|
||||
default:
|
||||
bbb.Upgrade_status = "升级失败";
|
||||
hostUpdateStatus.Status = 2;//升级失败
|
||||
updateHostList.Remove(updateHostWorker);
|
||||
break;
|
||||
}
|
||||
bbb.Upgrade_DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(updateHostWorker.Host, bbb);
|
||||
hostUpdateStatus.UpdatedTime = DateTime.Now;
|
||||
HostUpdateStatusRepository.SaveOrUpdate(hostUpdateStatus);
|
||||
}
|
||||
},TTT);
|
||||
}
|
||||
}
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.TFTPUpdate; }
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// 发送升级指令到RCU主机
|
||||
/// </summary>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="updateFileMd5">升级文件MD5值</param>
|
||||
private void SendUpdateRequest(Host host, string updateFileMd5, ushort blockNum, byte fileType, string fileName)
|
||||
{
|
||||
byte[] data = CreateUpdateRequestPacket(updateFileMd5, blockNum, fileType, fileName);
|
||||
Send(data, host.HostNumber, host.MAC);// host.IP, host.Port);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建主机升级请求数据包
|
||||
/// </summary>
|
||||
/// <param name="updateFileMd5">升级文件MD5值</param>
|
||||
/// <param name="blockNum">块数量</param>
|
||||
/// <param name="fileType">文件类型</param>
|
||||
/// <param name="fileName">文件名(含酒店编码)</param>
|
||||
/// <returns></returns>
|
||||
private byte[] CreateUpdateRequestPacket(string updateFileMd5, ushort blockNum, byte fileType, string fileName)
|
||||
{
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
int headerSize = StructConverter.SizeOf(systemHeader);
|
||||
|
||||
byte[] ipArr = IPAddress.Parse(MessageIP).GetAddressBytes();
|
||||
byte[] portArr = BitConverter.GetBytes((ushort)TFTP_PORT);//tftp通讯端口
|
||||
uint[] md5Arr = Tools.MD5StringToUIntArray(updateFileMd5);
|
||||
byte[] bFileName = System.Text.Encoding.Default.GetBytes(fileName); //Tools.GetBytes(fileName, 20);
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
stream.Seek(headerSize, SeekOrigin.Begin);
|
||||
|
||||
stream.Write(ipArr, 0, ipArr.Length);
|
||||
stream.Write(portArr, 0, portArr.Length);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[0]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[1]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[2]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(md5Arr[3]), 0, 4);
|
||||
stream.Write(BitConverter.GetBytes(blockNum), 0, 2);
|
||||
stream.Write(new byte[] { fileType }, 0, 1);//文件类型,0是bin,1是cfg,2是bat
|
||||
stream.Write(bFileName, 0, bFileName.Length);//文件名
|
||||
stream.Write(new byte[] { 0, 0 }, 0, 2);//补两个长度
|
||||
|
||||
//填充 SystemHeader
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
systemHeader.FrameLength = (ushort)stream.Length;
|
||||
systemHeader.FrameNo = 0xFFFF;
|
||||
byte[] headerData = StructConverter.StructToBytes(systemHeader);
|
||||
stream.Write(headerData, 0, headerData.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 解码 UpdateHostPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private UpdateHostPacketReply? DecodeUpdateHostPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(UpdateHostPacketReply)) as UpdateHostPacketReply?;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class UpdateHostWorker
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpdateHostWorker));
|
||||
|
||||
private readonly int TFTP_PORT = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["TFTPPort"]);//TFTP通信端口
|
||||
//private Comzept.Genesis.NetworkTools.TFTPClient tftpClient;
|
||||
public Host Host { get; private set; }
|
||||
public int Progress { get; private set; }
|
||||
public HostUpdate HostUpdate;
|
||||
//private Task task;
|
||||
public string RemoteFile;
|
||||
public string LocalFile;
|
||||
|
||||
public UpdateHostWorker(Host host, HostUpdate hostUpdate, string tftpServer, string remoteFile, string localFile)
|
||||
{
|
||||
this.Host = host;
|
||||
this.HostUpdate = hostUpdate;
|
||||
this.RemoteFile = remoteFile;
|
||||
this.LocalFile = localFile;
|
||||
//this.tftpClient = new Comzept.Genesis.NetworkTools.TFTPClient(tftpServer, TFTP_PORT);
|
||||
//this.tftpClient.ReportCompletedProgress += new Comzept.Genesis.NetworkTools.TFTPClient.ReportCompletedProgressEventDelegate(tftpClient_ReportCompletedProgress);
|
||||
}
|
||||
/// <summary>
|
||||
/// Puts the specified remote file.
|
||||
/// </summary>
|
||||
public void Update()
|
||||
{
|
||||
logger.Error(string.Format("酒店({0})客房({1})主机升级:RemoteFile:{2},LocalFile:{3}", this.Host.SysHotel.Code, this.Host.RoomNumber, this.RemoteFile, this.LocalFile));
|
||||
//this.tftpClient.Put(this.RemoteFile, this.LocalFile);
|
||||
}
|
||||
|
||||
private void tftpClient_ReportCompletedProgress(string remoteIP, int completedBlock, int totalBlock)
|
||||
{
|
||||
this.Progress = completedBlock;
|
||||
logger.Error(string.Format("收到酒店({0})客房({1})主机升级总块数({2})", this.Host.SysHotel.Code, this.Host.RoomNumber, totalBlock));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
//if (task != null)
|
||||
//{
|
||||
// task.Dispose();
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
143
RCUHost/Implement/UpdateRCUFileReceiver.cs
Normal file
143
RCUHost/Implement/UpdateRCUFileReceiver.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class UpdateRCUFileReceiver : GenericReceiverBase, IUpdateRCUFileReceiver
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpdateRCUFileReceiver));
|
||||
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
public void Update(HostUpdate hostUpdate, IList<Host> hosts)
|
||||
{
|
||||
if (hosts == null || hosts.Count == 0)
|
||||
{
|
||||
throw new ApplicationException("更新失败,没有找到需要更新有效期的主机。");
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(hostUpdate.Href))
|
||||
{
|
||||
throw new ApplicationException("更新失败,无效的RCU有效期文件。");
|
||||
}
|
||||
|
||||
string updateFile = Tools.GetApplicationPath() + hostUpdate.Href;
|
||||
if (!File.Exists(updateFile))
|
||||
{
|
||||
throw new ApplicationException("更新失败,【" + updateFile + "】不是有效的RCU有效期文件。");
|
||||
}
|
||||
|
||||
string strLicense = "";
|
||||
if (!Common.MyDes.DecodeFromFile(updateFile, ref strLicense))
|
||||
{
|
||||
throw new ApplicationException("更新失败,【" + updateFile + "】无效的RCU有效期文件。");
|
||||
}
|
||||
License license = Common.MyDes.ReadLicense(strLicense);
|
||||
if (license.SystemTypeID != 3)
|
||||
{
|
||||
throw new ApplicationException("更新失败,【" + updateFile + "】RCU有效期文件系统类型有误。");
|
||||
}
|
||||
|
||||
ushort days = Convert.ToUInt16((DateTime.Parse(license.EndDate.ToString("yyyy-MM-dd " + DateTime.Now.ToLongTimeString())) - DateTime.Now).TotalDays);
|
||||
|
||||
foreach (var host in hosts)
|
||||
{
|
||||
try
|
||||
{
|
||||
//先去掉加密
|
||||
//Send(CreateDataPacket(days), host.HostNumber, host.MAC);//host.IP, host.Port);
|
||||
host.AuthorizedHours = days;
|
||||
HostRepository.Update(host);
|
||||
//this.updateHostList.Add(new UpdateHostWorker(host, hostUpdate));
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
|
||||
//FileInfo fileInfo = new FileInfo(updateFile);
|
||||
//ushort blockNum = (ushort)Math.Ceiling((double)fileInfo.Length / BLOCK_SIZE);
|
||||
//this.updateHostList.Clear();
|
||||
|
||||
//foreach (var host in hosts)
|
||||
//{
|
||||
// SendUpdateRequest(host, hostUpdate.Md5, blockNum);
|
||||
// this.updateHostList.Add(new UpdateHostWorker(host, hostUpdate));
|
||||
//}
|
||||
}
|
||||
|
||||
private byte[] CreateDataPacket(ushort days)
|
||||
{
|
||||
byte[] day = BitConverter.GetBytes(days);
|
||||
SystemHeader systemHeader = CreateSystemHeader();
|
||||
int size = StructConverter.SizeOf(systemHeader) + day.Length + 2;
|
||||
systemHeader.FrameLength = (ushort)size;
|
||||
using (MemoryStream stream = new MemoryStream(size))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(StructConverter.StructToBytes(systemHeader));
|
||||
writer.Write(day);
|
||||
writer.Write(new byte[] { 0, 0 });
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//base.Process(context);
|
||||
|
||||
//int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
|
||||
//AuthorizationReply? reply = DecodeAuthorizationPacketReply(context.Data, startIndex);
|
||||
|
||||
//if (reply.HasValue)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// var authorizationHost = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());
|
||||
|
||||
// if (authorizationHost != null)
|
||||
// {
|
||||
// Int32 authorizationTime = System.BitConverter.ToInt32(reply.Value.Time, 0);
|
||||
|
||||
// authorizationHost.AuthorizedHours = authorizationTime;
|
||||
|
||||
// HostRepository.Update(authorizationHost);
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// if (logger.IsErrorEnabled)
|
||||
// {
|
||||
// string msg = context.RemoteEndPoint.Address.ToString() + ":" + context.RemoteEndPoint.Port.ToString() + ":" + Tools.ByteToString(context.Data);
|
||||
// logger.Error("解析升级主机有效期数据出错【" + msg + "】", ex);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.HostAuthorization; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private AuthorizationReply? DecodeAuthorizationPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(AuthorizationReply)) as AuthorizationReply?;
|
||||
}
|
||||
}
|
||||
}
|
||||
137
RCUHost/Implement/UpgradeProgressBar.cs
Normal file
137
RCUHost/Implement/UpgradeProgressBar.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Dao;
|
||||
using Common;
|
||||
using RCUHost.Protocols;
|
||||
using Domain;
|
||||
using CommonEntity;
|
||||
using RestSharp;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
public class UpgradeProgressBar : GenericReceiverBase, IUpgradeProgressBar
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpgradeProgressBar));
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
|
||||
Host host = null;
|
||||
string Key = CacheKey.HostInfo_Key_HostNumber + "_" + HostNumberOnly;
|
||||
object obj = MemoryCacheHelper.Get(Key);
|
||||
if (obj != null)
|
||||
{
|
||||
host = obj as Host;
|
||||
|
||||
var Da = context.Data;
|
||||
|
||||
DecodeUpdateHostPacketReply(Da, startIndex, host);
|
||||
}
|
||||
//if (host != null)
|
||||
//{
|
||||
// string dataHex = Tools.ByteToString(context.Data);
|
||||
// int offset = StructConverter.SizeOf(context.SystemHeader);
|
||||
// int length = context.Data.Length - offset - 2;
|
||||
//}
|
||||
|
||||
//if (reply.HasValue)
|
||||
//{
|
||||
|
||||
//}
|
||||
}
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.UpdateProgressBar; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解码 UpdateHostPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private void DecodeUpdateHostPacketReply(byte[] data, int startIndex, Host host)
|
||||
{
|
||||
var DDD = data.Skip(startIndex).ToList();
|
||||
var upgrade_status = DDD.Take(1).ToList().FirstOrDefault();
|
||||
string Upgrade_status = "";
|
||||
if (upgrade_status == 0x01)
|
||||
{
|
||||
Upgrade_status = "开始下载";
|
||||
}
|
||||
else if (upgrade_status == 0x02)
|
||||
{
|
||||
Upgrade_status = "下载中";
|
||||
}
|
||||
else if (upgrade_status == 0x03)
|
||||
{
|
||||
Upgrade_status = "下载完成,校验中";
|
||||
}
|
||||
else if (upgrade_status == 0x04)
|
||||
{
|
||||
Upgrade_status = "校验完成,RCU升级中";
|
||||
}
|
||||
else if (upgrade_status == 0x05)
|
||||
{
|
||||
Upgrade_status = "超时失败";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
string UpgradeFileType = "";
|
||||
var upgradeFileType = DDD.Skip(1).Take(1).ToList().FirstOrDefault();
|
||||
if (upgradeFileType == 0x00)
|
||||
{
|
||||
UpgradeFileType = "固件升级";
|
||||
}
|
||||
else
|
||||
{
|
||||
UpgradeFileType = "配置升级";
|
||||
}
|
||||
var FileBlockTotalCount = DDD.Skip(2).Take(2);
|
||||
var FileCurrentNumber = DDD.Skip(4).Take(2);
|
||||
byte FileLen = DDD.Skip(6).Take(1).ToList().FirstOrDefault();
|
||||
var FileName = DDD.Skip(7).Take(FileLen).ToList();
|
||||
|
||||
|
||||
int hotelid = host.SysHotel.ID;
|
||||
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = host.ID;
|
||||
bbb.Upgrade_status = Upgrade_status;
|
||||
bbb.UpgradeFileType = UpgradeFileType;
|
||||
ushort a = BitConverter.ToUInt16(FileBlockTotalCount.ToArray(), 0);
|
||||
ushort b = BitConverter.ToUInt16(FileCurrentNumber.ToArray(), 0);
|
||||
bbb.FileBlockTotalCount = a;
|
||||
bbb.FileCurrentNumber = b;
|
||||
double aa = (double)a;
|
||||
double bb = (double)b;
|
||||
double aaa = (bb / aa) * 100;
|
||||
int roundedNumber = (int)Math.Round(aaa);
|
||||
bbb.BaiFenBi = roundedNumber.ToString() + "%";
|
||||
bbb.FileName = Encoding.UTF8.GetString(FileName.ToArray());
|
||||
|
||||
UploadCurrentVersionReceiver.UP_Grade_Json(host, bbb);
|
||||
}
|
||||
}
|
||||
public class BarData
|
||||
{
|
||||
public int HostID { get; set; }
|
||||
public string Upgrade_status = "";
|
||||
public string Upgrade_DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
public string UpgradeFileType = "";
|
||||
public ushort FileBlockTotalCount = 0;
|
||||
public ushort FileCurrentNumber = 0;
|
||||
public string BaiFenBi = "";
|
||||
public string FileName = "";
|
||||
public string Version = "";
|
||||
public string ConfiguraVersion = "";
|
||||
}
|
||||
}
|
||||
117
RCUHost/Implement/UploadCurrentVersionReceiver.cs
Normal file
117
RCUHost/Implement/UploadCurrentVersionReceiver.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Dao;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using RestSharp;
|
||||
using CommonEntity;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 上传RCU软件当前版本号 RCU -> 服务器
|
||||
/// </summary>
|
||||
public class UploadCurrentVersionReceiver : GenericReceiverBase
|
||||
{
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
|
||||
public IHostUpdateStatusRepository HostUpdateStatusRepository { get; set; }
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//base.Process(context);
|
||||
int startIndex = StructConverter.SizeOf(context.SystemHeader);
|
||||
UploadCurrentVersionPacketReply? packet = DecodeUploadCurrentVersionPacketReply(context.Data, startIndex);
|
||||
if (packet.HasValue)
|
||||
{
|
||||
Host host = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());//(context.RemoteEndPoint.Address.ToString());
|
||||
if (host != null)
|
||||
{
|
||||
HostUpdateStatus hostUpdateStatus = HostUpdateStatusRepository.Get(host, 0);
|
||||
if (hostUpdateStatus != null)
|
||||
{
|
||||
//更新 HostUpdateStatus
|
||||
if (host.Version != packet.Value.Version)
|
||||
{
|
||||
hostUpdateStatus.Status = 1;
|
||||
hostUpdateStatus.UpdatedTime = DateTime.Now;
|
||||
HostUpdateStatusRepository.Update(hostUpdateStatus);
|
||||
}
|
||||
}
|
||||
//更新版本号
|
||||
if (host.Version != packet.Value.Version)
|
||||
{
|
||||
host.Version = packet.Value.Version;
|
||||
HostRepository.Update(host);
|
||||
|
||||
BarData bbb = new BarData();
|
||||
bbb.HostID = host.ID;
|
||||
bbb.Version = host.Version;
|
||||
UP_Grade_Json(host, bbb);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void UP_Grade_Json(Host host, BarData bbb)
|
||||
{
|
||||
try
|
||||
{
|
||||
string id = host.ID.ToString();
|
||||
string Key = CacheKey.UPGradeProgressBar + "_" + id;
|
||||
var DDD = CSRedisCacheHelper.Get<BarData>(Key);
|
||||
if (DDD != null)
|
||||
{
|
||||
DDD.BaiFenBi = bbb.BaiFenBi;
|
||||
DDD.Upgrade_status = bbb.Upgrade_status;
|
||||
DDD.Upgrade_DateTime = bbb.Upgrade_DateTime;
|
||||
DDD.Version = bbb.Version;
|
||||
DDD.ConfiguraVersion = bbb.ConfiguraVersion;
|
||||
DDD.FileBlockTotalCount = bbb.FileBlockTotalCount;
|
||||
DDD.FileCurrentNumber = bbb.FileCurrentNumber;
|
||||
CSRedisCacheHelper.Set(Key, DDD);
|
||||
}
|
||||
else
|
||||
{
|
||||
CSRedisCacheHelper.Set(Key, bbb);
|
||||
}
|
||||
string str = Newtonsoft.Json.JsonConvert.SerializeObject(bbb);
|
||||
int hotelid = host.SysHotel.ID;
|
||||
string debug_log_report_url = RCUHost.TimingHelper.Common.debug_log_report_url;
|
||||
string debug_log_report_mqtt_topic = "blw/upgrade_progress/report/" + hotelid;
|
||||
|
||||
var client1 = new RestClient(debug_log_report_url);
|
||||
var request1 = new RestRequest("/", Method.POST);
|
||||
|
||||
//注意方法是POST
|
||||
//两个参数名字必须是 topic 和payload ,区分大小写的
|
||||
request1.AddParameter("topic", debug_log_report_mqtt_topic);
|
||||
request1.AddParameter("payload", str);
|
||||
client1.ExecuteAsync(request1, (response) => { });
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.UploadCurrentVersion; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码 UploadCurrentVersionPacketReply
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
private UploadCurrentVersionPacketReply? DecodeUploadCurrentVersionPacketReply(byte[] data, int startIndex)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, startIndex, typeof(UploadCurrentVersionPacketReply)) as UploadCurrentVersionPacketReply?;
|
||||
}
|
||||
}
|
||||
}
|
||||
83
RCUHost/Implement/WordsReportReceiver.cs
Normal file
83
RCUHost/Implement/WordsReportReceiver.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Common;
|
||||
using Domain;
|
||||
using RCUHost.Protocols;
|
||||
using Dao;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RCUHost.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// RCU文字上报处理
|
||||
/// </summary>
|
||||
public class WordsReportReceiver : GenericReceiverBase
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(WordsReportReceiver));
|
||||
public IHostRepository HostRepository { get; set; }
|
||||
public IHostWordsReportRepository HostWordsReportRepository { get; set; }
|
||||
|
||||
public override void Process(ReceiverContext context)
|
||||
{
|
||||
//AA 55 24 00 54 33 53 41 14 67 80 2E 04 01 24 13 00 00 00 02 0C 00 D1 EB CA D3 B2 C6 BE AD C6 B5 B5 C0 93 A1
|
||||
//AA552400543353411467802E04012413000000020C00D1EBCAD3B2C6BEADC6B5B5C093A1
|
||||
Reply(context);
|
||||
Host host = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());
|
||||
if (host != null)
|
||||
{
|
||||
int offset = StructConverter.SizeOf(context.SystemHeader);
|
||||
int length = context.Data.Length - offset - 2;
|
||||
try
|
||||
{
|
||||
//启动任务更新数据库
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
using (MemoryStream stream = new MemoryStream(context.Data, offset, length))
|
||||
{
|
||||
using (BinaryReader reader = new BinaryReader(stream))
|
||||
{
|
||||
string faultNo = new DeviceAddress(reader.ReadBytes(4)).ToString();//设备类型1、设备地址1、回路地址2
|
||||
int wordType = reader.ReadByte();//文字类型:01客需服务,02电视控制,03RCU异常信息
|
||||
int sentenceLength = Tools.ByteToInt(reader.ReadBytes(2));//文字长度
|
||||
string sentence;
|
||||
if (wordType == 3)
|
||||
{
|
||||
sentence = Encoding.UTF8.GetString(reader.ReadBytes(sentenceLength));//文字内容
|
||||
}
|
||||
else
|
||||
{
|
||||
sentence = Encoding.GetEncoding("gb2312").GetString(reader.ReadBytes(sentenceLength));//文字内容
|
||||
}
|
||||
HostWordsReportRepository.Save(new HostWordsReport()
|
||||
{
|
||||
HostID = host.ID,
|
||||
HotelID = host.SysHotel.ID,
|
||||
ModalAddress = faultNo,
|
||||
Type = wordType,
|
||||
Sentence = sentence,
|
||||
CreatedDate = DateTime.Now
|
||||
});
|
||||
if (wordType == 2)//电视控制的时候上报给IPTV
|
||||
{
|
||||
Common.TVOperation.ReportIPTV(host.SysHotel.TVControlToken, host.SysHotel.TVControlUrl, host.SysHotel.Code, host.RoomNumber, sentence);//推送给IPTV电视
|
||||
}
|
||||
}
|
||||
}
|
||||
}, System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(string.Format("更新客房({0}:{1})文字服务出错。", host.SysHotel.Name, host.RoomNumber), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override CommandType CommandType
|
||||
{
|
||||
get { return CommandType.WordsReport; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user