using System;
using System.Net;
using System.Xml;
using System.Globalization;
using S7;
namespace OpenWeatherToS7DB
{
class Program
{
//PARAMETER
static string gsCity = "Dresden";
static string gsPLCIP = "10.49.107.30";
static string gsPLCType = "S7300";
static short gbRack = 0;
static short gbSlot = 2;
static int giDB = 253;
//END PARAMETER
struct ActData
{
public DateTime _dtRise;
public DateTime _dtSet;
public int _iTemp;
public int _iHum;
public int _iPres;
public int _iSpeed;
public int _iDir;
public DateTime _dtLastUpdate;
}
struct ForecastData
{
public int _iTempDay;
public int _iTempMorn;
public int _iTempEve;
public int _iTempNight;
public int _iTempMax;
public int _iTempMin;
public int _iHum;
public int _iPres;
public int _iSpeed;
public int _iDir;
}
static ForecastData[] WeekForecast = new ForecastData[7];
static ActData ActualWeather = new ActData();
static IFormatProvider gProvider = new CultureInfo("en-GB");
// <param name="args">e.g.: Dresden 10.49.107.30 S7300 0 2 253</param>
static void Main(string[] args)
{
if (args.Length == 6)
{
gsCity = args[0];
gsPLCIP = args[1];
gsPLCType = args[2];
gbRack = short.Parse(args[3]);
gbSlot = short.Parse(args[4]);
giDB = short.Parse(args[5]);
}
try
{
if(getActData())
if(getForecast())
writePLC();
}
catch { }
}
static bool getActData()
{
try
{
WebClient _WClient = new WebClient();
_WClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string _sUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + gsCity + "&mode=xml&units=metric";
XmlTextReader _XMLReader = new XmlTextReader(_WClient.OpenRead(_sUrl));
while (_XMLReader.Read())
{
switch (_XMLReader.NodeType)
{
case XmlNodeType.Element:
switch (_XMLReader.Name)
{
case "sun":
for (int i = 0; i < 2; i++)
{
_XMLReader.MoveToNextAttribute();
switch (_XMLReader.Name)
{
case "set": ActualWeather._dtSet = DateTime.Parse(_XMLReader.Value);
break;
case "rise": ActualWeather._dtRise = DateTime.Parse(_XMLReader.Value);
break;
}
}
break;
case "temperature":
_XMLReader.MoveToNextAttribute();
ActualWeather._iTemp = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "humidity":
_XMLReader.MoveToNextAttribute();
ActualWeather._iHum = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "pressure":
_XMLReader.MoveToNextAttribute();
ActualWeather._iPres = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "speed":
_XMLReader.MoveToNextAttribute();
ActualWeather._iSpeed = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "direction":
_XMLReader.MoveToNextAttribute();
ActualWeather._iDir = int.Parse(_XMLReader.Value);
break;
case "lastupdate":
_XMLReader.MoveToNextAttribute();
ActualWeather._dtLastUpdate = DateTime.Parse(_XMLReader.Value);
break;
}
break;
}
}
_XMLReader.Close();
return true;
}
catch
{
return false;
}
}
static bool getForecast()
{
try
{
int _iDayIndex = -1;
WebClient _WClient = new WebClient();
_WClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string _sUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?q=" + gsCity + "&mode=xml&units=metric";
XmlTextReader _XMLReader = new XmlTextReader(_WClient.OpenRead(_sUrl));
while (_XMLReader.Read())
{
switch (_XMLReader.NodeType)
{
case XmlNodeType.Element:
switch (_XMLReader.Name)
{
case "time":
_XMLReader.MoveToNextAttribute();
if (DateTime.Today <= DateTime.Parse(_XMLReader.Value))
{
_iDayIndex = DateTime.Parse(_XMLReader.Value).Subtract(DateTime.Now.Date).Days;
}
break;
case "temperature":
for (int i = 0; i < 6; i++)
{
_XMLReader.MoveToNextAttribute();
switch (_XMLReader.Name)
{
case "day": WeekForecast[_iDayIndex]._iTempDay = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "min": WeekForecast[_iDayIndex]._iTempMin = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "max": WeekForecast[_iDayIndex]._iTempMax = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "night": WeekForecast[_iDayIndex]._iTempNight = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "eve": WeekForecast[_iDayIndex]._iTempEve = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "morn": WeekForecast[_iDayIndex]._iTempMorn = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
}
}
break;
case "humidity":
_XMLReader.MoveToNextAttribute();
WeekForecast[_iDayIndex]._iHum = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "pressure":
for (int i = 0; i < 2; i++)
{
_XMLReader.MoveToNextAttribute();
switch (_XMLReader.Name)
{
case "value": WeekForecast[_iDayIndex]._iPres = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
}
}
break;
case "windSpeed":
_XMLReader.MoveToNextAttribute();
WeekForecast[_iDayIndex]._iSpeed = Convert.ToInt32(double.Parse(_XMLReader.Value, gProvider) * 10);
break;
case "windDirection":
_XMLReader.MoveToNextAttribute();
WeekForecast[_iDayIndex]._iDir = int.Parse(_XMLReader.Value);
break;
}
break;
}
}
_XMLReader.Close();
return true;
}
catch
{
return false;
}
}
static bool writePLC()
{
try
{
CPU_Type _Ct = new CPU_Type();
switch (gsPLCType)
{
case "S7200": _Ct = CPU_Type.S7200;
break;
case "S7300": _Ct = CPU_Type.S7300;
break;
case "S7400": _Ct = CPU_Type.S7400;
break;
}
PLC _PLC = new PLC(_Ct,gsPLCIP,gbRack,gbSlot);
_PLC.Open();
_PLC.Write(("DB" + giDB + ".DBW0"), ActualWeather._iTemp);
_PLC.Write(("DB" + giDB + ".DBW2"), ActualWeather._iHum);
_PLC.Write(("DB" + giDB + ".DBW4"), ActualWeather._iPres);
_PLC.Write(("DB" + giDB + ".DBW6"), ActualWeather._iSpeed);
_PLC.Write(("DB" + giDB + ".DBW8"), ActualWeather._iDir);
_PLC.Write(("DB" + giDB + ".DBT10"), ActualWeather._dtRise);
_PLC.Write(("DB" + giDB + ".DBT18"), ActualWeather._dtSet);
_PLC.Write(("DB" + giDB + ".DBT26"), ActualWeather._dtLastUpdate);
for (int i = 0; i < 7; i++)
{
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i+1) + 0)), WeekForecast[i]._iTempDay);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 2)), WeekForecast[i]._iTempMorn);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 4)), WeekForecast[i]._iTempEve);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 6)), WeekForecast[i]._iTempNight);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 8)), WeekForecast[i]._iTempMax);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 10)), WeekForecast[i]._iTempMin);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 12)), WeekForecast[i]._iHum);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 14)), WeekForecast[i]._iPres);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 16)), WeekForecast[i]._iSpeed);
_PLC.Write(("DB" + giDB + ".DBW" + (50 * (i + 1) + 18)), WeekForecast[i]._iDir);
_PLC.Write(("DB" + giDB + ".DBT" + (50 * (i + 1) + 20)), DateTime.Now);
}
_PLC.Close();
return true;
}
catch
{
return false;
}
}
}
}