-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvendingTweet.agent.nut
361 lines (292 loc) · 10.5 KB
/
vendingTweet.agent.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Author: Sigurd Jervelund Hansen - github.com/jervelund
// Arrays containing random messages
sales <- ["Sales are up 64% since last year!",
"With this rate, we\'re going to run out of stock soon!",
"All I do is - vend, vend, vend - no matter what!",
"Veni, Vidi, Vendi!",
"NASDAQ, here we come!",
"Say hello to my little vend!",
"I made them an offer they couldn\'t refuse.",
"Machine, Vending Machine.",
"HAL ain\'t got nothing on me!",
"A cold beverage. Shaken, not stirred.",
"Hasta la venda, Baby!",
"Madness? THIS IS BEVERAGES!",
"To vend, or not to vend. That is the question.",
"A day without vending is like a day without sunshine.",
"Ah, excellent, another valued customer!",
"Feeling overburdened by money?",
"A fantastic day for capitalism!",
"Vend, I am your father!",
"It's a me, Vending Machine!",
"Take this. It's dangerous to go alone.",
"M-M-M-M-M-MONSTER VEND!",
"A Wild Beverage Appears!",
"Vending Machine used Vend. It's super effective!",
"Job's done!",
"Get busy living, or get busy buying.",
"Come with me if you want to vend!",
"Life's like the random button on a vending machine. You never know what you gonna get.",
"May the vend be with you."
];
dry <- ["Well that\'s enough to drink for today!",
"Whew, let me cool off for a minute.",
"It\'s not you - it\'s me.",
"Dangit, I'm out!",
"It\'s time to kick ass and vend beverages - and I\'m all out of beverages!",
"I find my lack of beverages disturbing.",
"Do not try to vend the beverage. That's impossible. Instead only try to realize the truth: There is no beverage."
];
undry <- ["Bring the big wallet, I\'m restocked!",
"Back on track, and ready to vend!"];
jam <- ["Ugh - I don\'t feel so good.",
"I\'ll be back!",
"I used to vend beverages, but then I took an arrow to the knee!",
"I just can\'t do it captain - I don\'t have the power!"];
unjam <- ["I feel better already!",
"It\'s alive! It\'s alive!",
"Good as new, I think. Am I leaking?"];
// Actual tweet functions
device.on("Tweet", function(v) {
//twitter.update_status(v);
server.log("Tweet: "+v);
});
function randomInt(i){return math.floor(i * (1.0 * math.rand() / RAND_MAX));}
device.on("sales",function(i){twitter.update_status(sales[randomInt(sales.len())] + " (total vend count is now " + i + ")");})
device.on("dry" ,function(i){twitter.update_status( dry[randomInt( dry.len())] + " (slot " + i + " is empty) @CUnnerup");})
device.on("undry",function(i){twitter.update_status(undry[randomInt(undry.len())] + " (slot " + i + " refilled)");})
device.on("jam" ,function(i){twitter.update_status( jam[randomInt( jam.len())] + " (slot " + i + " jammed) @CUnnerup");})
device.on("unjam",function(i){twitter.update_status(unjam[randomInt(unjam.len())] + " (slot " + i + " is no longer jammed)");})
// Source: https://github.com/joel-wehr/Tutorial_Electric_Imp_MAX31855/blob/ef38b0fec29ec619e2c58ef2fd62e1c9776e85d3/agent.nut
//********************BEGIN TWITTER*****************
// Code by forums user bodinegl
// with a tiny bit of help from beardedinventor
function print(x) {
server.log(x);
}
function left_rotate(x, n) {
// this has to handle signed integers
return (x << n) | (x >> (32 - n)) & ~((-1 >> n) << n);
}
function swap32(val) {
return ((val & 0xFF) << 24) | ((val & 0xFF00) << 8) | ((val >>> 8) & 0xFF00) | ((val >>> 24) & 0xFF);
}
function sha1(message) {
local h0 = 0x67452301;
local h1 = 0xEFCDAB89;
local h2 = 0x98BADCFE;
local h3 = 0x10325476;
local h4 = 0xC3D2E1F0;
local mb=blob((message.len()+9+63) & ~63)
local original_byte_len = message.len();
local original_bit_len = original_byte_len * 8;
foreach (val in message) {
mb.writen(val, 'b');
}
mb.writen('\x80', 'b')
local l = ((56 - (original_byte_len + 1)) & 63) & 63;
while (l--) {
mb.writen('\x00', 'b')
}
mb.writen('\x00', 'i')
mb.writen(swap32(original_bit_len), 'i')
for (local i=0;i<mb.len();i+=64) {
local w=[]; w.resize(80);
for(local j=0;j<16;j++) {
local s = i + j*4;
mb.seek(s, 'b');
w[j] = swap32(mb.readn('i'));
}
for(local j=16;j<80;j++) {
w[j] = left_rotate(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
}
local a = h0;
local b = h1;
local c = h2;
local d = h3;
local e = h4;
for(local i=0;i<80;i+=1) {
local f=0;
local k=0;
if (i>=0 && i<=19) {
f = d ^ (b & (c ^ d));
k = 0x5A827999;
}
else if (i>=20 && i<= 39) {
f = b ^ c ^ d;
k = 0x6ED9EBA1;
}
else if (i>=40 && i<= 59) {
f = (b & c) | (b & d) | (c & d) ;
k = 0x8F1BBCDC;
}
else if (i>=60 && i<= 79) {
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
local _a=a
local _b=b
local _c=c
local _d=d
local _e=e
local _f=f
a = (left_rotate(_a, 5) + _f + _e + k + w[i]) & 0xffffffff;
b = _a;
c = left_rotate(_b, 30);
d = _c;
e = _d;
}
h0 = (h0 + a) & 0xffffffff
h1 = (h1 + b) & 0xffffffff
h2 = (h2 + c) & 0xffffffff
h3 = (h3 + d) & 0xffffffff
h4 = (h4 + e) & 0xffffffff
}
local hash = blob(20);
hash.writen(swap32(h0),'i');
hash.writen(swap32(h1),'i');
hash.writen(swap32(h2),'i');
hash.writen(swap32(h3),'i');
hash.writen(swap32(h4),'i');
return hash;
}
function blobxor_x5c(text) {
local len = text.len();
local a = blob(len)
for (local i = 0; i < len; i++) {
a.writen(text[i] ^ 0x5c ,'b');
}
return a;
}
function blobxor_x36(text) {
local len = text.len();
local a = blob(len)
for (local i = 0; i < len; i++) {
a.writen(text[i] ^ 0x36 ,'b');
}
return a;
}
function blobconcat(a,b) {
local len = b.len();
for(local i=0; i<len; i++) {
a.writen(b[i],'b');
}
return a;
}
function blobpad(s,n) {
local b = blob(n);
local len = s.len();
for(local i=0; i<len; i++) {
b.writen(s[i],'b');
}
for(local i=n-s.len(); i; i--) {
b.writen('\x00', 'b');
}
return b;
}
function hmac_sha1(key, message) {
local _key;
if ( key.len() > 64 ) {
_key = blobpad(sha1(key),64);
}
else if ( key.len() <= 64 ) {
_key = blobpad(key,64);
}
local _ok = blobxor_x5c(_key);
local _ik= blobxor_x36(_key);
return sha1(blobconcat(_ok, sha1(blobconcat(_ik, message))));
}
// helper function
function _printhex(s) {
local h = "";
for(local i=0;i<s.len();i++) h+=format("%02x", s[i]);
return h;
}
// Requires hmac_sha1.nut
class TwitterClient {
consumerKey = null
consumerSecret = null
accessToken = null
accessSecret = null
baseUrl = "https://api.twitter.com/";
constructor (_consumerKey, _consumerSecret, _accessToken, _accessSecret) {
this.consumerKey = _consumerKey;
this.consumerSecret = _consumerSecret;
this.accessToken = _accessToken;
this.accessSecret = _accessSecret;
}
function fmthex(s) {
local h = ""
for(local i=0;i<s.len();i++) h+=format("%02x", s[i]);
return h
}
function validChar(c) {
if (c >= 48 && c <= 57) return true; // numbers
if (c >= 65 && c <= 90) return true; // uppercase letter
if (c >= 97 && c <= 122) return true; // lowercase letter
if (c == 46 || c == 45 || c == 95 || c == 126) return true; // special characters
return false;
}
function encode(str) {
local r = "";
foreach(s in str){
if (validChar(s)) r += s.tochar();
else r += "%"+format("%00x", s).toupper();
}
return r;
}
function sign_hmac_sha1(key, str) {
local sign = hmac_sha1(key,str)
server.log("sign="+fmthex(sign));
return http.base64encode(sign)
}
function post_oauth1(postUrl, headers, post) {
local time = time()
local nonce = format("non%dce",time)
local parm_string = http.urlencode({oauth_consumer_key=consumerKey})
parm_string += "&"+http.urlencode({oauth_nonce=nonce})
parm_string += "&"+http.urlencode({oauth_signature_method="HMAC-SHA1"})
parm_string += "&"+http.urlencode({oauth_timestamp=time})
parm_string += "&"+http.urlencode({oauth_token=accessToken})
parm_string += "&"+http.urlencode({oauth_version="1.0"})
parm_string += "&"+post
local signature_string = "POST&"+encode(postUrl)+"&"+encode(parm_string)
server.log("signature="+signature_string)
local key = encode(consumerSecret)+"&"+encode(accessSecret)
local sha1 = encode(sign_hmac_sha1(key, signature_string))
server.log("key="+key+", sha1="+sha1)
local auth_header = "oauth_consumer_key=\""+consumerKey+"\","
auth_header += "oauth_nonce=\""+nonce+"\","
auth_header += "oauth_signature=\""+sha1+"\","
auth_header += "oauth_signature_method=\""+"HMAC-SHA1"+"\","
auth_header += "oauth_timestamp=\""+time+"\","
auth_header += "oauth_token=\""+accessToken+"\","
auth_header += "oauth_version=\"1.0\""
server.log(auth_header)
local headers = {
"Authorization": "OAuth "+auth_header,
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
};
local response = http.post(postUrl, headers, post).sendsync();
server.log("response="+response);
return response
}
function update_status(status) {
local postUrl = baseUrl + "1.1/statuses/update.json";
local headers = { };
local post = "status="+encode(status);
local response = post_oauth1(postUrl, headers, post)
if (response && response.statuscode != 200) {
server.log("Error updating_status tweet. HTTP Status Code " + response.statuscode);
server.log(response.body);
return null;
}
}
}
// Get your keys here: https://apps.twitter.com/app/
_CONSUMER_KEY <- "xxx"
_CONSUMER_SECRET <- "xxx"
_ACCESS_TOKEN <- "xxx"
_ACCESS_SECRET <- "xxx"
twitter <- TwitterClient(_CONSUMER_KEY, _CONSUMER_SECRET, _ACCESS_TOKEN, _ACCESS_SECRET);
//**************************END TWITTER*****************************************