private void AddRow(DeviceConfiguration config, int index)
 {
     var row = new Controls.Row();
     row.Configuration = config;
     row.Clicked += Row_Clicked;
     Rows.Insert(index, row);
 }
 private void AddRow(DeviceConfiguration config)
 {
     var row = new Row();
     row.Configuration = config;
     row.Clicked += Row_Clicked;
     Rows.Add(row);
 }
        public static List<GeneratedEvent> Process(DeviceConfiguration config, List<Instance> instances)
        {
            var result = new List<GeneratedEvent>();

            // Get Configuration object from Configuration.CustomClasses List (if exists)
            var gec = Configuration.Get(config);
            if (gec != null)
            {
                if (instances != null)
                {
                    var _instances = instances.FindAll(o => o != null);

                    // Loop through each InstanceData object in instanceDatas
                    foreach (var instance in _instances)
                    {
                        // Loop through all of the Events and process Event using instanceData object
                        foreach (var e in gec.Events)
                        {
                            var genEvent = ProcessEvent(e, instance);
                            result.Add(genEvent);
                        }
                    }
                }
            }

            return result;
        }
        // Example POST Data
        // -----------------------------------------------------
        // name = 'token'
        // value = Session Token
        // name = 'sender_id'
        // value = Sender ID
        // name = 'devices'
        // value =  [{
        //    
        //     "unique_id": "987654321",
        //     "data": [
        //        { "address": "/ClientEnabled", "value": "true", "" },
        //        { "address": "/ServerEnabled", "value": "true", "" },
        //        { "address": "/UniqueId", "value": "987654321", "" }
        //        ]
        //    },
        //    {
        //     "unique_id": "123456789",
        //     "data": [
        //        { "address": "/ClientEnabled", "value": "true", "" },
        //        { "address": "/ServerEnabled", "value": "true", "" },
        //        { "address": "/UniqueId", "value": "123456789", "" }
        //        ]
        // }]
        // -----------------------------------------------------
        public static bool Update(UserConfiguration userConfig, DeviceConfiguration deviceConfig)
        {
            bool result = false;

            if (userConfig != null)
            {
                var table = deviceConfig.ToTable();
                if (table != null)
                {
                    var infos = new List<DeviceInfo>();
                    infos.Add(new DeviceInfo(deviceConfig.UniqueId, table));

                    string json = JSON.FromObject(infos);
                    if (json != null)
                    {
                        Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                        string url = new Uri(apiHost, "devices/update/index.php").ToString();

                        var postDatas = new NameValueCollection();
                        postDatas["token"] = userConfig.SessionToken;
                        postDatas["sender_id"] = UserManagement.SenderId.Get();
                        postDatas["devices"] = json;

                        string response = HTTP.POST(url, postDatas);
                        if (response != null)
                        {
                            result = ApiError.ProcessResponse(response, "Update Devices");
                        }
                    }
                }
            }

            return result;
        }
 private void AddDevice(DeviceConfiguration config)
 {
     if (config != null)
     {
         var deviceThread = new Thread(new ParameterizedThreadStart(StartDeviceServer));
         deviceThread.Start(config);
     }
 }
 public DeviceDescription(DeviceConfiguration deviceConfig)
 {
     UniqueId = deviceConfig.UniqueId;
     Enabled = deviceConfig.Enabled;
     Index = deviceConfig.Index;
     Description = deviceConfig.Description;
     Agent = deviceConfig.Agent;
 }
        public static Configuration Get(DeviceConfiguration configuration)
        {
            Configuration result = null;

            var customClass = configuration.CustomClasses.Find(x => x.GetType() == typeof(Configuration));
            if (customClass != null) result = (Configuration)customClass;

            return result;
        }
        public void Initialize(DeviceConfiguration config)
        {
            var sdc = Configuration.Read(config.Xml);
            if (sdc != null)
            {
                config.CustomClasses.Add(sdc);

                configuration = config;
            }
        }
        public void Initialize(DeviceConfiguration config)
        {
            var pc = Configuration.Read(config.Xml);
            if (pc != null)
            {
                config.CustomClasses.Add(pc);

                configuration = config;
                lastSequence = LoadStoredSequence();
            }
        }
        public static AgentData Load(DeviceConfiguration config)
        {
            string json = Properties.Settings.Default.StoredAgentData;
            if (!string.IsNullOrEmpty(json))
            {
                var agentDatas = JSON.ToType<List<AgentData>>(json);
                if (agentDatas != null)
                {
                    var agentData = agentDatas.Find(o => o.UniqueId == config.UniqueId);
                    if (agentData != null) return agentData;
                }
            }

            return null;
        }
Exemple #11
0
        static bool NotEqualTo(DeviceConfiguration c1, DeviceConfiguration c2)
        {
            if (!object.ReferenceEquals(c1, null) && object.ReferenceEquals(c2, null))
            {
                return(true);
            }
            if (object.ReferenceEquals(c1, null) && !object.ReferenceEquals(c2, null))
            {
                return(true);
            }
            if (object.ReferenceEquals(c1, null) && object.ReferenceEquals(c2, null))
            {
                return(false);
            }

            return(c1.UniqueId != c2.UniqueId || c1.Index != c2.Index);
        }
        public static void Save(DeviceConfiguration config, long instanceId, long sequence)
        {
            string json = Properties.Settings.Default.StoredAgentData;
            if (!string.IsNullOrEmpty(json))
            {
                var agentDatas = JSON.ToType<List<AgentData>>(json);
                if (agentDatas != null)
                {
                    int i = agentDatas.FindIndex(o => o.UniqueId == config.UniqueId);
                    if (i < 0)
                    {
                        var agentData = new AgentData();
                        agentData.UniqueId = config.UniqueId;
                        agentDatas.Add(agentData);
                        i = agentDatas.FindIndex(o => o.UniqueId == config.UniqueId);
                    }

                    agentDatas[i].InstanceId = instanceId;
                    if (sequence >= 0) agentDatas[i].LastSequence = sequence;

                    Properties.Settings.Default.StoredAgentData = JSON.FromList<AgentData>(agentDatas);
                    Properties.Settings.Default.Save();
                }
                else System.Console.WriteLine(config.UniqueId + " :: Error Saving AgentData");
            }
            else
            {
                var agentDatas = new List<AgentData>();

                var agentData = new AgentData();
                agentData.UniqueId = config.UniqueId;
                agentData.InstanceId = instanceId;
                if (sequence >= 0) agentData.LastSequence = sequence;
                agentDatas.Add(agentData);

                Properties.Settings.Default.StoredAgentData = JSON.FromList<AgentData>(agentDatas);
                Properties.Settings.Default.Save();
            }
        }
Exemple #13
0
        public static DeviceConfiguration Read(XmlDocument xml)
        {
            var result = new DeviceConfiguration();

            result.Xml = xml;

            foreach (XmlNode node in xml.DocumentElement.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    if (node.InnerText != "")
                    {
                        switch (node.Name.ToLower())
                        {
                        case "description": result.Description = ProcessDescription(node); break;

                        case "agent": result.Agent = ProcessAgent(node); break;

                        default:

                            Type         Settings = typeof(DeviceConfiguration);
                            PropertyInfo info     = Settings.GetProperty(node.Name);

                            if (info != null)
                            {
                                Type t = info.PropertyType;
                                info.SetValue(result, Convert.ChangeType(node.InnerText, t), null);
                            }

                            break;
                        }
                    }
                }
            }

            return(result);
        }
        private static void SaveStoredSequence(DeviceConfiguration config, long sequence)
        {
            string json = Properties.Settings.Default.PartsStoredSequences;
            if (!string.IsNullOrEmpty(json))
            {
                var sequenceInfos = JSON.ToType<List<SequenceInfo>>(json);
                if (sequenceInfos != null)
                {
                    int i = sequenceInfos.FindIndex(o => o.UniqueId == config.UniqueId);
                    if (i < 0)
                    {
                        var sequenceInfo = new SequenceInfo();
                        sequenceInfo.UniqueId = config.UniqueId;
                        sequenceInfos.Add(sequenceInfo);
                        i = sequenceInfos.FindIndex(o => o.UniqueId == config.UniqueId);
                    }

                    sequenceInfos[i].Sequence = sequence;

                    Properties.Settings.Default.PartsStoredSequences = JSON.FromList<SequenceInfo>(sequenceInfos);
                    Properties.Settings.Default.Save();
                }
            }
            else
            {
                var sequenceInfos = new List<SequenceInfo>();

                var sequenceInfo = new SequenceInfo();
                sequenceInfo.UniqueId = config.UniqueId;
                sequenceInfo.Sequence = sequence;
                sequenceInfos.Add(sequenceInfo);

                Properties.Settings.Default.PartsStoredSequences = JSON.FromList<SequenceInfo>(sequenceInfos);
                Properties.Settings.Default.Save();
            }
        }
        private void Start(DeviceConfiguration config)
        {
            if (!started)
            {
                stop = new ManualResetEvent(false);
                started = true;
                probeData = null;

                var requestsThread = new Thread(new ParameterizedThreadStart(Worker));
                requestsThread.Start(config);
            }
        }
        private void SendSampleData(MTConnect.Application.Streams.ReturnData returnData, DeviceConfiguration config)
        {
            var data = new EventData(this);
            data.Id = "MTCONNECT_SAMPLE";
            data.Data01 = config;
            data.Data02 = returnData;

            SendData?.Invoke(data);
        }
        private void SendProbeData(MTConnect.Application.Components.ReturnData returnData, DeviceConfiguration config)
        {
            if (returnData != null && returnData.Header != null)
            {
                if (returnData.Header.InstanceId != agentInstanceId)
                {
                    SendAgentReset(config);
                }

                agentInstanceId = returnData.Header.InstanceId;
            }

            var data = new EventData(this);
            data.Id = "MTCONNECT_PROBE";
            data.Data01 = config;
            data.Data02 = returnData;

            SendData?.Invoke(data);
        }
 private void SendAvailability(bool available, int delay, DeviceConfiguration config)
 {
     var data = new EventData(this);
     data.Id = "DEVICE_AVAILABILITY";
     data.Data01 = config;
     data.Data02 = available;
     data.Data03 = delay;
     SendData?.Invoke(data);
 }
 static bool GreaterThan(DeviceConfiguration c1, DeviceConfiguration c2)
 {
     if (c1.Index < c2.Index) return false;
     else return true;
 }
Exemple #20
0
 public static bool Save(DeviceConfiguration config)
 {
     return(Save(config, FileLocations.Devices));
 }
        private MTConnect.Application.Streams.ReturnData GetSample(Streams header, Data.AgentInfo ac, DeviceConfiguration config)
        {
            MTConnect.Application.Streams.ReturnData result = null;

            string address = ac.Address;
            int port = ac.Port;
            string deviceName = ac.DeviceName;

            // Set Proxy Settings
            var proxy = new HTTP.ProxySettings();
            proxy.Address = ac.ProxyAddress;
            proxy.Port = ac.ProxyPort;

            SampleInfo info = GetSampleInfo(header, config);
            if (info != null)
            {
                if (info.Count > 0)
                {
                    DateTime requestTimestamp = DateTime.Now;

                    string url = HTTP.GetUrl(address, port, deviceName) + "sample?from=" + info.From.ToString() + "&count=" + info.Count.ToString();

                    result = MTConnect.Application.Streams.Requests.Get(url, proxy, ac.Heartbeat / 2, 2);
                    if (result != null)
                    {
                        UpdateAgentData(header.InstanceId, info.From + info.Count);

                        if (verbose) Logger.Log("Sample Successful : " + url + " @ " + requestTimestamp.ToString("o"), LogLineType.Console);
                    }
                    else
                    {
                        Logger.Log("Sample Error : " + url + " @ " + requestTimestamp.ToString("o"));
                    }
                }
                else UpdateAgentData(header.InstanceId, header.LastSequence);
            }

            return result;
        }
        public static DeviceConfiguration Read(XmlDocument xml)
        {
            var result = new DeviceConfiguration();

            result.Xml = xml;

            foreach (XmlNode node in xml.DocumentElement.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    if (node.InnerText != "")
                    {
                        switch (node.Name.ToLower())
                        {
                            case "description": result.Description = ProcessDescription(node); break;

                            case "agent": result.Agent = ProcessAgent(node); break;

                            default:

                                Type Settings = typeof(DeviceConfiguration);
                                PropertyInfo info = Settings.GetProperty(node.Name);

                                if (info != null)
                                {
                                    Type t = info.PropertyType;
                                    info.SetValue(result, Convert.ChangeType(node.InnerText, t), null);
                                }

                                break;
                        }
                    }
                }
            }

            return result;
        }
 public static bool Save(DeviceConfiguration config)
 {
     return Save(config, FileLocations.Devices);
 }
        public static List<Data.DeviceInfo> Load(DeviceConfiguration[] deviceConfigs)
        {
            var config = Configuration.Read();

            if (File.Exists(config.SQliteDatabasePath))
            {
                try
                {
                    var connection = Connection.Create(config);
                    if (connection != null)
                    {
                        DataTable hoursTable = Table.GetHours(connection, deviceConfigs);
                        if (hoursTable != null)
                        {
                            var hourRowInfos = new List<Row.HourRowInfo>();

                            // Get HourRowInfo objects from DataRows
                            foreach (DataRow row in hoursTable.Rows)
                            {
                                var hourRowInfo = Row.GetHourInfo(connection, row);
                                if (hourRowInfo != null) hourRowInfos.Add(hourRowInfo);
                            }

                            var result = new List<Data.DeviceInfo>();

                            // Create DeviceInfo object for each HourRowInfo
                            foreach (var hourRowInfo in hourRowInfos)
                            {
                                Data.DeviceInfo deviceInfo = null;
                                deviceInfo = result.Find(o => o.UniqueId == hourRowInfo.UniqueId);
                                if (deviceInfo == null)
                                {
                                    deviceInfo = new Data.DeviceInfo();
                                    deviceInfo.UniqueId = hourRowInfo.UniqueId;
                                    result.Add(deviceInfo);
                                }

                                deviceInfo.AddHourInfo(hourRowInfo.HourInfo);
                            }

                            Logger.Log("Local Data Server : Backup Loaded Successfully");

                            return result;
                        }

                        Connection.Close(connection);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log("Local Data Server : Error Loading Backup :: " + ex.Message);
                }
            }
            else Logger.Log("Local Server Backup File Not Found @ " + config.SQliteDatabasePath);

            return null;
        }
        static bool NotEqualTo(DeviceConfiguration c1, DeviceConfiguration c2)
        {
            if (!object.ReferenceEquals(c1, null) && object.ReferenceEquals(c2, null)) return true;
            if (object.ReferenceEquals(c1, null) && !object.ReferenceEquals(c2, null)) return true;
            if (object.ReferenceEquals(c1, null) && object.ReferenceEquals(c2, null)) return false;

            return c1.UniqueId != c2.UniqueId || c1.Index != c2.Index;
        }
 static bool LessThan(DeviceConfiguration c1, DeviceConfiguration c2)
 {
     if (c1.Index > c2.Index) return false;
     else return true;
 }
 public DeviceServer(DeviceConfiguration config, List<IServerPlugin> serverPlugins)
 {
     Configuration = config;
     plugins = serverPlugins;
 }
 private void DeviceManager_AddDevice_EditTableSelected(DeviceConfiguration config)
 {
 }
        private SampleInfo GetSampleInfo(Streams header, DeviceConfiguration config)
        {
            var result = new SampleInfo();

            //Get Sequence Number to use -----------------------
            long first = header.FirstSequence;
            if (!startFromFirst)
            {
                first = header.LastSequence;
                startFromFirst = true;
            }
            else if (lastInstanceId == agentInstanceId && lastSequenceSampled > 0 && lastSequenceSampled >= header.FirstSequence)
            {
                first = lastSequenceSampled;
            }
            else if (first > 0)
            {
                // Increment some sequences since the Agent might change the first sequence
                // before the Sample request gets read
                // (should be fixed in Agent to automatically read the first 'available' sequence
                // instead of returning an error)
                first += 20;
            }

            result.From = first;

            // Get Last Sequence Number available from Header
            long last = header.LastSequence;

            // Calculate Sample count
            long sampleCount = last - first;
            if (sampleCount > MaxSampleCount)
            {
                sampleCount = MaxSampleCount;
                last = first + MaxSampleCount;
            }

            result.Count = sampleCount;

            return result;
        }
        private void RunRequests(DeviceConfiguration config)
        {
            var ac = config.Agent;
            if (ac != null)
            {
                // Run a Probe request and get the returned data
                if (probeData == null)
                {
                    probeData = GetProbe(ac);

                    // Send the Probe data to other plugins
                    SendProbeData(probeData, config);
                }

                if (probeData != null)
                {
                    // Run a Current request and get the returned data
                    currentData = GetCurrent(ac);
                    if (currentData != null && currentData.Header != null)
                    {
                        // Send the Current data to other plugins
                        SendCurrentData(currentData, config);

                        //Run a Sample request and get the returned data
                        var sampleData = GetSample(currentData.Header, ac, config);

                        //Send the Sample data to other plugins
                        if (sampleData != null) SendSampleData(sampleData, config);

                        UpdateAgentData(currentData.Header.InstanceId, currentData.Header.LastSequence);
                    }
                    else
                    {
                        probeData = null;
                        SendAvailability(false, ac.Heartbeat, config);
                    }
                } else SendAvailability(false, ac.Heartbeat, config);
            }
        }
        private void SendAgentReset(DeviceConfiguration config)
        {
            Console.WriteLine(config.UniqueId + " :: Agent Reset");

            var data = new EventData(this);
            data.Id = "MTCONNECT_AGENT_RESET";
            data.Data01 = config;

            SendData?.Invoke(data);
        }
            public static DataTable GetHours(SQLiteConnection connection, DeviceConfiguration[] deviceConfigs)
            {
                if (deviceConfigs != null && deviceConfigs.Length > 0)
                {
                    string query = "SELECT * FROM `hours` WHERE ";

                    for (var x = 0; x < deviceConfigs.Length; x++)
                    {
                        query += "`unique_id`='" + deviceConfigs[x].UniqueId + "'";
                        if (x < deviceConfigs.Length - 1) query += " OR ";
                    }

                    return Query.GetDataTable(connection, query);
                }

                return null;
            }
        public static bool Save(DeviceConfiguration config, string path)
        {
            bool result = false;

            result = Save(config.Xml, path);

            return result;
        }