登峰造极境

  • WIN
    • CSharp
    • JAVA
    • OAM
    • DirectX
    • Emgucv
  • UNIX
    • FFmpeg
    • QT
    • Python
    • Opencv
    • Openwrt
    • Twisted
    • Design Patterns
    • Mysql
    • Mycat
    • MariaDB
    • Make
    • OAM
    • Supervisor
    • Nginx
    • KVM
    • Docker
    • OpenStack
  • WEB
    • ASP
    • Node.js
    • PHP
    • Directadmin
    • Openssl
    • Regex
  • APP
    • Android
  • AI
    • Algorithm
    • Deep Learning
    • Machine Learning
  • IOT
    • Device
    • MSP430
  • DIY
    • Algorithm
    • Design Patterns
    • MATH
    • X98 AIR 3G
    • Tucao
    • fun
  • LIFE
    • 美食
    • 关于我
  • LINKS
  • ME
Claves
长风破浪会有时,直挂云帆济沧海
  1. 首页
  2. Platforms
  3. WINDOWS
  4. CSharp
  5. 正文

C# 定时报时与天气预报-折腾笔记

2017-02-18

由于开始工作了,对当天天气预报比较关注,但是又经常出门忘记看天气,所以就做个整点报天气的程序了。

语言:C#

框架:.NET

系统:Windows 10

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Speech.Synthesis;
using System.IO;
using System.Net;
using Newtonsoft.Json;

namespace TTS
{
    class Program
    {
        private static void WriteLog( string message)
        {
            string logFile = "C:\\Users\\yaping\\Desktop\\TTS\\"+DateTime.Now.ToString("yyyy-MM-dd") + "_MAX.log";
            string context = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + message;
            StreamWriter sWriter = null;
            try
            {
                FileStream fileStream = new FileStream(logFile, FileMode.Append, FileAccess.Write);
                sWriter = new StreamWriter(fileStream);
                //写入到日志
                sWriter.WriteLine(context);
                //打印到屏幕

            }
            catch (Exception ex)
            {
                
            }
            finally
            {
                if (sWriter != null)
                {
                    sWriter.Close();
                }
            }
        }
        private static void Speak(string str)
        {
            try
            {
                SpeechSynthesizer synth = new SpeechSynthesizer();
                synth.Volume = 100;
                synth.Speak(str);
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }

        }
        private static void Visor()
        {
            string hour = DateTime.Now.ToString("HH");
            string minute = DateTime.Now.ToString("mm");
            if (minute == "00")
            {
                switch (hour)
                {
                    case "00": { break; }
                    case "01": { break; }
                    case "02": { break; }
                    case "03": { break; }
                    case "04": { break; }
                    case "05": { break; }
                    case "06": { ProcMorningWeather(); break; }
                    case "07": { ProcMorningWeather(); break; }
                    case "08": { ProcMorningWeather(); break; }
                    case "09": { ProcMorningWeather(); break; }
                    case "10": { SpeakTime(); break; }
                    case "11": { SpeakTime(); break; }
                    case "12": { SpeakTime(); break; }
                    case "13": { SpeakTime(); break; }
                    case "14": { SpeakTime(); break; }
                    case "15": { SpeakTime(); break; }
                    case "16": { SpeakTime(); break; }
                    case "17": { SpeakTime(); break; }
                    case "18": { SpeakTime(); break; }
                    case "19": { SpeakTime(); break; }
                    case "20": { SpeakTime(); break; }
                    case "21": { ProcNightWeather(); break; }
                    case "22": { ProcNightWeather(); break; }
                    case "23": { SpeakTime(); break; }


                }
            }
        }
        public static void SpeakTime()
        {
            try
            {
                Speak("当前时间" + DateTime.Now.ToString("HH:mm"));
                WriteLog("定点报时");
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }
        private static string getWeatherJson()
        {
            try
            {
                WebClient MyWebClient = new WebClient();
                MyWebClient.Credentials = CredentialCache.DefaultCredentials;
                Byte[] pageData = MyWebClient.DownloadData("https://free-api.heweather.com/v5/weather?key=b87608642c9010ea9a&city=shuangliu");
                return Encoding.UTF8.GetString(pageData);

            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
                return "{}";
            }
        }
        /// <summary>
        /// 早晨天气预报侧重于当天天气
        /// </summary>
        private static void ProcMorningWeather()
        {
            try
            {
                
                dynamic json = JsonConvert.DeserializeObject(getWeatherJson());
                string city = "," + json["HeWeather5"][0]["basic"]["city"];
                string time = "当前时间" + DateTime.Now.ToString("HH:mm");
                string now_cond = ",当前天气" + json["HeWeather5"][0]["now"]["cond"]["txt"];
                string now_fl = ",体感温度" + json["HeWeather5"][0]["now"]["fl"] + "摄氏度";
                string now_pop = ",温度" + json["HeWeather5"][0]["now"]["tmp"] + "摄氏度";
                string now_pcpn = ",降雨量" + json["HeWeather5"][0]["now"]["pcpn"]+"毫米";
                string tomorrow_d = ",明天白天" + json["HeWeather5"][0]["daily_forecast"][1]["cond"]["txt_d"];
                string tomorrow_n = ",夜晚" + json["HeWeather5"][0]["daily_forecast"][1]["cond"]["txt_n"];
                string suggestion = "," + json["HeWeather5"][0]["suggestion"]["comf"]["txt"];
                Speak(
                    time +
                    city +
                    now_cond +
                    now_fl +
                    now_pop +
                    now_pcpn +
                    tomorrow_d +
                    tomorrow_n +
                    suggestion);
                WriteLog("早晨天气预报");
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }
        /// <summary>
        /// 夜晚天气预报侧重于明天天气
        /// </summary>
        private static void ProcNightWeather()
        {
            try {
                
                dynamic json = JsonConvert.DeserializeObject(getWeatherJson());
                string city =","+ json["HeWeather5"][0]["basic"]["city"];
                string time ="当前时间"+ DateTime.Now.ToString("HH:mm");
                string now_cond = ",当前天气" + json["HeWeather5"][0]["now"]["cond"]["txt"];
                string now_fl = ",体感温度"+ json["HeWeather5"][0]["now"]["fl"]+"摄氏度";
                string tomorrow_d = ",明天白天" + json["HeWeather5"][0]["daily_forecast"][1]["cond"]["txt_d"];
                string tomorrow_n = ",夜晚" + json["HeWeather5"][0]["daily_forecast"][1]["cond"]["txt_n"];
                string tomorrow_pop = ",降雨概率百分之" + json["HeWeather5"][0]["daily_forecast"][1]["pop"];
                string tomorrow_pcpn = ",降雨量" + json["HeWeather5"][0]["daily_forecast"][1]["pcpn"] + "毫米";
                string tomorrow_tmp_max = ",最高温度" + json["HeWeather5"][0]["daily_forecast"][1]["tmp"]["max"]+"摄氏度";
                string tomorrow_tmp_min = ",最低温度" + json["HeWeather5"][0]["daily_forecast"][1]["tmp"]["min"] + "摄氏度";
                string suggestion = "," + json["HeWeather5"][0]["suggestion"]["comf"]["txt"];
                Speak(
                    time+ 
                    city+
                    now_cond+ 
                    now_fl+ 
                    tomorrow_d+ 
                    tomorrow_n+ 
                    tomorrow_pop+ 
                    tomorrow_pcpn+ 
                    tomorrow_tmp_max+ 
                    tomorrow_tmp_min+ 
                    suggestion);
                WriteLog("晚上天气预报");
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }
        static void Main(string[] args)
        {
           

            string context = "未知";
            while (true)
            {
                try
                {
                    System.Console.Clear();
                    Console.WriteLine("[当前时间]:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    Console.WriteLine("[当前状态]:" + context);
                    
                    string ss = DateTime.Now.ToString("ss");
                    if (ss == "30")
                    {
                        WriteLog(context);
                        Visor();
                    }       
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    context = ex.Message;
                }
                finally
                {
                    context = "ok";
                }
            }
        }
    }
}

 

 

标签: 暂无
最后更新:2017-02-19

代号山岳

知之为知之 不知为不知

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2099 登峰造极境. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备14031139号-5

川公网安备51012202000587号