Newer
Older
SensorHub / SensorHub.Lamp / LampConfig.cs
root on 17 Sep 2021 6 KB first commit
using Newtonsoft.Json;
using SensorHub.Servers;
using SensorHub.Servers.Commands.CASICCommands;
using SensorHub.Servers.JsonFormat;
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.Lamp
{
    public class LampConfig : 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);

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

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

            if (configJson.bType.Contains("Query"))
            {
                btPdu[0] = 0x01;

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

                btPdu[0] = 0x03;

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

        //获取配置tag
        private byte[] getConfigTag(LampConfigItemsJson configItems)
        {
            byte[] result = new byte[128];

            int flag = 0;

            if (configItems.acqStart != null)
            {
                //采集开始时间
                byte[] tag = { 0x10, 0x00, 0x01, 0x04, 
                                 0x00, 0x02, 
                                 0x00, 0x00 };

                getTimeTag(configItems.acqStart, tag).CopyTo(result, flag);
                flag += 8;
            }

            if (configItems.interval != 0)
            {
                //采集间隔
                byte[] tag = { 0x10, 0x00, 0x01, 0x05, 
                                 0x00, 0x02, 
                                 0x00, 0x00 };

                byte[] value = BitConverter.GetBytes(configItems.interval);
                value.CopyTo(tag, 6);

                tag.CopyTo(result, flag);
                flag += 8;
            }

            if (configItems.times != 0)
            {
                //采集次数
                byte[] tag = { 0x10, 0x00, 0x01, 0x06, 
                                  0x00, 0x02, 
                                  0x00, 0x00 };

                byte[] value = BitConverter.GetBytes(configItems.times);
                value.CopyTo(tag, 6);

                tag.CopyTo(result, flag);
                flag += 8;
            }

            if (configItems.repeat != 0)
            {
                //重试次数
                byte[] tag = { 0x10, 0x00, 0x00, 0x0A, 
                                  0x00, 0x01, 
                                  0x00 };

                byte value = Convert.ToByte(configItems.repeat);
                tag[6] = value;

                tag.CopyTo(result, flag);
                flag += 7;
            }

            if (configItems.thresh != 0.0f)
            {
                //电流阈值
                byte[] tag = { 0x10, 0x00, 0x09, 0x00, 
                                 0x00, 0x04, 
                                  0x00,0x00,0x00,0x00 };

                byte[] value = BitConverter.GetBytes(configItems.thresh);
                value.CopyTo(tag, 6);

                tag.CopyTo(result, flag);
                flag += 10;
            }

            if (configItems.Channel1TurnOnTime != null)
            {
                //路灯1开启时间
                byte[] tag = { 0x10, 0x00, 0x02, 0x01, 
                                 0x00, 0x02, 
                                 0x00, 0x00 };

                getTimeTag(configItems.acqStart, tag).CopyTo(result, flag); 
                flag += 8;
            }

            if (configItems.Channel1TurnOffTime != null)
            {
                //路灯1关闭时间
                byte[] tag = { 0x10, 0x00, 0x02, 0x02, 
                                 0x00, 0x02,
                                 0x00, 0x00 };

                getTimeTag(configItems.acqStart, tag).CopyTo(result, flag);
                flag += 8;
            }

            if (configItems.Channel2TurnOnTime!= null)
            {
                //路灯2开启时间
                byte[] tag = { 0x10, 0x00, 0x02, 0x03, 
                                 0x00, 0x02, 
                                 0x00, 0x00 };

                getTimeTag(configItems.acqStart, tag).CopyTo(result, flag);
                flag += 8;
            }

            if (configItems.Channel2TurnOffTime != null)
            {
                //路灯2关闭时间
                byte[] tag = { 0x10, 0x00, 0x02, 0x04, 
                                 0x00, 0x02,
                                 0x00, 0x00 };

                getTimeTag(configItems.acqStart, tag).CopyTo(result, flag);
                flag += 8;
            }

            if (configItems.RemoteControl != null)
            {
                //远程控制
                byte[] tag = { 0x50, 0x00, 0x00, 0x01, 
                                  0x00, 0x01, 
                                  0x00 };


                byte value = (Convert.ToByte(configItems.RemoteControl, 16));
                tag[6] = value;

                tag.CopyTo(result, flag);
                flag += 7;
            }

            if (configItems.SetBrightness != 0)
            {
                //调光
                byte[] tag = { 0x10, 0x00, 0x02, 0x05, 
                                  0x00, 0x01, 
                                  0x00 };

                byte value = Convert.ToByte(configItems.SetBrightness);
                tag[6] = value;

                tag.CopyTo(result, flag);
                flag += 7;
            }

            return result.Take(flag).ToArray();
        }

        private byte[] getTimeTag(String time, byte[] tag)
        {
            Int16 stime = (Int16)(Convert.ToInt16(time.Substring(0, 2)) * 60
                + Convert.ToInt16(time.Substring(3, 2)));
            byte[] value = BitConverter.GetBytes(stime);
            value.CopyTo(tag, 6);

            return tag;
        } 
    }
}