Newer
Older
SensorHub / SensorHub.WasteGas / HeartBeat.cs
root on 17 Sep 2021 7 KB first commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using SensorHub.Servers;
using Newtonsoft.Json;
using System.Threading;
using System.IO;
using SensorHub.Utility;
using System.Configuration;

namespace SensorHub.WasteGas
{
    public class HeartBeat : CommandBase<WGSession, StringRequestInfo>
    {
        public override void ExecuteCommand(WGSession session, StringRequestInfo requestInfo)
        {
            try
            {
                //HeartBeat:设备ID,上传时间,程序版本号\r\n
                session.Logger.Info("HeartBeat:" + requestInfo.Body);
                //session.Send("HeartBeat:" + requestInfo.Body);//下发心跳回复

                string[] bt = requestInfo.Body.Split(',');
                if (string.IsNullOrEmpty(session.MacID))
                {
                    session.MacID = bt[0];
                }


                ////////////////////进入判断是都远程升级步骤
                if (bt.Length >= 3)//上传了程序版本号
                {
                    string softwareVersion = bt[2];//Gas_detect_V1.0
                    string type = softwareVersion.Substring(0, softwareVersion.LastIndexOf('_'));//Gas_detect
                    string version = softwareVersion.Substring(softwareVersion.LastIndexOf('_') + 2);//1.0

                    //读取文件
                    //String path = Directory.GetCurrentDirectory();
                    String path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]);
                    path += "\\Update\\WasteGas";
                    String lastestFilePath = String.Empty;

                    var files = Directory.GetFiles(path, type + "*");
                    foreach (var file in files)
                    {
                        if (String.IsNullOrEmpty(lastestFilePath))
                        {
                            lastestFilePath = file;
                            continue;
                        }

                        if (lastestFilePath.CompareTo(file) < 0)
                        {
                            lastestFilePath = file;
                        }
                    }

                    int index = lastestFilePath.LastIndexOf('_');
                    String lastestVersion = lastestFilePath.Substring(index + 2);
                    if (version.CompareTo(lastestVersion) < 0)//判断是否需要升级
                    {
                        //读取二进制文件
                        BinaryReader br = null;
                        try
                        {
                            br = new BinaryReader(new FileStream(lastestFilePath, FileMode.Open, FileAccess.Read, FileShare.Read));
                            long length = br.BaseStream.Length;
                            byte[] data = br.ReadBytes((int)length);
                            uint crc = CodeUtils.CRC16_AD(data);

                            session.Send("HeartBeat:" + bt[0] + "," + bt[1] + ",1," + length + "," + crc + "\r\n");

                        }
                        catch (IOException e)
                        {
                            session.Logger.Error(e.Message + "\n Cannot read from file.");
                            session.Send("HeartBeat:" + bt[0] + "," + bt[1] + ",0,0,0\r\n"); ;//回复不升级
                        }
                        br.Close();
                    }
                    else
                    {
                        session.Send("HeartBeat:" + bt[0] + "," + bt[1] + ",0,0,0\r\n"); ;//回复不升级
                    }
                }
                //////////////判断是否远程升级结束


                string stime = bt[1];
                string sdate = stime.Substring(0, 4) + "-" + stime.Substring(4, 2) + "-" + stime.Substring(6, 2)
                  + " " + stime.Substring(8, 2) + ":" + stime.Substring(10, 2) + ":" + stime.Substring(12, 2);
                DateTime upTime = Convert.ToDateTime(sdate);

                TimeSpan ts = DateTime.Now.Subtract(upTime);
                double miniutes = ts.TotalMinutes;
                if (miniutes > 10.0)
                {
                    //下发校时
                    String preTimeCal = "SewTiming:" + session.MacID + ",";
                    String postTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                    String timeCal = preTimeCal + postTime;

                    byte[] data1 = new byte[timeCal.Length + 2];
                    Encoding.ASCII.GetBytes(timeCal, 0, timeCal.Length, data1, 0);
                    data1[timeCal.Length] = 0x0D;
                    data1[timeCal.Length + 1] = 0x0A;

                    session.Send(data1, 0, data1.Length);
                    session.Logger.Info("有害气体监测仪下发校时:" + timeCal +
                        "上传时间:" + upTime + "系统时间:" + DateTime.Now);
                }

                RedisHelper redis = new RedisHelper(0);

                String configItem = null;
                if(redis.Conn != null)
                {
                    configItem = redis.StringGet(session.MacID);
                }
                
                if (!string.IsNullOrEmpty(configItem))
                {
                    WasteGasConfigItemsJson configItems = JsonConvert.DeserializeObject<WasteGasConfigItemsJson>(configItem);

                    String configData = null;
                    if (configItems.interval != 0)
                    {
                        Thread.Sleep(1000);
                        configData = "SewAcquireInterval:" + session.MacID + "," + configItems.interval;

                        byte[] config = new byte[configData.Length + 2];
                        Encoding.ASCII.GetBytes(configData, 0, configData.Length, config, 0);
                        config[configData.Length] = 0x0D;
                        config[configData.Length + 1] = 0x0A;
                        session.Send(config, 0, config.Length);
                        session.Logger.Info("有害气体下发配置信息:" + configData);
                    }

                    if (!string.IsNullOrEmpty(configItems.ip) && configItems.port != 0)
                    {
                        Thread.Sleep(1000);
                        configData = "SewSetServerIP:" + session.MacID + "," + configItems.ip + "," + configItems.port;

                        byte[] config = new byte[configData.Length + 2];
                        Encoding.ASCII.GetBytes(configData, 0, configData.Length, config, 0);
                        config[configData.Length] = 0x0D;
                        config[configData.Length + 1] = 0x0A;
                        session.Send(config, 0, config.Length);
                        session.Logger.Info("有害气体下发配置信息:" + configData);
                    }
                }
            }
            catch (Exception e)
            {
                session.Logger.Error("有害气体监测仪心跳异常:" + e.ToString());
            }
        }
    }
}