private DeviceConfiguration GetLocalDeviceConfiguration(string uniqueId)
        {
            string filename = Path.ChangeExtension(uniqueId, ".xml");
            string path     = Path.Combine(FileLocations.Devices, filename);

            return(DeviceConfiguration.Read(path));
        }
        /// <summary>
        /// Load a Device from a local path
        /// </summary>
        /// <param name="path">Path to the file to load</param>
        /// <returns>Boolean whether load was successful</returns>
        private bool LoadDevice(string path)
        {
            bool result = false;

            // Get Configuration from path
            var config = DeviceConfiguration.Read(path);

            if (config != null)
            {
                if (currentUser != null)
                {
                    result = TrakHound.API.Devices.Update(currentUser, config);
                }
                // If not logged in Read from File in 'C:\TrakHound\'
                else
                {
                    result = DeviceConfiguration.Save(config);
                }

                if (result)
                {
                    // Send message that device was added
                    var data = new EventData(this);
                    data.Id     = "DEVICE_ADDED";
                    data.Data01 = new DeviceDescription(config);
                    SendData?.Invoke(data);
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static List <DeviceConfiguration> Get(UserConfiguration userConfig, string[] deviceUniqueIds)
        {
            if (deviceUniqueIds != null && deviceUniqueIds.Length > 0)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

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

                var getDeviceInfos = new List <GetDeviceInfo>();
                foreach (var deviceUniqueId in deviceUniqueIds)
                {
                    getDeviceInfos.Add(new GetDeviceInfo(deviceUniqueId));
                }

                string json = JSON.FromObject(getDeviceInfos);
                if (json != null)
                {
                    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)
                    {
                        bool success = ApiError.ProcessResponse(response, "Get Devices");
                        if (success)
                        {
                            var deviceInfos = JSON.ToType <List <DeviceInfo> >(response);
                            if (deviceInfos != null)
                            {
                                var deviceConfigs = new List <DeviceConfiguration>();

                                foreach (var deviceInfo in deviceInfos)
                                {
                                    var table = deviceInfo.ToTable();
                                    if (table != null)
                                    {
                                        var xml = DeviceConfiguration.TableToXml(table);
                                        if (xml != null)
                                        {
                                            var deviceConfig = DeviceConfiguration.Read(xml);
                                            if (deviceConfig != null && !string.IsNullOrEmpty(deviceConfig.UniqueId))
                                            {
                                                deviceConfigs.Add(deviceConfig);
                                            }
                                        }
                                    }
                                }

                                return(deviceConfigs);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a list of DeviceConfigurations of all of the user's devices
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <returns></returns>
        public static List <DeviceConfiguration> Get(UserConfiguration userConfig)
        {
            Uri apiHost = ApiConfiguration.AuthenticationApiHost;

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

            string format = "{0}?token={1}&sender_id={2}";

            string token    = userConfig.SessionToken;
            string senderId = UserManagement.SenderId.Get();

            url = string.Format(format, url, token, senderId);

            string response = HTTP.GET(url);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "Get Devices");
                if (success)
                {
                    var deviceInfos = JSON.ToType <List <DeviceInfo> >(response);
                    if (deviceInfos != null)
                    {
                        var deviceConfigs = new List <DeviceConfiguration>();

                        foreach (var deviceInfo in deviceInfos)
                        {
                            var table = deviceInfo.ToTable();
                            if (table != null)
                            {
                                var xml = DeviceConfiguration.TableToXml(table);
                                if (xml != null)
                                {
                                    var deviceConfig = DeviceConfiguration.Read(xml);
                                    if (deviceConfig != null && !string.IsNullOrEmpty(deviceConfig.UniqueId))
                                    {
                                        deviceConfigs.Add(deviceConfig);
                                    }
                                }
                            }
                        }

                        return(deviceConfigs);
                    }
                }
            }

            return(null);
        }
        private void Save_Worker(object o)
        {
            bool success = false;

            var dt = (DataTable)o;

            if (dt != null)
            {
                // Reset Update ID
                DataTable_Functions.UpdateTableValue(dt, "address", "/UpdateId", "value", Guid.NewGuid().ToString());

                if (userConfig != null)
                {
                    success = Devices.Update(userConfig, dt);
                }
                // If not logged in Save to File in 'C:\TrakHound\'
                else
                {
                    success = DeviceConfiguration.Save(dt);
                }

                if (success)
                {
                    XmlDocument xml = DeviceConfiguration.TableToXml(dt);
                    if (xml != null)
                    {
                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Configuration      = DeviceConfiguration.Read(xml);
                            ConfigurationTable = dt.Copy();
                        }), System.Windows.Threading.DispatcherPriority.Background, new object[] { });
                    }
                }
            }

            Dispatcher.BeginInvoke(new Action <bool>(Save_Finished), System.Windows.Threading.DispatcherPriority.Background, new object[] { success });
        }
        private void LoadLocalDevice(object o)
        {
            if (o != null)
            {
                string uniqueId = (string)o;

                try
                {
                    string filename = Path.ChangeExtension(uniqueId, ".xml");
                    string path     = Path.Combine(FileLocations.Devices, filename);

                    var config = DeviceConfiguration.Read(path);
                    if (config != null)
                    {
                        // Set Configuration Properties to new values
                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Configuration      = config;
                            ConfigurationTable = config.ToTable();
                        }), System.Windows.Threading.DispatcherPriority.Background, new object[] { });

                        // Reload Pages with new Device Configuration
                        Dispatcher.BeginInvoke(new Action(RestorePages), System.Windows.Threading.DispatcherPriority.Background, new object[] { });
                    }
                    else
                    {
                        Dispatcher.BeginInvoke(new Action(() => DeviceError = true), System.Windows.Threading.DispatcherPriority.Background, new object[] { });
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log("Local Device Backup Error :: Exception :: " + ex.Message);
                }
            }

            Dispatcher.BeginInvoke(new Action(() => DeviceLoading = false), System.Windows.Threading.DispatcherPriority.Background, new object[] { });
        }
        public static DeviceConfiguration Create(ProbeData probeData)
        {
            if (probeData != null && probeData.Device != null)
            {
                var table = new DataTable();
                table.Columns.Add("address");
                table.Columns.Add("name");
                table.Columns.Add("value");
                table.Columns.Add("attributes");

                var items = probeData.Device.GetAllDataItems();

                SetIds(table);
                SetEnabled(table);
                Description.Add(table, probeData.Device);
                Agent.Add(table, probeData.Address, probeData.Port, probeData.Device.Name);
                GeneratedEvents.DeviceStatus.Add(table, items);
                GeneratedEvents.ProductionStatus.Add(table, items);
                GeneratedEvents.CycleExecution.Add(table, items);
                GeneratedEvents.PartsCount.Add(table, items);
                Cycles.Add(table, items);
                Parts.Add(table, items);
                Overrides.Add(table, items);
                Oee.Add(table, items);

                // Manufacturer Specific Processing
                Manufacturers.Okuma.Process(table, probeData.Device);

                //var xml = Converters.DeviceConfigurationConverter.TableToXML(dt);
                var xml = DeviceConfiguration.TableToXml(table);

                return(DeviceConfiguration.Read(xml));
            }

            return(null);
        }