public byte[] GetCmdBytes(CMDS command, byte argument) { byte[] cmd_bytes = BitConverter.GetBytes((int)command); byte[] r = new byte[3]; r[0] = cmd_bytes[0]; r[1] = cmd_bytes[1]; r[2] = argument; return(r); }
private string CreateMessage(CMDS command) { // Create a string builder, and the message starts with "$". StringBuilder message = new StringBuilder("$", 10); // Add the command. message.Append((char)command); // Calculate checksum and convert to string. string checksum = Checksum.Calculate(Encoding.ASCII.GetBytes(message.ToString())).ToString(); // Add checksum to the message, preceeded by an asterisk. message.Append('*' + checksum + '\n'); return(message.ToString()); }
private void SendCMDPacket(CMDS cmd, int length, params object[] fields) { CMDPacket packet = new CMDPacket { magic = CMD_PACKET_MAGIC, cmd = (uint)cmd, datalen = (uint)length }; byte[] data = null; if (length > 0) { MemoryStream rs = new MemoryStream(); foreach (object field in fields) { byte[] bytes = null; switch (field) { case char c: bytes = BitConverter.GetBytes(c); break; case byte b: bytes = BitConverter.GetBytes(b); break; case short s: bytes = BitConverter.GetBytes(s); break; case ushort us: bytes = BitConverter.GetBytes(us); break; case int i: bytes = BitConverter.GetBytes(i); break; case uint u: bytes = BitConverter.GetBytes(u); break; case long l: bytes = BitConverter.GetBytes(l); break; case ulong ul: bytes = BitConverter.GetBytes(ul); break; case byte[] ba: bytes = ba; break; } if (bytes != null) { rs.Write(bytes, 0, bytes.Length); } } data = rs.ToArray(); rs.Dispose(); } SendData(GetBytesFromObject(packet), CMD_PACKET_SIZE); if (data != null) { SendData(data, length); } }
/// <summary> /// Registers a command. /// </summary> /// <param name="command">The Command class instance representing the command to register.</param> public void RegisterCommand(Command command) { CMDs.Add(command.CommandText); CMDS.Add(command); }
public void RunCommand(CMDS command, byte argument, IPEndPoint sender) { switch (command) { case CMDS.TurnOnLoopListenLight: if (listenButtons.Count > (int)argument) { listenButtons[(int)argument].Invoke(new Action(() => { listenButtons[(int)argument].setLitState(true); loopListen[(int)argument] = true; })); } break; case CMDS.TurnOffLoopListenLight: if (listenButtons.Count > (int)argument) { listenButtons[(int)argument].Invoke(new Action(() => { listenButtons[(int)argument].setLitState(false); loopListen[(int)argument] = false; })); } break; case CMDS.TurnOnLoopTalkLight: if (talkButtons.Count > (int)argument) { talkButtons[(int)argument].Invoke(new Action(() => { talkButtons[(int)argument].setLitState(true); })); } break; case CMDS.TurnOffLoopTalkLight: if (talkButtons.Count > (int)argument) { talkButtons[(int)argument].Invoke(new Action(() => { talkButtons[(int)argument].setLitState(false); })); } break; case CMDS.TurnOnLoopTalkFlash: if (talkButtons.Count > (int)argument) { talkButtons[(int)argument].Invoke(new Action(() => { talkButtons[(int)argument].setTalkState(true); loopTalk[(int)argument] = true; })); } break; case CMDS.TurnOffLoopTalkFlash: if (talkButtons.Count > (int)argument) { talkButtons[(int)argument].Invoke(new Action(() => { talkButtons[(int)argument].setTalkState(false); loopTalk[(int)argument] = false; })); } break; case CMDS.Pong: // SERVER IS ALIVE!!! break; } }
private string CreateMessage(CMDS command, SENSOR_ID sensor, uint ppm) { // Create a byte array. byte[] outbuff = new byte[30]; // Keep track of the message length. int i = 0; // Message starts with "$". outbuff[i++] = (byte)'$'; // Add the command. outbuff[i++] = (byte)command; // Add comma delimiter. outbuff[i++] = (byte)','; // Add the sensor ID. string str = ((int)sensor).ToString(); for (int j = 0; j < str.Length; j++) { outbuff[i++] = (byte)str[j]; } // Add comma delimiter. outbuff[i++] = (byte)','; // Add sensor data. str = ppm.ToString(); for (int j = 0; j < str.Length; j++) { outbuff[i++] = (byte)str[j]; } // Add comma delimiter. outbuff[i++] = (byte)','; // Add unit of measure. str = ((int)UNIT.PPM).ToString(); for (int j = 0; j < str.Length; j++) { outbuff[i++] = (byte)str[j]; } // Calculate checksum. ushort crc_value = Checksum.Calculate(outbuff); // Checksum is preceeded by an asterisk. outbuff[i++] = (byte)'*'; // Add the checksum to the message. string crc_str = crc_value.ToString(); int crc_str_size = crc_str.Length; for (int j = 0; j < crc_str_size; j++) { outbuff[i++] = (byte)crc_str[j]; } // Terminate message with newline. outbuff[i++] = (byte)('\n'); return(Encoding.ASCII.GetString(outbuff)); }
static public void RunCommand(CMDS command, byte argument, string sender) { switch (command) { case CMDS.StartListeningToLoop: if (!clients[sender].GetSources().Contains((int)argument)) { clients[sender].AddSource((int)argument); loops[(int)argument].addListenerToLoop(sender); UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOnLoopListenLight, argument), clients[sender].endpoint); } break; case CMDS.StopListeningToLoop: if (clients[sender].GetSources().Contains((int)argument)) { clients[sender].RemoveSource((int)argument); loops[(int)argument].removeListenerFromLoop(sender); UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopListenLight, argument), clients[sender].endpoint); } break; case CMDS.StartTalkingToLoop: // REMOVE TALKER FROM ALL LOOPS for (int i = 0; i < loops.Count; i++) { if (loops[i].talkers.Contains(sender)) { loops[i].removeTalkerFromLoop(sender); clients[sender].destinations.Remove(i); UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkFlash, (byte)i), clients[sender].endpoint); } } if (!clients[sender].destinations.Contains((int)argument)) { clients[sender].destinations.Add((int)argument); loops[(int)argument].addTalkerToLoop(sender); UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOnLoopTalkFlash, argument), clients[sender].endpoint); // ACTIVATE ACTIVE-LIGHT FOR ALL USERS foreach (KeyValuePair <string, Client> kvp in clients) { Client c = kvp.Value; if (c.IsConnected() && kvp.Key != sender) { UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOnLoopTalkLight, argument), c.endpoint); } } } // Check if loop is left without talkers for (int i = 0; i < loops.Count; i++) { if (loops[i].talkers.Count == 0) { // No more talkers in this loop, turn of active talker light for all users foreach (KeyValuePair <string, Client> kvp in clients) { Client c = kvp.Value; if (c.IsConnected()) { UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkLight, (byte)i), c.endpoint); } } } } break; case CMDS.StopTalkingToLoop: if (clients[sender].destinations.Contains((int)argument)) { clients[sender].destinations.Remove((int)argument); loops[(int)argument].removeTalkerFromLoop(sender); UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkFlash, argument), clients[sender].endpoint); // IF LOOP IS EMPTY BLANK TALK BUTTONS if (loops[(int)argument].talkers.Count == 0) { foreach (KeyValuePair <string, Client> kvp in clients) { Client c = kvp.Value; if (c.IsConnected()) { UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkLight, argument), c.endpoint); } } } } break; case CMDS.TurnOnLoopListenLight: if (lButtons.ContainsKey((int)argument)) { lButtons[(int)argument].Invoke(new Action(() => { lButtons[(int)argument].setLitState(true); })); } break; case CMDS.TurnOffLoopListenLight: if (lButtons.ContainsKey((int)argument)) { lButtons[(int)argument].Invoke(new Action(() => { lButtons[(int)argument].setLitState(false); })); } break; case CMDS.TurnOnLoopTalkLight: if (tButtons.ContainsKey((int)argument)) { tButtons[(int)argument].Invoke(new Action(() => { tButtons[(int)argument].setLitState(true); })); } break; case CMDS.TurnOffLoopTalkLight: if (tButtons.ContainsKey((int)argument)) { tButtons[(int)argument].Invoke(new Action(() => { tButtons[(int)argument].setLitState(false); })); } break; case CMDS.SetClientAudioFormat: clients[sender].audioFormat = (int)argument; clients[sender].decoder.SetFormat((int)argument); clients[sender].encoder.SetFormat((int)argument); break; case CMDS.Ping: UdpServer.Reply(CMD.GetCmdBytes(CMDS.Pong, (byte)0), clients[sender].endpoint); break; } }