Skip to content

Commit

Permalink
Updated MPS to switch between mqtt and opcua, and added a mqtt test a…
Browse files Browse the repository at this point in the history
…nd also a broker to the test suite
  • Loading branch information
Karatekuh committed Mar 23, 2024
1 parent ff6d544 commit a552e12
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 45 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Start Mosquitto
uses: namoshek/mosquitto-github-action@v1
with:
version: '1.6'
ports: '1883:1883'
container-name: 'mosquitto'
- name: Test
run: dotnet test Simulatortests/Simulatortests.csproj --no-build --verbosity normal
40 changes: 36 additions & 4 deletions Simulator/Configurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class Configurations
public uint WebguiPort { get; private set;}
public bool BarcodeScanner { get; private set; }


public Configurations()
{
MpsConfigs = new List<MpsConfig>();
Expand Down Expand Up @@ -357,8 +356,10 @@ public void LoadConfig(string path)
private static RefboxConfig? CreateRefboxConfig(YamlMappingNode refbox)
{
string? ip = null;
int publicSendPort = 0, publicRecvPort = 0, cyanSendPort = 0, cyanRecvPort = 0, magentaSendPort = 0, magentaRecvPort = 0, tcpPort = 0;
int publicSendPort = 0, publicRecvPort = 0, cyanSendPort = 0, cyanRecvPort = 0, magentaSendPort = 0, magentaRecvPort = 0, tcpPort = 0, broker_port = 1883;
var children = refbox.Children;
var broker_ip = "";
var mqtt_active = false;
// Step into the public information
//Console.WriteLine(children[0].Key.ToString());
var map = (YamlMappingNode)children[0].Value;
Expand Down Expand Up @@ -427,9 +428,31 @@ public void LoadConfig(string path)
}
}
break;
case "mqtt":
var mqttChild = ((YamlMappingNode)value).Children;
foreach (var (yamlNode, yamlNode1) in mqttChild)
{
switch (yamlNode.ToString().ToLower())
{
case "broker_ip":
broker_ip = yamlNode1.ToString().ToLower();
break;
case "broker_port":
broker_port = int.Parse(yamlNode1.ToString());
break;
case "active":
mqtt_active = bool.Parse(yamlNode1.ToString());
break;
default:
Console.WriteLine("Unknown key " + yamlNode.ToString());
return null;
}
}
break;
default:
Console.WriteLine("Unknown key " + key.ToString());
return null;

}

//Console.WriteLine(val);
Expand All @@ -439,7 +462,7 @@ public void LoadConfig(string path)
return null;
}
var config = new RefboxConfig(ip, tcpPort, publicSendPort, publicRecvPort, cyanSendPort, cyanRecvPort,
magentaSendPort, magentaRecvPort);
magentaSendPort, magentaRecvPort,broker_ip,broker_port ,mqtt_active);
return config;
}

Expand Down Expand Up @@ -537,8 +560,11 @@ public class RefboxConfig
public int CyanRecvPort { get; }
public int MagentaSendPort { get; }
public int MagentaRecvPort { get; }
public string BrokerIp { get; }
public int BrokerPort { get; }
public bool MqttMode { get; }

public RefboxConfig(string ip,int tcpPort, int publicSend, int publicRecv, int cyanSend, int cyanRecv, int magentaSend, int magentaRecv)
public RefboxConfig(string ip,int tcpPort, int publicSend, int publicRecv, int cyanSend, int cyanRecv, int magentaSend, int magentaRecv, string broker_ip = "localhost", int brokerPort = 1883, bool active = false)
{
IP = ip;
TcpPort = tcpPort;
Expand All @@ -548,6 +574,9 @@ public RefboxConfig(string ip,int tcpPort, int publicSend, int publicRecv, int c
CyanSendPort = cyanSend;
MagentaRecvPort = magentaRecv;
MagentaSendPort = magentaSend;
BrokerIp = broker_ip;
BrokerPort = brokerPort;
MqttMode = active;
}
public void PrintConfig()
{
Expand All @@ -560,6 +589,9 @@ public void PrintConfig()
Console.WriteLine("CyanSendPort = [" + CyanSendPort + "]");
Console.WriteLine("MagentaRecvPort = [" + MagentaRecvPort + "]");
Console.WriteLine("MagentaSendPort = [" + MagentaSendPort + "]");
Console.WriteLine("BrokerIp = [" + BrokerIp + "]");
Console.WriteLine("BrokerPort = [" + BrokerPort + "]");
Console.WriteLine("MqttMode = [" + MqttMode + "]");
}
}

Expand Down
3 changes: 2 additions & 1 deletion Simulator/MPS/MQTThelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum bits
private ManualResetEvent BasicEvent;
private MyLogger _myLogger;

public MQTThelper(string name, string url, ManualResetEvent inevent, ManualResetEvent basicevent, MyLogger logger)
public MQTThelper(string name, string url, int port, ManualResetEvent inevent, ManualResetEvent basicevent, MyLogger logger)
{
Name = name;
_myLogger = logger;
Expand All @@ -58,6 +58,7 @@ public void Connect()
_myLogger.Log("Starting connection!");
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer(Url)
.WithClientId(Name)
.WithWillQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce)
.Build();
Client.ConnectAsync(mqttClientOptions, CancellationToken.None).GetAwaiter().GetResult();
Expand Down
87 changes: 54 additions & 33 deletions Simulator/MPS/Mps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class Mps
public uint SlideCount { get; set; }
public string JsonInformation;
protected Configurations Config;
protected MQTThelper MqttHelper;
public MQTThelper MqttHelper;
protected bool MQTT;
public bool Working { get; private set; }
public enum MpsType
Expand Down Expand Up @@ -90,16 +90,16 @@ protected Mps(Configurations config, string name, int port, int id, Team team, b
GreenLight = new Light(LightColor.Green, LightEvent);
TaskDescription = "idle";
Config = config;
MQTT = true;
MQTT = Config.Refbox != null;
// Belt = new Belt(this, BeltEvent);
// Checking whether we have mockup mode or normal mode
/*if (Configurations.GetInstance().MockUp)
return;*/
try
{
if (MQTT)
if (Config.Refbox != null && Config.Refbox.MqttMode)
{
MqttHelper= new MQTThelper(Name, "mosquitto", InEvent, BasicEvent, MyLogger);
MqttHelper= new MQTThelper(Name, config.Refbox.BrokerIp,config.Refbox.BrokerPort, InEvent, BasicEvent, MyLogger);
MqttHelper.Connect();
MqttHelper.Setup();
}
Expand Down Expand Up @@ -128,38 +128,58 @@ public void Run()
public void ResetMachine()
{
TaskDescription = "Reseting!";
MqttHelper.InNodes.Status.SetBusy(true);
//InNodes.StatusNodes.busy.Value = true;
//Refbox.ApplyChanges(InNodes.StatusNodes.busy);
MqttHelper.InNodes.Status.SetEnable(false);
//InNodes.StatusNodes.enable.Value = false;
//Refbox.ApplyChanges(InNodes.StatusNodes.enable);
if(MQTT)
{
MqttHelper.InNodes.Status.SetBusy(true);
MqttHelper.InNodes.Status.SetEnable(false);
}
else
{
InNodes.StatusNodes.busy.Value = true;
Refbox.ApplyChanges(InNodes.StatusNodes.busy);
InNodes.StatusNodes.enable.Value = false;
Refbox.ApplyChanges(InNodes.StatusNodes.enable);
}
Thread.Sleep(1000);
MqttHelper.InNodes.SetActionId(0);
//InNodes.ActionId.Value = 0;
////Refbox.ApplyChanges(InNodes.ActionId);
MqttHelper.InNodes.SetData0(0);
//InNodes.Data0.Value = 0;
////Refbox.ApplyChanges(InNodes.Data0);
MqttHelper.InNodes.SetData1(0);
//InNodes.Data1.Value = 0;
////Refbox.ApplyChanges(InNodes.Data1);
MqttHelper.InNodes.SetError(0);
//InNodes.ByteError.Value = 0;
////Refbox.ApplyChanges(InNodes.ByteError);
MqttHelper.InNodes.Status.SetReady(false);
//InNodes.StatusNodes.ready.Value = false;
////Refbox.ApplyChanges(InNodes.StatusNodes.ready);
MqttHelper.InNodes.Status.SetError(false);
//InNodes.StatusNodes.error.Value = false;
////Refbox.ApplyChanges(InNodes.StatusNodes.error);
if(MQTT)
{
MqttHelper.InNodes.SetActionId(0);
MqttHelper.InNodes.SetData0(0);
MqttHelper.InNodes.SetData1(0);
MqttHelper.InNodes.SetError(0);
MqttHelper.InNodes.Status.SetReady(false);
MqttHelper.InNodes.Status.SetError(false);
}
else
{
InNodes.ActionId.Value = 0;
Refbox.ApplyChanges(InNodes.ActionId);
InNodes.Data0.Value = 0;
Refbox.ApplyChanges(InNodes.Data0);
InNodes.Data1.Value = 0;
Refbox.ApplyChanges(InNodes.Data1);
InNodes.ByteError.Value = 0;
Refbox.ApplyChanges(InNodes.ByteError);
InNodes.StatusNodes.ready.Value = false;
Refbox.ApplyChanges(InNodes.StatusNodes.ready);
InNodes.StatusNodes.error.Value = false;
Refbox.ApplyChanges(InNodes.StatusNodes.error);
}


ProductAtIn = null;
ProductAtOut = null;
ProductOnBelt = null;

MqttHelper.InNodes.Status.SetBusy(false);
//InNodes.StatusNodes.busy.Value = false;
//Refbox.ApplyChanges(InNodes.StatusNodes.busy);
if(MQTT)
{
MqttHelper.InNodes.Status.SetBusy(false);
}
else
{
InNodes.StatusNodes.busy.Value = false;
Refbox.ApplyChanges(InNodes.StatusNodes.busy);
}

TaskDescription = "Idle";
}

Expand Down Expand Up @@ -545,7 +565,8 @@ public void SerializeMachineToJson()

public void StopMachine()
{
Refbox.Stop();
if (Refbox != null)
Refbox.Stop();
}
}
}
2 changes: 1 addition & 1 deletion Simulator/MPS/MpsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Simulator.Utility;
 using Simulator.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
6 changes: 5 additions & 1 deletion Simulator/cfg/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ teams:
port: 2016
refbox:
public:
ip: 127.0.0.1
ip: 127.0.0.1 # is also used as mqtt broker address
# THe simulation communicates via tcp to get ground-truth
tcp: 4444
send: 4445
Expand All @@ -155,6 +155,10 @@ refbox:
magenta:
send: 4442
recv: 4447
mqtt:
broker_ip: localhost
broker_port: 1883
active: true
webui:
prefix: http
port: 8000
Expand Down
34 changes: 29 additions & 5 deletions Simulatortests/BaseStationTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Simulator.MPS;
using LlsfMsgs;
using System.Threading;
using LlsfMsgs;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Simulator;
using Opc.UaFx.Client;
using Simulator.MPS;
using Simulator.Utility;
using System.Threading;

namespace Simulatortests
{
Expand Down Expand Up @@ -63,6 +62,31 @@ public void OPC_DispenseBase()
testHelper.CloseConnection();
machine.StopMachine();
}

[TestMethod]
public void MQTT_DispenseBase()
{
var ip = "mosquitto";
var port = 1883;
var name = "C-BS";
var config = new Configurations();
config.Refbox = new RefboxConfig(ip:"127.0.0.1",0,0,0,0,0,0,0, ip, port, true);
var machine = new MPS_BS(config, name, port, 0, Team.Cyan, true);
var thread = new Thread(machine.Run);
thread.Start();
Thread.Sleep(500);
Assert.AreEqual(machine.MqttHelper.InNodes.ActionId, 0);
var testHelper = new MQTTTestHelper(ip, port, name);
if (!testHelper.CreateConnection())
Assert.Fail();
testHelper.SendTask((ushort)MPS_BS.BaseSpecificActions.GetBase, (ushort)1, (ushort)0);
Thread.Sleep(config.BSTaskDuration + 300);
Assert.IsNotNull(machine.ProductOnBelt);
testHelper.CloseConnection();
machine.StopMachine();
}


[TestMethod]
public void OPC_WrongDispenseBase()
{
Expand Down
Loading

0 comments on commit a552e12

Please sign in to comment.