Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for energyloggerconfig.cs

(view source code of energyloggerconfig.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class EnergyLoggerConfig
  9. 	{
  10. 		static string progver = "1.00";
  11. 		static int minutesinfuture = 1;
  12. 		static int deviceID = 1;
  13. 		static double daytimetariff = 0.076;
  14. 		static double nighttimetariff = 0.051;
  15. 		static Currency currency = Currency.Euro;
  16. 		static DateFormat dateformat = DateFormat.DateDmy;
  17. 		static TimeFormat timeformat = TimeFormat.Clock24Hour;
  18.  
  19.  
  20. 		static int Main( string[] args )
  21. 		{
  22. 			if ( args.Length < 2 || args.Length > 7 )
  23. 			{
  24. 				return ShowHelp( );
  25. 			}
  26.  
  27. 			try
  28. 			{
  29. 				foreach ( string arg in args )
  30. 				{
  31. 					string key, val;
  32. 					if ( arg.IndexOf( ':' ) == -1 )
  33. 					{
  34. 						key = arg.ToLower( );
  35. 						val = null;
  36. 					}
  37. 					else
  38. 					{
  39. 						key = arg.Substring( 0, arg.IndexOf( ':' ) ).ToLower( );
  40. 						val = arg.Substring( arg.IndexOf( ':' ) + 1 ).ToLower( );
  41. 					}
  42. 					switch ( key )
  43. 					{
  44. 						case "/12h":
  45. 							timeformat = TimeFormat.Clock12Hour;
  46. 							break;
  47. 						case "/c":
  48. 							switch ( val[0] )
  49. 							{
  50. 								case 'd':
  51. 								case 'u':
  52. 								case '$':
  53. 									currency = Currency.USDollar;
  54. 									break;
  55. 								case 'e':
  56. 									currency = Currency.Euro;
  57. 									break;
  58. 								case 'f':
  59. 								case 's':
  60. 									currency = Currency.SwissFrank;
  61. 									break;
  62. 								case 'g':
  63. 								case 'p':
  64. 									currency = Currency.GBPound;
  65. 									break;
  66. 								default:
  67. 									return ShowHelp( );
  68. 							}
  69. 							break;
  70. 						case "/dt":
  71. 							daytimetariff = Convert.ToDouble( val );
  72. 							break;
  73. 						case "/f":
  74. 							minutesinfuture = Convert.ToInt32( val );
  75. 							break;
  76. 						case "/id":
  77. 							deviceID = Convert.ToInt32( val );
  78. 							break;
  79. 						case "/mdy":
  80. 							dateformat = DateFormat.DateMdy;
  81. 							break;
  82. 						case "/nt":
  83. 							nighttimetariff = Convert.ToDouble( val );
  84. 							break;
  85. 						default:
  86. 							return ShowHelp( );
  87. 					}
  88. 				}
  89. 			}
  90. 			catch ( Exception )
  91. 			{
  92. 				return ShowHelp( );
  93. 			}
  94.  
  95. 			if ( daytimetariff <= 0 || daytimetariff >= 10 )
  96. 			{
  97. 				return ShowHelp( );
  98. 			}
  99.  
  100. 			if ( nighttimetariff <= 0 || nighttimetariff >= 10 )
  101. 			{
  102. 				return ShowHelp( );
  103. 			}
  104.  
  105. 			if ( deviceID < 0 || deviceID > 9 )
  106. 			{
  107. 				return ShowHelp( );
  108. 			}
  109.  
  110. 			if ( minutesinfuture < 0 || minutesinfuture > 60 )
  111. 			{
  112. 				return ShowHelp( );
  113. 			}
  114.  
  115. 			// Minute(s) to write the file to SD card and then load the SD card into the Energy Logger 3500
  116. 			DateTime soon = DateTime.Now.AddMinutes( minutesinfuture );
  117.  
  118. 			byte[] bindata = new byte[] { 
  119. 				0xB8,
  120. 				0xAD,
  121. 				0xF2, // fixed 3 bytes header
  122. 				Convert.ToByte( deviceID),
  123. 				Convert.ToByte( timeformat ),
  124. 				Convert.ToByte( dateformat ),
  125. 				Convert.ToByte( soon.Hour ),
  126. 				Convert.ToByte( soon.Minute ),
  127. 				Convert.ToByte( soon.Month ),
  128. 				Convert.ToByte( soon.Day ),
  129. 				Convert.ToByte( soon.Year % 100 ), // 2-digit year (without the century)
  130. 				Convert.ToByte( currency ),
  131. 				Convert.ToByte( Convert.ToInt32( nighttimetariff ) ),
  132. 				Convert.ToByte( Convert.ToInt32( nighttimetariff * 10 ) % 10 ),
  133. 				Convert.ToByte( Convert.ToInt32( nighttimetariff * 100 ) % 100 ),
  134. 				Convert.ToByte( Convert.ToInt32( nighttimetariff * 1000 ) % 1000 ),
  135. 				Convert.ToByte( Convert.ToInt32( daytimetariff ) ),
  136. 				Convert.ToByte( Convert.ToInt32( daytimetariff * 10 ) % 10 ),
  137. 				Convert.ToByte( Convert.ToInt32( daytimetariff * 100 ) % 100 ),
  138. 				Convert.ToByte( Convert.ToInt32( daytimetariff * 1000 ) % 1000 )
  139. 			};
  140.  
  141. 			// Create the binary file
  142. 			using ( BinaryWriter binfile = new BinaryWriter( File.Open( Path.Combine( Directory.GetCurrentDirectory( ), "setupel3.bin" ), FileMode.Create ) ) )
  143. 			{
  144. 				binfile.Write( bindata );
  145. 			}
  146.  
  147. 			return 0;
  148. 		}
  149.  
  150.  
  151. 		static void ReadConfig( )
  152. 		{
  153. 		}
  154.  
  155.  
  156. 		static int ShowHelp( params string[] errmsg )
  157. 		{
  158. 			/*
  159. 			EnergyLoggerConfig.exe,  Version 1.00
  160. 			Create a binary configuration file for the VoltCraft Energy Logger 3500 or 4000
  161.  
  162. 			Usage:    EnergyLoggerConfig /dt:daytimetariff /nt:nighttimetariff [ options ]
  163.  
  164. 			Where:    daytimetariff      is the day time tariff for 1 kWh
  165. 			          nighttimetariff    is the night time tariff for 1 kWh
  166.  
  167. 			Options:  /ID:deviceID       set device ID (0..9; default: 1)
  168. 			          /12H               use 12 hour clock (default: 24 hour clock)
  169. 			          /MDY               use date format M/D/Y (default: D-M-Y)
  170. 			          /F:minutes         set time to specified number of minutes in future
  171. 			                             to allow sufficient time to load the SD card into
  172. 			                             the device (0..60; default: 1 minute)
  173. 			          /C:currency        set currency: G = GB Pound, S = Swiss Frank,
  174. 			                             U = US Dollar, E = Euro (default: Euro)
  175.  
  176. 			The settings will be written to a binary file named "setupel3.bin", located in
  177. 			the current directory. It should be written to SD card and loaded into the
  178. 			device in the specified number of minutes (/F switch) for optimal accuracy of
  179. 			cost calculations. Once loaded, the device will erase the file from the SD card.
  180. 			Detailed information on the setupel3.bin file format can be found at
  181. 			http://wiki.td-er.nl/index.php?title=Energy_Logger_3500
  182.  
  183. 			Written by Rob van der Woude
  184. 			http://www.robvanderwoude.com
  185. 			*/
  186.  
  187. 			if ( errmsg.Length > 0 )
  188. 			{
  189. 				List<string> errargs = new List<string>( errmsg );
  190. 				errargs.RemoveAt( 0 );
  191. 				Console.Error.WriteLine( );
  192. 				Console.ForegroundColor = ConsoleColor.Red;
  193. 				Console.Error.Write( "ERROR:\t" );
  194. 				Console.ForegroundColor = ConsoleColor.White;
  195. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  196. 				Console.ResetColor( );
  197. 			}
  198.  
  199. 			Console.Error.WriteLine( );
  200.  
  201. 			Console.Error.WriteLine( "EnergyLoggerConfig.exe,  Version {0}", progver );
  202.  
  203. 			Console.Error.WriteLine( "Create a binary configuration file for the VoltCraft Energy Logger 3500 or 4000" );
  204.  
  205. 			Console.Error.WriteLine( );
  206.  
  207. 			Console.Error.Write( "Usage:    " );
  208. 			Console.ForegroundColor = ConsoleColor.White;
  209. 			Console.Error.WriteLine( "EnergyLoggerConfig /dt:daytimetariff /nt:nighttimetariff [ options ]" );
  210. 			Console.ResetColor( );
  211.  
  212. 			Console.Error.WriteLine( );
  213.  
  214. 			Console.Error.Write( "Where:    " );
  215. 			Console.ForegroundColor = ConsoleColor.White;
  216. 			Console.Error.Write( "daytimetariff" );
  217. 			Console.ResetColor( );
  218. 			Console.Error.WriteLine( "      is the day time tariff for 1 kWh" );
  219.  
  220. 			Console.ForegroundColor = ConsoleColor.White;
  221. 			Console.Error.Write( "          nighttimetariff" );
  222. 			Console.ResetColor( );
  223. 			Console.Error.WriteLine( "    is the night time tariff for 1 kWh" );
  224.  
  225. 			Console.Error.WriteLine( );
  226.  
  227. 			Console.Error.Write( "Options:  " );
  228. 			Console.ForegroundColor = ConsoleColor.White;
  229. 			Console.Error.Write( "/ID:deviceID" );
  230. 			Console.ResetColor( );
  231. 			Console.Error.WriteLine( "       set device ID (0..9; default: 1)" );
  232.  
  233. 			Console.ForegroundColor = ConsoleColor.White;
  234. 			Console.Error.Write( "          /12H" );
  235. 			Console.ResetColor( );
  236. 			Console.Error.WriteLine( "               use 12 hour clock (default: 24 hour clock)" );
  237.  
  238. 			Console.ForegroundColor = ConsoleColor.White;
  239. 			Console.Error.Write( "          /MDY" );
  240. 			Console.ResetColor( );
  241. 			Console.Error.WriteLine( "               use date format M/D/Y (default: D-M-Y)" );
  242.  
  243. 			Console.ForegroundColor = ConsoleColor.White;
  244. 			Console.Error.Write( "          /F:minutes" );
  245. 			Console.ResetColor( );
  246. 			Console.Error.WriteLine( "         set time to specified number of minutes in future" );
  247.  
  248. 			Console.Error.WriteLine( "                             to allow sufficient time to load the SD card into" );
  249.  
  250. 			Console.Error.WriteLine( "                             the device (0..60; default: 1 minute)" );
  251.  
  252. 			Console.ForegroundColor = ConsoleColor.White;
  253. 			Console.Error.Write( "          /C:currency" );
  254. 			Console.ResetColor( );
  255. 			Console.Error.Write( "        set currency: " );
  256. 			Console.ForegroundColor = ConsoleColor.White;
  257. 			Console.Error.Write( "G" );
  258. 			Console.ResetColor( );
  259. 			Console.Error.Write( " = GB Pound, " );
  260. 			Console.ForegroundColor = ConsoleColor.White;
  261. 			Console.Error.Write( "S" );
  262. 			Console.ResetColor( );
  263. 			Console.Error.WriteLine( " = Swiss Frank," );
  264.  
  265. 			Console.ForegroundColor = ConsoleColor.White;
  266. 			Console.Error.Write( "                             U" );
  267. 			Console.ResetColor( );
  268. 			Console.Error.Write( " = US Dollar, " );
  269. 			Console.ForegroundColor = ConsoleColor.White;
  270. 			Console.Error.Write( "E" );
  271. 			Console.ResetColor( );
  272. 			Console.Error.WriteLine( " = Euro (default: Euro)" );
  273.  
  274. 			Console.Error.WriteLine( );
  275.  
  276. 			Console.Error.WriteLine( "The settings will be written to a binary file named \"setupel3.bin\", located in" );
  277.  
  278. 			Console.Error.WriteLine( "the current directory. It should be written to SD card and loaded into the" );
  279.  
  280. 			Console.Error.WriteLine( "device in the specified number of minutes (/F switch) for optimal accuracy of" );
  281.  
  282. 			Console.Error.WriteLine( "cost calculations. Once loaded, the device will erase the file from the SD card." );
  283.  
  284. 			Console.Error.WriteLine( "Detailed information on the setupel3.bin file format can be found at" );
  285.  
  286. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  287. 			Console.Error.WriteLine( "http://wiki.td-er.nl/index.php?title=Energy_Logger_3500" );
  288. 			Console.ResetColor( );
  289.  
  290. 			Console.Error.WriteLine( );
  291.  
  292. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  293.  
  294. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  295.  
  296. 			return 1;
  297. 		}
  298. 	}
  299.  
  300.  
  301. 	public enum Currency
  302. 	{
  303. 		GBPound = 1,
  304. 		SwissFrank = 2,
  305. 		USDollar = 4,
  306. 		Euro = 8
  307. 	}
  308.  
  309.  
  310. 	public enum DateFormat
  311. 	{
  312. 		DateMdy = 1,
  313. 		DateDmy = 2
  314. 	}
  315.  
  316.  
  317. 	public enum TimeFormat
  318. 	{
  319. 		Clock12Hour = 1,
  320. 		Clock24Hour = 2
  321. 	}
  322. }
  323.  

page last modified: 2024-04-16; loaded in 0.0103 seconds