示例#1
0
        private async Task <TValue> ProcessRequest <TValue>(string url)
        {
            var sw = new Stopwatch();

            sw.Start();
            using (var ms = new MemoryStream())
            {
                using (var client = new MyWebClient(_compression))
                {
                    AddHeaders(client);

                    using (var requestStream = await client.OpenReadTaskAsync(url))
                    {
                        await requestStream.CopyToAsync(ms);
                    }
                }
                LastRequestSize = (int)ms.Length;
                ms.Seek(0, SeekOrigin.Begin);
                var result = Deserialize <TValue>(ms);
                sw.Stop();
                this.LastRequestDuration = sw.Elapsed;

                return(result);
            }
        }
 private static async Task <bool> CanReadArtifactsUrl()
 {
     try
     {
         using (var client = new MyWebClient())
             using (var stream = await client.OpenReadTaskAsync(new Uri("https://www.google.com")))
                 return(stream != null);
     }
     catch
     {
         return(false);
     }
 }
        public virtual async void SetDeviceDimLevelAsync(string devid, int level)
        {
            //          /street_light.set_dim_level /street_light.set_dim_level /street_light.set_dim_level
            //Parameters dev (optional) string device id Parameters
            //level int dim level
            //Response Body - - -
            //Remark dim level = 0 (off), 20~100 (level in %) dim level = 0 (off), 20~100 (level in %) dim level = 0 (off), 20~100 (level in %)
            //Example Set dimming level of device 8020 to 85% Set dimming level of device 8020 to 85% Set dimming level of device 8020 to 85%Example
            //10.10.1.1:8080/street_light.set_dim_level?dev=8020&level=85 
            //if(devid=="*")
            //    using (Stream stream = wc.OpenRead(UriBase + "/street_light.set_dim_level?level=" + level))
            //    {
            //        while (stream.ReadByte() != -1) ;
            //    }
            //else
            string args;
            //lock (this)
            //{
            MyWebClient client = new MyWebClient();
            //if (devid == "*")
            //    args = "/street_light.set_dim_level?level=" + level;
            //else
            args = "/lighting.set_dim_level?dev=" + devid + "&level=" + level;
            using (Stream stream = await client.OpenReadTaskAsync(new Uri(UriBase + args, UriKind.Absolute)))
            {
                while (stream.ReadByte() != -1) ;
                // System.Diagnostics.Debug.Print(UriBase + "/street_light.set_dim_level?dev=" + devid + "&level=" + level);

            }

            //  client.Dispose();
            //}     
            // client.Dispose();

        }
        public virtual async Task<DeviceInfo[]> GetDeviceListAsync()  //version3
        {


            System.Runtime.Serialization.Json.DataContractJsonSerializer jsonsr = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(DeviceInfoListBase));
            //          Command /get_dev_list /get_dev_list /get_dev_list
            //Parameters - - -
            //Response Body {  list: [  {    addr: “”,    type: “”,    pan: “”,    mac: “”,    visibility: 1,  },...] }
            //array(object)
            //string string string string int
            //device id
            //device id device type device pan id MAC Address visibility
            DeviceInfoListBase infolist = null;
            MyWebClient wc = new MyWebClient();
            using (Stream stream = await wc.OpenReadTaskAsync(UriBase + "/get_dev_list"))
            {
                infolist = jsonsr.ReadObject(stream) as DeviceInfoListBase; ;
            }
            return infolist.list.Where(n => n != null).ToArray();

        }
        public virtual async void SetDeviceScheduleEnableAsync(string devid, bool enable)
        {
            //10.10.1.1:8080/street_light.set_dev_schedule?dev=081f&enable=0 

            MyWebClient wc = new MyWebClient();
            Stream stream;
            if (devid == "*")
            {
                stream = await wc.OpenReadTaskAsync(UriBase + "/lighting.set_dev_schedule?enable=" + (enable ? "1" : "0"));
            }
            else
            {
                stream = await wc.OpenReadTaskAsync(UriBase + "/lighting.set_dev_schedule?dev=" + devid + "&enable=" + (enable ? "1" : "0"));
            }
            while (stream.ReadByte() != -1) ;
            stream.Close();
            stream.Dispose();
        }
        public virtual async void SetDeviceScheduleAsync(string devid, string timeStr, string levelStr)
        {

            MyWebClient wc = new MyWebClient();
            
            Stream stream;
            System.DateTime dt = DateTime.Now;
            if (devid == "*")
            {
                stream = await wc.OpenReadTaskAsync(new Uri(UriBase + "/lighting.set_dev_schedule?time=" + timeStr + "&level=" + levelStr/*+"&on="+dt.Year+"-"+dt.Month+"-"+dt.Day*/));

               
            }
            else
            {
                stream = await wc.OpenReadTaskAsync(UriBase + "/lighting.set_dev_schedule?dev=" + devid + "&time=" + timeStr + "&level=" + levelStr/* + "&on=" + dt.Year + "-" + dt.Month + "-" + dt.Day*/);
            }
            while (stream.ReadByte() != -1) ;
            stream.Close();
            stream.Dispose();

        }
        public virtual async Task SetDeviceRTCAsync(string devid, DateTime dt)
        {
            //10.10.1.1:8080/street_light.set_rtc? dev=8201&rtc=13-08-31-12-10-11 
            MyWebClient wc = new MyWebClient();
            Stream stream;
            if (devid == "*")
                stream = await wc.OpenReadTaskAsync(UriBase + "/lighting.set_rtc?rtc=" + dt.ToString("yy-MM-dd-HH-mm-ss"));
            else
                stream = await wc.OpenReadTaskAsync(UriBase + "/lighting.set_rtc?dev=" + devid + "&rtc=" + dt.ToString("yy-MM-dd-HH-mm-ss"));

            while (stream.ReadByte() != -1) ;
            stream.Close();
            stream.Dispose();
           


        }