Skip to content

Commit

Permalink
Code Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gieldid committed Jan 19, 2022
1 parent d55dbbf commit 03540b2
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DatawoodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Drawing;

namespace csvModule
namespace DatawoodGH
{
public class DatawoodInfo : GH_AssemblyInfo
{
Expand Down
2 changes: 1 addition & 1 deletion Local/CsvCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Globalization;
using System.IO;

namespace csvModule
namespace DatawoodGH
{
public class CsvCreator : LocalComponent
{
Expand Down
2 changes: 1 addition & 1 deletion Local/DataWoodObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using CsvHelper.Configuration;
using CsvHelper.Configuration.Attributes;

namespace csvModule
namespace DatawoodGH
{
public class DataWoodObject
{
Expand Down
2 changes: 1 addition & 1 deletion Network/ApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Grasshopper.Kernel;
using Rhino.Geometry;

namespace CSVModule
namespace DatawoodGH.Network
{
public class ApiCall : NetworkComponent
{
Expand Down
2 changes: 1 addition & 1 deletion Network/FtpTransfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Renci.SshNet;
using Rhino.Geometry;

namespace CSVModule
namespace DatawoodGH.Network
{
public class FtpTransfer : NetworkComponent
{
Expand Down
76 changes: 76 additions & 0 deletions Network/SocketConnection/ModFileObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatawoodGH.Utility;

namespace DatawoodGH.Network.SocketConnection
{
public class ModFileObject
{
public List<string> MovesFull { get; private set; }
public Dictionary<string, string> Speeds { get; private set; }
public Dictionary<string, string> Zones { get; private set; }
public List<MoveObject> Voves { get; set; }

/// <summary>
/// Reads the modfile and puts the Moves, speeds and zones in their respective variables.
/// </summary>
/// <param name="path">path of the modfile</param>
public ModFileObject(string path) {
MovesFull = new List<string>();
Speeds = new Dictionary<string, string>();
Zones = new Dictionary<string, string>();
ReadModFile(path);
}

private void ReadModFile(string path) {
string[] lines = System.IO.File.ReadAllLines(path);
foreach (var line in lines)
{
ReadMoves(line);
ReadSpeeds(line);
ReadZones(line);
}
}

private void ReadMoves(string line) {
if (line.Contains("MoveL") || line.Contains("MoveAbsJ"))
{
MovesFull.Add(line);
}
}

private void ReadSpeeds(string line) {
if (line.Contains("speeddata")) {
var startName = Utils.GetNthIndex(line, 'S', 3);
var endName = line.IndexOf(':');
var speedName = line.Substring(startName, endName - startName);

var startData = line.IndexOf('[');
var endData = line.IndexOf(']') + 1;

var speedData = line.Substring(startData, endData - startData);

Speeds.Add(speedName, speedData);
}
}

private void ReadZones(string line) {
if (line.Contains("zonedata"))
{
var startName = line.IndexOf('Z');
var endName = line.IndexOf(':');
var zoneName = line.Substring(startName, endName - startName);

var startData = line.IndexOf('[');
var endData = line.IndexOf(']') + 1;

var zoneData = line.Substring(startData, endData - startData);

Speeds.Add(zoneName, zoneData);
}
}
}
}
26 changes: 26 additions & 0 deletions Network/SocketConnection/MoveAbsJObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatawoodGH.Utility;

namespace DatawoodGH.Network.SocketConnection
{
public class MoveAbsJObject : MoveObject
{
public string RobJoint { get; set; }
public MoveAbsJObject(string line, Dictionary<string, string> speeds, Dictionary<string, string> zones) : base(line, speeds, zones)
{
ReadRobJoint(line);
}

private void ReadRobJoint(string line) {
int openBracket = Utils.GetNthIndex(line, '[', 2);
int closeBracket = line.IndexOf(']') + 1;

RobJoint = line.Substring(openBracket, closeBracket - openBracket);
}

}
}
36 changes: 36 additions & 0 deletions Network/SocketConnection/MoveLObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatawoodGH.Utility;

namespace DatawoodGH.Network.SocketConnection
{
public class MoveLObject : MoveObject
{
public string Pos { get; set; }
public string Orient { get; set; }

public MoveLObject(string line, Dictionary<string, string> speeds, Dictionary<string, string> zones) : base(line, speeds, zones)
{
ReadPos(line);
ReadOrient(line);
}

private void ReadPos(string line) {
int openBracket = Utils.GetNthIndex(line, '[', 2);
int closeBracket = line.IndexOf(']') + 1;

Pos = line.Substring(openBracket, closeBracket - openBracket);
}

private void ReadOrient(string line) {
int openBracket = Utils.GetNthIndex(line, '[', 3);
int closeBracket = Utils.GetNthIndex(line, ']', 2) + 1;

Orient = line.Substring(openBracket, closeBracket - openBracket);
}

}
}
58 changes: 58 additions & 0 deletions Network/SocketConnection/MoveObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;

namespace DatawoodGH.Network.SocketConnection
{
public abstract class MoveObject
{


public string Name { get; set; }
public string Speed { get; set; }
public string Zone { get; set; }
public string ExternalJoint { get; set; }

public MoveObject(string line, Dictionary<string , string> speeds, Dictionary<string, string> zones) {
ReadExternalJoint(line);
ReadSpeed(line, speeds);
ReadZone(line, zones);
}

private void ReadExternalJoint(string line) {
int openBracket = line.LastIndexOf('[');
int closeBracket = line.LastIndexOf(']');
ExternalJoint = line.Substring(openBracket, closeBracket - openBracket);
}

private void ReadSpeed(string line, Dictionary<string, string> speeds) {
string[] values = line.Split(',');
string speedKey = null;
foreach (var value in values)
{
if (value.Contains("Speed"))
{
speedKey = value;
}
}

speeds.TryGetValue(speedKey, out string speedValue);

Speed = speedValue;
}

private void ReadZone(string line, Dictionary<string, string> zones) {
string[] values = line.Split(',');
string zoneKey = null;
foreach (var value in values)
{
if (value.Contains("Zone"))
{
zoneKey = value;
}
}

zones.TryGetValue(zoneKey, out string zoneValue);

Zone = zoneValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using System.Net.Sockets;
using System.Net;
using System.Text;
using DatawoodGH.Utils;
using DatawoodGH.Utility;
using DatawoodGH.Properties;

namespace CSVModule.Network
namespace DatawoodGH.Network.SocketConnection
{
public class SocketClient : NetworkComponent
{
Expand Down Expand Up @@ -77,9 +77,9 @@ protected override void SolveInstance(IGH_DataAccess DA)
DA.GetData("Run", ref run);

if (run) {
List<string> targets = ReadModFile(path);
ModFileObject mod = new ModFileObject(path);
Socket client = SocketConnection(ip, port);
SendTargets(client, targets);
SendTargets(client, mod.MovesFull);
CloseConnection(client);
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ private string GetSpeed(string RAPID) {
{
if (value.Contains("Speed"))
{
speed = value;
speed = value;
}
}
return speed;
Expand Down Expand Up @@ -220,11 +220,13 @@ private string GetExtJoint(string RAPID) {
private List<string> ReadModFile(string path) {
string[] lines = System.IO.File.ReadAllLines(path);
List<string> targets = new List<string>();
Dictionary<string,string> speeds = new Dictionary<string,string>();
foreach (var line in lines) {
if (line.Contains("MoveL") || line.Contains("MoveAbsJ")) {
targets.Add(line);
}
}

return targets;
}

Expand Down
2 changes: 1 addition & 1 deletion Utils/Utils.cs → Utility/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace DatawoodGH.Utils
namespace DatawoodGH.Utility
{
public static class Utils
{
Expand Down

0 comments on commit 03540b2

Please sign in to comment.