public async void SendMessage(MCP msg) { msg.Token = this._token; var tpMsg = this._clientId + this._gatewayMac + Helpers.ToHex(msg.ToByteArray()); tpMsg = tpMsg.ToUpper(); int checksum = 0; foreach (char c in tpMsg) { checksum += c; } checksum = checksum & 0xff; tpMsg += checksum.ToString("X2"); var msgBytes = Encoding.UTF8.GetBytes(tpMsg); await _client.Send(msgBytes); }
public static MCP FromByteArray(byte[] data) { var ms = new MemoryStream(data); var bw = new BinaryReader(ms); int length = bw.ReadUInt16(); var mcp = new MCP(); mcp.Tag = bw.ReadByte(); mcp.Token = bw.ReadUInt32(); byte commandByte = bw.ReadByte(); mcp.Command = (McpCommand)(commandByte & 0x7F); byte[] buffer = new byte[2048]; int read = bw.Read(buffer, 0, buffer.Length); mcp.Payload = new byte[read]; Array.Copy(buffer, mcp.Payload, read); return(mcp); }
public static MCP CreateSetGroupTypeCmd(int groupId, McpGroupType type) { return(MCP.Create(McpCommand.SetValue, groupId, (int)type)); }
public static MCP CreateLoginCmd(string user, string password) { return(MCP.Create(McpCommand.Login, user, password)); }
private static MCP Create(McpCommand cmd, int val, string val2) { return(MCP.Create(cmd, val, Encoding.UTF8.GetBytes(val2))); }
public static MCP CreateSetPortStateCmd(int portId, McpPortState state) { return(MCP.Create(McpCommand.SetState, portId, (int)state)); }
public static MCP CreateRemovePortCmd(int portId) { return(MCP.Create(McpCommand.RemovePort, portId)); }
//public static MCP CreateSetValueCmd(int address, int value) //{ // return MCP.Create(McpCommand.SetValue, address, value); //} public static MCP CreateGetTransitionCmd(int newId) { return(MCP.Create(McpCommand.HmGetTransition, newId)); }
public static MCP CreateSetUserRightsCmd(int userId, IList <byte> groupIds) { return(MCP.Create(McpCommand.SetUserRights, userId, groupIds.ToArray())); }
public static MCP CreateAddUserCmd(string username, string password) { return(MCP.Create(McpCommand.AddUser, username, password)); }
public static MCP CreateGetUsersCmd() { return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_USERS\"}")); }
public static MCP CreateScanWifiCmd() { return(MCP.Create(McpCommand.ScanWifi)); }
public static MCP CreateGetWifiStateCmd() { return(MCP.Create(McpCommand.GetWifiState)); }
public static MCP CreateSetGatewayNameCmd(string name) { return(MCP.Create(McpCommand.SetName, name)); }
public static MCP CreateChangePassCmd(string newpassword) { return(MCP.Create(McpCommand.ChangePasswd, newpassword)); }
public static MCP CreateSetRequestableCmd(int groupId, int portId) { return(MCP.Create(McpCommand.SetValue, groupId + 16, portId)); }
public static MCP CreateSetGroupedPortsCmd(int groupId, IList <byte> portIds) { return(MCP.Create(McpCommand.SetGroupedPorts, groupId, portIds.ToArray())); }
public static MCP CreateRemoveUserCmd(int userId) { return(MCP.Create(McpCommand.RemoveUser, userId)); }
public static MCP CreateGetPortsCmd() { return(MCP.Create(McpCommand.GetPorts)); }
internal JmcpEventArgs(MCP mcp) { this.Mcp = mcp; this.Data = Encoding.UTF8.GetString(mcp.Payload); }
public static MCP CreateSetPortTypeCmd(int portId, McpPortType type) { return(MCP.Create(McpCommand.SetType, portId, (int)type)); }
public static MCP CreateGetGroupsCmd() { return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_GROUPS\"}")); }
public static MCP CreateJmcpGetValuesCmd() { return(MCP.Create(McpCommand.Jmcp, "{\"cmd\":\"GET_VALUES\"}")); }
public static MCP CreateAddGroupCmd() { return(MCP.Create(McpCommand.AddGroup)); }
internal TransitionEventArgs(MCP mcp) { this.Mcp = mcp; //this.Data = Encoding.UTF8.GetString(mcp.Payload); }
public static MCP CreateRemoveGroupCmd(int groupId) { return(MCP.Create(McpCommand.RemoveGroup, groupId)); }
private async Task SocketReader() { byte[] buffer = new byte[2048]; int offset = 0; while (true) { try { offset += await _client.Read(buffer, (uint)offset, (uint)(buffer.Length - offset)); } catch (Exception ex) { Debug.WriteLine("Ex " + ex.StackTrace); throw ex; } if (offset < (MCP.ADDRESS_SIZE * 2) + MCP.LENGTH_SIZE) { continue; } var tp = Encoding.UTF8.GetString(buffer, 0, offset); if (!tp.StartsWith(this._gatewayMac)) { //not good! continue; } byte[] tpBytes = Helpers.HexToBytes(tp); int length = (tpBytes[12] << 8) + tpBytes[12 + 1]; if (tpBytes.Length - 12 - length - 1 < 0) { continue; } byte[] tpmsgbytes = new byte[length]; Array.Copy(tpBytes, 12, tpmsgbytes, 0, length); var test = MCP.FromByteArray(tpmsgbytes); Array.Copy(buffer, (12 + length + 1) * 2, buffer, 0, offset - (12 + length + 1) * 2); offset = offset - (12 + length + 1) * 2; // if (test.Command == McpCommand.Login) { // remember access token to be used on consecutive calls this._token = BitConverter.ToUInt32(test.Payload, 1); this.OnLogin?.Invoke(this, EventArgs.Empty); //var msg = MCP.CreateJmcpGetValuesCmd(); //var msg = MCP.CreateGetGroupsCmd(); //var msg = MCP.CreateGetUsersCmd(); //var msg = MCP.CreateGetPortsCmd(); //var msg = MCP.CreateGetWifiStateCmd(); var msg = MCP.CreateGetTransitionCmd(0); this.SendMessage(msg); } else if (test.Command == McpCommand.Logout) { this.OnLogout?.Invoke(this, EventArgs.Empty); } else if (test.Command == McpCommand.Jmcp) { this.OnJmcpMessage?.Invoke(this, new JmcpEventArgs(test)); } else if (test.Command == McpCommand.HmGetTransition) { this.OnTransition?.Invoke(this, new TransitionEventArgs(test)); } else if (test.Command == McpCommand.Error) { this.OnError?.Invoke(this, EventArgs.Empty); } } }
public static MCP CreateSetGroupNameCmd(int groupId, string name) { return(MCP.Create(McpCommand.SetGroupName, groupId, name)); }