Example #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;
                }
            }
        }
Example #2
0
        public List <BsonDocument> UpdateDatastreams(IPlc PLC, IDatabase Database)
        {
            List <BsonDocument> Temp = Database.GetCollection("Config", "DataStreams", PLC.ClientDetails["id"].ToString(), "ConnectionId");

            Temp = Temp.Where(Doc => BsonResolver.Resolve(Doc["Active"])).Distinct().ToList();

            List <string> Sets = Database.GetCollections("Data");

            foreach (BsonDocument Stream in Temp)
            {
                if (!Sets.Contains(BsonResolver.Resolve(Stream["Collection"])))
                {
                    Database.CreateCollection(BsonResolver.Resolve(Stream["Collection"]) + "_" + BsonResolver.Resolve(Stream["id"]), "Data");
                }
            }

            return(Temp);
        }