Commit fe7a6ef8 LN

增加数据上传到共享文件夹功能

1 个父辈 669a1644
...@@ -12,6 +12,16 @@ ...@@ -12,6 +12,16 @@
<!--二维码参数文件所在路径,文件名与二维码类型名一样--> <!--二维码参数文件所在路径,文件名与二维码类型名一样-->
<add key="CodeParamPath" value="Conifg\"/> <add key="CodeParamPath" value="Conifg\"/>
<add key="CodeCount" value="3"/> <add key="CodeCount" value="3"/>
<!--共享文件夹配置-->
<add key ="SMB_Path" value =""/>
<add key ="SMB_UserName" value =""/>
<add key ="SMB_Pwd" value =""/>
<!--标题节点配置,需要和标签模板里面的key对应-->
<add key ="FileTitle_RID" value ="NB"/>
<add key ="FileTitle_PN" value ="P/N"/>
<add key ="FileTitle_QTY" value ="QTY"/>
<!--StorageUnit列的值-->
<add key ="SMB_StorageUnitValue" value ="SMF"/>
</appSettings> </appSettings>
<log4net> <log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
......
...@@ -62,6 +62,7 @@ namespace AutoScanAndLabel ...@@ -62,6 +62,7 @@ namespace AutoScanAndLabel
this.listView1 = new System.Windows.Forms.ListView(); this.listView1 = new System.Windows.Forms.ListView();
this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox();
this.panel1 = new System.Windows.Forms.Panel(); this.panel1 = new System.Windows.Forms.Panel();
this.lastFile = new System.Windows.Forms.Label();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
this.tabPage1.SuspendLayout(); this.tabPage1.SuspendLayout();
this.groupBox4.SuspendLayout(); this.groupBox4.SuspendLayout();
...@@ -202,6 +203,7 @@ namespace AutoScanAndLabel ...@@ -202,6 +203,7 @@ namespace AutoScanAndLabel
// //
// tabPage1 // tabPage1
// //
this.tabPage1.Controls.Add(this.lastFile);
this.tabPage1.Controls.Add(this.groupBox4); this.tabPage1.Controls.Add(this.groupBox4);
this.tabPage1.Controls.Add(this.groupBox3); this.tabPage1.Controls.Add(this.groupBox3);
this.tabPage1.Controls.Add(this.cb_IgnoreSafecheck); this.tabPage1.Controls.Add(this.cb_IgnoreSafecheck);
...@@ -210,7 +212,7 @@ namespace AutoScanAndLabel ...@@ -210,7 +212,7 @@ namespace AutoScanAndLabel
this.tabPage1.Controls.Add(this.pictureBox1); this.tabPage1.Controls.Add(this.pictureBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 30); this.tabPage1.Location = new System.Drawing.Point(4, 30);
this.tabPage1.Name = "tabPage1"; this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(1025, 438); this.tabPage1.Size = new System.Drawing.Size(1025, 468);
this.tabPage1.TabIndex = 0; this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "状体"; this.tabPage1.Text = "状体";
this.tabPage1.UseVisualStyleBackColor = true; this.tabPage1.UseVisualStyleBackColor = true;
...@@ -348,7 +350,7 @@ namespace AutoScanAndLabel ...@@ -348,7 +350,7 @@ namespace AutoScanAndLabel
this.tabControl1.Location = new System.Drawing.Point(0, 188); this.tabControl1.Location = new System.Drawing.Point(0, 188);
this.tabControl1.Name = "tabControl1"; this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0; this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1033, 472); this.tabControl1.Size = new System.Drawing.Size(1033, 502);
this.tabControl1.TabIndex = 1; this.tabControl1.TabIndex = 1;
// //
// btn_stop // btn_stop
...@@ -404,12 +406,20 @@ namespace AutoScanAndLabel ...@@ -404,12 +406,20 @@ namespace AutoScanAndLabel
this.panel1.Size = new System.Drawing.Size(1029, 619); this.panel1.Size = new System.Drawing.Size(1029, 619);
this.panel1.TabIndex = 6; this.panel1.TabIndex = 6;
// //
// lastFile
//
this.lastFile.AutoSize = true;
this.lastFile.Location = new System.Drawing.Point(14, 435);
this.lastFile.Name = "lastFile";
this.lastFile.Size = new System.Drawing.Size(0, 21);
this.lastFile.TabIndex = 11;
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White; this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(1033, 662); this.ClientSize = new System.Drawing.Size(1033, 692);
this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox2);
this.Controls.Add(this.btn_stop); this.Controls.Add(this.btn_stop);
this.Controls.Add(this.btn_run); this.Controls.Add(this.btn_run);
...@@ -472,6 +482,7 @@ namespace AutoScanAndLabel ...@@ -472,6 +482,7 @@ namespace AutoScanAndLabel
private System.Windows.Forms.ToolStripMenuItem 打印设置ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 打印设置ToolStripMenuItem;
private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label lastFile;
} }
} }
...@@ -365,6 +365,7 @@ namespace AutoScanAndLabel ...@@ -365,6 +365,7 @@ namespace AutoScanAndLabel
lvi.ForeColor = Color.Red; lvi.ForeColor = Color.Red;
listView1.Items.Add(lvi); listView1.Items.Add(lvi);
} }
lastFile.Text = DataUploadUtil.lastFileInfo;
this.ResumeLayout(true); this.ResumeLayout(true);
} }
bool userpause = false; bool userpause = false;
......
...@@ -70,6 +70,7 @@ ...@@ -70,6 +70,7 @@
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="util\CTcpClient.cs" /> <Compile Include="util\CTcpClient.cs" />
<Compile Include="util\SmbFileContainer.cs" />
<Compile Include="util\TcpServer.cs" /> <Compile Include="util\TcpServer.cs" />
<Compile Include="util\UdpServer.cs" /> <Compile Include="util\UdpServer.cs" />
<Compile Include="util\UnifiedDataHandler.cs" /> <Compile Include="util\UnifiedDataHandler.cs" />
......
...@@ -62,5 +62,17 @@ namespace OnlineStore.Common ...@@ -62,5 +62,17 @@ namespace OnlineStore.Common
public static string RStation = "RStation"; public static string RStation = "RStation";
public static string Standby = "Standby"; public static string Standby = "Standby";
public static string STD_IP = "STD_IP"; public static string STD_IP = "STD_IP";
public static string SMB_Path = "SMB_Path";
public static string SMB_UserName = "SMB_UserName";
public static string SMB_Pwd = "SMB_Pwd";
public static string FileTitle_RID = "FileTitle_RID";
public static string FileTitle_PN = "FileTitle_PN";
public static string FileTitle_QTY= "FileTitle_QTY";
public static string SMB_StorageUnitValue = "SMB_StorageUnitValue";
} }
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore
{
/// <summary>
/// SMB共享文件夹
/// </summary>
public class SmbFileContainer
{
private readonly NetworkCredential networkCredential;
// Path to shared folder:
private readonly string networkPath;
/// <summary>
/// SMB共享文件夹
/// </summary>
/// <param name="connectionInfo">连接信息</param>
public SmbFileContainer(SmbFileConnectionInfo connectionInfo)
{
networkPath = connectionInfo.NetworkPath;
networkCredential = new NetworkCredential(connectionInfo.UserName, connectionInfo.Password, connectionInfo.Domain);
}
/// <summary>
/// 检查是否是有效连接
/// </summary>
/// <returns></returns>
public bool IsValidConnection()
{
using (var network = new NetworkConnection($"{networkPath}", networkCredential))
{
var result = network.Connect();
return result == 0;
}
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="targetFile"></param>
/// <param name="content"></param>
public void CreateFile(string targetFile, string content)
{
using (var network = new NetworkConnection(networkPath, networkCredential))
{
network.Connect();
var file = System.IO.Path.Combine(networkPath, targetFile);
System.IO.FileStream fs = System.IO.File.Create(file);
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs))
{
sw.Write(content);
}
}
}
}
/// <summary>
/// SMB共享文件夹连接信息
/// </summary>
public class SmbFileConnectionInfo
{
/// <summary>
/// 网络路径
/// 例:@"\\192.168.101.125\Monitor\ScanResult"
/// </summary>
public string NetworkPath { get; set; }
/// <summary>
/// 用户名
/// 例:administrator
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// 例:123
/// </summary>
public string Password { get; set; }
/// <summary>
/// 域名
/// </summary>
public string Domain { get; set; } = "";
}
[StructLayout(LayoutKind.Sequential)]
class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
class NetworkConnection : IDisposable
{
private string _networkName;
private NetworkCredential _credentials;
public NetworkConnection(string networkName, NetworkCredential credentials)
{
_networkName = networkName;
_credentials = credentials;
}
public int Connect()
{
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = _networkName
};
var userName = string.IsNullOrEmpty(_credentials.Domain)
? _credentials.UserName
: string.Format(@"{0}\{1}", _credentials.Domain, _credentials.UserName);
var result = WNetAddConnection2(
netResource,
_credentials.Password,
userName,
0);
return result;
}
~NetworkConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}
enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
};
enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
};
}
...@@ -133,6 +133,10 @@ namespace DeviceLibrary ...@@ -133,6 +133,10 @@ namespace DeviceLibrary
{ {
MiddleMoveInfo.NextMoveStep(MoveStep.M08); MiddleMoveInfo.NextMoveStep(MoveStep.M08);
UnifiedDataHandler.RecordPrintNg(false, true, out string[] strarrys); UnifiedDataHandler.RecordPrintNg(false, true, out string[] strarrys);
//数据保存到共享文件夹
DataUploadUtil.LabelUpload(MiddleMoveInfo.MoveParam.Keyword);
} }
else if (MiddleMoveInfo.CanWhileCount <= 0) else if (MiddleMoveInfo.CanWhileCount <= 0)
{ {
......
...@@ -117,6 +117,7 @@ ...@@ -117,6 +117,7 @@
<Compile Include="AutoScanAndLabel\IRobot.cs" /> <Compile Include="AutoScanAndLabel\IRobot.cs" />
<Compile Include="AutoScanAndLabel\MainMachine.cs" /> <Compile Include="AutoScanAndLabel\MainMachine.cs" />
<Compile Include="AGVManager.cs" /> <Compile Include="AGVManager.cs" />
<Compile Include="DeviceLibrary\DataUploadUtil.cs" />
<Compile Include="DeviceLibrary\ElectricGripper.cs" /> <Compile Include="DeviceLibrary\ElectricGripper.cs" />
<Compile Include="DeviceLibrary\IAxisManager.cs" /> <Compile Include="DeviceLibrary\IAxisManager.cs" />
<Compile Include="DeviceLibrary\AxisManager.cs" /> <Compile Include="DeviceLibrary\AxisManager.cs" />
......
using HalconDotNet;
using OnlineStore;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public class DataUploadUtil
{
private static string smb_path = ConfigAppSettings.GetValue(Setting_Init.SMB_Path);
private static string smb_uname = ConfigAppSettings.GetValue(Setting_Init.SMB_UserName);
private static string smb_pwd =ConfigAppSettings.GetValue(Setting_Init.SMB_Pwd);
private static string title_ri = ConfigAppSettings.GetValue(Setting_Init.FileTitle_RID);
private static string title_pn = ConfigAppSettings.GetValue(Setting_Init.FileTitle_PN);
private static string title_qty = ConfigAppSettings.GetValue(Setting_Init.FileTitle_QTY);
private static string storage_value = ConfigAppSettings.GetValue(Setting_Init.SMB_StorageUnitValue);
//[2024-06-18 23:23:24,907][24] INFO 调用 PrintLabel 打印标签 StartPrintLabel
// ,[PON=101003292, POL=00050, QTY=10000, MPN=CL05B104KP5NNNC,
// MPG=SAMSUNG EM, D/C=31.01.20, P/N=1012004106, S/N=20230901, SHP=00052120, LOT=Y233466517, NB=SA001256] 开始
public static string lastFileInfo = "";
private static SmbFileContainer fileContainer = null;
static DataUploadUtil()
{
Init();
}
public static void Init()
{
if (smb_path == "")
{
LogUtil.info($"未配置共享文件夹,不需要连接");
return;
}
SmbFileConnectionInfo con = new SmbFileConnectionInfo();
con.NetworkPath = smb_path;
con.UserName = smb_uname;
con.Password = smb_pwd;
fileContainer = new SmbFileContainer(con);
bool isV = fileContainer.IsValidConnection();
LogUtil.info($"共享文件夹{smb_path}、{smb_uname}、{smb_pwd}连接结果:{isV}");
if (title_ri == "")
{
title_ri = "ReelID";
}
if (title_pn == "")
{
title_pn = "PartNumber";
}
if (title_qty == "")
{
title_qty = "QuantityOverride";
}
if (storage_value == "")
{
storage_value = "SMF";
}
}
private static string GetValue(Dictionary<string, string> dictionary, string key, string defV = "") {
if (dictionary.ContainsKey(key))
{
return dictionary[key];
}
return defV;
}
public static void LabelUpload(Dictionary<string, string> data )
{
Task.Factory.StartNew(() =>
{
try
{
if (fileContainer != null)
{
}
else if (fileContainer.IsValidConnection())
{
string rid = GetValue(data, title_ri);
string pn = GetValue(data, title_pn);
string qty = GetValue(data, title_qty);
//string s = GetValue(data, storage_value);
if (rid != "")
{
string title = "ReelID,PartNumber,Vendor,Lot,UserData1,UserData2,UserData3,UserData4,UserData5,InitialQuantity,MSDLevel,MSDInitialFloorTime ,MSDBagSealDate,MarketUsage,QuantityOverride,ShelfTime,SPMaterialName,WarningLimit,MaximumLimit,Comments,WarmupTime,StorageUnit,SubStorageUnit,LocationOverride,ExpirationDate,ManufacturingDate,PartClass,PSDOverride,AltPartNumber";
string fileName = rid + "-scan.csv";
List<string> rows = new List<string>();
string[] titleArray = title.Split(',');
int col = titleArray.Length;
for (int index = 0; index < col; index++)
{
string tilte = titleArray[index];
if (title == "ReelID")
{
rows.Add(rid);
}
else if (title == "PartNumber")
{
rows.Add(pn);
}
else if (title == "InitialQuantity")
{
rows.Add(qty);
}
else if (title == "StorageUnit")
{
rows.Add(storage_value);
}
else
{
rows.Add("");
}
}
string line = String.Join(",", rows);
string con = title + "\r\n" + line;
fileContainer.CreateFile(fileName, con);
lastFileInfo="["+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]: " + fileName+"\t"+line;
LogUtil.error($"共享文件夹{smb_path} 创建文件{fileName}完成 ");
}
else
{
var kv = data.Select(k => $"{k.Key}={k.Value}").ToArray();
LogUtil.error($"调用 LabelUpload 未找到RI,key={title_ri}, Data=[" + string.Join(", ", kv) + "] ");
}
}
else
{
LogUtil.error($"共享文件夹{smb_path}、{smb_uname}、{smb_pwd}连接失败 ");
}
}
catch (Exception ex)
{
LogUtil.error("LabelDataProcess 出错:" + ex.ToString());
}
});
}
}
}
...@@ -421,12 +421,12 @@ namespace OnlineStore.LoadCSVLibrary ...@@ -421,12 +421,12 @@ namespace OnlineStore.LoadCSVLibrary
/// <summary> /// <summary>
/// PRO,0,贴标覆盖X轴偏移量,Label_XOffset,200,,,,,,,,,,,, /// PRO,0,贴标覆盖X轴偏移量,Label_XOffset,200,,,,,,,,,,,,
/// </summary> /// </summary>
[ConfigProAttribute("Label_XOffset")] [ConfigProAttribute("Label_XOffset",false)]
public int Label_XOffset { get; set; } public int Label_XOffset { get; set; }
/// <summary> /// <summary>
/// PRO,0,贴标覆盖X轴偏移量,Label_YOffset,200,,,,,,,,,,,, /// PRO,0,贴标覆盖X轴偏移量,Label_YOffset,200,,,,,,,,,,,,
/// </summary> /// </summary>
[ConfigProAttribute("Label_YOffset")] [ConfigProAttribute("Label_YOffset",false )]
public int Label_YOffset { get; set; } public int Label_YOffset { get; set; }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!