pi_web_api_cs_helper.PIWebAPIClient.WriteTagValueAsync C# (CSharp) Method

WriteTagValueAsync() public method

Write a value at current time to a particular PI tag.
public WriteTagValueAsync ( string serverWebId, string tagName, string valueToWrite ) : System.Threading.Tasks.Task
serverWebId string WebId of the PI Data Archive.
tagName string Name of the PI tag.
valueToWrite string Value to write.
return System.Threading.Tasks.Task
        public async Task WriteTagValueAsync(string serverWebId, string tagName, string valueToWrite)
        {
            string url = await GetTagValueLink(serverWebId, tagName);
            object payload = new
            {
                Value = valueToWrite
            };
            string data = JsonConvert.SerializeObject(payload);
            await PostAsync(url, data);
        }

Usage Example

Beispiel #1
0
        static void Main(string[] args)
        {
            Console.Write("Enter username: "******"Enter password: "******"Getting list of PI Data Archives...");
                Dictionary<string, string> serversWebId = client.GetDataServersWebIdAsync().Result;
                foreach (var server in serversWebId)
                {
                    Console.WriteLine(server.Key);
                }
                do
                {
                    Console.Write("Please type in name of the PI Data Archive: ");
                    string myPI = Console.ReadLine();
                    Console.Write("Please type in name of the tag: ");
                    string myTag = Console.ReadLine();

                    if (IsRead())
                    {
                        dynamic value = client.GetTagValueAsync(serversWebId[myPI], myTag).Result;
                        Console.WriteLine("Current value = {0}", value);
                    }
                    else
                    {
                        Console.Write("Value to write? ");
                        string valueToWrite = Console.ReadLine();
                        client.WriteTagValueAsync(serversWebId[myPI], myTag, valueToWrite).Wait();
                        Console.WriteLine("Value written.");
                    }
                    Console.WriteLine("Press any key to continue (esc to exit)...");
                } while (Console.ReadKey().Key != ConsoleKey.Escape);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("The specified PI Data Archive cannot be found.");
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine(e.Message);
                }
            }
            finally
            {
                client.Dispose();
                Console.ReadKey();
            }
        }