DeviceViewController.m 5.7 KB

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