Newer
Older
SensorHub / SensorHub.Tube / Tube.cs
TAN YUE on 21 Jan 2022 8 KB 20210121 增加管盯Tube
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;

namespace SensorHub.Tube
{
    public class Tube : CommandBase<CasicSession, StringRequestInfo>
    {
        public override void ExecuteCommand(CasicSession session, StringRequestInfo requestInfo)
        {
            //TODO: construct the receving casic data
            String preamble = requestInfo.Parameters[0];
            String version = requestInfo.Parameters[1];
            String leng = requestInfo.Parameters[2];
            String devCode = requestInfo.Parameters[3];
            String routeFlag = requestInfo.Parameters[4];
            String dstNodeAddr = requestInfo.Parameters[5];
            String pduType = requestInfo.Parameters[6];
            String seq = requestInfo.Parameters[7];
            String settings = requestInfo.Parameters[8];
            String source = requestInfo.Parameters[9];
            String exist = requestInfo.Parameters[10];

            String devName = "Tube";

            if (source.Contains("-"))
            {
                session.Send("HTTP/1.1 200 OK\r\n\r\n\r\n");
                session.Close();
            }

            //print the receving data
            String devType = "管盯监测终端";
            String operType = Common.getOpeTypeByPdu(pduType);
            session.Logger.Info("AD接收数据:" + requestInfo.Body);
            session.Logger.Info("设备类型:" + devType);
            session.Logger.Info("操作类型:" + operType);
            session.Logger.Info("会话:" + session.HubAddr + "," + session.SessionID);

            byte[] btPdu = new byte[2]; //2个字节
            btPdu[0] = Common.getRespOperType(operType, source == "433" ? true : false);
            btPdu[1] = 0x9C;

            if (exist == "0")//数据没有缓存
            {
                //判断是返回的设置确认数据帧, 回复第三方
                if (operType == "SetResponse")
                {
                    Common.sendSetResponse(session, devCode, "Tube");
                    return;
                }

                //获取电量信息,系统时间,传递给对应的handler
                List<Tag> tags = Common.getTags(settings, session);

                //具体业务处理
                String collectDate = "";
                int cell = -1;
                int? pci = null;
                int? rsrp = null;
                int? snr = null;

                String softwareVersion = "";
                uint offset = 0;
                uint size = 0;

                List<String> eventList = new List<String>();
                List<DatasJson> datasList = new List<DatasJson>();
                List<String> startupList = new List<String>();

                foreach (Tag tag in tags)
                {
                    if (!(tag is UploadTag))
                    {
                        //非业务处理
                        if (tag != null && tag is CellTag)
                        {
                            CellTag cellTag = (CellTag)tag;
                            cell = cellTag.Cell;
                            continue;
                        }

                        if (tag != null && tag is PCITag)
                        {
                            PCITag pciTag = (PCITag)tag;
                            pci = pciTag.PCI;
                            continue;
                        }

                        if (tag != null && tag is RSRPTag)
                        {
                            RSRPTag rsrpTag = (RSRPTag)tag;
                            rsrp = rsrpTag.RSRP;
                            continue;
                        }

                        if (tag != null && tag is SNRTag)
                        {
                            SNRTag snrTag = (SNRTag)tag;
                            snr = snrTag.SNR;
                            continue;
                        }

                        if (tag != null && tag is SystemDateTag)
                        {
                            SystemDateTag systemDateTag = (SystemDateTag)tag;
                            collectDate = systemDateTag.CollectDate;
                            continue;
                        }

                        if (tag != null && tag is SensorException0Tag)
                        {
                            SensorException0Tag sensorException0 = tag as SensorException0Tag;
                            int state = sensorException0.state;

                            if (state == 0) continue;

                            eventList.Add(getTubeAlarm(state));

                            session.Logger.Info("通道一发送容错信息:oid:" + tag.Oid + ";value:" + state);
                            continue;
                        }

                        if (tag != null && tag is SensorStartupTag)
                        {
                            SensorStartupTag sensorStartup = tag as SensorStartupTag;
                            String imei = sensorStartup.IMEI;
                            String iccid = sensorStartup.ICCID;

                            startupList.Add(imei);
                            startupList.Add(iccid);

                            session.Logger.Info("设备开机上报,设备编号DEVCODE:" + devCode + " IMEI:" + imei + " ICCID:" + iccid);
                            continue;
                        }

                        //非业务处理
                        if (tag != null && tag is SoftwareVersionTag)
                        {
                            SoftwareVersionTag versionTag = (SoftwareVersionTag)tag;
                            softwareVersion = versionTag.Version;
                            continue;
                        }

                        if (tag != null && tag is OffsetTag)
                        {
                            OffsetTag offsetTag = (OffsetTag)tag;
                            offset = offsetTag.Offset;
                            continue;
                        }

                        if (tag != null && tag is SizeTag)
                        {
                            SizeTag sizeTag = (SizeTag)tag;
                            size = sizeTag.Size;
                            continue;
                        }
                    }
                    else
                    {
                        //业务处理
                        UploadTag uploadTag = tag as UploadTag;
                        switch (uploadTag.BizType)
                        {
                            case 5:
                                //气体浓度
                                TagHandler pWatcherHandler = new TubeTagHandler();
                                pWatcherHandler.resolve(tag, session);

                                DateTime baseTime = Convert.ToDateTime(collectDate + " " + pWatcherHandler.CollecTime);
                                for (int i = 0; i < pWatcherHandler.DataList.Count; i++)
                                {
                                    DateTime upTime = baseTime.AddMinutes(i * pWatcherHandler.Interval);
                                    String uptime = upTime.ToString("yyyy") + upTime.ToString("MM") + upTime.ToString("dd")
                                        + upTime.ToString("HH") + upTime.ToString("mm") + upTime.ToString("ss");

                                    datasList.Add(new TubeDatasJson(uptime, (float)pWatcherHandler.DataList[i]));
                                }

                                break;
                            default:
                                session.Logger.Info("未知业务类型!");
                                break;
                        }
                    }
                }

                //Common.sendMessage(session, devName, devCode, cell, pci, rsrp, snr, eventList, datasList, startupList);
                Common.kafkaProduce(session, devName, devCode, cell, pci, rsrp, snr, eventList, datasList, startupList);

                if (softwareVersion != "" || size != 0 || offset != 0)//进入远程升级流程
                {
                    Common.remoteUpgrade(session, operType, devName, devCode, btPdu, softwareVersion, size, offset, source);
                    return;
                }
            }
            
            Common.sendConfig(session, devCode, routeFlag, source, btPdu);
        }

        private String getTubeAlarm(int state)
        {
            switch (state)
            {
                case 0:
                    return "TubeNormal";// 管盯正常
                case 1:
                    return "TubeCommunicationFailure"; // 传感器通信失败 采集失败
                case 2:
                    return "TubeError"; // 管盯传感器异常
                default:
                    return "TubeUnknown"; // 管盯未知异常

            }
        }
    }
}