// |
// -------------------------------------------------------------------------- |
// Gurux Ltd |
// |
// |
// |
// Filename: $HeadURL$ |
// |
// Version: $Revision$, |
// $Date$ |
// $Author$ |
// |
// Copyright (c) Gurux Ltd |
// |
//--------------------------------------------------------------------------- |
// |
// DESCRIPTION |
// |
// This file is a part of Gurux Device Framework. |
// |
// Gurux Device Framework is Open Source software; you can redistribute it |
// and/or modify it under the terms of the GNU General Public License |
// as published by the Free Software Foundation; version 2 of the License. |
// Gurux Device Framework is distributed in the hope that it will be useful, |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
// See the GNU General Public License for more details. |
// |
// More information of Gurux products: https://www.gurux.org |
// |
// This code is licensed under the GNU General Public License v2. |
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt |
//--------------------------------------------------------------------------- |
usingSystem; |
usingSystem.Collections.Generic; |
usingSystem.IO; |
usingSystem.Xml.Serialization; |
usingSystem.Xml; |
usingSystem.Globalization; |
usingSystem.Net; |
namespaceGurux.DLMS.ManufacturerSettings |
{ |
#if!WINDOWS_UWP |
[Serializable] |
#endif |
publicclassGXManufacturerCollection : List<GXManufacturer> |
{ |
publicGXManufacturerFindByIdentification(stringid) |
{ |
foreach (GXManufactureritinthis) |
{ |
if (string.Compare(it.Identification, id, true) 0) |
{ |
returnit; |
} |
} |
returnnull; |
} |
#if!WINDOWS_UWP |
/// <summary> |
/// Is this first run. |
/// </summary> |
/// <returns></returns> |
publicstaticboolIsFirstRun() |
{ |
stringpath=Path.Combine(ObisCodesPath, 'files.xml'); |
if (!System.IO.Directory.Exists(ObisCodesPath)) |
{ |
System.IO.Directory.CreateDirectory(ObisCodesPath); |
} |
if (!System.IO.File.Exists(path)) |
{ |
System.IO.FileStreamstream=System.IO.File.Create(path); |
stream.Close(); |
GXFileInfo.UpdateFileSecurity(path); |
returntrue; |
} |
returnnewFileInfo(path).Length0; |
} |
/// <summary> |
/// Check if there are any updates available in Gurux www server. |
/// </summary> |
/// <returns>Returns true if there are any updates available.</returns> |
publicstaticboolIsUpdatesAvailable() |
{ |
//Do not check updates while debugging. |
if (System.Diagnostics.Debugger.IsAttached) |
{ |
returnfalse; |
} |
stringpath=Path.Combine(ObisCodesPath, 'files.xml'); |
if (!System.IO.File.Exists(path)) |
{ |
returntrue; |
} |
System.Xml.XmlDocumentxml=newSystem.Xml.XmlDocument(); |
try |
{ |
xml.Load(path); |
WebClientclient=newWebClient(); |
IWebProxyproxy=WebRequest.GetSystemWebProxy(); |
if (proxy!=null&&proxy.Credentials!=null) |
{ |
client.Proxy=proxy; |
} |
//This will fix the error: request was aborted could not create ssl/tls secure channel. |
//For Net45 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
ServicePointManager.SecurityProtocol= (SecurityProtocolType)3072; |
// Put the byte array into a stream and rewind it to the beginning |
MemoryStreamms=newMemoryStream(client.DownloadData('https://www.gurux.fi/obis/files.xml')); |
ms.Flush(); |
ms.Position=0; |
System.Xml.XmlDocumentdownloadsXml=newSystem.Xml.XmlDocument(); |
downloadsXml.Load(ms); |
//Find manufacturer setting and compare dates. |
foreach (System.Xml.XmlNodeitindownloadsXml.ChildNodes[1].ChildNodes) |
{ |
boolupdated=true; |
foreach (System.Xml.XmlNodenodeinxml.ChildNodes[1].ChildNodes) |
{ |
if (string.Compare(node.InnerText, it.InnerText, true) 0) |
{ |
if (DateTime.ParseExact(node.Attributes['Modified'].Value, 'dd-MM-yyyy', null).DateDateTime.ParseExact(it.Attributes['Modified'].Value, 'dd-MM-yyyy', CultureInfo.CurrentCulture).Date) |
{ |
updated=false; |
} |
break; |
} |
} |
if (updated) |
{ |
returntrue; |
} |
} |
} |
catch |
{ |
returntrue; |
} |
returnfalse; |
} |
staticprotectedstringGetMD5Hash(byte[] data) |
{ |
byte[] buff; |
System.Security.Cryptography.MD5md5=newSystem.Security.Cryptography.MD5CryptoServiceProvider(); |
buff=md5.ComputeHash(data); |
returnBitConverter.ToString(buff); |
} |
staticprotectedstringGetMD5HashFromFile(stringfileName) |
{ |
byte[] buff; |
using (FileStreamfile=newFileStream(fileName, FileMode.Open)) |
{ |
System.Security.Cryptography.MD5md5=newSystem.Security.Cryptography.MD5CryptoServiceProvider(); |
buff=md5.ComputeHash(file); |
file.Close(); |
} |
returnBitConverter.ToString(buff); |
} |
/// <summary> |
/// Update manufacturer settings from the Gurux www server. |
/// </summary> |
/// <returns>Returns directory where backups are made. Return null if no backups are made.</returns> |
publicstaticstringUpdateManufactureSettings() |
{ |
stringbackupPath=Path.Combine(ObisCodesPath, 'backup'); |
if (!System.IO.Directory.Exists(backupPath)) |
{ |
System.IO.Directory.CreateDirectory(backupPath); |
} |
backupPath=Path.Combine(backupPath, Guid.NewGuid().ToString()); |
System.Net.WebClientclient=newSystem.Net.WebClient(); |
IWebProxyproxy=WebRequest.GetSystemWebProxy(); |
if (proxy!=null&&proxy.Credentials!=null) |
{ |
client.Proxy=proxy; |
} |
//This will fix the error: request was aborted could not create ssl/tls secure channel. |
//For Net45 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
ServicePointManager.SecurityProtocol= (SecurityProtocolType)3072; |
byte[] files=client.DownloadData('https://www.gurux.fi/obis/files.xml'); |
System.IO.File.WriteAllBytes(Path.Combine(ObisCodesPath, 'files.xml'), files); |
Gurux.DLMS.ManufacturerSettings.GXFileInfo.UpdateFileSecurity(Path.Combine(ObisCodesPath, 'files.xml')); |
// Put the byte array into a stream and rewind it to the beginning |
MemoryStreamms=newMemoryStream(files); |
ms.Flush(); |
ms.Position=0; |
XmlDocumentxml=newXmlDocument(); |
xml.Load(ms); |
boolbackup=false; |
foreach (XmlNodeitinxml.ChildNodes[1].ChildNodes) |
{ |
stringpath=Path.Combine(ObisCodesPath, it.InnerText); |
byte[] data=client.DownloadData('https://www.gurux.fi/obis/'+it.InnerText); |
//Make backup if file exists or content has change. |
if (System.IO.File.Exists(path) &&GetMD5Hash(data) !=GetMD5HashFromFile(path)) |
{ |
if (!System.IO.Directory.Exists(backupPath)) |
{ |
System.IO.Directory.CreateDirectory(backupPath); |
} |
backup=true; |
System.IO.File.Copy(path, Path.Combine(backupPath, it.InnerText)); |
} |
System.IO.File.WriteAllBytes(path, data); |
Gurux.DLMS.ManufacturerSettings.GXFileInfo.UpdateFileSecurity(path); |
} |
if (backup) |
{ |
returnbackupPath; |
} |
returnnull; |
} |
#endif |
publicstaticstringObisCodesPath |
{ |
get |
{ |
stringpath=string.Empty; |
#ifWINDOWS_UWP |
path='OBIS'; |
#else |
if (Environment.OSVersion.PlatformPlatformID.Unix) |
{ |
path=Environment.GetFolderPath(Environment.SpecialFolder.Personal); |
path=Path.Combine(path, '.Gurux'); |
} |
else |
{ |
//Vista: C:ProgramData |
//XP: c:Program FilesCommon Files |
//XP = 5.1 & Vista = 6.0 |
if (Environment.OSVersion.Version.Major>=6) |
{ |
path=Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); |
} |
else |
{ |
path=Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles); |
} |
path=Path.Combine(path, 'Gurux'); |
} |
path=Path.Combine(path, 'OBIS'); |
#endif |
returnpath; |
} |
} |
publicstaticvoidReadManufacturerSettings(GXManufacturerCollectionManufacturers) |
{ |
ReadManufacturerSettings(Manufacturers, ObisCodesPath); |
} |
publicstaticvoidReadManufacturerSettings(GXManufacturerCollectionManufacturers, Stringpath) |
{ |
Manufacturers.Clear(); |
if (Directory.Exists(path)) |
{ |
Type[] extraTypes=newType[] { typeof(GXManufacturerCollection), typeof(GXManufacturer), typeof(GXObisCodeCollection), typeof(GXObisCode), typeof(GXObisValueItem), typeof(GXObisValueItemCollection), typeof(GXDLMSAttribute), typeof(GXAttributeCollection) }; |
XmlSerializerx=newXmlSerializer(typeof(GXManufacturer), extraTypes); |
foreach (stringitinDirectory.GetFiles(path, '*.obx')) |
{ |
using (Streamstream=File.Open(it, FileMode.Open)) |
{ |
using (TextReaderreader=newStreamReader(stream)) |
{ |
try |
{ |
GXManufacturerman= (GXManufacturer)x.Deserialize(reader); |
Manufacturers.Add(man); |
} |
catch (ExceptionEx) |
{ |
System.Diagnostics.Debug.WriteLine(Ex.Message); |
} |
} |
} |
} |
} |
} |
publicvoidWriteManufacturerSettings() |
{ |
WriteManufacturerSettings(ObisCodesPath); |
} |
publicvoidWriteManufacturerSettings(stringdirectory) |
{ |
//Do not save empty list. |
if (this.Count!=0) |
{ |
if (!Directory.Exists(directory)) |
{ |
Directory.CreateDirectory(directory); |
} |
Type[] extraTypes=newType[] { typeof(GXManufacturerCollection), typeof(GXManufacturer), typeof(GXObisCodeCollection), typeof(GXObisCode), typeof(GXObisValueItem), typeof(GXObisValueItemCollection), typeof(GXDLMSAttribute), typeof(GXAttributeCollection) }; |
XmlSerializerx=newXmlSerializer(typeof(GXManufacturer), extraTypes); |
foreach (GXManufactureritinthis) |
{ |
stringpath=Path.Combine(directory, it.Identification) +'.obx'; |
if (it.Identification'AUX') |
{ |
path=Path.Combine(directory, '_'+it.Identification) +'.obx'; |
} |
elseif (it.Identification'CON') |
{ |
path=Path.Combine(directory, '_'+it.Identification) +'.obx'; |
} |
if (!it.Removed) |
{ |
using (Streamstream=File.Open(path, FileMode.Create)) |
{ |
GXFileInfo.UpdateFileSecurity(path); |
using (TextWriterwriter=newStreamWriter(stream)) |
{ |
x.Serialize(writer, it); |
} |
} |
} |
else |
{ |
File.Delete(path); |
} |
} |
} |
} |
} |
} |