예제 #1
0
        // This process updates acquired values in the mongodb collection for realtime data
        static async void ProcessMongo(JSONSCADAConfig jsConfig)
        {
            do
            {
                try
                {
                    var Client     = ConnectMongoClient(jsConfig);
                    var DB         = Client.GetDatabase(jsConfig.mongoDatabaseName);
                    var collection =
                        DB.GetCollection <rtData>(RealtimeDataCollectionName);
                    var collection_cmd =
                        DB
                        .GetCollection
                        <rtCommand>(CommandsQueueCollectionName);

                    var listWrites = new List <WriteModel <rtData> >();
                    do
                    {
                        bool isMongoLive =
                            DB
                            .RunCommandAsync((Command <BsonDocument>)
                                             "{ping:1}")
                            .Wait(1000);
                        if (!isMongoLive)
                        {
                            throw new Exception("Error on MongoDB connection ");
                        }

                        PLC_Value iv;
                        while (PLCDataQueue.TryDequeue(out iv))
                        {
                            //below code will update one record of the data
                            var update =
                                new BsonDocument {
                                {
                                    "$set",
                                    new BsonDocument {
                                        {
                                            "sourceDataUpdate",
                                            new BsonDocument {
                                                {
                                                    "valueAtSource",
                                                    BsonValue
                                                    .Create(iv.value)
                                                },
                                                {
                                                    "valueStringAtSource",
                                                    BsonValue
                                                    .Create(iv.value.ToString())
                                                },
                                                {
                                                    "asduAtSource",
                                                    BsonValue
                                                    .Create(iv.asdu.ToString())
                                                },
                                                {
                                                    "causeOfTransmissionAtSource",
                                                    BsonValue.Create(iv.cot.ToString())
                                                },
                                                {
                                                    "timeTag",
                                                    BsonValue
                                                    .Create(iv.time_tag)
                                                },
                                                {
                                                    "notTopicalAtSource",
                                                    BsonValue
                                                    .Create(false)
                                                },
                                                {
                                                    "invalidAtSource",
                                                    BsonValue
                                                    .Create(false)
                                                },
                                                {
                                                    "overflowAtSource",
                                                    BsonValue
                                                    .Create(false)
                                                },
                                                {
                                                    "blockedAtSource",
                                                    BsonValue
                                                    .Create(false)
                                                },
                                                {
                                                    "substitutedAtSource",
                                                    BsonValue
                                                    .Create(false)
                                                }
                                            }
                                        }
                                    }
                                }
                            };

                            var filt =
                                new rtFilt
                            {
                                protocolSourceConnectionNumber =
                                    iv.conn_number,
                                protocolSourceCommonAddress =
                                    iv.common_address,
                                protocolSourceObjectAddress = iv.address
                            };
                            Log("MongoDB - ADD " + iv.address + " " + iv.value,
                                LogLevelDetailed);

                            listWrites
                            .Add(new UpdateOneModel <rtData>(filt
                                                             .ToBsonDocument(),
                                                             update));
                        }

                        if (listWrites.Count > 0)
                        {
                            Log("MongoDB - Bulk write " + listWrites.Count);
                            var bulkWriteResult =
                                await collection.BulkWriteAsync(listWrites);

                            listWrites.Clear();
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }while (true);
                }
                catch (Exception e)
                {
                    Log("Exception Mongo");
                    Log(e);
                    Log(e
                        .ToString()
                        .Substring(0,
                                   e.ToString().IndexOf(Environment.NewLine)));
                    System.Threading.Thread.Sleep(3000);

                    while (PLCDataQueue.Count > DataBufferLimit // do not let data queue grow more than a limit
                           )
                    {
                        Log("Dequeue Data", LogLevelDetailed);
                        PLC_Value iv;
                        PLCDataQueue.TryDequeue(out iv);
                    }
                }
            }while (true);
        }
예제 #2
0
        static void ProcessPLCScan(PLC_connection srv)
        {
            for (; ;)
            {
                try
                {
                    if (!Active)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }

                    var asyncStopWatch = Stopwatch.StartNew();

                    foreach (var tag in srv.listBoolTags)
                    {
                        try
                        {
                            var type = "bool";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value?1:0,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listSintTags)
                    {
                        try
                        {
                            var type = "sint";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listIntTags)
                    {
                        try
                        {
                            var type = "int";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listDintTags)
                    {
                        try
                        {
                            var type = "dint";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listLintTags)
                    {
                        try
                        {
                            var type = "lint";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listRealTags)
                    {
                        try
                        {
                            var type = "real";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listLrealTags)
                    {
                        try
                        {
                            var type = "lreal";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                PLC_Value iv =
                                    new PLC_Value()
                                {
                                    conn_number    = 81,
                                    address        = tag.Name,
                                    common_address = tag.Path,
                                    asdu           = type,
                                    value          = tag.Value,
                                    time_tag       = DateTime.Now,
                                    cot            = 20
                                };
                                PLCDataQueue.Enqueue(iv);
                                Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                            }
                        }
                        catch (LibPlcTagException e)
                        {
                            Log(srv.name + "- " + "Error scanning tag: " + tag.Name);
                            Log(srv.name + "- " + e);
                            Thread.Sleep(10000);
                        }
                    }

                    foreach (var tag in srv.listBoolArrayTags)
                    {
                        try
                        {
                            var type = "bool";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value ? 1 : 0,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    foreach (var tag in srv.listSintArrayTags)
                    {
                        try
                        {
                            var type = "sint";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    foreach (var tag in srv.listIntArrayTags)
                    {
                        try
                        {
                            var type = "int";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    foreach (var tag in srv.listDintArrayTags)
                    {
                        try
                        {
                            var type = "dint";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    foreach (var tag in srv.listLintArrayTags)
                    {
                        try
                        {
                            var type = "lint";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    foreach (var tag in srv.listRealArrayTags)
                    {
                        try
                        {
                            var type = "real";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    foreach (var tag in srv.listLrealArrayTags)
                    {
                        try
                        {
                            var type = "lreal";
                            tag.Read();
                            Log(tag.GetStatus().ToString());
                            if (tag.GetStatus() != Status.Ok)
                            {
                                Log(srv.name + " - ERROR!");
                            }
                            else
                            {
                                var cnt = 0;
                                foreach (var value in tag.Value)
                                {
                                    PLC_Value iv =
                                        new PLC_Value()
                                    {
                                        conn_number    = 81,
                                        address        = tag.Name + "[" + cnt + "]",
                                        common_address = tag.Path,
                                        asdu           = type,
                                        value          = value,
                                        time_tag       = DateTime.Now,
                                        cot            = 20
                                    };
                                    PLCDataQueue.Enqueue(iv);
                                    Log(srv.name + " - " + iv.address + " " + iv.asdu + " " + iv.value);
                                    cnt++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log("Error scanning tag: " + tag.Name);
                            Log(e);
                        }
                    }

                    asyncStopWatch.Stop();
                    Log($"{srv.name} - Connection scan took {(float)asyncStopWatch.ElapsedMilliseconds} ms.");

                    Log($"{srv.name} - Sleep {(float)srv.giInterval} ms...");
                    Thread.Sleep(srv.giInterval);
                }
                catch (Exception e)
                {
                    Log(srv.name + " - Error scanning!");
                    Log(e);
                }
            }
        }