void connect(SettingsMain smii) { try { ((Form1)this.Parent.Parent.Parent.Parent).scan = true; lblConnect.Text = "Connecting..."; bool checks = true; for (int x = 0; x < smii.Readers.Count; x++) { ImpinjRevolution ir = new ImpinjRevolution(); ir.ReaderScanMode = ScanMode.FullScan; ir.HostName = smii.Readers[x].IPaddress; ir.Antennas = smii.Readers[x].antennas; ir.Connect(); impinjrev.Add(ir); if (!ir.isConnected) { if (checks == true) { checks = false; } } } if (checks == true) { lblConnect.Text = ""; MessageBox.Show("All the readers connected succesfully!"); for (int i = 0; i < impinjrev.Count; i++) { impinjrev[i].StopRead(); impinjrev[i].Disconnect(); } ((Form1)this.Parent.Parent.Parent.Parent).scan = false; } else { lblConnect.Text = ""; MessageBox.Show("The readers did not connect succesfully!"); for (int i = 0; i < impinjrev.Count; i++) { impinjrev[i].StopRead(); impinjrev[i].Disconnect(); } ((Form1)this.Parent.Parent.Parent.Parent).scan = false; } } catch (Exception ex) { MessageBox.Show("Sorry Something went wrong, the action was not completed!"); } }
//注册委托函数 //private static bool reader_regist_delegate(ref ImpinjRevolution ir, int iReaderType, delegate_read_handler dRHandler, delegate_write_handler dWHandler, delegate_check_handler dCHandler) private static bool reader_regist_delegate(ImpinjRevolution ir, int iReaderType, delegate_read_handler dRHandler, delegate_write_handler dWHandler, delegate_check_handler dCHandler) { Log.WriteLog(LogType.Trace, "come in reader_regist_delegate"); switch (iReaderType) { case Macro.READER_TYPE_READER: { if (dRHandler != null) { ir.dReadHandler += dRHandler; Log.WriteLog(LogType.Trace, "success to regist read delegate function to the reader[" + ir.HostName + "] with type[" + iReaderType + "]."); } } break; case Macro.READER_TYPE_WRITER: { if (dWHandler != null) { ir.dWriteHandler += dWHandler; Log.WriteLog(LogType.Trace, "success to regist write delegate function to the reader[" + ir.HostName + "] with type[" + iReaderType + "]."); } } break; case Macro.READER_TYPE_CHECKR: { if (dCHandler != null) { ir.dCheckHandler += dCHandler; Log.WriteLog(LogType.Trace, "success to regist check delegate function to the reader[" + ir.HostName + "] with type[" + iReaderType + "]."); } } break; default: { //目前只提示出错,不退出处理,后续处理交事务流程合法性进行判断 Log.WriteLog(LogType.Error, "error:the reader[" + ir.HostName + "] type[" + iReaderType + "] is invaliable."); return(false); } break; } return(true); }
//判断读写器是否有委托处理函数 private static bool reader_has_delegate(ImpinjRevolution imp) { Log.WriteLog(LogType.Trace, "come in reader_has_delegate"); switch (imp.iReaderType) { case Macro.READER_TYPE_READER: { if (!imp.isdReadHandlerEnable) { Log.WriteLog(LogType.Error, "error:there is not read delegate function in reader[" + imp.HostName + "], so no need to start it"); MessageBox.Show("error:there is not read delegate function in reader[" + imp.HostName + "], so no need to start it"); } } break; case Macro.READER_TYPE_WRITER: { if (!imp.isdWriteHandlerEnable) { Log.WriteLog(LogType.Error, "error:there is not write delegate function in reader[" + imp.HostName + "], so no need to start it"); MessageBox.Show("error:there is not write delegate function in reader[" + imp.HostName + "], so no need to start it"); } } break; case Macro.READER_TYPE_CHECKR: { if (!imp.isdCheckHandlerEnable) { Log.WriteLog(LogType.Error, "error:there is not check delegate function in reader[" + imp.HostName + "], so no need to start it"); MessageBox.Show("error:there is not chech delegate function in reader[" + imp.HostName + "], so no need to start it"); } } break; default: { Log.WriteLog(LogType.Error, "error:unknow operation type[" + imp.iReaderType + "] with reader[" + imp.HostName + "]"); return(false); } break; } return(true); }
//Margo bool connect(LTS.Settings se) { try { lblConnect.Text = "Connecting..."; int index = comboBoxStore.SelectedIndex; if (st != null) { int storeID = st[index].StoreID; LTS.Settings set = se; sm = null; sm = new SettingsMain(); impinjrev.Clear(); sm.SettingsID = set.SettingsID; sm.SettingsName = set.SettingsName; sm.SettingsSelect = set.SettingsSelect; sm.StoreID = set.StoreID; LTS.Store store = DAT.DataAccess.GetStore().Where(i => i.StoreID == sm.StoreID).FirstOrDefault(); sm.StoreLocation = store.StoreLocation; sm.StoreName = store.StoreName; List <LTS.Reader> readers = new List <LTS.Reader>(); readers = DAT.DataAccess.GetReader().Where(j => j.SettingsID == sm.SettingsID).ToList(); for (int j = 0; j < readers.Count; j++) { ReaderMain rm = new ReaderMain(); rm.ReaderID = readers[j].ReaderID; rm.IPaddress = readers[j].IPaddress; rm.NumAntennas = readers[j].NumAntennas; rm.antennas = DAT.DataAccess.GetAntenna().Where(q => q.ReaderID == rm.ReaderID).ToList(); sm.Readers.Add(rm); } bool checks = true; for (int x = 0; x < sm.Readers.Count; x++) { ImpinjRevolution ir = new ImpinjRevolution(); ir.ReaderScanMode = ScanMode.ScanItem; ir.HostName = sm.Readers[x].IPaddress; ir.Antennas = sm.Readers[x].antennas; ir.TagRead += ir_TagRead; ir.Connect(); impinjrev.Add(ir); if (!ir.isConnected) { if (checks == true) { checks = false; } } } if (checks == true) { lblConnect.Text = "Connected"; timer.Start(); impinjrev.ForEach(imp => { imp.TagRead += ir_TagRead; imp.StartRead(); }); ((Form1)this.Parent.Parent.Parent.Parent).scan = true; lblConnect.Text = "Reading..."; lblTimer.Text = time.ToString(); } else { lblConnect.Text = "Not Connected!"; timer.Stop(); timer.Elapsed -= timer_Elapsed; time = 0; for (int i = 0; i < impinjrev.Count; i++) { impinjrev[i].StopRead(); impinjrev[i].Disconnect(); } EnableOrDisable(true); ((Form1)this.Parent.Parent.Parent.Parent).scan = false; } } } catch (Exception ex) { MessageBox.Show("Sorry Something went wrong, the action was not completed!"); } return(true); }
//Margo private void button5_Click(object sender, EventArgs e) { try { lblSelect.Visible = false; if (listS.Where(u => u.StoreName == comboBoxStore.SelectedItem.ToString()).FirstOrDefault() != null) { if (set != null) { button4.Enabled = false; button2.Enabled = false; comboBoxStore.Enabled = false; lblConnect.Text = "Connecting ..."; lblStartRead.Visible = false; lblStop.Visible = false; btnStart.Enabled = false; lblConnect.Visible = true; sm = null; sm = new SettingsMain(); impinjrev.Clear(); sm.SettingsID = set.SettingsID; sm.SettingsName = set.SettingsName; sm.SettingsSelect = set.SettingsSelect; sm.StoreID = set.StoreID; LTS.Store store = DAT.DataAccess.GetStore().Where(i => i.StoreID == sm.StoreID).FirstOrDefault(); sm.StoreLocation = store.StoreLocation; sm.StoreName = store.StoreName; List <LTS.Reader> readers = new List <LTS.Reader>(); readers = DAT.DataAccess.GetReader().Where(j => j.SettingsID == sm.SettingsID).ToList(); for (int j = 0; j < readers.Count; j++) { ReaderMain rm = new ReaderMain(); rm.ReaderID = readers[j].ReaderID; rm.IPaddress = readers[j].IPaddress; rm.NumAntennas = readers[j].NumAntennas; rm.antennas = DAT.DataAccess.GetAntenna().Where(q => q.ReaderID == rm.ReaderID).ToList(); sm.Readers.Add(rm); } bool checks = true; for (int x = 0; x < sm.Readers.Count; x++) { ImpinjRevolution ir = new ImpinjRevolution(); ir.ReaderScanMode = ScanMode.FullScan; ir.HostName = sm.Readers[x].IPaddress; ir.Antennas = sm.Readers[x].antennas; ir.TagRead += ir_TagRead; ir.Connect(); impinjrev.Add(ir); if (!ir.isConnected) { if (checks == true) { checks = false; } } } if (checks == true) { config = true; lblConnect.Text = "Connected"; btnStart.Enabled = true; btnStop.Enabled = false; ((Form1)this.Parent.Parent.Parent.Parent).scan = true; } else { lblConnect.Text = "Not Connected"; } } else { MessageBox.Show("The Store selected does not have a setting set, please go to the Select Setting page and choose a setting!", "", MessageBoxButtons.OKCancel); } } else { lblSelect.Visible = true; } } catch (Exception ex) { MessageBox.Show("Sorry Something went wrong, the action was not completed!"); } }
bool updateItem_connect(LTS.Settings se) { Log.WriteLog(LogType.Trace, "come in updateItem_connect"); try { lConnectMsg.Text = "Connecting..."; int index = cbStore.SelectedIndex; int storeID = listS[index].StoreID; LTS.Settings set = se; sm = null; sm = new SettingsMain(); impinjrev.Clear(); sm.SettingsID = set.SettingsID; sm.SettingsName = set.SettingsName; sm.SettingsSelect = set.SettingsSelect; sm.StoreID = set.StoreID; LTS.Store store = DAT.DataAccess.GetStore().Where(i => i.StoreID == sm.StoreID).FirstOrDefault(); if (null == store) { Log.WriteLog(LogType.Error, "error to get store with id[" + set.StoreID + "]"); return(false); } sm.StoreLocation = store.StoreLocation; sm.StoreName = store.StoreName; Log.WriteLog(LogType.Trace, "success to get settings[" + sm.SettingsID + "] witch in store[" + set.StoreID + "] info:settingsName[" + set.SettingsName + "], " + "select flag[" + set.SettingsSelect + "], the storeName[" + store.StoreName + "] and storeLocation[" + store.StoreLocation + "]"); List <LTS.Reader> readers = new List <LTS.Reader>(); readers = DAT.DataAccess.GetReader().Where(j => j.SettingsID == sm.SettingsID).ToList(); if (null == readers) { Log.WriteLog(LogType.Error, "error to get readers in settings[" + sm.SettingsID + "]"); return(false); } for (int j = 0; j < readers.Count; j++) { ReaderMain rm = new ReaderMain(); rm.ReaderID = readers[j].ReaderID; rm.IPaddress = readers[j].IPaddress; rm.NumAntennas = readers[j].NumAntennas; rm.antennas = DAT.DataAccess.GetAntenna().Where(q => q.ReaderID == rm.ReaderID).ToList(); sm.Readers.Add(rm); } bool checks = true; Log.WriteLog(LogType.Trace, "success go get " + readers.Count + " readers with in settings[" + sm.SettingsID + "] into memery"); Log.WriteLog(LogType.Trace, "success go get " + readers.Count + " readers with in settings[" + sm.SettingsID + "] into memery"); //根据settings上的读写器配置,进行读写器连接操作 for (int x = 0; x < sm.Readers.Count; x++) { bool bOk = true; //针对每一个reader配置生成一个reader解决方案节点 ImpinjRevolution ir = new ImpinjRevolution(); ir.ReaderScanMode = ScanMode.ScanItem; ir.HostName = sm.Readers[x].IPaddress; ir.Antennas = sm.Readers[x].antennas; ir.isConnected = false; //ir.TagRead += ir_TagRead; Log.WriteLog(LogType.Trace, "goto connect to reader with ip[" + ir.HostName + "]"); //连接到指定的读写器上 if (!ir.ir_connectReader()) { bOk = false; Log.WriteLog(LogType.Error, "error to connect to reader[" + ir.HostName + "]"); } impinjrev.Add(ir); if (!bOk) { if (checks == true) { Log.WriteLog(LogType.Trace, "goto set the check flag into false status."); checks = false; } } } Log.WriteLog(LogType.Trace, "after connect to all the readers"); if (checks == true) { lConnectMsg.Text = "Connected"; //启动定时器,定时器超时后,执行定时器超时函数 timer.Start(); //遍历读写器数组,为他们注册委托事件,并启动读写器 impinjrev.ForEach(imp => { imp.dReadHandler += updateItem_ir_tagRead; imp.ir_startRead(); }); Log.WriteLog(LogType.Trace, "success to start all readers"); ((Form1)this.Parent.Parent.Parent.Parent).scan = true; lConnectMsg.Text = "Reading..."; } else { lConnectMsg.Text = "Not Connected!"; timer.Stop(); timer.Elapsed -= updateItem_timer_elapsed; time = 0; for (int i = 0; i < impinjrev.Count; i++) { impinjrev[i].ir_stopRead(); impinjrev[i].ir_disconnect(); } updateItem_enableOrDisable(true); ((Form1)this.Parent.Parent.Parent.Parent).scan = false; } return(true); } catch (Exception ex) { Log.WriteLog(LogType.Error, "there is something wrong during connect to readers. the error msg is " + ex.Message + ""); MessageBox.Show("Sorry Something went wrong, the action was not completed!"); return(true); } }
/*SettingsMain smii, settings的配置信息; * ref List<ImpinjRevolution> impinjrev, reader控制节点列表; * delegate_read_handler dRHandler, 读操作函数; * delegate_write_handler dWHandler, 写操作函数; * delegate_check_handler dCHandler, 校验函数; * bool bTest, 测试标志位; *描述:为每个读写器申请一个读写器节点,并连接到物理读写器上,将配置应用到读写器中。*/ private static bool reader_connect_handler(SettingsMain smii, List <ImpinjRevolution> impinjrev, BusinessQue stBQue, delegate_read_handler dRHandler, delegate_write_handler dWHandler, delegate_check_handler dCHandler, bool bTest) { bool bAllConnected = true; Log.WriteLog(LogType.Trace, "come reader_connect_handler"); try { impinjrev.Clear(); //初始化解决方案数组; //连接到对应读写器上,并对读写器进行配置(一个reader配置对应一个impinj解决方案对象) for (int x = 0; x < smii.Readers.Count; x++) { bool bProcessOk = true; //如果系统没有内存分配给revolution,则会抛出异常,所以在这里不需要对ir返回值进行合法性判断 ImpinjRevolution ir = new ImpinjRevolution(); ir.ReaderScanMode = ScanMode.FullScan; ir.HostName = smii.Readers[x].IPaddress; ir.Antennas = smii.Readers[x].antennas; ir.iReaderType = smii.Readers[x].iReaderType; ir.stBQue = stBQue; ir.isConnected = false; //注册委托函数 if (reader_regist_delegate(ir, ir.iReaderType, dRHandler, dWHandler, dCHandler)) { //连接到读写器,并进行配置 if (!ir.ir_connectReader()) { Log.WriteLog(LogType.Trace, "error to connect to reader[" + ir.HostName + "]"); bProcessOk = false; } else { Log.WriteLog(LogType.Trace, "success to connect to reader[" + ir.HostName + "]"); } } else { bProcessOk = false; } //读写器控制节点加入数组 impinjrev.Add(ir); //根据本节点的操作,决定全局操作标志位 if (!bProcessOk) { if (bAllConnected == true) { bAllConnected = false; Log.WriteLog(LogType.Warning, "Warning: reader[" + smii.Readers[x].IPaddress + "] can not be connect to, so we will not using the revolution"); } else { Log.WriteLog(LogType.Warning, "Warning: reader[" + smii.Readers[x].IPaddress + "] can not be connect to"); } } } if (bAllConnected == true) { MessageBox.Show("All the readers in settings[" + smii.SettingsID + "] are connected succesfully!"); } else { MessageBox.Show("there are some readers in settins[" + smii.SettingsID + "] are not connect succesfully!"); } if (bTest || !bAllConnected) { Log.WriteLog(LogType.Trace, "this is a test process or some reader(s) can not connect to, so go to disconnect reader(s)"); //去连接 if (!reader_disconnect(impinjrev)) { Log.WriteLog(LogType.Error, "error to call reader_disconnect"); } } } catch (Exception ex) { MessageBox.Show("Sorry Something went wrong, the connect was not completed!"); bAllConnected = false; } return(bAllConnected); }