由于开始工作了,对当天天气预报比较关注,但是又经常出门忘记看天气,所以就做个整点报天气的程序了。
语言: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";
}
}
}
}
}