コード例 #1
0
        public void WriteValues(string host, string serviceProgId, string groupKey, Dictionary <string, object> itemValuePairs)
        {
            OpcDaService server = GetOpcDaService(host, serviceProgId);

            if (server == null)
            {
                return;
            }

            if (server.OpcDaGroupS.ContainsKey(groupKey) == true)
            {
                OpcDaGroup       group    = server.OpcDaGroupS[groupKey];
                var              keyList  = itemValuePairs.Keys.ToList();
                List <OpcDaItem> itemList = new List <OpcDaItem>();
                keyList.ForEach(ids =>
                {
                    var daItem = group.Items
                                 .Where(a => a.ItemId == ids)
                                 .FirstOrDefault();
                    itemList.Add(daItem);
                });

                object[]  dd  = itemValuePairs.Values.ToArray();
                HRESULT[] res = group.Write(itemList, dd);

                LogHelper.Log("Write HRESULT " + res.ToString());
            }
        }
コード例 #2
0
        public void AddGroup(OpcDaService server, string groupKey, TreeNode groupNode, int interval, int count)
        {
            if (server.Service == null)
            {
                return;
            }

            foreach (TreeNode tmpNode in groupNode.Nodes)
            {
                if (tmpNode.Checked)
                {
                    string     md5Str = groupKey + tmpNode.Text;
                    OpcDaGroup group  = null;
                    if (server.OpcDaGroupS.ContainsKey(md5Str) == false)
                    {
                        group = server.Service.AddGroup(md5Str);  // maybe cost lot of time
                        server.OpcDaGroupS.Add(md5Str, group);
                        group.IsActive = true;
                        group.UserData = groupNode;

                        List <OpcDaItemDefinition> itemDefList = new List <OpcDaItemDefinition>();

                        var def = new OpcDaItemDefinition
                        {
                            ItemId   = tmpNode.Text,
                            UserData = groupKey,
                            IsActive = true
                        };
                        itemDefList.Add(def);

                        group.AddItems(itemDefList);

                        GroupEntity groupEntity = new GroupEntity()
                        {
                            Host   = server.Host,
                            ProgId = server.ServiceId
                        };
                        groupCollection.Add(md5Str, groupEntity);
                        group.UpdateRate = TimeSpan.FromMilliseconds(interval); // 1000毫秒触发一次
                    }
                    else
                    {
                        group = server.OpcDaGroupS[md5Str];
                    }
                    groupKeys.Add(md5Str);
                    group.ValuesChanged += MonitorValuesChanged;
                    SetGroupFlag(groupKey, count);
                    // LogHelper.Log("GroupFlag " + GetGroupFlag(groupKey).ToString(), (int)LogHelper.Level.INFO);
                }
                // GetUnits(group);
                // LogHelper.Log("aaa: " + GetUnits(group), (int)LogHelper.Level.INFO);
            }
        }
コード例 #3
0
        public OpcDaService GetOpcDaService(string host, string serviceProgId)
        {
            var service = hostCollection.Where(a => a.ServiceIds.Contains(serviceProgId) && a.Host == host)
                          .FirstOrDefault();

            if (service == null)
            {
                return(null);
            }

            OpcDaService service1 = null;

            if (CheckServiceExisted(service, serviceProgId))
            {
                service1 = opcDaServices.Find(item => { return(item.Host == service.Host && item.ServiceId == serviceProgId); });
            }
            else
            {
                OpcDaServer daService = new OpcDaServer(serviceProgId, service.Host);

                service1 = new OpcDaService()
                {
                    Host = service.Host,

                    ServiceId = serviceProgId,

                    Service = daService,

                    OpcDaGroupS = new Dictionary <string, OpcDaGroup>()
                };
                opcDaServices.Add(service1);
            }

            if (service1.Service.IsConnected == false)
            {
                try
                {
                    service1.Service.ConnectionStateChanged += new EventHandler <OpcDaServerConnectionStateChangedEventArgs>(ConnectionStateChanged);

                    service1.Service.Connect();
                }
                catch (Exception e)
                {
                    LogHelper.Log("Connect " + service1.Host + ", ServiceId " + service1.ServiceId + "error!!" + e.Message);
                }
            }

            return(service1);
        }
コード例 #4
0
        public void StartMonitor(string groupKey, List <string> items, string serverID, string host = "127.0.0.1")
        {
            JsonArray properties = new JsonArray();

            OpcDaService server = GetOpcDaService(host, serverID);

            if (server == null)
            {
                return;
            }
            foreach (string item in items)
            {
                string md5Str = groupKey + item;
                if (server.OpcDaGroupS.ContainsKey(md5Str) == false)
                {
                    OpcDaGroup group = server.Service.AddGroup(md5Str);  // maybe cost lot of time
                    group.IsActive = true;
                    server.OpcDaGroupS.Add(md5Str, group);
                    List <OpcDaItemDefinition> itemDefList = new List <OpcDaItemDefinition>();
                    var def = new OpcDaItemDefinition
                    {
                        ItemId   = item,
                        UserData = groupKey,
                        IsActive = true
                    };
                    itemDefList.Add(def);

                    group.AddItems(itemDefList);
                    groupKeys.Add(md5Str);
                    SetGroupFlag(groupKey, 0);
                    LogHelper.Log("StartMonitoring  is groupId " + md5Str + " interval " + "1000  ms", (int)LogHelper.Level.INFO);
                    setItemsCount(groupKey, group);
                    group.UpdateRate     = TimeSpan.FromMilliseconds(1000); // 1000毫秒触发一次
                    group.ValuesChanged += MonitorValuesChanged;
                    if (!groupCollection.ContainsKey(md5Str))
                    {
                        GroupEntity groupEntity = new GroupEntity()
                        {
                            Host   = server.Host,
                            ProgId = server.ServiceId
                        };
                        groupCollection.Add(md5Str, groupEntity);
                    }
                    LogHelper.Log("groupKeygroupKeygroupKey: " + groupKey);
                    // LogHelper.Log("aaaa: " + GetUnits(group), (int)LogHelper.Level.INFO);
                }
            }
        }
コード例 #5
0
        public string StartMonitoringItems(string host, string serviceProgId, TreeNode groupNode, int interval, int count)
        {
            OpcDaService server   = GetOpcDaService(host, serviceProgId);
            string       groupKey = OPCDAViewHelper.Key(groupNode.Text);

            if (server == null)
            {
                LogHelper.Log("StartMonitoringItems  is null");
                return(null);
            }

            // if (server.OpcDaGroupS.Count == 0 || !server.OpcDaGroupS.ContainsKey(groupKey))
            // {
            LogHelper.Log("StartMonitoringItems  is host opcda://" + host + "/" + serviceProgId);
            AddGroup(server, groupKey, groupNode, interval, count);
            // }

            return(groupKey);
        }
コード例 #6
0
        public List <Item> ReadItemsValues(string host, string serverID, string groupKey)
        {
            OpcDaService server = GetOpcDaService(host, serverID);

            if (server == null)
            {
                return(null);
            }

            if (server.OpcDaGroupS.ContainsKey(groupKey) == true)
            {
                OpcDaGroup       group  = server.OpcDaGroupS[groupKey];
                OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);

                if (values.Length != group.Items.Count)
                {
                    LogHelper.Log($"values.Length(${values.Length}) != group.Items.Count(${group.Items.Count}) ");
                    return(null);
                }

                List <Item> itemValues = new List <Item>();
                for (int i = 0; i < values.Length; ++i)
                {
                    Item it = new Item();
                    it.ItemId = group.Items[i].ItemId;
                    it.Data   = values[i].Value;
                    if (values[i].Value != null)
                    {
                        it.Data = values[i].Value;
                        it.Type = values[i].Value.GetType().ToString();
                        itemValues.Add(it);
                    }
                }

                return(itemValues);
            }

            return(null);
        }
コード例 #7
0
        public void StopMonitoringItems(string md5Str)
        {
            if (groupCollection.ContainsKey(md5Str))
            {
                LogHelper.Log("StopMonitoringItems: " + md5Str);
                Thread.Sleep(100);
                string       host          = groupCollection[md5Str].Host;
                string       serviceProgId = groupCollection[md5Str].ProgId;
                OpcDaService server        = GetOpcDaService(host, serviceProgId);
                if (server == null)
                {
                    LogHelper.Log("StopMonitoringItems  is null");
                    return;
                }

                // OpcDaGroup group = server.OpcDaGroupS[groupKey];
                OpcDaGroup group = server.OpcDaGroupS[md5Str];
                group.ValuesChanged -= MonitorValuesChanged;
                // server.OpcDaGroupS.Remove(groupKey);
                // server.Service.RemoveGroup(group);
                // groupCollection.Remove(groupKey);
                // groupFlagCollection.Remove(groupKey);
            }
        }