2
0

DeviceViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // ThingsViewController.m
  3. // WildFireChat
  4. //
  5. // Created by Tom Lee on 2020/4/26.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "DeviceViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "DeviceInfoViewController.h"
  11. #import <WFChatUIKit/WFChatUIKit.h>
  12. @interface DeviceViewController ()
  13. @property(nonatomic, assign)BOOL ledOn;
  14. @property(nonatomic, strong)UILabel *dthLabel;
  15. @property(nonatomic, strong)UIButton *ledBtn;
  16. @end
  17. @implementation DeviceViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.view.backgroundColor = [UIColor whiteColor];
  21. CGRect bounds = self.view.bounds;
  22. self.ledBtn = [[UIButton alloc] initWithFrame:CGRectMake((bounds.size.width - 100 - 100 - 20)/2, (bounds.size.height - 20)/2, 100, 40)];
  23. [self.ledBtn addTarget:self action:@selector(onLedBtn:) forControlEvents:UIControlEventTouchDown];
  24. [self.ledBtn setTitle:@"打开LED" forState:UIControlStateNormal];
  25. [self.ledBtn setBackgroundColor:[UIColor redColor]];
  26. self.ledBtn.layer.masksToBounds = YES;
  27. self.ledBtn.layer.cornerRadius = 5.f;
  28. self.ledBtn.tag = 0;
  29. UIButton *btnDHT = [[UIButton alloc] initWithFrame:CGRectMake((bounds.size.width - 100 - 100 - 20)/2 + 100 + 20, (bounds.size.height - 20)/2, 100, 40)];
  30. [btnDHT addTarget:self action:@selector(onLedBtn:) forControlEvents:UIControlEventTouchDown];
  31. [btnDHT setTitle:@"手动更新数据" forState:UIControlStateNormal];
  32. [btnDHT setBackgroundColor:[UIColor greenColor]];
  33. btnDHT.layer.masksToBounds = YES;
  34. btnDHT.layer.cornerRadius = 5.f;
  35. btnDHT.tag = 1;
  36. [self.view addSubview:self.ledBtn];
  37. [self.view addSubview:btnDHT];
  38. self.dthLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, [WFCUUtilities wf_navigationFullHeight] + 16.f, bounds.size.width - 16, 100)];
  39. self.dthLabel.textAlignment = NSTextAlignmentCenter;
  40. self.dthLabel.backgroundColor = [UIColor grayColor];
  41. self.dthLabel.textColor = [UIColor greenColor];
  42. [self.view addSubview:self.dthLabel];
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessages:) name:kReceiveMessages object:nil];
  44. [self sendCmd:@"GET_DHT"];
  45. [self sendCmd:@"GET_LED"];
  46. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Info" style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  47. }
  48. - (NSString *)deviceId {
  49. return self.device.deviceId;
  50. }
  51. - (void)onRightBtn:(id)sender {
  52. DeviceInfoViewController *vc = [[DeviceInfoViewController alloc] init];
  53. vc.device = self.device;
  54. [self.navigationController pushViewController:vc animated:YES];
  55. }
  56. - (void)onLedBtn:(UIButton *)sender {
  57. if (sender.tag == 0) {
  58. if (!self.ledOn) {
  59. [self sendCmd:@"LED_ON"];
  60. } else {
  61. [self sendCmd:@"LED_OFF"];
  62. }
  63. } else if(sender.tag == 1) {
  64. [self sendCmd:@"GET_DHT"];
  65. }
  66. }
  67. - (void)setLedOn:(BOOL)ledOn {
  68. _ledOn = ledOn;
  69. if (self.ledOn) {
  70. [self.ledBtn setTitle:@"关闭LED" forState:UIControlStateNormal];
  71. self.ledBtn.backgroundColor = [UIColor redColor];
  72. } else {
  73. [self.ledBtn setTitle:@"打开LED" forState:UIControlStateNormal];
  74. self.ledBtn.backgroundColor = [UIColor grayColor];
  75. }
  76. }
  77. - (void)sendCmd:(NSString *)cmdStr {
  78. WFCCThingsDataContent *cmdMsg = [[WFCCThingsDataContent alloc] init];
  79. cmdMsg.data = [cmdStr dataUsingEncoding:NSUTF8StringEncoding];
  80. [[WFCCIMService sharedWFCIMService] send:[WFCCConversation conversationWithType:Things_Type target:self.deviceId line:0] content:cmdMsg success:^(long long messageUid, long long timestamp) {
  81. NSLog(@"send msg success");
  82. } error:^(int error_code) {
  83. NSLog(@"send msg error");
  84. }];
  85. }
  86. - (void)onReceiveMessages:(NSNotification *)notification {
  87. NSArray<WFCCMessage *> *messages = notification.object;
  88. for (WFCCMessage *msg in messages) {
  89. if (msg.conversation.type == Things_Type) {
  90. if (![msg.conversation.target isEqualToString:self.deviceId]) {
  91. NSLog(@"Unknown things.....");
  92. } else {
  93. if ([msg.content isKindOfClass:[WFCCThingsDataContent class]]) {
  94. WFCCThingsDataContent *thingsData = (WFCCThingsDataContent *)msg.content;
  95. if (thingsData.data.length > 0) {
  96. unsigned char* bytes = (unsigned char*)thingsData.data.bytes;
  97. if (bytes[0] == 0) {
  98. if (thingsData.data.length == 7) {
  99. int thHi = bytes[1];
  100. int thLo = bytes[2];
  101. int dHi = bytes[3];
  102. int dLo = bytes[4];
  103. NSString *str = [NSString stringWithFormat:@"湿度:%d.%d%% 温度:%d.%dC", thHi, thLo, dHi, dLo];
  104. self.dthLabel.text = str;
  105. }
  106. } else if(bytes[0] == 1) {
  107. if (thingsData.data.length == 2) {
  108. int led_status = bytes[1];
  109. self.ledOn = led_status > 0;
  110. }
  111. }
  112. }
  113. } else if ([msg.content isKindOfClass:[WFCCThingsLostEventContent class]]) {
  114. WFCCThingsLostEventContent *thingsLostEvent = (WFCCThingsLostEventContent *)msg.content;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. @end