Пример #1
0
        private void ReadModule(ref List <BsonDocument> Datastreams, ref long ReqTime, Action <int, dynamic> Callback, IPlc PLC)
        {
            long      TimeStamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            LogObject logObj;
            Stopwatch watch = new Stopwatch();

            foreach (BsonDocument Stream in Datastreams)
            {
                Stream["oldTime"] = 0;
            }
            Callback(1, Datastreams);

            watch.Start();
            long Req = 0;

            while (true)
            {
                Callback(0, null);
                TimeStamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
                watch.Restart();
                foreach (BsonDocument Stream in Datastreams)
                {
                    try
                    {
                        if (BsonResolver.Resolve(Stream["Active"]) && BsonResolver.Resolve(Stream["Interval"]) <= TimeStamp - BsonResolver.Resolve(Stream["oldTime"]))
                        {
                            byte[] buffer = GetBuffer(BsonResolver.Resolve(Stream["DataType"]));
                            PLC.Read(Stream, ref buffer);

                            logObj            = new LogObject();
                            logObj.PLC        = BsonResolver.Resolve(PLC.ClientDetails["id"].ToString());
                            logObj.Datastream = BsonResolver.Resolve(Stream["Collection"]) + "_" + BsonResolver.Resolve(Stream["id"]);
                            logObj.Value      = buffer;
                            logObj._ts        = TimeStamp;
                            logObj.DataType   = BsonResolver.Resolve(Stream["DataType"]);
                            logObj.Position   = BsonResolver.Resolve(Stream["Position"]);

                            Queue.Enqueue(logObj);

                            Stream["oldTime"] = TimeStamp;
                        }
                    }
                    catch
                    {
                        PLC.Disconnect();
                        while (!PLC.Connect())
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
                Req = watch.ElapsedMilliseconds;
                if (Req > 0)
                {
                    ReqTime = Req;
                }
            }
        }
Пример #2
0
        private void GetPLCsToConnect()
        {
            PLCConnections = Database.GetCollection("Config", "Connections", config.GatewayId, "Site");
            Database.CreateCollection("Monitor", "RequestTime", 30000000);


            foreach (BsonDocument PLCDoc in PLCConnections)
            {
                IPlc PLC = PlcHandler.getClient(PLCDoc["PLCType"].ToString());
                PLC.ClientDetails = PLCDoc;
                PLC.Connect();

                Thread ClientThread = new Thread(() =>
                {
                    RunConnection(PLC, Database);
                });

                ClientThread.Name = PLC.ClientDetails["Name"].ToString();
                ClientThread.Start();
            }
        }