/// <summary> /// OnCustomCommand(): If you need to send a command to your /// service without the need for Remoting or Sockets, use /// this method to do custom methods. /// </summary> /// <param name="command">Arbitrary Integer between 128 & 256</param> protected override void OnCustomCommand(int command) { // A custom command can be sent to a service by using this method: //# int command = 128; //Some Arbitrary number between 128 & 256 //# ServiceController sc = new ServiceController("NameOfService"); //# sc.ExecuteCommand(command); log.Debug(LogHelp.LogText($"OnCustomCommand...({command})")); base.OnCustomCommand(command); }
/// <summary> /// Public Constructor for WindowsService. /// - Put all of your Initialization code here. /// </summary> public WindowsService() { var allOk = false; this.ServiceName = "StationMockService"; this.EventLog.Log = "Application"; // These Flags set whether or not to handle that specific // type of event. Set to true if you need it, false otherwise. this.CanHandlePowerEvent = true; this.CanHandleSessionChangeEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanStop = true; var setting = ConfigStatic.GetConfigSetting(); if (setting.UseConfigSetting) { socket = new ServerSocket(setting.HostIPAddress, setting.Port); log.Debug(LogHelp.LogText($"Socket Loaded {setting.HostIPAddress}:{setting.Port}")); allOk = true; } else if (setting.UseAutoIPconfig) { var ipAddresses = IPAddressResolver.GetList(); foreach (var item in ipAddresses) { ConfigStatic.HostIpAddress = item.Address; } ConfigStatic.HostPort = "3000"; ConfigStatic.UseConfigSetting = "true"; socket = new ServerSocket(setting.HostIPAddress, setting.Port); log.Info(LogHelp.LogText($"Socket Loaded {setting.HostIPAddress}:{setting.Port}")); allOk = true; } if (allOk) { log.Debug(LogHelp.LogText($"Start... Initialize")); socket.Initialize(); log.Debug(LogHelp.LogText($"Start... Stories")); log.Debug(LogHelp.LogText($"Start... Timer")); aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); aTimer.Interval = 5000; aTimer.Enabled = true; log.Debug(LogHelp.LogText($"Start... StartListening")); socket.StartListening(); log.Debug(LogHelp.LogText($"Start... OK!")); } }
/// <summary> /// Test data to pump /// Set how much the pump have dispenced in Currenct, with 2 decimals /// </summary> /// <param name="pumpNo">Pump number / pump Story</param> /// <param name="Amount">Amount of Currenct, with 2 decimals</param> public void SetAmount(int pumpNo, decimal Amount) { if (Pumps.ContainsKey(pumpNo)) { Pump pump = Pumps[pumpNo]; pump.SetAmount(Amount); if (Pumps.ContainsKey(pumpNo)) { Pumps.Remove(pumpNo); } Pumps.Add(pumpNo, pump); log.Info(LogHelp.LogText($"Pump #{pumpNo:00}: Amount:{Pumps[pumpNo].Amount:N2}RM")); } }
/// <summary> /// Set Volume on Pump, to change Volume of fuel the pump have used /// A way to make test data on the pump for the test case to see data change /// </summary> /// <param name="pumpNo">Pump Number / Pump Story</param> /// <param name="Volume">How many liter use by pump, with 3 decimals</param> public void SetVolume(int pumpNo, decimal Volume) { if (Pumps.ContainsKey(pumpNo)) { Pump pump = Pumps[pumpNo]; pump.Volume = Volume; if (Pumps.ContainsKey(pumpNo)) { Pumps.Remove(pumpNo); } Pumps.Add(pumpNo, pump); log.Info(LogHelp.LogText($"Pump #{pumpNo:00}: Volume:{Pumps[pumpNo].Volume:N3}L")); } }
/// <summary> /// Initializes the server socket /// </summary> public void Initialize() { try { LogHelp.log.Info(LogHelp.LogText($"Starting server at {hostIPAddress}:{port}")); tcpListener = new TcpListener(IPAddress.Parse(hostIPAddress), port); } catch (ArgumentNullException argNullEx) { throw new POCSocketException(argNullEx.Message, argNullEx); } catch (ArgumentOutOfRangeException argOutOfRangeEx) { throw new POCSocketException(argOutOfRangeEx.Message, argOutOfRangeEx); } catch (Exception generalEx) { throw new POCSocketException(generalEx.Message, generalEx); } }
/// <summary> /// /// </summary> public async Task <bool> StartListening() { if (tcpListener == null) { LogHelp.log.Error(LogHelp.LogText($"Something went wrong. Consider calling Initialize() method first")); throw new POCSocketException("Something went wrong. Consider calling Initialize() method first"); } tcpListener.Start(); LogHelp.localEndpoint = tcpListener.LocalEndpoint; LogHelp.log.Info(LogHelp.LogText($"Connection started at {tcpListener.LocalEndpoint}")); Action task = delegate() { while (true) { //try() //{ } //catch //{ } var tcpClient = tcpListener.AcceptTcpClient(); var clientId = Guid.NewGuid(); lock (lockObj) { clients[clientId] = tcpClient; } LogHelp.remoteEndPoint = tcpClient.Client.RemoteEndPoint; LogHelp.log.Info(LogHelp.LogText($"Client connected from {tcpClient.Client.RemoteEndPoint}")); var t = new Thread(HandleClients); t.Start(clientId); } ; }; await Task.Run(task); return(true); }
/// <summary> /// Making story lines move to next step in scripted story /// </summary> /// <param name="pumpNo">Pump Number / Pump Story</param> public void NextStepHistory(int pumpNo) { if (Pumps.ContainsKey(pumpNo)) { Pump pump = Pumps[pumpNo]; PumpStatusEnum OrgStatus = pump.PumpStatus; if (Story.stationStories.ContainsKey(pumpNo)) { pumpStories = Story.stationStories[pumpNo]; PumpStory pumpStory = pumpStories[Story.stationStoriesLine[pumpNo]]; DateTime tick = pump.PumpTime; int nextTick = pumpStory.NextTick; long pumpStatusTimeOut = pumpStory.PumpStatusTimeOut; // If NextTick is gt 0 then the story line will automatic do next step in the story linie // And move on the story for next event to happen. if (nextTick > 0) { if (DateTime.Compare(DateTime.Now, tick.AddSeconds(nextTick)) > 0) { Story.stationStoriesLine[pumpNo] = pumpStory.NextStoryLine; string storytext = pumpStory.Name; if (pumpStory.NextStatus.Equals(PumpStatusEnum.NozzlePickUp) || pumpStory.NextStatus.Equals(PumpStatusEnum.DispenseCompleted)) { if (!pumpStory.Amount.Equals(0)) { pump.Amount = pumpStory.Amount; storytext += $"|Amount:{pump.Amount}"; } if (!pumpStory.Volume.Equals(0)) { pump.Volume = pumpStory.Volume; storytext += $"|Volume:{pump.Volume}"; } if (!pumpStory.PctOfLimit.Equals(0)) { pump.Amount = pump.Limit * ((decimal)pumpStory.PctOfLimit / 100M); storytext += $"|Amount:{pump.Amount}"; } if (!pumpStory.Ppu.Equals(0)) { pump.Volume = pump.Amount / (decimal)pumpStory.Ppu; storytext += $"|Volume:{pump.Volume}"; } decimal vol = pump.Volume; vol = vol.Equals(0) ? 1 : vol; log.Debug(LogHelp.LogText( $"Pump #{pumpNo:00}: " + $"Status #{Enum.GetName(typeof(PumpStatusEnum), (int)pump.PumpStatus)}: " + $"Hose: {pump.Hose:0} " + $"Flag: {pump.Flag:0} " + $"Amount: {pump.Amount:N2}RM " + $"Volume: {pump.Volume:N3} " + $"Price pr unit: {(pump.Amount / (vol)):N3}RM " + $"Limit: {pump.Limit:N3}L" ) ); } pump.PumpStatus = pumpStory.NextStatus; if (Pumps.ContainsKey(pumpNo)) { Pumps.Remove(pumpNo); } Pumps.Add(pumpNo, pump); log.Info(LogHelp.LogText($"Pump #{pumpNo:00}: " + $"{Enum.GetName(typeof(PumpStatusEnum), (int)OrgStatus)} " + $" =) " + $"{Enum.GetName(typeof(PumpStatusEnum), (int)pumpStory.NextStatus)} " /*+*/ //$"\"{storytext}\"" ) ); } } // If pumpStatusTimeOut is gt 0 then the Storyline will TimeOut, when the time become more // than the timout value and reset the pump / story line to Zero / eg first step in story if (nextTick.Equals(0) && !pumpStatusTimeOut.Equals(0)) { if (DateTime.Compare(DateTime.Now, tick.AddSeconds(pumpStatusTimeOut)) > 0) { Story.stationStoriesLine[pumpNo] = 1; pump.ResetPump(); pump.PumpStatus = PumpStatusEnum.Idle; if (Pumps.ContainsKey(pumpNo)) { Pumps.Remove(pumpNo); } Pumps.Add(pumpNo, pump); log.Info(LogHelp.LogText($"Pump #{pumpNo:00}: " + $"{Enum.GetName(typeof(PumpStatusEnum), (int)OrgStatus)} " + $" =) " + $"{Enum.GetName(typeof(PumpStatusEnum), (int)pumpStory.NextStatus)} " /*+*/ //$"\"{pumpStory.Name}\"" ) ); } } } } }
public void SetStatus(int pumpNo, PumpStatusEnum Status) { if (Pumps.ContainsKey(pumpNo)) { Pump pump = Pumps[pumpNo]; PumpStatusEnum OrgStatus = pump.PumpStatus; string old = Enum.GetName(typeof(PumpStatusEnum), (int)OrgStatus); string ny = Enum.GetName(typeof(PumpStatusEnum), (int)Status); // Pump #1: Idle => Reserved" if (Story.stationStories.ContainsKey(pumpNo)) { pumpStories = Story.stationStories[pumpNo]; PumpStory pumpStory = pumpStories[Story.stationStoriesLine[pumpNo]]; if (Status.Equals(PumpStatusEnum.Idle) && pumpStory.CanResetPump) { pump.ResetPump(); Story.stationStoriesLine[pumpNo] = 1; } if (Status.Equals(pumpStory.NextStatus)) { pump.PumpStatus = Status; Story.stationStoriesLine[pumpNo] = pumpStory.NextStoryLine; } if (Pumps.ContainsKey(pumpNo)) { Pumps.Remove(pumpNo); } Pumps.Add(pumpNo, pump); string storyText = ""; switch (pump.PumpStatus) { case PumpStatusEnum.Authorized: if (OrgStatus.Equals(PumpStatusEnum.Reserved)) { storyText = $"| ({pump.Volume:N3}L or {pump.Limit:N2}RM)"; } break; case PumpStatusEnum.Idle: if (OrgStatus.Equals(PumpStatusEnum.DispenseCompleted)) { storyText = $"| ({pump.Volume:N3}L or {pump.Amount:N2}RM)"; } break; default: break; } old = Enum.GetName(typeof(PumpStatusEnum), (int)OrgStatus); ny = Enum.GetName(typeof(PumpStatusEnum), (int)Status); log.Info(LogHelp.LogText($"Pump #{pumpNo:00}: {old} =) {ny} {storyText}")); } else { pump.PumpStatus = Status; if (Pumps.ContainsKey(pumpNo)) { Pumps.Remove(pumpNo); } Pumps.Add(pumpNo, pump); old = Enum.GetName(typeof(PumpStatusEnum), (int)OrgStatus); ny = Enum.GetName(typeof(PumpStatusEnum), (int)Status); log.Warn(LogHelp.LogText($"Pump #{pumpNo:00}: No Pump Storie: {old} =) {ny}")); } } else { log.Error(LogHelp.LogText($"Pump #{pumpNo:00}: Does not exist")); } }
/// <summary> /// Dispose of objects that need it here. /// </summary> /// <param name="disposing">Whether or not disposing is going on.</param> protected override void Dispose(bool disposing) { log.Debug(LogHelp.LogText($"Dispose...")); base.Dispose(disposing); }
/// <summary> /// OnSessionChange(): To handle a change event /// from a Terminal Server session. /// Useful if you need to determine /// when a user logs in remotely or logs off, /// or when someone logs into the console. /// </summary> /// <param name="changeDescription">The Session Change /// Event that occured.</param> protected override void OnSessionChange(SessionChangeDescription changeDescription) { log.Debug(LogHelp.LogText($"OnSessionChange...({changeDescription.ToString()})")); base.OnSessionChange(changeDescription); }
/// <summary> /// OnPowerEvent(): Useful for detecting power status changes, /// such as going into Suspend mode or Low Battery for laptops. /// </summary> /// <param name="powerStatus">The Power Broadcast Status /// (BatteryLow, Suspend, etc.)</param> protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { log.Debug(LogHelp.LogText($"OnPowerEvent...({powerStatus.ToString()})")); return(base.OnPowerEvent(powerStatus)); }
/// <summary> /// OnShutdown(): Called when the System is shutting down /// - Put code here when you need special handling /// of code that deals with a system shutdown, such /// as saving special data before shutdown. /// </summary> protected override void OnShutdown() { log.Debug(LogHelp.LogText($"OnShutdown...")); base.OnShutdown(); }
/// <summary> /// OnContinue(): Put your continue code here /// - Un-pause working threads, etc. /// </summary> protected override void OnContinue() { log.Debug(LogHelp.LogText($"OnContinue...")); base.OnContinue(); }
/// <summary> /// OnStop(): Put your stop code here /// - Stop threads, set final data, etc. /// </summary> protected override void OnStop() { log.Debug(LogHelp.LogText($"OnStop...")); base.OnStop(); }