| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace ErpServer
- {
- public partial class MainForm
- {
- Dictionary<string, ServerList> serverDics = new Dictionary<string, ServerList>();
- Dictionary<string, string> carnoLst = new Dictionary<string, string>();
- byte[] server_ip;
- int pServer = -1;
- private void carLedTimerCall(object obj)
- {
- if (carDics.Count < 1) return;
- var keys2 = redis.RedisHelper.getFilterKeys("carled_*");
- if (keys2 != null && keys2.Length > 0)
- {
- try
- {
- string txt = "";
- bool isSend = false;
- foreach (RedisKey rk in keys2)
- {
- isSend = false;
- string carno = rk.ToString().Replace("carled_", "");
- if (carDics.ContainsKey(carno))
- {
- txt = redis.RedisHelper.StringGet(rk.ToString());
- AppendLog(carno + "," + txt + "要发送LED屏" + "," + carDics[carno]);
- if (txt != null && txt.Length > 0)
- {
- string[] cArr = carDics[carno].Split(',');
- if (serverDics.ContainsKey(cArr[0]))
- {
- lock (sendObj)
- {
- isSend = false;
- isSend = sendCarLedText(serverDics[cArr[0]], cArr[0], txt, carno);
- if (isSend)
- {
- AppendLog("自动发送成功!" + cArr[0]);
- redis.RedisHelper.KeyDelete("carled_" + carno);
- }
- //else
- //{
- // isSend = sendCarLedText(serverDics[cArr[1]], cArr[1], txt, carno);
- // if (isSend)
- // {
- // AppendLog("自动发送成功!" + cArr[1]);
- // redis.RedisHelper.KeyDelete("carled_" + carno);
- // }
- //}
- }
- }
- else if (cArr.Length > 1 && serverDics.ContainsKey(cArr[1]))
- {
- lock (sendObj)
- {
- isSend = false;
- isSend = sendCarLedText(serverDics[cArr[1]], cArr[1], txt, carno);
- if (isSend)
- {
- AppendLog("自动发送成功!" + cArr[1]);
- redis.RedisHelper.KeyDelete("carled_" + carno);
- }
- //else
- //{
- // isSend = sendCarLedText(serverDics[cArr[0]], cArr[0], txt, carno);
- // if (isSend)
- // {
- // AppendLog("自动发送成功!" + cArr[0]);
- // redis.RedisHelper.KeyDelete("carled_" + carno);
- // }
- //}
- }
- }
- }
- else
- {
- AppendLog("车号:" + carno + ",led编号:" + carDics[carno]);
- helper.writeLog("车号:" + carno + ",led编号:" + carDics[carno] + ",找不到相应的卡号");
- return;
- }
- }
- else
- {
- AppendLog("车号:" + carno + ",找不到相应的卡号");
- helper.writeLog("车号:" + carno + ",找不到相应的卡号");
- }
- }
- }
- catch (Exception ex)
- {
- helper.writeLog("carLedTimerCall:" + ex.Message);
- }
- }
- }
- Dictionary<string, string> carDics = new Dictionary<string, string>();
- private void initCar()
- {
- carDics.Clear();
- string sql = "select carid,cnid from car where cnid<>''";
- DataTable dt = SqlHelper.ExecuteDataset(sql).Tables[0];
- foreach (DataRow dr in dt.Rows)
- {
- //AppendLog(dr["carid"].ToString()+","+dr["cnid"].ToString());
- carDics.Add(dr["carid"].ToString(), dr["cnid"].ToString());
- }
- }
- public void runLedServer()
- {
- int err = bxdualsdk_56.bxDual_InitSdk();
- if (err != 0)
- {
- AppendLog(err + ",初始化发生错误");
- return;
- }
- //控制卡IP 服务器模式用服务器IP或者127.0.0.1
- server_ip = Encoding.GetEncoding("GBK").GetBytes("127.0.0.1");
- int port = 1244;
- //启动服务器
- pServer = Server.bxDual_Start_Server(port);
- AppendLog("LED屏开启监听");
- //循环获取led控制卡
- getCardList();
- }
- bool isGetCard = false;
- byte[] ocards = new byte[2048];
- private static object getcarObj = new object();
- string wkey = "";
- string owkey = "";
- byte[] cards = null;
- public void getCardList()
- {
- if (isGetCard) return;
- //isGetCard = true;
- new Thread(new ThreadStart(delegate ()
- {
- int count = 0;
- while (true)
- {
- try
- {
- cards = new byte[2048];
- for (int i = 0; i < 2048; i++) { cards[i] = 0; }
- //AppendLog("获取在线个数!");
- //控制卡上线个数
- count = Server.bxDual_Get_CardList(cards);
- //AppendLog(count.ToString());
- if (count > 0)
- {
- //if (CompareArray(ocards, cards)) return;
- //ocards = cards;
- //一个控制卡数据20个长度
- for (int i = 0; i < count; i++)
- {
- lock (getcarObj)
- {
- //前14位数据是控制卡网络ID编号
- byte[] barcode = cards.Skip(0 + i * 20).Take(16 + i * 20).ToArray();
- //根据网络ID获取通讯使用端口
- int port = Server.bxDual_Get_Port_Barcode(barcode);
- if (port == -1) continue;
- ServerList serverObj = new ServerList(barcode, port);
- string key = System.Text.Encoding.GetEncoding("GBK").GetString(barcode).Trim();
- if (key.Substring(0, 3) == "JH-") key = key.Substring(0, 11);
- else if (key.Substring(0, 4) == "B6A0") key = key.Substring(0, 16);
- //if (key.Length >= 16) key = key.Substring(0, 16);
- if (!serverDics.ContainsKey(key))
- {
- helper.writeLog("新的号:" + key);
- wkey = getLedCarNo(key);
- if (wkey == "" || owkey == wkey) continue;
- owkey = wkey;
- if (carnoLst.ContainsKey(wkey))//如果已经包含了一个,就删除之前的一个
- {
- if (serverDics.ContainsKey(carnoLst[wkey]))
- {
- serverDics.Remove(carnoLst[wkey]);
- selCars.Items.Remove(wkey + "-" + carnoLst[wkey]);
- }
- carnoLst[wkey] = key;
- }
- else
- {
- carnoLst.Add(wkey, key);
- }
- serverDics.Add(key, serverObj);
- selCars.Items.Add(wkey + "-" + key);
- AppendLog(wkey + "_" + key + "_" + port + ":上线了!");
- helper.writeLog(wkey + "_" + key + "|" + port + ":上线了!");
- }
- else
- {
- //AppendLog(getLedCarNo(key) + "-" + key + "|" + port + ":更新!");
- if (serverDics[key].port != serverObj.port)
- {
- serverDics[key] = serverObj;
- helper.writeLog(getLedCarNo(key) + "-" + key + "|" + port + ":更新!");
- }
- }
- Thread.Sleep(20);
- }
- }
- }
- }
- catch (Exception ex)
- {
- //AppendLog("发生错误"+ex.Message);
- helper.writeLog("发生错误" + ex.Message);
- }
- finally
- {
- //isGetCard = false;
- Thread.Sleep(30000);
- }
- }
- })).Start();
- }
- private string getLedCarNo(string code)
- {
- code = code.Trim();
- //helper.writeLog("检查:" + code + "," + carDics.Count);
- if (carDics.Count > 0)
- {
- //AppendLog(code.Length + "," + code);
- string[] tmp = null;
- foreach (KeyValuePair<string, string> kvp in carDics)
- {
- if (kvp.Value.IndexOf(",") != -1)
- {
- tmp = kvp.Value.Split(',');
- if (tmp[0].Trim() == code || tmp[1].Trim() == code) return kvp.Key;
- }
- else
- {
- if (kvp.Value == code) return kvp.Key;
- }
- //helper.writeLog(tmp[0] + "_" + tmp[1] + "_" + tmp[1].Length + "_" + code + "_" + code.Length);
- }
- }
- helper.writeLog(code + "_没有找到");
- return "";
- }
- /// <summary>
- /// 数组比较是否相等
- /// </summary>
- /// <param name="bt1">数组1</param>
- /// <param name="bt2">数组2</param>
- /// <returns>true:相等,false:不相等</returns>
- public bool CompareArray(byte[] bt1, byte[] bt2)
- {
- var len1 = bt1.Length;
- var len2 = bt2.Length;
- if (len1 != len2)
- {
- return false;
- }
- for (var i = 0; i < len1; i++)
- {
- if (bt1[i] != bt2[i])
- return false;
- }
- return true;
- }
- public void stopLedServer()
- {
- //关闭服务器
- int err = Server.bxDual_Stop_Server(pServer);
- //释放动态库
- bxdualsdk_56.bxDual_ReleaseSdk();
- }
- public static object sendObj = new object();
- byte off = 0x00;
- public bool sendCarLedText(ServerList sl, string key, string txt, string carno)
- {
- //new Thread(new ThreadStart(delegate ()
- //{
- try
- {
- int port = sl.port;
- bxdualsdk_56.Ping_data data = new bxdualsdk_56.Ping_data();
- int err = bxdualsdk_56.bxDual_cmd_tcpPing(server_ip, (ushort)port, ref data);
- if (err == -1)
- {
- AppendLog(key + ":" + port + ", ping不通");
- helper.writeLog(key + ":" + port + ", ping不通");
- return false;
- }
- if (data.CurrentOnOffStatus == off)
- {
- AppendLog("屏幕关闭不发送");
- return false;
- }
- //Console.WriteLine("tcpPing:" + err);
- byte cmb_ping_Color = 1;
- ushort ct = 17763;//类型
- err = bxdualsdk_56.bxDual_program_setScreenParams_G56((bxdualsdk_56.E_ScreenColor_G56)cmb_ping_Color, ct, bxdualsdk_56.E_DoubleColorPixel_G56.eDOUBLE_COLOR_PIXTYPE_1);
- if (err != 0)
- {
- AppendLog("program_setScreenParams_G56:" + err);
- helper.writeLog("program_setScreenParams_G56:" + err);
- return false;
- }
- //创建节目
- program_6.Creat_Program_6();
- //创建图文区
- program_6.Creat_Area_6(0, 0, 0, 64, 16, 0);
- //添加显示文本
- if (txt.Length <= 6)
- {
- program_6.Creat_AddStr_6S(0, " " + txt + " ", "宋体");
- }
- else
- {
- program_6.Creat_AddStr_6(0, " " + txt + " ", "宋体");
- }
- //发送节目显示
- program_6.Net_SengProgram_6(server_ip, (ushort)port);
- AppendLog("发送:" + key + "|" + txt);
- helper.writeLog("发送:" + key + "|" + txt);
- return true;
- }
- catch (Exception ex)
- {
- AppendLog("发送失败:" + ex.Message);
- helper.writeLog("发送失败:" + ex.Message);
- return false;
- }
- }
- }
- public class Item
- {
- public string Name;
- public string Value;
- public Item(string name, string value)
- {
- Name = name; Value = value;
- }
- }
- }
|