interface.ino (3371B)
1 #include "include/config.h" 2 #include "include/interface.h" 3 #include "include/led.h" 4 5 json null_response; 6 7 static const char* method_strings[] = { 8 [interface::METHOD_GET] = "GET", 9 [interface::METHOD_POST] = "POST", 10 }; 11 12 void interface::begin() { 13 writeLED(COLOR_MAGENTA); 14 15 usbSerial.begin(remoteBaud); 16 usbSerial.setTimeout(remoteTimeout); 17 18 if (remoteForce) 19 return; 20 21 pinMode(modemPowerPin, OUTPUT); // Put voltage on the nb-iot module 22 pinMode(modemVoltagePin, OUTPUT); // Switch module voltage 23 pinMode(modemEnablePin, OUTPUT); // Set state to active 24 25 digitalWrite(modemPowerPin, HIGH); 26 digitalWrite(modemVoltagePin, LOW); 27 digitalWrite(modemEnablePin, HIGH); 28 modemSerial.begin(modemBaud); 29 30 while (!modemSerial) 31 ; 32 33 // -*- module initialization -*- 34 for (;;) { 35 modemSerial.write("AT\r\n"); 36 delay(1000); 37 if (modemSerial.available()) 38 break; 39 } 40 41 while (modemSerial.available()) // clear cache 42 modemSerial.read(); 43 44 // commands... 45 46 modemReady = true; 47 48 writeLED(COLOR_BLUE); 49 } 50 51 void interface::beginRemote() { 52 if (remoteReady) // already initalizised 53 return; 54 55 delay(2500); 56 57 sendToken(); 58 59 writeLED(COLOR_MAGENTA); 60 remoteReady = true; 61 } 62 63 void interface::endRemote() { 64 if (!remoteReady) 65 return; 66 67 writeLED(COLOR_BLUE); 68 remoteReady = false; 69 } 70 71 int interface::send(interface::method method, const char* endpoint) { 72 if (usbSerial) { 73 beginRemote(); 74 75 json cmd_request; 76 cmd_request["method"] = method_strings[method]; 77 cmd_request["endpoint"] = endpoint; 78 cmd_request["body"] = request; 79 80 json cmd_response; 81 if (remote("send", cmd_request, cmd_response)) 82 return 0; 83 84 request = nullptr; 85 response = cmd_response["body"]; 86 return cmd_response["code"]; 87 } else if (modemReady) { 88 endRemote(); 89 // modem 90 return 0; 91 } else { 92 endRemote(); 93 writeLED(COLOR_RED); 94 return 0; 95 } 96 } 97 98 void interface::sendToken() { 99 json req; 100 req["token"] = config.token; 101 remote("set_token", req, null_response, COMMAND_FORCE); 102 } 103 104 105 interface::command_status interface::remote(const char* command, json params, json& response, command_flags flags) { 106 bool force = flags & COMMAND_FORCE; 107 108 if (!force && !remoteReady) 109 return interface::STATUS_NOT_READY; 110 111 usbSerial.print(command); 112 usbSerial.print(" "); 113 params.printTo(usbSerial); 114 usbSerial.print("\n"); 115 116 String line = usbSerial.readStringUntil('\n'); 117 String status; 118 if (line.indexOf(' ') != -1) { 119 status = line.substring(0, line.indexOf(' ')); 120 response = json::parse(line.substring(line.indexOf(' ') + 1)); 121 } else { 122 status = line; 123 } 124 if (!status.length()) { 125 return interface::STATUS_TIMEOUT; 126 } else if (status == "ok") { 127 return interface::STATUS_OK; 128 } else { 129 response = status; 130 return interface::STATUS_ERROR; 131 } 132 } 133 134 interface::command_status interface::modem(const char* request, char* response, command_flags flags) { 135 return STATUS_NOT_READY; 136 /* 137 char line[lineBuffer]; 138 size_t lineLen; 139 char buf; 140 141 bool force = flags & COMMAND_FORCE, 142 block = flags & COMMAND_BLOCK, 143 ignore = flags & COMMAND_IGNORE; 144 145 if (!force && !modemReady) 146 return interface::STATUS_NOT_READY; 147 148 if (response) 149 response[0] = '\0'; 150 151 modemSerial.write("AT"); 152 modemSerial.write(request); 153 modemSerial.write("\r\n"); 154 155 156 delay(commandDelay * 1000);*/ 157 } 158 159 interface::command_status interface::modem(const char* request, command_flags flags) { 160 return modem(request, NULL, flags); 161 }