Newer
Older
SensorHub / SensorHub.WasteGas / SewProUpdateFile.cs
root on 17 Sep 2021 2 KB first commit
using System;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using Newtonsoft.Json;
using SensorHub.Servers.JsonFormat;
using SensorHub.Servers;
using System.IO;
using System.Configuration;

namespace SensorHub.WasteGas
{
    public class SewProUpdateFile : CommandBase<WGSession, StringRequestInfo>
    {
        public override void ExecuteCommand(WGSession session, StringRequestInfo requestInfo)
        {
            try
            {
                session.Logger.Info("SewProUpdateFile:" + requestInfo.Body);

                String devCode = requestInfo.Parameters[0];
                if (requestInfo.Parameters.Length == 2)
                {
                    string result = requestInfo.Parameters[1];
                    if(result == "success")
                    {
                        return;
                    }
                }

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

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

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

                //读取二进制文件
                BinaryReader br = null;
                try
                {
                    br = new BinaryReader(new FileStream(lastestFilePath, FileMode.Open, FileAccess.Read, FileShare.Read));

                    int size = (int)br.BaseStream.Length;
                    byte[] data = new byte[size];
                    br.Read(data, 0, size);

                    session.Send(data,0,data.Length);
                }
                catch (IOException e)
                {
                    session.Logger.Error(e.Message + "\n Cannot read from file.");
                }
                br.Close();
            }
            catch (Exception e)
            {
                session.Logger.Error("SewProUpdateFile返回异常:" + e.ToString());
            }
        }

    }
}