/////////////////////////////// // //Read from file // /////////////////////////////// public static void ReadData() { CrestronLogger.Initialize(5, LoggerModeEnum.DEFAULT); char[] delimiterChars = { ',' }; List <string> DataLines = new List <string>(); if (File.Exists(FilePath)) { using (StreamReader SR = new StreamReader(FilePath))//Create Streamwriter { while (SR.EndOfStream != true) { DataLines.Add(SR.ReadLine()); } int z = 0; foreach (string i in DataLines) { string[] line = i.Split(delimiterChars); Users.Add(new user(line[0], line[1], Convert.ToUInt16(line[2]))); UserNames[z] = line[0]; z++; } CrestronLogger.WriteToLog("User data loaded.", 2); SR.Close(); } SendUIUpdate(); } FileRead = 1; }
override public void DisconnectClient(uint clientIndex, bool withDisconnectPacket) { MqttClient client = GetClientByIndex(clientIndex, true); try { oldDecodedFrame.Remove(clientIndex); if (Server.ClientConnected(clientIndex)) { SendCloseFrame(clientIndex); Server.Disconnect(clientIndex); } if (client != null) { OnClientDisconnected(client, withDisconnectPacket); } } catch (Exception e) { CrestronLogger.WriteToLog("MQTTSERVER - DISCONNECT_CLIENT - Client number : " + clientIndex + " errors occured during disconnection. " + e.Message + "\n" + e.StackTrace, 8); Server.Disconnect(clientIndex); } finally { if (client != null) { Clients.Remove(client); } } }
public void Send(MqttMsgBase packet) { CrestronLogger.WriteToLog("MQTTCLIENT - SEND - Sending packet type " + packet, 2); #if PACKET_DEBUG CrestronLogger.WriteToLog("MQTTCLIENT - SEND - " + BitConverter.ToString(packet.GetBytes(ProtocolVersion)), 2); #endif ClientSendDataAsync(packet.GetBytes(ProtocolVersion)); }
/// <summary> /// Parse bytes for a UNSUBSCRIBE message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>UNSUBSCRIBE message instance</returns> public static MqttMsgUnsubscribe Parse(byte[] data) { byte[] buffer; int index = 0; byte[] topicUtf8; int topicUtf8Length; MqttMsgUnsubscribe msg = new MqttMsgUnsubscribe(); byte fixedHeaderFirstByte = data[0]; // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBSCRIBE_FLAG_BITS) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(data); buffer = new byte[remainingLength]; // buffer is filled with remaing lenght... for (int i = 2, j = 0; j < remainingLength; i++, j++) { buffer[j] = data[i]; } // read bytes from socket... int received = data.Length; // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); // payload contains topics // NOTE : before, I don't know how many topics will be in the payload (so use List) IList <String> tmpTopics = new List <String>(); do { // topic name topicUtf8Length = ((buffer[index++] << 8) & 0xFF00); topicUtf8Length |= buffer[index++]; topicUtf8 = new byte[topicUtf8Length]; Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length); index += topicUtf8Length; tmpTopics.Add(new String(Encoding.UTF8.GetChars(topicUtf8))); } while (index < remainingLength); // copy from list to array msg.topics = new string[tmpTopics.Count]; for (int i = 0; i < tmpTopics.Count; i++) { msg.topics[i] = (string)tmpTopics[i]; } CrestronLogger.WriteToLog("UNSIBSCRIBER PARSE SUCCESS", 6); return(msg); }
/////////////////////////////// //Update Access // /////////////////////////////// static public void UpdateAccessLevel(string UserName, ushort Level) { int index = Users.FindIndex(x => x.UserName == UserName); Users[index].AccessLevel = Level; CrestronLogger.WriteToLog("User Access Level Updated : " + UserName, 2); WriteData(); SendUIUpdate(); }
private void OnSocketStatusChange(SocketStatus serverSocketStatus) { CrestronLogger.WriteToLog("MQTTCLIENT - OnSocketStatusChange - socket status : " + serverSocketStatus, 1); if (serverSocketStatus != SocketStatus.SOCKET_STATUS_CONNECTED) { OnConnectionStateChanged(0); if (connectionRequested && (disconnectTimer == null)) { disconnectTimer = new CTimer(DisconnectTimerCallback, 5000); } } }
/////////////////////////////// //Delete User Method // /////////////////////////////// static public void DeleteUser(string UserName) { if (Users.Remove(Users.Find(x => x.UserName == UserName)))//Remove user from list (Find User in list) { CrestronLogger.WriteToLog("User Deleted : " + UserName, 2); WriteData(); //if it found and removed a user write the data back to disk SendUIUpdate(); } else { CrestronConsole.PrintLine("No User found"); } }
/////////////////////////////// //Update Passcode // /////////////////////////////// static public void UpdatePasscode(string UserName, string PassCode) { if (IsAllDigits(PassCode) && PassCode.Length > 5) { int index = Users.FindIndex(x => x.UserName == UserName); Users[index].Password = PassCode.GetHashCode().ToString(); CrestronLogger.WriteToLog("User Passcode Updated : " + UserName, 2); WriteData(); } else { SendDialogMessage("Pascode must be six or more digits, and can only be numbers 0-9."); } }
/////////////////////////////// // Method for S+ to send passcode //for unlock /////////////////////////////// static public ushort SendLogin(string password, ref InterfaceInstance x) //Must pass passcode, and its object reference { ushort ReturnValue = 0; /*if (x.InterfaceLocked) * { * x.DialogMessage("Too many login attempts. Interface Locked."); * return 0; * } * else*/ { if (password == BackdoorPassword) { x.unlock(BackdoorAccessLevel); //z.LastLogin = DateTime.Now + " at " + x.LocationName; CrestronConsole.PrintLine(BackdoorUser + " Unlocked System"); CrestronLogger.WriteToLog(BackdoorUser + " Unlocked System From " + x.LocationName, 2); ReturnValue = 1; } else { foreach (user z in Users) { if (z.Password == password.GetHashCode().ToString()) //Hash passcode and check { x.unlock(z.AccessLevel); z.LastLogin = DateTime.Now + " at " + x.LocationName; CrestronConsole.PrintLine(z.UserName + " Unlocked System"); CrestronLogger.WriteToLog(z.UserName + " Unlocked System From " + x.LocationName, 2); ReturnValue = 1; } } } if (ReturnValue == 0) { //CrestronConsole.PrintLine("No User found"); x.DialogMessage("Invalid Passcode. Please try again"); //x.LoginAttempts++; //if (x.LoginAttempts >= 3) //{ // x.LockInterface(); //} CrestronLogger.WriteToLog("Failed loggin attempt at " + x.LocationName, 2); } return(ReturnValue); } }