Newer
Older
SensorHub-NoiseCorr / SensorHub.FireHydrant / FireHydrantConfig.cs
using Newtonsoft.Json;
using SensorHub.Servers;
using SensorHub.Servers.Commands.CASICCommands;
using SensorHub.Servers.JsonFormat;
using SensorHub.Utility;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SensorHub.FireHydrant
{
    public class FireHydrantConfig : CommandBase<ThirdpartySession, StringRequestInfo>
    {
        public override void ExecuteCommand(ThirdpartySession session, StringRequestInfo requestInfo)
        {
            String mType = requestInfo.Parameters[0];
            String devCode = requestInfo.Parameters[1];
            String mBody = requestInfo.Parameters[2];
            String ts = requestInfo.Parameters[3];

            session.Logger.Info("接收下发配置数据如下:");
            session.Logger.Info("消息类型:" + mType);
            session.Logger.Info("设备编号:" + devCode);
            session.Logger.Info("配置参数:" + mBody);
            session.Logger.Info("时间戳:" + ts);

            FireHydrantConfigJson configJson = JsonConvert.DeserializeObject<FireHydrantConfigJson>(mBody);

            if (configJson.bType.Contains("Query"))
            {
                byte[] btPdu = new byte[2]; //2个字节
                btPdu[0] = 0x01;
                btPdu[1] = 0x8F;

                Common.dispatchQueryMessage(session, "FireHydrant", devCode, configJson.concentratorCode, btPdu);
            }
            else if (configJson.bType.Contains("Config"))
            {
                FireHydrantConfigItemsJson configItems = configJson.configItems;
                byte[] configTag = getConfigTag(configItems);

                byte[] btPdu = new byte[2]; //2个字节
                btPdu[0] = 0x03;
                btPdu[1] = 0x8F;

                Common.dispatchConfigMessage(session, "FireHydrant", devCode, configJson.netType, configJson.concentratorCode, btPdu, configTag);
            }
            else
            {
                session.Logger.Info("业务类型bType无法识别:" + configJson.bType);
            }
        }


        //获取配置tag
        private byte[] getConfigTag(FireHydrantConfigItemsJson configItems)
        {
            byte[] tag = new byte[0];

            if (configItems.ip != null && configItems.port != 0)
            {
                //修改ip 0x10000022
                byte[] ipOid = { 0x10, 0x00, 0x00, 0x22 };
                byte[] ipValue = System.Text.Encoding.Default.GetBytes(configItems.ip);
                byte[] ipLen = BitConverter.GetBytes((short)ipValue.Length);
                Array.Reverse(ipLen);

                //修改port 0x10000023
                byte[] portOid = { 0x10, 0x00, 0x00, 0x23 };
                byte[] portValue = System.Text.Encoding.Default.GetBytes(configItems.port.ToString());
                byte[] portLen = BitConverter.GetBytes((short)portValue.Length);
                Array.Reverse(portLen);

                tag = new byte[4 + 2 + ipValue.Length + 4 + 2 + portValue.Length];

                ipOid.CopyTo(tag, 0);
                ipLen.CopyTo(tag, 4);
                ipValue.CopyTo(tag, 6);

                portOid.CopyTo(tag, 6 + ipValue.Length);
                portLen.CopyTo(tag, 10 + ipValue.Length);
                portValue.CopyTo(tag, 12 + ipValue.Length);
            }

            byte[] result = new byte[tag.Length];
            tag.CopyTo(result,0);

            return result;
        }
    }
}