Esempio n. 1
0
        private void StationEvent(ZeroNetEventArgument e)
        {
            switch (e.Event)
            {
            case ZeroNetEventType.CenterStationJoin:
            case ZeroNetEventType.CenterStationLeft:
            case ZeroNetEventType.CenterStationPause:
            case ZeroNetEventType.CenterStationResume:
            case ZeroNetEventType.CenterStationClosing:
            case ZeroNetEventType.CenterStationInstall:
            case ZeroNetEventType.CenterStationUpdate:
            case ZeroNetEventType.CenterStationRemove:
                PublishConfig(e.EventConfig);
                return;

            case ZeroNetEventType.AppStop:
            case ZeroNetEventType.AppRun:
                NewBaseTime();
                ZeroApplication.Config.Foreach(cfg =>
                {
                    PublishConfig(cfg);
                    if (!KLines.ContainsKey(cfg.StationName))
                    {
                        KLines.Add(cfg.StationName, new List <KLine>());
                    }
                    var nLine = new KLine
                    {
                        Time = GetTime(BaseLine)
                    };
                    if (minuteLine.ContainsKey(cfg.StationName))
                    {
                        minuteLine[cfg.StationName] = nLine;
                    }
                    else
                    {
                        minuteLine.Add(cfg.StationName, nLine);
                    }
                });
                return;

            case ZeroNetEventType.AppEnd:
                File.WriteAllText(Path.Combine(ZeroApplication.Config.DataFolder, "kline_station.json"),
                                  JsonConvert.SerializeObject(KLines));
                return;

            case ZeroNetEventType.CenterStationState:
                if (e.EventConfig == null || e.Context == null)
                {
                    return;
                }
                Collect(e.EventConfig, e.Context);
                return;
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     执行命令
        /// </summary>
        /// <returns></returns>
        private void Collect(StationConfig station, string json)
        {
            using (OnceScope.CreateScope(minuteLine))
            {
                var now = JsonConvert.DeserializeObject <StationCountItem>(json);
                if (!StationCountItems.TryGetValue(station.StationName, out var item))
                {
                    now.Count = 1;
                    StationCountItems.Add(station.StationName, now);
                    return;
                }

                item.CheckValue(station, now);

                WebSocketNotify.Publish("status", station.StationName, JsonConvert.SerializeObject(item));
                long value = station.StationType == ZeroStationType.Api ||
                             station.StationType == ZeroStationType.Vote
                    ? item.LastTps
                    : item.LastQps;
                if (minuteLine.TryGetValue(station.StationName, out var kLine))
                {
                    if (kLine.Count == 0)
                    {
                        kLine.Total = value;
                        kLine.Open  = value;
                        kLine.Close = value;
                        kLine.Max   = value;
                        kLine.Min   = value;
                    }
                    else
                    {
                        kLine.Total += value;
                        kLine.Close  = value;
                        if (kLine.Max < value)
                        {
                            kLine.Max = value;
                        }
                        if (kLine.Min > value)
                        {
                            kLine.Min = value;
                        }
                    }

                    kLine.Count++;
                }
                else
                {
                    minuteLine.Add(station.StationName, kLine = new KLine
                    {
                        Time  = GetTime(BaseLine),
                        Count = 1,
                        Total = value,
                        Open  = value,
                        Close = value,
                        Max   = value,
                        Min   = value,
                        Avg   = value
                    });
                    KLines.Add(station.StationName, new List <KLine> {
                        kLine
                    });
                }
            }

            if ((DateTime.Now - BaseLine).TotalMinutes < 1)
            {
                return;
            }
            NewBaseTime();
            foreach (var key in KLines.Keys.ToArray())
            {
                if (!minuteLine.TryGetValue(key, out var line))
                {
                    minuteLine.Add(key, line = new KLine
                    {
                        Time  = GetTime(BaseLine),
                        Count = 1
                    });
                }
                if (line.Count == 0)
                {
                    line.Avg = 0;
                }
                else
                {
                    line.Avg = decimal.Round(line.Total / line.Count, 4);
                }
                while (KLines[key].Count > 240)
                {
                    KLines[key].RemoveAt(0);
                }
                KLines[key].Add(line);
                WebSocketNotify.Publish("kline", key, JsonConvert.SerializeObject(line));
                var nLine = new KLine
                {
                    Time = GetTime(BaseLine)
                };
                minuteLine[key] = nLine;
            }
        }